From b5365d82069f08efd7ae04da048a65ea2271b1ec Mon Sep 17 00:00:00 2001 From: Michael Bangas Date: Thu, 12 Dec 2024 11:20:16 +0100 Subject: [PATCH] Utilize mark and reset for stream handling --- .../org/eclipse/swt/svg/JSVGRasterizer.java | 26 +++++++++++++---- .../eclipse/swt/graphics/SVGRasterizer.java | 4 +-- .../org/eclipse/swt/graphics/ImageLoader.java | 29 +++++++------------ 3 files changed, 33 insertions(+), 26 deletions(-) diff --git a/bundles/org.eclipse.swt.svg/src/org/eclipse/swt/svg/JSVGRasterizer.java b/bundles/org.eclipse.swt.svg/src/org/eclipse/swt/svg/JSVGRasterizer.java index 97226be972..6042a8d4d4 100644 --- a/bundles/org.eclipse.swt.svg/src/org/eclipse/swt/svg/JSVGRasterizer.java +++ b/bundles/org.eclipse.swt.svg/src/org/eclipse/swt/svg/JSVGRasterizer.java @@ -58,11 +58,22 @@ public static void intializeJSVGRasterizer() { @Override public ImageData rasterizeSVG(InputStream stream, float scalingFactor) throws IOException { + if (stream == null) { + throw new IllegalArgumentException("InputStream cannot be null"); + } + stream.mark(Integer.MAX_VALUE); if(svgLoader == null) { svgLoader = new SVGLoader(); } SVGDocument svgDocument = null; - svgDocument = svgLoader.load(stream, null, LoaderContext.createDefault()); + InputStream nonClosingStream = new FilterInputStream(stream) { + @Override + public void close() throws IOException { + // Do nothing to prevent closing the underlying stream + } + }; + svgDocument = svgLoader.load(nonClosingStream, null, LoaderContext.createDefault()); + stream.reset(); if (svgDocument != null) { FloatSize size = svgDocument.size(); double originalWidth = size.getWidth(); @@ -150,11 +161,16 @@ else if (bufferedImage.getColorModel() instanceof ComponentColorModel) { return null; } - public boolean isSVGFile(InputStream inputStream) throws IOException { - if (inputStream == null) { + public boolean isSVGFile(InputStream stream) throws IOException { + if (stream == null) { throw new IllegalArgumentException("InputStream cannot be null"); } - int firstByte = inputStream.read(); - return firstByte == '<'; + stream.mark(Integer.MAX_VALUE); + try { + int firstByte = stream.read(); + return firstByte == '<'; + } finally { + stream.reset(); + } } } diff --git a/bundles/org.eclipse.swt/Eclipse SWT/common/org/eclipse/swt/graphics/SVGRasterizer.java b/bundles/org.eclipse.swt/Eclipse SWT/common/org/eclipse/swt/graphics/SVGRasterizer.java index a7c70fea94..46140bffe5 100644 --- a/bundles/org.eclipse.swt/Eclipse SWT/common/org/eclipse/swt/graphics/SVGRasterizer.java +++ b/bundles/org.eclipse.swt/Eclipse SWT/common/org/eclipse/swt/graphics/SVGRasterizer.java @@ -37,11 +37,11 @@ public interface SVGRasterizer { /** * Determines whether the given {@link InputStream} contains a SVG file. * - * @param inputStream the input stream to check. + * @param stream the input stream to check. * @return {@code true} if the input stream contains SVG content; {@code false} * otherwise. * @throws IOException if an error occurs while reading the stream. * @throws IllegalArgumentException if the input stream is {@code null}. */ - public boolean isSVGFile(InputStream inputStream) throws IOException; + public boolean isSVGFile(InputStream stream) throws IOException; } diff --git a/bundles/org.eclipse.swt/Eclipse SWT/win32/org/eclipse/swt/graphics/ImageLoader.java b/bundles/org.eclipse.swt/Eclipse SWT/win32/org/eclipse/swt/graphics/ImageLoader.java index afd2da911d..9db760bec2 100644 --- a/bundles/org.eclipse.swt/Eclipse SWT/win32/org/eclipse/swt/graphics/ImageLoader.java +++ b/bundles/org.eclipse.swt/Eclipse SWT/win32/org/eclipse/swt/graphics/ImageLoader.java @@ -178,35 +178,26 @@ public ImageData[] load(InputStream stream) { public ImageData[] load(InputStream stream, int zoom) { if (stream == null) SWT.error(SWT.ERROR_NULL_ARGUMENT); reset(); - byte[] bytes = null; - try { - bytes = stream.readAllBytes(); - } catch (IOException e) { - SWT.error(SWT.ERROR_IO, e); + if (!stream.markSupported()) { + stream = new BufferedInputStream(stream); } + SVGRasterizer rasterizer = SVGRasterizerRegistry.getRasterizer(); if (rasterizer != null && zoom != 0) { - try (InputStream imageStream = new ByteArrayInputStream(bytes)) { - if (rasterizer.isSVGFile(imageStream)) { + try { + if (rasterizer.isSVGFile(stream)) { float scalingFactor = zoom / 100.0f; - try (InputStream svgFileStream = new ByteArrayInputStream(bytes)) { - ImageData rasterizedData = rasterizer.rasterizeSVG(svgFileStream, scalingFactor); - if (rasterizedData != null) { - data = new ImageData[]{rasterizedData}; - return data; - } + ImageData rasterizedData = rasterizer.rasterizeSVG(stream, scalingFactor); + if (rasterizedData != null) { + data = new ImageData[]{rasterizedData}; + return data; } } } catch (IOException e) { //ignore. } } - try (InputStream fallbackStream = new ByteArrayInputStream(bytes)) { - return loadDefault(fallbackStream); - } catch (IOException e) { - SWT.error(SWT.ERROR_IO, e); - } - return null; + return loadDefault(stream); } private ImageData[] loadDefault(InputStream stream) {