DefaultFormatHandler.java
4 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
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
/*
* Decompiled with CFR 0_118.
*
* Could not load the following classes:
* com.day.cq.dam.api.Context
* com.day.cq.dam.api.FormatHandler
* com.day.cq.dam.api.Processor
* com.day.cq.dam.api.ProcessorException
*/
package com.day.cq.dam.commons.handler;
import com.day.cq.dam.api.Context;
import com.day.cq.dam.api.FormatHandler;
import com.day.cq.dam.api.Processor;
import com.day.cq.dam.api.ProcessorException;
import com.day.cq.dam.commons.handler.SimpleContext;
import com.day.cq.dam.commons.handler.XMPProcessor;
import com.day.cq.dam.commons.thumbnail.XapThumbnailsProcessor;
import java.awt.image.BufferedImage;
import java.io.IOException;
import java.io.InputStream;
public class DefaultFormatHandler
implements FormatHandler {
public boolean accepts(byte[] data, int off, int len) {
return true;
}
protected Processor[] getThumbnailProcessors(Context context) {
return new Processor[]{new XapThumbnailsProcessor(context)};
}
protected Processor[] getMetadataProcessors(Context context) {
return new Processor[]{new XMPProcessor(context)};
}
protected Processor[] getAllProcessors(Context context) {
Processor[] t = this.getThumbnailProcessors(context);
Processor[] m = this.getMetadataProcessors(context);
Processor[] result = new Processor[t.length + m.length];
System.arraycopy(t, 0, result, 0, t.length);
System.arraycopy(m, 0, result, t.length, m.length);
return result;
}
public final void process(InputStream in, Context context) throws IOException {
int len;
Processor[] processors = this.getAllProcessors(context);
byte[] data = new byte[8192];
while ((len = in.read(data)) > 0) {
for (Processor processor : processors) {
processor.process(data, 0, len);
}
}
}
public BufferedImage getThumbnailImage(InputStream in) throws IOException, ProcessorException {
int len;
SimpleContext context = new SimpleContext();
Processor[] processors = this.getThumbnailProcessors(context);
byte[] data = new byte[8192];
while ((len = in.read(data)) > 0) {
for (Processor processor : processors) {
processor.process(data, 0, len);
}
}
BufferedImage[] thumbnails = context.getThumbnails();
if (thumbnails != null && thumbnails.length > 0) {
return thumbnails[0];
}
ProcessorException[] exceptions = context.getExceptions();
if (exceptions != null) {
throw exceptions[exceptions.length - 1];
}
return null;
}
public InputStream getMetadata(InputStream in) throws IOException, ProcessorException {
int len;
SimpleContext context = new SimpleContext();
Processor[] processors = this.getMetadataProcessors(context);
byte[] data = new byte[8192];
while ((len = in.read(data)) > 0) {
for (Processor processor : processors) {
processor.process(data, 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;
}
protected int locate(byte[] pattern, byte[] data, int off, int len) {
int i = 0;
while (i < pattern.length && off < len) {
i = pattern[i] == data[off] ? ++i : 0;
++off;
}
return i == pattern.length ? off : -1;
}
protected boolean matchPrefix(byte[] expected, byte[] actual) throws IOException {
if (actual.length < expected.length) {
return false;
}
for (int i = 0; i < expected.length; ++i) {
if (expected[i] == actual[i]) continue;
return false;
}
return true;
}
}