Skip to content

Commit 9648549

Browse files
committed
Remove SVGUtil and move utility methods into JSVGRasterizer
1 parent ffa17e4 commit 9648549

File tree

3 files changed

+25
-56
lines changed

3 files changed

+25
-56
lines changed

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

+15-2
Original file line numberDiff line numberDiff line change
@@ -16,13 +16,13 @@
1616
import java.awt.*;
1717
import java.awt.image.*;
1818
import java.io.*;
19+
import java.nio.charset.StandardCharsets;
1920
import java.util.*;
2021
import org.eclipse.swt.graphics.SVGRasterizer;
2122
import org.eclipse.swt.graphics.ImageData;
2223
import org.eclipse.swt.graphics.PaletteData;
2324
import org.eclipse.swt.graphics.RGB;
2425
import org.eclipse.swt.graphics.SVGRasterizerRegistry;
25-
import org.eclipse.swt.graphics.SVGUtil;
2626

2727
import com.github.weisj.jsvg.*;
2828
import com.github.weisj.jsvg.geometry.size.*;
@@ -63,7 +63,7 @@ public ImageData rasterizeSVG(byte[] bytes, float scalingFactor) throws IOExcept
6363
svgLoader = new SVGLoader();
6464
}
6565
SVGDocument svgDocument = null;
66-
if (SVGUtil.isSVGFile(bytes)) {
66+
if (isSVGFile(bytes)) {
6767
try (InputStream stream = new ByteArrayInputStream(bytes)) {
6868
svgDocument = svgLoader.load(stream, null, LoaderContext.createDefault());
6969
}
@@ -154,4 +154,17 @@ else if (bufferedImage.getColorModel() instanceof ComponentColorModel) {
154154
}
155155
return null;
156156
}
157+
158+
private boolean isSVGFile(byte[] data) throws IOException {
159+
String content = new String(data, 0, Math.min(data.length, 512), StandardCharsets.UTF_8);
160+
return content.contains("<svg");
161+
}
162+
163+
public boolean isSVGFile(InputStream inputStream) throws IOException {
164+
if (inputStream == null) {
165+
throw new IllegalArgumentException("InputStream cannot be null");
166+
}
167+
byte[] data = inputStream.readNBytes(512);
168+
return isSVGFile(data);
169+
}
157170
}

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

+10
Original file line numberDiff line numberDiff line change
@@ -34,4 +34,14 @@ public interface SVGRasterizer {
3434
*/
3535
public ImageData rasterizeSVG(byte[] bytes, float scalingFactor) throws IOException;
3636

37+
/**
38+
* Determines whether the given {@link InputStream} contains a SVG file.
39+
*
40+
* @param inputStream the input stream to check.
41+
* @return {@code true} if the input stream contains SVG content; {@code false}
42+
* otherwise.
43+
* @throws IOException if an error occurs while reading the stream.
44+
* @throws IllegalArgumentException if the input stream is {@code null}.
45+
*/
46+
public boolean isSVGFile(InputStream inputStream) throws IOException;
3747
}

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

-54
This file was deleted.

0 commit comments

Comments
 (0)