Skip to content

Commit c9559f4

Browse files
committed
Use Path to represent file-system paths for ImageData creation
Use java.nio.file.Path to model file-system paths instead of String to represent file-system paths. Add the new way to create ImageData as static factory instead of a constructor, because a constructor is not really suitable to create a copy of the first element of an array of ImageData. Also inline the ImageDataLoader and use the new ImageData.load() factories instead.
1 parent 9f60279 commit c9559f4

File tree

11 files changed

+573
-144
lines changed

11 files changed

+573
-144
lines changed

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

+62-24
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*******************************************************************************
2-
* Copyright (c) 2000, 2020 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,7 @@
1515

1616

1717
import java.io.*;
18+
import java.nio.file.Path;
1819
import java.util.*;
1920

2021
import org.eclipse.swt.*;
@@ -127,9 +128,9 @@ public final class Image extends Resource implements Drawable {
127128
static final int DEFAULT_SCANLINE_PAD = 4;
128129

129130
/**
130-
* ImageFileNameProvider to provide file names at various Zoom levels
131+
* ImageFileProvider to provide files at various Zoom levels
131132
*/
132-
private ImageFileNameProvider imageFileNameProvider;
133+
private ImageFileProvider imageFileProvider;
133134

134135
/**
135136
* ImageDataProvider to provide ImageData at various Zoom levels
@@ -388,11 +389,11 @@ public Image(Device device, Image srcImage, int flag) {
388389
/* Create the 100% representation for the new image from source image & apply flag */
389390
createRepFromSourceAndApplyFlag(srcImage.getRepresentation (100), srcWidth, srcHeight, flag);
390391

391-
imageFileNameProvider = srcImage.imageFileNameProvider;
392+
imageFileProvider = srcImage.imageFileProvider;
392393
imageDataProvider = srcImage.imageDataProvider;
393394
imageGcDrawer = srcImage.imageGcDrawer;
394395
this.styleFlag = srcImage.styleFlag | flag;
395-
if (imageFileNameProvider != null || imageDataProvider != null ||srcImage.imageGcDrawer != null) {
396+
if (imageFileProvider != null || imageDataProvider != null ||srcImage.imageGcDrawer != null) {
396397
/* If source image has 200% representation then create the 200% representation for the new image & apply flag */
397398
NSBitmapImageRep rep200 = srcImage.getRepresentation (200);
398399
if (rep200 != null) createRepFromSourceAndApplyFlag(rep200, srcWidth * 2, srcHeight * 2, flag);
@@ -692,7 +693,7 @@ public Image(Device device, InputStream stream) {
692693
NSAutoreleasePool pool = null;
693694
if (!NSThread.isMainThread()) pool = (NSAutoreleasePool) new NSAutoreleasePool().alloc().init();
694695
try {
695-
init(new ImageData(stream));
696+
init(ImageData.load(stream));
696697
init();
697698
} finally {
698699
if (pool != null) pool.release();
@@ -737,8 +738,9 @@ public Image(Device device, String filename) {
737738
if (!NSThread.isMainThread()) pool = (NSAutoreleasePool) new NSAutoreleasePool().alloc().init();
738739
try {
739740
if (filename == null) SWT.error(SWT.ERROR_NULL_ARGUMENT);
740-
initNative(filename);
741-
if (this.handle == null) init(new ImageData(filename));
741+
Path file = Path.of(filename);
742+
initNative(file);
743+
if (this.handle == null) init(ImageData.load(file));
742744
init();
743745
} finally {
744746
if (pool != null) pool.release();
@@ -773,23 +775,58 @@ public Image(Device device, String filename) {
773775
* <li>ERROR_NO_HANDLES if a handle could not be obtained for image creation</li>
774776
* </ul>
775777
* @since 3.104
778+
* @deprecated Instead use {@link #Image(Device, ImageFileProvider)}
776779
*/
780+
@Deprecated(since = "2025-06")
777781
public Image(Device device, ImageFileNameProvider imageFileNameProvider) {
782+
this(device, DPIUtil.asImageFileProvider(imageFileNameProvider));
783+
}
784+
785+
/**
786+
* Constructs an instance of this class by loading its representation
787+
* from the file retrieved from the {@link ImageFileProvider}. Throws an
788+
* error if an error occurs while loading the image, or if the result
789+
* is an image of an unsupported type.
790+
* <p>
791+
* This constructor is provided for convenience for loading image as
792+
* per DPI level.
793+
*
794+
* @param device the device on which to create the image
795+
* @param imageFileProvider the {@link ImageFileProvider} object that is
796+
* to be used to get the file
797+
*
798+
* @exception IllegalArgumentException <ul>
799+
* <li>ERROR_NULL_ARGUMENT - if device is null and there is no current device</li>
800+
* <li>ERROR_NULL_ARGUMENT - if the ImageFileNameProvider is null</li>
801+
* <li>ERROR_INVALID_ARGUMENT - if the fileName provided by ImageFileNameProvider is null at 100% zoom</li>
802+
* </ul>
803+
* @exception SWTException <ul>
804+
* <li>ERROR_IO - if an IO error occurs while reading from the file</li>
805+
* <li>ERROR_INVALID_IMAGE - if the image file contains invalid data </li>
806+
* <li>ERROR_UNSUPPORTED_DEPTH - if the image file describes an image with an unsupported depth</li>
807+
* <li>ERROR_UNSUPPORTED_FORMAT - if the image file contains an unrecognized format</li>
808+
* </ul>
809+
* @exception SWTError <ul>
810+
* <li>ERROR_NO_HANDLES if a handle could not be obtained for image creation</li>
811+
* </ul>
812+
* @since 3.129
813+
*/
814+
public Image(Device device, ImageFileProvider imageFileProvider) {
778815
super(device);
779-
if (imageFileNameProvider == null) SWT.error(SWT.ERROR_NULL_ARGUMENT);
780-
this.imageFileNameProvider = imageFileNameProvider;
781-
String filename = imageFileNameProvider.getImagePath(100);
782-
if (filename == null) SWT.error(SWT.ERROR_INVALID_ARGUMENT);
816+
if (imageFileProvider == null) SWT.error(SWT.ERROR_NULL_ARGUMENT);
817+
this.imageFileProvider = imageFileProvider;
818+
Path file = imageFileProvider.getImagePath(100);
819+
if (file == null) SWT.error(SWT.ERROR_INVALID_ARGUMENT);
783820
NSAutoreleasePool pool = null;
784821
if (!NSThread.isMainThread()) pool = (NSAutoreleasePool) new NSAutoreleasePool().alloc().init();
785822
try {
786-
initNative(filename);
787-
if (this.handle == null) init(new ImageData(filename));
823+
initNative(file);
824+
if (this.handle == null) init(ImageData.load(file));
788825
init();
789-
String filename2x = imageFileNameProvider.getImagePath(200);
790-
if (filename2x != null) {
826+
Path file2x = imageFileProvider.getImagePath(200);
827+
if (file2x != null) {
791828
alphaInfo_200 = new AlphaInfo();
792-
id id = NSImageRep.imageRepWithContentsOfFile(NSString.stringWith(filename2x));
829+
id id = NSImageRep.imageRepWithContentsOfFile(NSString.stringWith(file2x.toString()));
793830
NSImageRep rep = new NSImageRep(id);
794831
handle.addRepresentation(rep);
795832
}
@@ -880,7 +917,7 @@ public Image(Device device, ImageGcDrawer imageGcDrawer, int width, int height)
880917
if (!NSThread.isMainThread()) pool = (NSAutoreleasePool) new NSAutoreleasePool().alloc().init();
881918
try {
882919
init (data);
883-
init ();
920+
init ();
884921
} finally {
885922
if (pool != null) pool.release();
886923
}
@@ -902,7 +939,7 @@ private ImageData drawWithImageGcDrawer(ImageGcDrawer imageGcDrawer, int width,
902939

903940
private AlphaInfo _getAlphaInfoAtCurrentZoom (NSBitmapImageRep rep) {
904941
int deviceZoom = DPIUtil.getDeviceZoom();
905-
if (deviceZoom != 100 && (imageFileNameProvider != null || imageDataProvider != null)) {
942+
if (deviceZoom != 100 && (imageFileProvider != null || imageDataProvider != null)) {
906943
if (alphaInfo_100.alphaData != null && alphaInfo_200 != null) {
907944
if (alphaInfo_200.alphaData == null) initAlpha_200(rep);
908945
return alphaInfo_200;
@@ -1176,8 +1213,8 @@ public boolean equals (Object object) {
11761213
if (device != image.device || alphaInfo_100.transparentPixel != image.alphaInfo_100.transparentPixel) return false;
11771214
if (imageDataProvider != null && image.imageDataProvider != null) {
11781215
return styleFlag == image.styleFlag && imageDataProvider.equals (image.imageDataProvider);
1179-
} else if (imageFileNameProvider != null && image.imageFileNameProvider != null) {
1180-
return styleFlag == image.styleFlag && imageFileNameProvider.equals (image.imageFileNameProvider);
1216+
} else if (imageFileProvider != null && image.imageFileProvider != null) {
1217+
return styleFlag == image.styleFlag && imageFileProvider.equals (image.imageFileProvider);
11811218
} else if (imageGcDrawer != null && image.imageGcDrawer != null) {
11821219
return styleFlag == image.styleFlag && imageGcDrawer.equals(image.imageGcDrawer) && width == image.width
11831220
&& height == image.height;
@@ -1415,8 +1452,8 @@ NSBitmapImageRep createImageRep(NSSize targetSize) {
14151452
public int hashCode () {
14161453
if (imageDataProvider != null) {
14171454
return imageDataProvider.hashCode();
1418-
} else if (imageFileNameProvider != null) {
1419-
return imageFileNameProvider.hashCode();
1455+
} else if (imageFileProvider != null) {
1456+
return imageFileProvider.hashCode();
14201457
} else if (imageGcDrawer != null) {
14211458
return Objects.hash(imageGcDrawer, height, width);
14221459
} else {
@@ -1507,7 +1544,8 @@ void initAlpha_100(NSBitmapImageRep nativeRep) {
15071544

15081545
}
15091546

1510-
void initNative(String filename) {
1547+
void initNative(Path file) {
1548+
String filename = file.toString();
15111549
NSAutoreleasePool pool = null;
15121550
NSImage nativeImage = null;
15131551

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

+71-4
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*******************************************************************************
2-
* Copyright (c) 2000, 2016 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
@@ -13,8 +13,9 @@
1313
*******************************************************************************/
1414
package org.eclipse.swt.graphics;
1515

16-
1716
import java.io.*;
17+
import java.nio.file.*;
18+
import java.nio.file.Path;
1819
import java.util.*;
1920

2021
import org.eclipse.swt.*;
@@ -172,10 +173,36 @@ public ImageData[] load(InputStream stream) {
172173
* <li>ERROR_INVALID_IMAGE - if the image file contains invalid data</li>
173174
* <li>ERROR_UNSUPPORTED_FORMAT - if the image file contains an unrecognized format</li>
174175
* </ul>
176+
* @deprecated Instead use {@link #load(Path)}
175177
*/
178+
@Deprecated(since = "2025-06")
176179
public ImageData[] load(String filename) {
177180
if (filename == null) SWT.error(SWT.ERROR_NULL_ARGUMENT);
178-
try (InputStream stream = new FileInputStream(filename)) {
181+
return load(Path.of(filename));
182+
}
183+
184+
/**
185+
* Loads an array of <code>ImageData</code> objects from the
186+
* file with the specified name. Throws an error if either
187+
* an error occurs while loading the images, or if the images are
188+
* not of a supported type. Returns the loaded image data array.
189+
*
190+
* @param file the name of the file to load the images from
191+
* @return an array of <code>ImageData</code> objects loaded from the specified file
192+
*
193+
* @exception IllegalArgumentException <ul>
194+
* <li>ERROR_NULL_ARGUMENT - if the file name is null</li>
195+
* </ul>
196+
* @exception SWTException <ul>
197+
* <li>ERROR_IO - if an IO error occurs while reading the file</li>
198+
* <li>ERROR_INVALID_IMAGE - if the image file contains invalid data</li>
199+
* <li>ERROR_UNSUPPORTED_FORMAT - if the image file contains an unrecognized format</li>
200+
* </ul>
201+
* @since 3.129
202+
*/
203+
public ImageData[] load(Path file) {
204+
if (file == null) SWT.error(SWT.ERROR_NULL_ARGUMENT);
205+
try (InputStream stream = Files.newInputStream(file)) {
179206
return load(stream);
180207
} catch (IOException e) {
181208
SWT.error(SWT.ERROR_IO, e);
@@ -251,10 +278,50 @@ public void save(OutputStream stream, int format) {
251278
* <li>ERROR_INVALID_IMAGE - if the image data contains invalid data</li>
252279
* <li>ERROR_UNSUPPORTED_FORMAT - if the image data cannot be saved to the requested format</li>
253280
* </ul>
281+
* @deprecated Instead use {@link #save(Path, int)}
254282
*/
283+
@Deprecated(since = "2025-06")
255284
public void save(String filename, int format) {
256285
if (filename == null) SWT.error(SWT.ERROR_NULL_ARGUMENT);
257-
try (OutputStream stream = new FileOutputStream(filename)) {
286+
save(Path.of(filename), format);
287+
}
288+
289+
/**
290+
* Saves the image data in this ImageLoader to a file with the specified name.
291+
* The format parameter can have one of the following values:
292+
* <dl>
293+
* <dt>{@link SWT#IMAGE_BMP}</dt>
294+
* <dd>Windows BMP file format, no compression</dd>
295+
* <dt>{@link SWT#IMAGE_BMP_RLE}</dt>
296+
* <dd>Windows BMP file format, RLE compression if appropriate</dd>
297+
* <dt>{@link SWT#IMAGE_GIF}</dt>
298+
* <dd>GIF file format</dd>
299+
* <dt>{@link SWT#IMAGE_ICO}</dt>
300+
* <dd>Windows ICO file format</dd>
301+
* <dt>{@link SWT#IMAGE_JPEG}</dt>
302+
* <dd>JPEG file format</dd>
303+
* <dt>{@link SWT#IMAGE_PNG}</dt>
304+
* <dd>PNG file format</dd>
305+
* <dt>{@link SWT#IMAGE_TIFF}</dt>
306+
* <dd>TIFF file format</dd>
307+
* </dl>
308+
*
309+
* @param file the name of the file to write the images to
310+
* @param format the format to write the images in
311+
*
312+
* @exception IllegalArgumentException <ul>
313+
* <li>ERROR_NULL_ARGUMENT - if the file name is null</li>
314+
* </ul>
315+
* @exception SWTException <ul>
316+
* <li>ERROR_IO - if an IO error occurs while writing to the file</li>
317+
* <li>ERROR_INVALID_IMAGE - if the image data contains invalid data</li>
318+
* <li>ERROR_UNSUPPORTED_FORMAT - if the image data cannot be saved to the requested format</li>
319+
* </ul>
320+
* @since 3.129
321+
*/
322+
public void save(Path file, int format) {
323+
if (file == null) SWT.error(SWT.ERROR_NULL_ARGUMENT);
324+
try (OutputStream stream = Files.newOutputStream(file)) {
258325
save(stream, format);
259326
} catch (IOException e) {
260327
SWT.error(SWT.ERROR_IO, e);

0 commit comments

Comments
 (0)