Skip to content

Commit 9eb78b3

Browse files
committed
Avoid reflection for FileFormat factories and document tiff-format
1 parent 5ed5577 commit 9eb78b3

File tree

5 files changed

+119
-98
lines changed

5 files changed

+119
-98
lines changed

bundles/org.eclipse.swt/Eclipse SWT/cocoa/org/eclipse/swt/graphics/ImageLoader.java

Lines changed: 16 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -187,18 +187,20 @@ public ImageData[] load(String filename) {
187187
* Saves the image data in this ImageLoader to the specified stream.
188188
* The format parameter can have one of the following values:
189189
* <dl>
190-
* <dt><code>IMAGE_BMP</code></dt>
190+
* <dt>{@link SWT#IMAGE_BMP}</dt>
191191
* <dd>Windows BMP file format, no compression</dd>
192-
* <dt><code>IMAGE_BMP_RLE</code></dt>
192+
* <dt>{@link SWT#IMAGE_BMP_RLE}</dt>
193193
* <dd>Windows BMP file format, RLE compression if appropriate</dd>
194-
* <dt><code>IMAGE_GIF</code></dt>
194+
* <dt>{@link SWT#IMAGE_GIF}</dt>
195195
* <dd>GIF file format</dd>
196-
* <dt><code>IMAGE_ICO</code></dt>
196+
* <dt>{@link SWT#IMAGE_ICO}</dt>
197197
* <dd>Windows ICO file format</dd>
198-
* <dt><code>IMAGE_JPEG</code></dt>
198+
* <dt>{@link SWT#IMAGE_JPEG}</dt>
199199
* <dd>JPEG file format</dd>
200-
* <dt><code>IMAGE_PNG</code></dt>
200+
* <dt>{@link SWT#IMAGE_PNG}</dt>
201201
* <dd>PNG file format</dd>
202+
* <dt>{@link SWT#IMAGE_TIFF}</dt>
203+
* <dd>TIFF file format</dd>
202204
* </dl>
203205
*
204206
* @param stream the output stream to write the images to
@@ -222,18 +224,20 @@ public void save(OutputStream stream, int format) {
222224
* Saves the image data in this ImageLoader to a file with the specified name.
223225
* The format parameter can have one of the following values:
224226
* <dl>
225-
* <dt><code>IMAGE_BMP</code></dt>
227+
* <dt>{@link SWT#IMAGE_BMP}</dt>
226228
* <dd>Windows BMP file format, no compression</dd>
227-
* <dt><code>IMAGE_BMP_RLE</code></dt>
229+
* <dt>{@link SWT#IMAGE_BMP_RLE}</dt>
228230
* <dd>Windows BMP file format, RLE compression if appropriate</dd>
229-
* <dt><code>IMAGE_GIF</code></dt>
231+
* <dt>{@link SWT#IMAGE_GIF}</dt>
230232
* <dd>GIF file format</dd>
231-
* <dt><code>IMAGE_ICO</code></dt>
233+
* <dt>{@link SWT#IMAGE_ICO}</dt>
232234
* <dd>Windows ICO file format</dd>
233-
* <dt><code>IMAGE_JPEG</code></dt>
235+
* <dt>{@link SWT#IMAGE_JPEG}</dt>
234236
* <dd>JPEG file format</dd>
235-
* <dt><code>IMAGE_PNG</code></dt>
237+
* <dt>{@link SWT#IMAGE_PNG}</dt>
236238
* <dd>PNG file format</dd>
239+
* <dt>{@link SWT#IMAGE_TIFF}</dt>
240+
* <dd>TIFF file format</dd>
237241
* </dl>
238242
*
239243
* @param filename the name of the file to write the images to

bundles/org.eclipse.swt/Eclipse SWT/common/org/eclipse/swt/internal/image/FileFormat.java

Lines changed: 38 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*******************************************************************************
2-
* Copyright (c) 2000, 2018 IBM Corporation and others.
2+
* Copyright (c) 2000, 2025 IBM Corporation and others.
33
*
44
* This program and the accompanying materials
55
* are made available under the terms of the Eclipse Public License 2.0
@@ -15,6 +15,8 @@
1515

1616

1717
import java.io.*;
18+
import java.util.*;
19+
import java.util.function.*;
1820

1921
import org.eclipse.swt.*;
2022
import org.eclipse.swt.graphics.*;
@@ -24,22 +26,39 @@
2426
* in various image file formats.
2527
*/
2628
public abstract class FileFormat {
27-
static final String FORMAT_PACKAGE = "org.eclipse.swt.internal.image"; //$NON-NLS-1$
28-
static final String FORMAT_SUFFIX = "FileFormat"; //$NON-NLS-1$
29-
static final String[] FORMATS = {"WinBMP", "WinBMP", "GIF", "WinICO", "JPEG", "PNG", "TIFF", "OS2BMP"}; //$NON-NLS-1$//$NON-NLS-2$ //$NON-NLS-3$ //$NON-NLS-4$//$NON-NLS-5$ //$NON-NLS-6$//$NON-NLS-7$//$NON-NLS-8$
29+
private static final List<Supplier<FileFormat>> FORMAT_FACTORIES = new ArrayList<>();
30+
static {
31+
try {
32+
FORMAT_FACTORIES.add(WinBMPFileFormat::new);
33+
} catch (NoClassDefFoundError e) { } // ignore format
34+
try {
35+
FORMAT_FACTORIES.add(WinBMPFileFormat::new);
36+
} catch (NoClassDefFoundError e) { } // ignore format
37+
try {
38+
FORMAT_FACTORIES.add(GIFFileFormat::new);
39+
} catch (NoClassDefFoundError e) { } // ignore format
40+
try {
41+
FORMAT_FACTORIES.add(WinICOFileFormat::new);
42+
} catch (NoClassDefFoundError e) { } // ignore format
43+
try {
44+
FORMAT_FACTORIES.add(JPEGFileFormat::new);
45+
} catch (NoClassDefFoundError e) { } // ignore format
46+
try {
47+
FORMAT_FACTORIES.add(PNGFileFormat::new);
48+
} catch (NoClassDefFoundError e) { } // ignore format
49+
try {
50+
FORMAT_FACTORIES.add(TIFFFileFormat::new);
51+
} catch (NoClassDefFoundError e) { } // ignore format
52+
try {
53+
FORMAT_FACTORIES.add(OS2BMPFileFormat::new);
54+
} catch (NoClassDefFoundError e) { } // ignore format
55+
}
3056

3157
LEDataInputStream inputStream;
3258
LEDataOutputStream outputStream;
3359
ImageLoader loader;
3460
int compression;
3561

36-
static FileFormat getFileFormat (LEDataInputStream stream, String format) throws Exception {
37-
Class<?> clazz = Class.forName(FORMAT_PACKAGE + '.' + format + FORMAT_SUFFIX);
38-
FileFormat fileFormat = (FileFormat) clazz.getDeclaredConstructor().newInstance();
39-
if (fileFormat.isFileFormat(stream)) return fileFormat;
40-
return null;
41-
}
42-
4362
/**
4463
* Return whether or not the specified input stream
4564
* represents a supported file format.
@@ -71,20 +90,11 @@ public ImageData[] loadFromStream(LEDataInputStream stream) {
7190
* return the device independent image array represented by the stream.
7291
*/
7392
public static ImageData[] load(InputStream is, ImageLoader loader) {
74-
FileFormat fileFormat = null;
7593
LEDataInputStream stream = new LEDataInputStream(is);
76-
for (int i = 1; i < FORMATS.length; i++) {
77-
if (FORMATS[i] != null) {
78-
try {
79-
fileFormat = getFileFormat (stream, FORMATS[i]);
80-
if (fileFormat != null) break;
81-
} catch (ClassNotFoundException e) {
82-
FORMATS[i] = null;
83-
} catch (Exception e) {
84-
}
85-
}
86-
}
87-
if (fileFormat == null) SWT.error(SWT.ERROR_UNSUPPORTED_FORMAT);
94+
FileFormat fileFormat = FORMAT_FACTORIES.stream().skip(1) //
95+
.map(Supplier::get).filter(f -> f.isFileFormat(stream)) //
96+
.findFirst().orElse(null);
97+
if (fileFormat == null) SWT.error(SWT.ERROR_UNSUPPORTED_FORMAT);
8898
fileFormat.loader = loader;
8999
return fileFormat.loadFromStream(stream);
90100
}
@@ -94,18 +104,13 @@ public static ImageData[] load(InputStream is, ImageLoader loader) {
94104
* to the specified output stream using the specified file format.
95105
*/
96106
public static void save(OutputStream os, int format, ImageLoader loader) {
97-
if (format < 0 || format >= FORMATS.length) SWT.error(SWT.ERROR_UNSUPPORTED_FORMAT);
98-
if (FORMATS[format] == null) SWT.error(SWT.ERROR_UNSUPPORTED_FORMAT);
107+
if (format < 0 || format >= FORMAT_FACTORIES.size()) {
108+
SWT.error(SWT.ERROR_UNSUPPORTED_FORMAT);
109+
}
99110
if (loader.data == null || loader.data.length < 1) SWT.error(SWT.ERROR_INVALID_ARGUMENT);
100111

101112
LEDataOutputStream stream = new LEDataOutputStream(os);
102-
FileFormat fileFormat = null;
103-
try {
104-
Class<?> clazz = Class.forName(FORMAT_PACKAGE + '.' + FORMATS[format] + FORMAT_SUFFIX);
105-
fileFormat = (FileFormat) clazz.getDeclaredConstructor().newInstance();
106-
} catch (Exception e) {
107-
SWT.error(SWT.ERROR_UNSUPPORTED_FORMAT);
108-
}
113+
FileFormat fileFormat = FORMAT_FACTORIES.get(format).get();
109114
if (format == SWT.IMAGE_BMP_RLE) {
110115
switch (loader.data[0].depth) {
111116
case 8: fileFormat.compression = 1; break;

bundles/org.eclipse.swt/Eclipse SWT/common/org/eclipse/swt/internal/image/WinICOFileFormat.java

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,9 +14,10 @@
1414
package org.eclipse.swt.internal.image;
1515

1616

17+
import java.io.*;
18+
1719
import org.eclipse.swt.*;
1820
import org.eclipse.swt.graphics.*;
19-
import java.io.*;
2021

2122
public final class WinICOFileFormat extends FileFormat {
2223

@@ -130,8 +131,8 @@ ImageData[] loadFromByteStream() {
130131
*/
131132
ImageData loadIcon(int[] iconHeader) {
132133
try {
133-
FileFormat png = getFileFormat(inputStream, "PNG");
134-
if (png != null) {
134+
FileFormat png = new PNGFileFormat();
135+
if (png.isFileFormat(inputStream)) {
135136
png.loader = this.loader;
136137
return png.loadFromStream(inputStream)[0];
137138
}

bundles/org.eclipse.swt/Eclipse SWT/gtk/org/eclipse/swt/graphics/ImageLoader.java

Lines changed: 45 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -316,34 +316,37 @@ ImageData[] loadFromFile(String filename) {
316316
*
317317
* It is expressed as one of the following values:
318318
* <dl>
319-
* <dt><code>IMAGE_BMP</code></dt>
319+
* <dt>{@link SWT#IMAGE_BMP}</dt>
320320
* <dd>Windows BMP file format, no compression</dd>
321-
* <dt><code>IMAGE_BMP_RLE</code></dt>
321+
* <dt>{@link SWT#IMAGE_BMP_RLE}</dt>
322322
* <dd>Windows BMP file format, RLE compression if appropriate</dd>
323-
* <dt><code>IMAGE_GIF</code></dt>
323+
* <dt>{@link SWT#IMAGE_GIF}</dt>
324324
* <dd>GIF file format</dd>
325-
* <dt><code>IMAGE_ICO</code></dt>
325+
* <dt>{@link SWT#IMAGE_ICO}</dt>
326326
* <dd>Windows ICO file format</dd>
327-
* <dt><code>IMAGE_JPEG</code></dt>
327+
* <dt>{@link SWT#IMAGE_JPEG}</dt>
328328
* <dd>JPEG file format</dd>
329-
* <dt><code>IMAGE_PNG</code></dt>
329+
* <dt>{@link SWT#IMAGE_PNG}</dt>
330330
* <dd>PNG file format</dd>
331+
* <dt>{@link SWT#IMAGE_TIFF}</dt>
332+
* <dd>TIFF file format</dd>
331333
* </dl>
332334
*/
333335
int getImageFormat(long loader) {
334336
long format = GDK.gdk_pixbuf_loader_get_format(loader);
335337
long name = GDK.gdk_pixbuf_format_get_name(format);
336338
String nameStr = Converter.cCharPtrToJavaString(name, false);
337339
OS.g_free(name);
338-
switch (nameStr) {
339-
case "bmp": return SWT.IMAGE_BMP;
340-
case "gif": return SWT.IMAGE_GIF;
341-
case "ico": return SWT.IMAGE_ICO;
342-
case "jpeg": return SWT.IMAGE_JPEG;
343-
case "png": return SWT.IMAGE_PNG;
344-
case "svg": return SWT.IMAGE_SVG;
345-
default: return SWT.IMAGE_UNDEFINED;
346-
}
340+
return switch (nameStr) {
341+
case "bmp" -> SWT.IMAGE_BMP;
342+
case "gif" -> SWT.IMAGE_GIF;
343+
case "ico" -> SWT.IMAGE_ICO;
344+
case "jpeg" -> SWT.IMAGE_JPEG;
345+
case "png" -> SWT.IMAGE_PNG;
346+
case "tiff" -> SWT.IMAGE_TIFF;
347+
case "svg" -> SWT.IMAGE_SVG;
348+
default -> SWT.IMAGE_UNDEFINED;
349+
};
347350
}
348351

349352
/**
@@ -423,18 +426,20 @@ static long gdk_pixbuf_new_from_file(String filename) {
423426
* Saves the image data in this ImageLoader to the specified stream.
424427
* The format parameter can have one of the following values:
425428
* <dl>
426-
* <dt><code>IMAGE_BMP</code></dt>
429+
* <dt>{@link SWT#IMAGE_BMP}</dt>
427430
* <dd>Windows BMP file format, no compression</dd>
428-
* <dt><code>IMAGE_BMP_RLE</code></dt>
431+
* <dt>{@link SWT#IMAGE_BMP_RLE}</dt>
429432
* <dd>Windows BMP file format, RLE compression if appropriate</dd>
430-
* <dt><code>IMAGE_GIF</code></dt>
433+
* <dt>{@link SWT#IMAGE_GIF}</dt>
431434
* <dd>GIF file format</dd>
432-
* <dt><code>IMAGE_ICO</code></dt>
435+
* <dt>{@link SWT#IMAGE_ICO}</dt>
433436
* <dd>Windows ICO file format</dd>
434-
* <dt><code>IMAGE_JPEG</code></dt>
437+
* <dt>{@link SWT#IMAGE_JPEG}</dt>
435438
* <dd>JPEG file format</dd>
436-
* <dt><code>IMAGE_PNG</code></dt>
439+
* <dt>{@link SWT#IMAGE_PNG}</dt>
437440
* <dd>PNG file format</dd>
441+
* <dt>{@link SWT#IMAGE_TIFF}</dt>
442+
* <dd>TIFF file format</dd>
438443
* </dl>
439444
*
440445
* @param stream the output stream to write the images to
@@ -539,17 +544,17 @@ public void save(OutputStream stream, int format) {
539544
}
540545

541546
// Write pixbuf to byte array and then to OutputStream
542-
String typeStr = "";
543-
switch (format) {
544-
case SWT.IMAGE_BMP_RLE: typeStr = "bmp"; break;
545-
case SWT.IMAGE_BMP: typeStr = "bmp"; break;
546-
case SWT.IMAGE_GIF: typeStr = "gif"; break;
547-
case SWT.IMAGE_ICO: typeStr = "ico"; break;
548-
case SWT.IMAGE_JPEG: typeStr = "jpeg"; break;
549-
case SWT.IMAGE_PNG: typeStr = "png"; break;
550-
case SWT.IMAGE_TIFF: typeStr = "tiff"; break;
551-
case SWT.IMAGE_SVG: typeStr = "svg"; break;
552-
}
547+
String typeStr = switch (format) {
548+
case SWT.IMAGE_BMP_RLE -> "bmp";
549+
case SWT.IMAGE_BMP -> "bmp";
550+
case SWT.IMAGE_GIF -> "gif";
551+
case SWT.IMAGE_ICO -> "ico";
552+
case SWT.IMAGE_JPEG -> "jpeg";
553+
case SWT.IMAGE_PNG -> "png";
554+
case SWT.IMAGE_TIFF -> "tiff";
555+
case SWT.IMAGE_SVG -> "svg";
556+
default -> "";
557+
};
553558
byte [] type = Converter.wcsToMbcs(typeStr, true);
554559

555560
long [] buffer = new long [1];
@@ -575,18 +580,20 @@ public void save(OutputStream stream, int format) {
575580
* Saves the image data in this ImageLoader to a file with the specified name.
576581
* The format parameter can have one of the following values:
577582
* <dl>
578-
* <dt><code>IMAGE_BMP</code></dt>
583+
* <dt>{@link SWT#IMAGE_BMP}</dt>
579584
* <dd>Windows BMP file format, no compression</dd>
580-
* <dt><code>IMAGE_BMP_RLE</code></dt>
585+
* <dt>{@link SWT#IMAGE_BMP_RLE}</dt>
581586
* <dd>Windows BMP file format, RLE compression if appropriate</dd>
582-
* <dt><code>IMAGE_GIF</code></dt>
587+
* <dt>{@link SWT#IMAGE_GIF}</dt>
583588
* <dd>GIF file format</dd>
584-
* <dt><code>IMAGE_ICO</code></dt>
589+
* <dt>{@link SWT#IMAGE_ICO}</dt>
585590
* <dd>Windows ICO file format</dd>
586-
* <dt><code>IMAGE_JPEG</code></dt>
591+
* <dt>{@link SWT#IMAGE_JPEG}</dt>
587592
* <dd>JPEG file format</dd>
588-
* <dt><code>IMAGE_PNG</code></dt>
593+
* <dt>{@link SWT#IMAGE_PNG}</dt>
589594
* <dd>PNG file format</dd>
595+
* <dt>{@link SWT#IMAGE_TIFF}</dt>
596+
* <dd>TIFF file format</dd>
590597
* </dl>
591598
*
592599
* @param filename the name of the file to write the images to

bundles/org.eclipse.swt/Eclipse SWT/win32/org/eclipse/swt/graphics/ImageLoader.java

Lines changed: 16 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -187,18 +187,20 @@ public ImageData[] load(String filename) {
187187
* Saves the image data in this ImageLoader to the specified stream.
188188
* The format parameter can have one of the following values:
189189
* <dl>
190-
* <dt><code>IMAGE_BMP</code></dt>
190+
* <dt>{@link SWT#IMAGE_BMP}</dt>
191191
* <dd>Windows BMP file format, no compression</dd>
192-
* <dt><code>IMAGE_BMP_RLE</code></dt>
192+
* <dt>{@link SWT#IMAGE_BMP_RLE}</dt>
193193
* <dd>Windows BMP file format, RLE compression if appropriate</dd>
194-
* <dt><code>IMAGE_GIF</code></dt>
194+
* <dt>{@link SWT#IMAGE_GIF}</dt>
195195
* <dd>GIF file format</dd>
196-
* <dt><code>IMAGE_ICO</code></dt>
196+
* <dt>{@link SWT#IMAGE_ICO}</dt>
197197
* <dd>Windows ICO file format</dd>
198-
* <dt><code>IMAGE_JPEG</code></dt>
198+
* <dt>{@link SWT#IMAGE_JPEG}</dt>
199199
* <dd>JPEG file format</dd>
200-
* <dt><code>IMAGE_PNG</code></dt>
200+
* <dt>{@link SWT#IMAGE_PNG}</dt>
201201
* <dd>PNG file format</dd>
202+
* <dt>{@link SWT#IMAGE_TIFF}</dt>
203+
* <dd>TIFF file format</dd>
202204
* </dl>
203205
*
204206
* @param stream the output stream to write the images to
@@ -222,18 +224,20 @@ public void save(OutputStream stream, int format) {
222224
* Saves the image data in this ImageLoader to a file with the specified name.
223225
* The format parameter can have one of the following values:
224226
* <dl>
225-
* <dt><code>IMAGE_BMP</code></dt>
227+
* <dt>{@link SWT#IMAGE_BMP}</dt>
226228
* <dd>Windows BMP file format, no compression</dd>
227-
* <dt><code>IMAGE_BMP_RLE</code></dt>
229+
* <dt>{@link SWT#IMAGE_BMP_RLE}</dt>
228230
* <dd>Windows BMP file format, RLE compression if appropriate</dd>
229-
* <dt><code>IMAGE_GIF</code></dt>
231+
* <dt>{@link SWT#IMAGE_GIF}</dt>
230232
* <dd>GIF file format</dd>
231-
* <dt><code>IMAGE_ICO</code></dt>
233+
* <dt>{@link SWT#IMAGE_ICO}</dt>
232234
* <dd>Windows ICO file format</dd>
233-
* <dt><code>IMAGE_JPEG</code></dt>
235+
* <dt>{@link SWT#IMAGE_JPEG}</dt>
234236
* <dd>JPEG file format</dd>
235-
* <dt><code>IMAGE_PNG</code></dt>
237+
* <dt>{@link SWT#IMAGE_PNG}</dt>
236238
* <dd>PNG file format</dd>
239+
* <dt>{@link SWT#IMAGE_TIFF}</dt>
240+
* <dd>TIFF file format</dd>
237241
* </dl>
238242
*
239243
* @param filename the name of the file to write the images to

0 commit comments

Comments
 (0)