Skip to content

Commit b611c02

Browse files
ars18wrwiText-CI
authored andcommitted
Add ImageTypeDetector's stream overload
DEVSIX-5172
1 parent 131777e commit b611c02

File tree

2 files changed

+130
-13
lines changed

2 files changed

+130
-13
lines changed

io/src/main/java/com/itextpdf/io/image/ImageTypeDetector.java

Lines changed: 23 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,8 @@ private ImageTypeDetector() {
4949
}
5050

5151
/**
52-
* Detect image type by magic bytes given the byte array source
52+
* Detect image type by magic bytes given the byte array source.
53+
*
5354
* @param source image bytes
5455
* @return detected image type, see{@link ImageType}. Returns {@link ImageType#NONE} if image type is unknown
5556
*/
@@ -59,7 +60,8 @@ public static ImageType detectImageType(byte[] source) {
5960
}
6061

6162
/**
62-
* Detect image type by magic bytes given the source URL
63+
* Detect image type by magic bytes given the source URL.
64+
*
6365
* @param source image URL
6466
* @return detected image type, see{@link ImageType}. Returns {@link ImageType#NONE} if image type is unknown
6567
*/
@@ -68,6 +70,17 @@ public static ImageType detectImageType(URL source) {
6870
return detectImageTypeByHeader(header);
6971
}
7072

