XMPProcessor.java
2.69 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
/*
* Decompiled with CFR 0_118.
*
* Could not load the following classes:
* com.adobe.xmp.XMPException
* com.adobe.xmp.XMPMetaFactory
* com.day.cq.dam.api.Context
* com.day.cq.dam.api.Processor
* com.day.cq.dam.api.ProcessorException
*/
package com.day.cq.dam.commons.handler;
import com.adobe.xmp.XMPException;
import com.adobe.xmp.XMPMetaFactory;
import com.day.cq.dam.api.Context;
import com.day.cq.dam.api.Processor;
import com.day.cq.dam.api.ProcessorException;
import com.day.cq.dam.commons.handler.Filter;
import com.day.cq.dam.commons.handler.FilterStateListener;
import com.day.cq.dam.commons.handler.SimpleContext;
import com.day.cq.dam.commons.handler.XPacketFilter;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
public class XMPProcessor
implements Processor,
FilterStateListener {
private Context context;
private XPacketFilter topFilter;
public XMPProcessor(Context context) {
this.context = context;
this.topFilter = new XPacketFilter();
this.topFilter.setFilterStateListener(this);
this.topFilter.setAutoReset(true);
}
public final void process(byte[] buf, int off, int len) throws IOException {
this.topFilter.filter(buf, off, len);
}
@Override
public OutputStream started(Filter filter) {
return new ByteArrayOutputStream(8192);
}
@Override
public void ended(Filter filter, OutputStream out) {
ByteArrayOutputStream bos = (ByteArrayOutputStream)out;
try {
byte[] buf = bos.toByteArray();
XMPMetaFactory.parse((InputStream)new ByteArrayInputStream(buf));
this.context.addMetadata((InputStream)new ByteArrayInputStream(buf));
}
catch (XMPException e) {
String msg = "Unable to get XMP object from input";
this.context.addException(new ProcessorException(msg, (Throwable)e, (Processor)this));
}
}
public static InputStream process(InputStream in) throws IOException, ProcessorException {
int len;
SimpleContext context = new SimpleContext();
XMPProcessor processor = new XMPProcessor(context);
byte[] buf = new byte[8192];
while ((len = in.read(buf)) > 0) {
processor.process(buf, 0, len);
}
InputStream[] metadata = context.getMetadata();
if (metadata != null && metadata.length > 0) {
return metadata[0];
}
ProcessorException[] exceptions = context.getExceptions();
if (exceptions != null) {
throw exceptions[exceptions.length - 1];
}
return null;
}
}