Skip to content

Commit b122cef

Browse files
committed
Utilize mark and reset for stream handling
1 parent 1ccfe65 commit b122cef

File tree

3 files changed

+25
-23
lines changed

3 files changed

+25
-23
lines changed

bundles/org.eclipse.swt.svg/src/org/eclipse/swt/svg/JSVGRasterizer.java

+14-3
Original file line numberDiff line numberDiff line change
@@ -58,11 +58,22 @@ public static void intializeJSVGRasterizer() {
5858

5959
@Override
6060
public ImageData rasterizeSVG(InputStream stream, float scalingFactor) throws IOException {
61+
if (stream == null) {
62+
throw new IllegalArgumentException("InputStream cannot be null");
63+
}
64+
stream.mark(Integer.MAX_VALUE);
6165
if(svgLoader == null) {
6266
svgLoader = new SVGLoader();
6367
}
6468
SVGDocument svgDocument = null;
65-
svgDocument = svgLoader.load(stream, null, LoaderContext.createDefault());
69+
InputStream nonClosingStream = new FilterInputStream(stream) {
70+
@Override
71+
public void close() throws IOException {
72+
// Do nothing to prevent closing the underlying stream
73+
}
74+
};
75+
svgDocument = svgLoader.load(nonClosingStream, null, LoaderContext.createDefault());
76+
stream.reset();
6677
if (svgDocument != null) {
6778
FloatSize size = svgDocument.size();
6879
double originalWidth = size.getWidth();
@@ -150,8 +161,8 @@ else if (bufferedImage.getColorModel() instanceof ComponentColorModel) {
150161
return null;
151162
}
152163

153-
public boolean isSVGFile(InputStream inputStream) throws IOException {
154-
if (inputStream == null) {
164+
public boolean isSVGFile(InputStream stream) throws IOException {
165+
if (stream == null) {
155166
throw new IllegalArgumentException("InputStream cannot be null");
156167
}
157168
int firstByte = inputStream.read();

bundles/org.eclipse.swt/Eclipse SWT/common/org/eclipse/swt/graphics/SVGRasterizer.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -43,5 +43,5 @@ public interface SVGRasterizer {
4343
* @throws IOException if an error occurs while reading the stream.
4444
* @throws IllegalArgumentException if the input stream is {@code null}.
4545
*/
46-
public boolean isSVGFile(InputStream inputStream) throws IOException;
46+
public boolean isSVGFile(InputStream stream) throws IOException;
4747
}

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

+10-19
Original file line numberDiff line numberDiff line change
@@ -178,35 +178,26 @@ public ImageData[] load(InputStream stream) {
178178
public ImageData[] load(InputStream stream, int zoom) {
179179
if (stream == null) SWT.error(SWT.ERROR_NULL_ARGUMENT);
180180
reset();
181-
byte[] bytes = null;
182-
try {
183-
bytes = stream.readAllBytes();
184-
} catch (IOException e) {
185-
SWT.error(SWT.ERROR_IO, e);
181+
if (!stream.markSupported()) {
182+
stream = new BufferedInputStream(stream);
186183
}
184+
187185
SVGRasterizer rasterizer = SVGRasterizerRegistry.getRasterizer();
188186
if (rasterizer != null && zoom != 0) {
189-
try (InputStream imageStream = new ByteArrayInputStream(bytes)) {
190-
if (rasterizer.isSVGFile(imageStream)) {
187+
try {
188+
if (rasterizer.isSVGFile(stream)) {
191189
float scalingFactor = zoom / 100.0f;
192-
try (InputStream svgFileStream = new ByteArrayInputStream(bytes)) {
193-
ImageData rasterizedData = rasterizer.rasterizeSVG(svgFileStream, scalingFactor);
194-
if (rasterizedData != null) {
195-
data = new ImageData[]{rasterizedData};
196-
return data;
197-
}
190+
ImageData rasterizedData = rasterizer.rasterizeSVG(stream, scalingFactor);
191+
if (rasterizedData != null) {
192+
data = new ImageData[]{rasterizedData};
193+
return data;
198194
}
199195
}
200196
} catch (IOException e) {
201197
//ignore.
202198
}
203199
}
204-
try (InputStream fallbackStream = new ByteArrayInputStream(bytes)) {
205-
return loadDefault(fallbackStream);
206-
} catch (IOException e) {
207-
SWT.error(SWT.ERROR_IO, e);
208-
}
209-
return null;
200+
return loadDefault(stream);
210201
}
211202

212203
private ImageData[] loadDefault(InputStream stream) {

0 commit comments

Comments
 (0)