@@ -63,24 +63,22 @@ public ImageData rasterizeSVG(byte[] bytes, float scalingFactor) throws IOExcept
6363 svgLoader = new SVGLoader ();
6464 }
6565 SVGDocument svgDocument = null ;
66- if (isSVGFile (bytes )) {
67- try (InputStream stream = new ByteArrayInputStream (bytes )) {
68- svgDocument = svgLoader .load (stream , null , LoaderContext .createDefault ());
69- }
70- if (svgDocument != null ) {
71- FloatSize size = svgDocument .size ();
72- double originalWidth = size .getWidth ();
73- double originalHeight = size .getHeight ();
74- int scaledWidth = (int ) Math .round (originalWidth * scalingFactor );
75- int scaledHeight = (int ) Math .round (originalHeight * scalingFactor );
76- BufferedImage image = new BufferedImage (scaledWidth , scaledHeight , BufferedImage .TYPE_INT_ARGB );
77- Graphics2D g = image .createGraphics ();
78- g .setRenderingHints (RENDERING_HINTS );
79- g .scale (scalingFactor , scalingFactor );
80- svgDocument .render (null , g );
81- g .dispose ();
82- return convertToSWT (image );
83- }
66+ try (InputStream stream = new ByteArrayInputStream (bytes )) {
67+ svgDocument = svgLoader .load (stream , null , LoaderContext .createDefault ());
68+ }
69+ if (svgDocument != null ) {
70+ FloatSize size = svgDocument .size ();
71+ double originalWidth = size .getWidth ();
72+ double originalHeight = size .getHeight ();
73+ int scaledWidth = (int ) Math .round (originalWidth * scalingFactor );
74+ int scaledHeight = (int ) Math .round (originalHeight * scalingFactor );
75+ BufferedImage image = new BufferedImage (scaledWidth , scaledHeight , BufferedImage .TYPE_INT_ARGB );
76+ Graphics2D g = image .createGraphics ();
77+ g .setRenderingHints (RENDERING_HINTS );
78+ g .scale (scalingFactor , scalingFactor );
79+ svgDocument .render (null , g );
80+ g .dispose ();
81+ return convertToSWT (image );
8482 }
8583 return null ;
8684 }
@@ -155,16 +153,11 @@ else if (bufferedImage.getColorModel() instanceof ComponentColorModel) {
155153 return null ;
156154 }
157155
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-
163156 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 ) ;
157+ if (inputStream == null ) {
158+ throw new IllegalArgumentException ("InputStream cannot be null" );
159+ }
160+ int firstByte = inputStream .read ( );
161+ return firstByte == '<' ;
169162 }
170163}
0 commit comments