-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathMultiCoder.java
More file actions
416 lines (378 loc) · 12.5 KB
/
MultiCoder.java
File metadata and controls
416 lines (378 loc) · 12.5 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
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
/**
*
* Copyright (c) 2014, the Railo Company Ltd. All rights reserved.
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library. If not, see <http://www.gnu.org/licenses/>.
*
**/
package org.lucee.extension.image.coder;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.lang.reflect.Constructor;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import org.lucee.extension.image.Image;
import org.lucee.extension.image.ImageUtil;
import org.lucee.extension.image.format.FormatExtract;
import org.lucee.extension.image.format.FormatNames;
import org.lucee.extension.image.util.MultiException;
import lucee.commons.io.res.Resource;
import lucee.loader.engine.CFMLEngine;
import lucee.loader.engine.CFMLEngineFactory;
import lucee.loader.util.Util;
import lucee.runtime.exp.CatchBlock;
import lucee.runtime.exp.PageException;
import lucee.runtime.type.Array;
import lucee.runtime.type.Struct;
import lucee.runtime.util.Creation;
public class MultiCoder extends Coder implements FormatNames, FormatExtract {
private List<Coder> coders = new ArrayList();
private String[] writerFormatNames;
private String[] readerFormatNames;
private static final String tokenw = "MultiCoderTokenWrite";
private static final String tokenr = "MultiCoderTokenRead";
public MultiCoder() {
}
public void add(Coder coder) {
writerFormatNames = null;
readerFormatNames = null;
coders.add(coder);
}
@Override
public BufferedImage read(Resource res, String format) throws IOException {
return read(res, format, null);
}
public BufferedImage read(Resource res, String format, Array detail) throws IOException {
if (Util.isEmpty(format, true)) {
format = ImageUtil.getFormat(res);
}
MultiException me = null;
BufferedImage bi = null;
Struct data = null;
long start = 0;
for (Coder coder: coders) {
if (coder instanceof FormatNames && !_supported(((FormatNames) coder).getReaderFormatNames(), format)) continue;
try {
if (detail != null) {
Creation cre = CFMLEngineFactory.getInstance().getCreationUtil();
data = cre.createStruct();
detail.appendEL(data);
data.set("class", coder.getClass().getName());
start = System.currentTimeMillis();
}
bi = coder.read(res, format);
if (detail != null) {
data.set("time", System.currentTimeMillis() - start);
data.set("image", new Image(bi));
}
if (detail == null && bi != null) {
return bi;
}
}
catch (Throwable t) {
if (t instanceof ThreadDeath) throw (ThreadDeath) t;
if (detail != null) {
data.setEL("exception", toCatchBlock(t));
}
if (me == null) me = new MultiException(t);
else me.initCause(t);
}
}
if (bi != null) return bi;
if (me != null && detail == null) throw toIOException(me);
String mt = null;
String msg = "could not read the file [" + res + "],";
if (!Util.isEmpty(format, true)) {
msg += "the format [" + format + "] is not supported to read,";
if (!Util.isEmpty(mt = ImageUtil.getMimeType(res, null))) {
msg += "the mime-type of the file is [" + mt + "],";
}
}
else if (!Util.isEmpty(mt = ImageUtil.getMimeType(res, null))) {
msg += "the mime-type [" + mt + "] is not supported to read,";
}
msg += " supported formats are [" + CFMLEngineFactory.getInstance().getListUtil().toList(ImageUtil.getReaderFormatNames(), ", ") + "]";
throw new IOException(msg);
}
private CatchBlock toCatchBlock(Throwable t) throws IOException {
CFMLEngine eng = CFMLEngineFactory.getInstance();
try {
Class<?> clazz = eng.getClassUtil().loadClass("lucee.runtime.exp.CatchBlockImpl");
Constructor<?> constr = clazz.getConstructor(PageException.class);
return (CatchBlock) constr.newInstance(eng.getCastUtil().toPageException(t));
}
catch (Exception e1) {
throw eng.getExceptionUtil().toIOException(e1);
}
}
@Override
public BufferedImage read(byte[] bytes, String format) throws IOException {
if (Util.isEmpty(format, true)) {
format = ImageUtil.getFormat(bytes);
}
MultiException me = null;
for (Coder coder: coders) {
if (coder instanceof FormatNames && !_supported(((FormatNames) coder).getReaderFormatNames(), format)) continue;
try {
BufferedImage bi = coder.read(bytes, format);
if (bi != null) return bi;
}
catch (Exception e) {
if (me == null) me = new MultiException(e);
else me.initCause(e);
}
}
if (me != null) {
throw toIOException(me);
}
String mt = null;
String msg = "could not read the image object,";
if (!Util.isEmpty(format, true)) {
msg += "the format [" + format + "] is not supported to read,";
if (!Util.isEmpty(mt = ImageUtil.getMimeType(bytes, null))) {
msg += "the mime-type of this image object is [" + mt + "],";
}
}
else if (!Util.isEmpty(mt = ImageUtil.getMimeType(bytes, null))) {
msg += "the mime-type [" + mt + "] is not supported to read,";
}
msg += " supported formats are [" + CFMLEngineFactory.getInstance().getListUtil().toList(ImageUtil.getReaderFormatNames(), ", ") + "]";
throw new IOException(msg);
}
@Override
public void write(Image img, Resource destination, String format, float quality, boolean noMeta) throws IOException {
try {
write(img, destination, format, quality, noMeta, null);
}
catch (IOException e) {
throw e;
}
}
public void write(Image img, Resource destination, String format, final float quality, boolean noMeta, Array detail) throws IOException {
if (Util.isEmpty(format, true)) {
format = img.getFormat();
}
Struct data = null;
long start = 0;
Resource tmp = null;
MultiException me = null;
boolean success = false;
for (Coder coder: coders) {
if (coder instanceof FormatNames && !_supported(((FormatNames) coder).getWriterFormatNames(), format)) continue;
try {
if (detail != null) {
Creation cre = CFMLEngineFactory.getInstance().getCreationUtil();
data = cre.createStruct();
detail.appendEL(data);
data.set("class", coder.getClass().getName());
start = System.currentTimeMillis();
destination = tmp = ImageUtil.createTempFile(format);
}
coder.write(img, destination, format, quality, noMeta);
success = true;
if (detail != null) {
data.set("time", System.currentTimeMillis() - start);
}
if (detail == null) return;
}
catch (Throwable t) {
if (t instanceof ThreadDeath) throw (ThreadDeath) t;
if (detail != null) {
data.setEL("exception", toCatchBlock(t));
}
if (me == null) me = new MultiException(t);
else me.initCause(t);
}
finally {
if (tmp != null && !tmp.delete() && tmp instanceof File) ((File) tmp).deleteOnExit();
tmp = null;
}
}
if (me != null && detail == null) throw toIOException(me);
if (!success) {
String msg = "could not write the file [" + destination + "],";
if (!Util.isEmpty(format, true)) {
msg += "the format [" + format + "] is not supported to write,";
}
msg += " supported formats are [" + CFMLEngineFactory.getInstance().getListUtil().toList(ImageUtil.getWriterFormatNames(), ", ") + "]";
throw new IOException(msg);
}
}
@Override
public String getFormat(Resource res) throws IOException {
MultiException me = null;
for (Coder coder: coders) {
if (!(coder instanceof FormatExtract)) continue;
try {
String format = ((FormatExtract) coder).getFormat(res);
if (!Util.isEmpty(format)) {
return format;
}
}
catch (Exception e) {
if (me == null) me = new MultiException(e);
else me.initCause(e);
}
}
if (me != null) throw toIOException(me);
return null;
}
@Override
public String getFormat(byte[] bytes) throws IOException {
MultiException me = null;
for (Coder coder: coders) {
if (!(coder instanceof FormatExtract)) continue;
try {
String format = ((FormatExtract) coder).getFormat(bytes);
if (!Util.isEmpty(format)) {
return format;
}
}
catch (Exception e) {
if (me == null) me = new MultiException(e);
else me.initCause(e);
}
}
if (me != null) throw toIOException(me);
throw new IOException("throw could not detect the format for the given image object");
}
@Override
public String getFormat(Resource res, String defaultValue) {
return getFormat(res, null, defaultValue);
}
@Override
public String getFormat(Resource res, String mimeType, String defaultValue) {
for (Coder coder: coders) {
if (!(coder instanceof FormatExtract)) continue;
String format = ((FormatExtract) coder).getFormat(res, mimeType, null);
if (!Util.isEmpty(format)) {
return format;
}
}
return defaultValue;
}
@Override
public String getFormat(byte[] bytes, String defaultValue) {
return getFormat(bytes, null, defaultValue);
}
@Override
public String getFormat(byte[] bytes, String mimeType, String defaultValue) {
for (Coder coder: coders) {
if (!(coder instanceof FormatExtract)) continue;
String format = ((FormatExtract) coder).getFormat(bytes, mimeType, null);
if (!Util.isEmpty(format)) return format;
}
return defaultValue;
}
@Override
public final String[] getWriterFormatNames() {
if (writerFormatNames == null) {
synchronized (tokenw) {
if (writerFormatNames == null) {
List<String> list = new ArrayList<>();
for (Coder c: coders) {
if (!(c instanceof FormatNames)) continue;
try {
for (String n: ((FormatNames) c).getWriterFormatNames()) {
if (!list.contains(n.toUpperCase())) list.add(n.toUpperCase());
}
}
catch (Exception e) {
}
}
writerFormatNames = list.toArray(new String[list.size()]);
Arrays.sort(writerFormatNames);
}
}
}
return writerFormatNames;
}
@Override
public final String[] getReaderFormatNames() {
if (readerFormatNames == null) {
synchronized (tokenr) {
if (readerFormatNames == null) {
List<String> list = new ArrayList<>();
for (Coder c: coders) {
if (!(c instanceof FormatNames)) continue;
try {
for (String n: ((FormatNames) c).getReaderFormatNames()) {
if (!list.contains(n.toUpperCase())) list.add(n.toUpperCase());
}
}
catch (Exception e) {
}
}
readerFormatNames = list.toArray(new String[list.size()]);
Arrays.sort(readerFormatNames);
}
}
}
return readerFormatNames;
}
public final Struct getWriterFormatNamesByGroup() {
Creation cre = CFMLEngineFactory.getInstance().getCreationUtil();
Struct sct = cre.createStruct();
for (Coder c: coders) {
if (!(c instanceof FormatNames)) continue;
try {
List<String> list = new ArrayList<>();
for (String n: ((FormatNames) c).getWriterFormatNames()) {
if (!list.contains(n.toUpperCase())) list.add(n.toUpperCase());
}
sct.setEL(c.getClass().getName(), sortAndConvert(list));
}
catch (Exception e) {
}
}
return sct;
}
public final Struct getReaderFormatNamesByGroup() {
Creation cre = CFMLEngineFactory.getInstance().getCreationUtil();
Struct sct = cre.createStruct();
for (Coder c: coders) {
if (!(c instanceof FormatNames)) continue;
try {
List<String> list = new ArrayList<>();
// filter out duplicates
for (String n: ((FormatNames) c).getReaderFormatNames()) {
if (!list.contains(n.toUpperCase())) list.add(n.toUpperCase());
}
sct.setEL(c.getClass().getName(), sortAndConvert(list));
}
catch (Exception e) {
}
}
return sct;
}
@Override
public boolean supported() {
return true;
}
public boolean _supported(String[] formats, String format) {
if (formats == null || format == null) return true;
if ("JPG".equalsIgnoreCase(format)) format = "JPEG";
if ("JPE".equalsIgnoreCase(format)) format = "JPEG";
for (String f: formats) {
if (format.equalsIgnoreCase(f)) return true;
}
return false;
}
private static IOException toIOException(MultiException me) {
if (me.size() == 1) return CFMLEngineFactory.getInstance().getExceptionUtil().toIOException(me.getThrowable(0));
else return CFMLEngineFactory.getInstance().getExceptionUtil().toIOException(me);
}
}