Skip to content

Commit

Permalink
Utilize mark and reset for stream handling
Browse files Browse the repository at this point in the history
  • Loading branch information
Michael5601 committed Dec 12, 2024
1 parent 1ccfe65 commit b5365d8
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 26 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down Expand Up @@ -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();
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down

0 comments on commit b5365d8

Please sign in to comment.