73+
/**
74+
* Detect image type by magic bytes given the input stream.
75+
*
76+
* @param stream image stream
77+
* @return detected image type, see{@link ImageType}. Returns {@link ImageType#NONE} if image type is unknown
78+
*/
79+
public static ImageType detectImageType(InputStream stream) {
80+
byte[] header = readImageType(stream);
81+
return detectImageTypeByHeader(header);
82+
}
83+
7184
private static ImageType detectImageTypeByHeader(byte[] header) {
7285
if (imageTypeIs(header, gif)) {
7386
return ImageType.GIF;
@@ -99,6 +112,14 @@ private static boolean imageTypeIs(byte[] imageType, byte[] compareWith) {
99112

100113
private static byte[] readImageType(URL source) {
101114
try (InputStream stream = UrlUtil.openStream(source)) {
115+
return readImageType(stream);
116+
} catch (java.io.IOException e) {
117+
throw new IOException(IOException.IoException, e);
118+
}
119+
}
120+
121+
private static byte[] readImageType(InputStream stream) {
122+
try {
102123
byte[] bytes = new byte[8];
103124
stream.read(bytes);
104125
return bytes;

io/src/test/java/com/itextpdf/io/image/ImageTypeDetectorTest.java

Lines changed: 107 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -22,49 +22,145 @@ This file is part of the iText (R) project.
2222
*/
2323
package com.itextpdf.io.image;
2424

25+
import com.itextpdf.io.util.StreamUtil;
2526
import com.itextpdf.io.util.UrlUtil;
2627
import com.itextpdf.test.ExtendedITextTest;
2728
import com.itextpdf.test.annotations.type.UnitTest;
2829

30+
import java.io.FileInputStream;
31+
import java.io.FileNotFoundException;
32+
import java.io.IOException;
33+
import java.io.InputStream;
2934
import java.net.MalformedURLException;
3035
import java.net.URL;
3136
import org.junit.Assert;
37+
import org.junit.Rule;
3238
import org.junit.Test;
3339
import org.junit.experimental.categories.Category;
40+
import org.junit.rules.ExpectedException;
3441

3542
@Category(UnitTest.class)
3643
public class ImageTypeDetectorTest extends ExtendedITextTest {
3744

3845
private static final String SOURCE_FOLDER = "./src/test/resources/com/itextpdf/io/image/ImageTypeDetectorTest/";
3946
private static final String IMAGE_NAME = "image";
4047

48+
@Rule
49+
public ExpectedException junitExpectedException = ExpectedException.none();
50+
51+
@Test
52+
public void testUrlUnknown() throws MalformedURLException {
53+
testURL(UrlUtil.toURL(SOURCE_FOLDER + IMAGE_NAME + ".txt"), ImageType.NONE);
54+
}
55+
56+
@Test
57+
public void testUrlGif() throws MalformedURLException {
58+
testURL(UrlUtil.toURL(SOURCE_FOLDER + IMAGE_NAME + ".gif"), ImageType.GIF);
59+
}
60+
61+
@Test
62+
public void testUrlJpeg() throws MalformedURLException {
63+
testURL(UrlUtil.toURL(SOURCE_FOLDER + IMAGE_NAME + ".jpg"), ImageType.JPEG);
64+
}
65+
66+
@Test
67+
public void testUrlTiff() throws MalformedURLException {
68+
testURL(UrlUtil.toURL(SOURCE_FOLDER + IMAGE_NAME + ".tiff"), ImageType.TIFF);
69+
}
70+
71+
@Test
72+
public void testUrlWmf() throws MalformedURLException {
73+
testURL(UrlUtil.toURL(SOURCE_FOLDER + IMAGE_NAME + ".wmf"), ImageType.WMF);
74+
}
75+
76+
@Test
77+
public void testNullUrl() throws MalformedURLException {
78+
junitExpectedException.expect(com.itextpdf.io.IOException.class);
79+
80+
ImageTypeDetector.detectImageType(UrlUtil.toURL("not existing path"));
81+
82+
Assert.fail("This line is not expected to be triggered: "
83+
+ "an exception should have been thrown");
84+
}
85+
86+
@Test
87+
public void testStreamUnknown() throws FileNotFoundException {
88+
testStream(new FileInputStream(SOURCE_FOLDER + IMAGE_NAME + ".txt"), ImageType.NONE);
89+
}
90+
91+
@Test
92+
public void testStreamGif() throws FileNotFoundException {
93+
testStream(new FileInputStream(SOURCE_FOLDER + IMAGE_NAME + ".gif"), ImageType.GIF);
94+
}
95+
96+
@Test
97+
public void testStreamJpeg() throws FileNotFoundException {
98+
testStream(new FileInputStream(SOURCE_FOLDER + IMAGE_NAME + ".jpg"), ImageType.JPEG);
99+
}
100+
101+
@Test
102+
public void testStreamTiff() throws FileNotFoundException {
103+
testStream(new FileInputStream(SOURCE_FOLDER + IMAGE_NAME + ".tiff"), ImageType.TIFF);
104+
}
105+
41106
@Test
42-
public void testUnknown() throws MalformedURLException {
43-
test(UrlUtil.toURL(SOURCE_FOLDER + IMAGE_NAME + ".txt"), ImageType.NONE);
107+
public void testStreamWmf() throws FileNotFoundException {
108+
testStream(new FileInputStream(SOURCE_FOLDER + IMAGE_NAME + ".wmf"), ImageType.WMF);
44109
}
45110

46111
@Test
47-
public void testGif() throws MalformedURLException {
48-
test(UrlUtil.toURL(SOURCE_FOLDER + IMAGE_NAME + ".gif"), ImageType.GIF);
112+
public void testStreamClosed() throws IOException {
113+
// A common exception is expected instead of com.itextpdf.io.IOException, because in .NET
114+
// the thrown exception is different
115+
junitExpectedException.expect(Exception.class);
116+
117+
InputStream stream = new FileInputStream(SOURCE_FOLDER + IMAGE_NAME + ".wmf");
118+
stream.close();
119+
ImageTypeDetector.detectImageType(stream);
120+
121+
Assert.fail("This line is not expected to be triggered: "
122+
+ "an exception should have been thrown");
123+
}
124+
125+
@Test
126+
public void testBytesUnknown() throws IOException {
127+
testBytes(StreamUtil.inputStreamToArray(new FileInputStream(SOURCE_FOLDER + IMAGE_NAME + ".txt")),
128+
ImageType.NONE);
49129
}
50130

51131
@Test
52-
public void testJpeg() throws MalformedURLException {
53-
test(UrlUtil.toURL(SOURCE_FOLDER + IMAGE_NAME + ".jpg"), ImageType.JPEG);
132+
public void testBytesGif() throws IOException {
133+
testBytes(StreamUtil.inputStreamToArray(new FileInputStream(SOURCE_FOLDER + IMAGE_NAME + ".gif")),
134+
ImageType.GIF);
54135
}
55136

56137
@Test
57-
public void testTiff() throws MalformedURLException {
58-
test(UrlUtil.toURL(SOURCE_FOLDER + IMAGE_NAME + ".tiff"), ImageType.TIFF);
138+
public void testBytesJpeg() throws IOException {
139+
testBytes(StreamUtil.inputStreamToArray(new FileInputStream(SOURCE_FOLDER + IMAGE_NAME + ".jpg")),
140+
ImageType.JPEG);
59141
}
60142

61143
@Test
62-
public void testWmf() throws MalformedURLException {
63-
test(UrlUtil.toURL(SOURCE_FOLDER + IMAGE_NAME + ".wmf"), ImageType.WMF);
144+
public void testBytesTiff() throws IOException {
145+
testBytes(StreamUtil.inputStreamToArray(new FileInputStream(SOURCE_FOLDER + IMAGE_NAME + ".tiff")),
146+
ImageType.TIFF);
64147
}
65148

66-
private void test(URL location, ImageType expectedType) {
149+
@Test
150+
public void testBytesWmf() throws IOException {
151+
testBytes(StreamUtil.inputStreamToArray(new FileInputStream(SOURCE_FOLDER + IMAGE_NAME + ".wmf")),
152+
ImageType.WMF);
153+
}
154+
155+
private static void testURL(URL location, ImageType expectedType) {
67156
Assert.assertEquals(expectedType, ImageTypeDetector.detectImageType(location));
68157
}
69158

159+
private static void testStream(InputStream stream, ImageType expectedType) {
160+
Assert.assertEquals(expectedType, ImageTypeDetector.detectImageType(stream));
161+
}
162+
163+
private static void testBytes(byte[] bytes, ImageType expectedType) {
164+
Assert.assertEquals(expectedType, ImageTypeDetector.detectImageType(bytes));
165+
}
70166
}

0 commit comments

Comments
 (0)