embulk-decoder-commons-compress 0.3.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (34) hide show
  1. checksums.yaml +7 -0
  2. data/.gitignore +7 -0
  3. data/LICENSE.txt +23 -0
  4. data/README.md +70 -0
  5. data/build.gradle +63 -0
  6. data/gradle/wrapper/gradle-wrapper.jar +0 -0
  7. data/gradle/wrapper/gradle-wrapper.properties +6 -0
  8. data/gradlew +164 -0
  9. data/gradlew.bat +90 -0
  10. data/lib/embulk/decoder/commons-compress.rb +3 -0
  11. data/src/main/java/org/embulk/decoder/ArchiveInputStreamIterator.java +68 -0
  12. data/src/main/java/org/embulk/decoder/CommonsCompressDecoderPlugin.java +52 -0
  13. data/src/main/java/org/embulk/decoder/CommonsCompressFileInput.java +73 -0
  14. data/src/main/java/org/embulk/decoder/CommonsCompressProvider.java +182 -0
  15. data/src/main/java/org/embulk/decoder/CommonsCompressUtil.java +135 -0
  16. data/src/test/java/org/embulk/decoder/TestArchiveInputStreamIterator.java +106 -0
  17. data/src/test/java/org/embulk/decoder/TestCommonsCompressDecoderPlugin.java +593 -0
  18. data/src/test/java/org/embulk/decoder/TestCommonsCompressFileInput.java +152 -0
  19. data/src/test/java/org/embulk/decoder/TestCommonsCompressProvider.java +369 -0
  20. data/src/test/java/org/embulk/decoder/TestCommonsCompressUtil.java +80 -0
  21. data/src/test/resources/org/embulk/decoder/sample_0.tar +0 -0
  22. data/src/test/resources/org/embulk/decoder/sample_1.csv +1 -0
  23. data/src/test/resources/org/embulk/decoder/sample_1.csv.bz2 +0 -0
  24. data/src/test/resources/org/embulk/decoder/sample_1.tar +0 -0
  25. data/src/test/resources/org/embulk/decoder/sample_2.csv +1 -0
  26. data/src/test/resources/org/embulk/decoder/samples.ar +5 -0
  27. data/src/test/resources/org/embulk/decoder/samples.tar +0 -0
  28. data/src/test/resources/org/embulk/decoder/samples.tar.Z +0 -0
  29. data/src/test/resources/org/embulk/decoder/samples.tar.bz2 +0 -0
  30. data/src/test/resources/org/embulk/decoder/samples.tar.gz +0 -0
  31. data/src/test/resources/org/embulk/decoder/samples.tar.xz +0 -0
  32. data/src/test/resources/org/embulk/decoder/samples.tgz +0 -0
  33. data/src/test/resources/org/embulk/decoder/samples.zip +0 -0
  34. metadata +106 -0
@@ -0,0 +1,152 @@
1
+ package org.embulk.decoder;
2
+
3
+ import static org.junit.Assert.assertFalse;
4
+ import static org.junit.Assert.assertTrue;
5
+
6
+ import java.io.IOException;
7
+ import java.io.InputStream;
8
+
9
+ import mockit.Mocked;
10
+ import mockit.NonStrictExpectations;
11
+ import mockit.Verifications;
12
+
13
+ import org.embulk.spi.Buffer;
14
+ import org.embulk.spi.BufferAllocator;
15
+ import org.embulk.spi.util.InputStreamFileInput.Provider;
16
+ import org.junit.Test;
17
+
18
+ public class TestCommonsCompressFileInput {
19
+
20
+ @Mocked BufferAllocator allocator;
21
+ @Mocked Provider provider;
22
+
23
+ @Test
24
+ public void testPoll(@Mocked final InputStream in, @Mocked final Buffer allocBuffer) throws Exception {
25
+ final byte[] bytes = new byte[]{'f', 'o', 'o'};
26
+ final int readLength = 3;
27
+
28
+ new NonStrictExpectations() {{
29
+ provider.openNext(); result = in;
30
+ allocator.allocate(); result = allocBuffer;
31
+ allocBuffer.array(); result = bytes;
32
+ allocBuffer.offset(); result = 0;
33
+ allocBuffer.capacity(); result = bytes.length;
34
+ allocBuffer.limit(readLength); result = allocBuffer;
35
+ allocBuffer.release();
36
+ in.read(bytes, 0, bytes.length); result = readLength;
37
+ }};
38
+
39
+ CommonsCompressFileInput input = new CommonsCompressFileInput(allocator, provider);
40
+ assertTrue("Verify there is a new stream.", input.nextFile());
41
+ assertTrue("Verify allocated buffer is returned.", allocBuffer == input.poll());
42
+ input.close();
43
+
44
+ new Verifications() {{
45
+ allocBuffer.limit(readLength); times = 1;
46
+ allocBuffer.release(); times = 0;
47
+ }};
48
+ }
49
+
50
+ @Test(expected=RuntimeException.class)
51
+ public void testPollThrowsException(@Mocked final InputStream in, @Mocked final Buffer allocBuffer) throws Exception {
52
+ final byte[] bytes = new byte[]{'f', 'o', 'o'};
53
+
54
+ new NonStrictExpectations() {{
55
+ provider.openNext(); result = in;
56
+ allocator.allocate(); result = allocBuffer;
57
+ allocBuffer.array(); result = bytes;
58
+ allocBuffer.offset(); result = 0;
59
+ allocBuffer.capacity(); result = bytes.length;
60
+ in.read(bytes, 0, bytes.length); result = new IOException("read throws IOException.");
61
+ }};
62
+
63
+ try {
64
+ CommonsCompressFileInput input = new CommonsCompressFileInput(allocator, provider);
65
+ assertTrue("Verify there is a new stream.", input.nextFile());
66
+ input.poll();
67
+ input.close();
68
+ } finally {
69
+ new Verifications() {{
70
+ allocBuffer.release(); times = 1;
71
+ }};
72
+ }
73
+ }
74
+
75
+ @Test(expected=IllegalStateException.class)
76
+ public void testPollWithoutNextFile() {
77
+ CommonsCompressFileInput input = new CommonsCompressFileInput(allocator, provider);
78
+ input.poll();
79
+ input.close();
80
+ }
81
+
82
+ @Test
83
+ public void testNextFile(@Mocked final InputStream in) throws Exception {
84
+ new NonStrictExpectations() {{
85
+ provider.openNext(); result = in; result = null;
86
+ provider.close();
87
+ }};
88
+
89
+ CommonsCompressFileInput input = new CommonsCompressFileInput(allocator, provider);
90
+ assertTrue("Return a stream by a provider.", input.nextFile());
91
+ assertFalse("Return no stream by a provider.", input.nextFile());
92
+ input.close();
93
+
94
+ new Verifications() {{
95
+ in.close(); times = 0;
96
+ provider.close(); times = 1;
97
+ }};
98
+ }
99
+
100
+ @Test(expected=RuntimeException.class)
101
+ public void testNextFileThrowsException() throws Exception {
102
+ new NonStrictExpectations() {{
103
+ provider.openNext(); result = new IOException("openNext throws IOException.");
104
+ provider.close();
105
+ }};
106
+
107
+ CommonsCompressFileInput input = new CommonsCompressFileInput(allocator, provider);
108
+ input.nextFile();
109
+ input.close();
110
+ }
111
+
112
+ @Test
113
+ public void testClose() throws Exception {
114
+ new NonStrictExpectations() {{
115
+ provider.close();
116
+ }};
117
+
118
+ CommonsCompressFileInput input = new CommonsCompressFileInput(allocator, provider);
119
+ input.close();
120
+
121
+ new Verifications() {{
122
+ provider.close(); times = 1;
123
+ }};
124
+ }
125
+
126
+ @Test
127
+ public void testCloseDoNotCloseStream(@Mocked final InputStream in) throws Exception {
128
+ new NonStrictExpectations() {{
129
+ provider.openNext(); result = in;
130
+ provider.close();
131
+ }};
132
+
133
+ CommonsCompressFileInput input = new CommonsCompressFileInput(allocator, provider);
134
+ assertTrue("Return a stream", input.nextFile());
135
+ input.close();
136
+
137
+ new Verifications() {{
138
+ in.close(); times = 0;
139
+ provider.close(); times = 1;
140
+ }};
141
+ }
142
+
143
+ @Test(expected=RuntimeException.class)
144
+ public void testCloseThrowsRuntimeException() throws Exception {
145
+ new NonStrictExpectations() {{
146
+ provider.close(); result = new IOException("close throws IOException.");
147
+ }};
148
+
149
+ CommonsCompressFileInput input = new CommonsCompressFileInput(allocator, provider);
150
+ input.close();
151
+ }
152
+ }
@@ -0,0 +1,369 @@
1
+ package org.embulk.decoder;
2
+
3
+ import static org.junit.Assert.assertEquals;
4
+ import static org.junit.Assert.assertFalse;
5
+ import static org.junit.Assert.assertNotNull;
6
+ import static org.junit.Assert.assertNull;
7
+ import static org.junit.Assert.assertTrue;
8
+
9
+ import java.io.ByteArrayInputStream;
10
+ import java.io.ByteArrayOutputStream;
11
+ import java.io.IOException;
12
+ import java.io.InputStream;
13
+ import java.util.Iterator;
14
+
15
+ import mockit.Mocked;
16
+ import mockit.NonStrictExpectations;
17
+ import mockit.Verifications;
18
+
19
+ import org.apache.commons.compress.archivers.ArchiveException;
20
+ import org.apache.commons.compress.archivers.ArchiveInputStream;
21
+ import org.apache.commons.compress.archivers.ArchiveStreamFactory;
22
+ import org.apache.commons.compress.compressors.CompressorException;
23
+ import org.apache.commons.compress.compressors.CompressorInputStream;
24
+ import org.apache.commons.compress.compressors.CompressorStreamFactory;
25
+ import org.embulk.decoder.CommonsCompressDecoderPlugin.PluginTask;
26
+ import org.embulk.spi.util.FileInputInputStream;
27
+ import org.junit.Test;
28
+
29
+ public class TestCommonsCompressProvider {
30
+ @Mocked PluginTask task;
31
+ @Mocked FileInputInputStream files;
32
+
33
+ @Test
34
+ public void testCommonsCompressProviderAutoDetect() throws Exception {
35
+ new NonStrictExpectations() {{
36
+ task.getFormat(); result = "";
37
+ }};
38
+
39
+ try (CommonsCompressProvider provider = new CommonsCompressProvider(task, files)) {
40
+ assertTrue("Auto-detect is set.", provider.isFormatAutoDetection());
41
+ assertNull("formats is set to null", provider.getFormats());
42
+ }
43
+ }
44
+
45
+ @Test
46
+ public void testCommonsCompressProviderSetFormat() throws Exception {
47
+ new NonStrictExpectations() {{
48
+ task.getFormat(); result = "tar";
49
+ }};
50
+
51
+ try (CommonsCompressProvider provider = new CommonsCompressProvider(task, files)) {
52
+ assertFalse("Auto-detect is not set.", provider.isFormatAutoDetection());
53
+ assertEquals("1 format is set", 1, provider.getFormats().length);
54
+ assertEquals("a configured format is set", "tar", provider.getFormats()[0]);
55
+ }
56
+ }
57
+
58
+ @Test
59
+ public void testOpenNextNoFile() throws Exception {
60
+ new NonStrictExpectations() {{
61
+ files.nextFile(); result = false;
62
+ }};
63
+
64
+ try (CommonsCompressProvider provider = new CommonsCompressProvider(task, files)) {
65
+ assertNull("No file is returned.", provider.openNext());
66
+ }
67
+ }
68
+
69
+ @Test
70
+ public void testOpenNextOneFileOneStream(
71
+ @Mocked final ArchiveInputStreamIterator iterator,
72
+ @Mocked final InputStream in) throws Exception {
73
+ final CommonsCompressProvider forPartialMock = new CommonsCompressProvider(task, files);
74
+ new NonStrictExpectations(CommonsCompressProvider.class) {{
75
+ forPartialMock.createInputStreamIterator((InputStream)any); result = iterator;
76
+ files.nextFile(); result = true; result = false;
77
+ task.getFormat(); result = "";
78
+ iterator.hasNext(); result = true; result = false;
79
+ iterator.next(); result = in;
80
+ }};
81
+
82
+ try (CommonsCompressProvider provider = new CommonsCompressProvider(task, files)) {
83
+ assertTrue("Return a stream", in == provider.openNext());
84
+ assertNull("No stream found", provider.openNext());
85
+ }
86
+
87
+ new Verifications() {{
88
+ files.nextFile(); times = 2;
89
+ iterator.hasNext(); times = 2;
90
+ iterator.next(); times = 1;
91
+ }};
92
+
93
+ forPartialMock.close();
94
+ }
95
+
96
+ @Test
97
+ public void testOpenNextOneFileOneStreamForFormat(
98
+ @Mocked final ArchiveInputStreamIterator iterator,
99
+ @Mocked final InputStream in) throws Exception {
100
+ final CommonsCompressProvider forPartialMock = new CommonsCompressProvider(task, files);
101
+ new NonStrictExpectations(CommonsCompressProvider.class) {{
102
+ forPartialMock.createInputStreamIterator((String[])any, 0, (InputStream)any); result = iterator;
103
+ files.nextFile(); result = true; result = false;
104
+ task.getFormat(); result = "tar";
105
+ iterator.hasNext(); result = true; result = false;
106
+ iterator.next(); result = in;
107
+ }};
108
+
109
+ try (CommonsCompressProvider provider = new CommonsCompressProvider(task, files)) {
110
+ assertTrue("Return a stream", in == provider.openNext());
111
+ assertNull("No stream found", provider.openNext());
112
+ }
113
+
114
+ new Verifications() {{
115
+ files.nextFile(); times = 2;
116
+ iterator.hasNext(); times = 2;
117
+ iterator.next(); times = 1;
118
+ }};
119
+
120
+ forPartialMock.close();
121
+ }
122
+
123
+ @Test
124
+ public void testOpenNextOneFileTwoStreams(@Mocked final ArchiveInputStreamIterator iterator,
125
+ @Mocked final InputStream in1, @Mocked final InputStream in2) throws Exception {
126
+ final CommonsCompressProvider forPartialMock = new CommonsCompressProvider(task, files);
127
+ new NonStrictExpectations(CommonsCompressProvider.class) {{
128
+ forPartialMock.createInputStreamIterator((InputStream)any); result = iterator;
129
+ files.nextFile(); result = true; result = false;
130
+ task.getFormat(); result = "";
131
+ iterator.hasNext(); result = true; result = true; result = false;
132
+ iterator.next(); result = in1; result = in2;
133
+ }};
134
+
135
+ try (CommonsCompressProvider provider = new CommonsCompressProvider(task, files)) {
136
+ assertNotNull("Return 1st stream", in1 == provider.openNext());
137
+ assertNotNull("Return 2nd stream", in2 == provider.openNext());
138
+ assertNull("No stream found", provider.openNext());
139
+ }
140
+
141
+ new Verifications() {{
142
+ files.nextFile(); times = 2;
143
+ iterator.hasNext(); times = 3;
144
+ iterator.next(); times = 2;
145
+ }};
146
+
147
+ forPartialMock.close();
148
+ }
149
+
150
+ @Test
151
+ public void testOpenNextTwoFilesTwoStreams(
152
+ @Mocked final ArchiveInputStreamIterator iterator1,
153
+ @Mocked final ArchiveInputStreamIterator iterator2,
154
+ @Mocked final InputStream in1,
155
+ @Mocked final InputStream in2) throws Exception {
156
+ final CommonsCompressProvider forPartialMock = new CommonsCompressProvider(task, files);
157
+ new NonStrictExpectations(CommonsCompressProvider.class) {{
158
+ forPartialMock.createInputStreamIterator((InputStream)any); result = iterator1; result = iterator2;
159
+ files.nextFile(); result = true; result = true; result = false;
160
+ task.getFormat(); result = "";
161
+ iterator1.hasNext(); result = true; result = true; result = false;
162
+ iterator1.next(); result = in1; result = in2;
163
+ iterator2.hasNext(); result = true; result = true; result = false;
164
+ iterator2.next(); result = in1; result = in2;
165
+ }};
166
+
167
+ try (CommonsCompressProvider provider = new CommonsCompressProvider(task, files)) {
168
+ assertNotNull("Return 1st stream", in1 == provider.openNext());
169
+ assertNotNull("Return 2nd stream", in2 == provider.openNext());
170
+ assertNotNull("Return 3rd stream", in1 == provider.openNext());
171
+ assertNotNull("Return 4th stream", in2 == provider.openNext());
172
+ assertNull("No stream found", provider.openNext());
173
+ }
174
+
175
+ new Verifications() {{
176
+ files.nextFile(); times = 3;
177
+ iterator1.hasNext(); times = 3;
178
+ iterator1.next(); times = 2;
179
+ iterator2.hasNext(); times = 3;
180
+ iterator2.next(); times = 2;
181
+ }};
182
+
183
+ forPartialMock.close();
184
+ }
185
+
186
+ @Test
187
+ public void testCreateInputStreamIteratorAutoDetectForArchive() throws Exception {
188
+ try (CommonsCompressProvider provider = new CommonsCompressProvider(task, files)) {
189
+ Iterator<InputStream> it = provider.createInputStreamIterator(
190
+ getResourceInputStream("samples.tar"));
191
+ verifyContents(it, "1,foo", "2,bar");
192
+ }
193
+ }
194
+
195
+ @Test
196
+ public void testCreateInputStreamIteratorAutDetectForCompressor() throws Exception {
197
+ try (CommonsCompressProvider provider = new CommonsCompressProvider(task, files)) {
198
+ Iterator<InputStream> it = provider.createInputStreamIterator(
199
+ getResourceInputStream("sample_1.csv.bz2"));
200
+ verifyContents(it, "1,foo");
201
+ }
202
+ }
203
+
204
+ // It looks difficult to detect these solid compression format. So, right now,
205
+ // this doesn't support to use auto detect.
206
+ /*
207
+ @Test
208
+ public void testCreateInputStreamIteratorAutoDetectForSolidCompression() throws Exception {
209
+ try (CommonsCompressProvider provider = new CommonsCompressProvider(task, files)) {
210
+ Iterator<InputStream> it = provider.createInputStreamIterator(
211
+ getResourceInputStream("samples.tar.bz2"));
212
+ verifyContents(it, "1,foo", "2,bar");
213
+ }
214
+ }*/
215
+
216
+ @Test
217
+ public void testCreateInputStreamIteratorFormatsAndArchive() throws Exception {
218
+ try (CommonsCompressProvider provider = new CommonsCompressProvider(task, files)) {
219
+ Iterator<InputStream> it = provider.createInputStreamIterator(
220
+ new String[]{ArchiveStreamFactory.TAR}, 0, getResourceInputStream("samples.tar"));
221
+ verifyContents(it, "1,foo", "2,bar");
222
+ }
223
+ }
224
+
225
+ @Test
226
+ public void testCreateInputStreamIteratorFormatsAndCompressor() throws Exception {
227
+ try (CommonsCompressProvider provider = new CommonsCompressProvider(task, files)) {
228
+ Iterator<InputStream> it = provider.createInputStreamIterator(
229
+ new String[]{CompressorStreamFactory.BZIP2}, 0, getResourceInputStream("sample_1.csv.bz2"));
230
+ verifyContents(it, "1,foo");
231
+ }
232
+ }
233
+
234
+ @Test
235
+ public void testCreateInputStreamIteratorFormatsAndSolidCompression() throws Exception {
236
+ try (CommonsCompressProvider provider = new CommonsCompressProvider(task, files)) {
237
+ Iterator<InputStream> it = provider.createInputStreamIterator(
238
+ new String[]{CompressorStreamFactory.BZIP2, ArchiveStreamFactory.TAR},
239
+ 0, getResourceInputStream("samples.tar.bz2"));
240
+ verifyContents(it, "1,foo", "2,bar");
241
+ }
242
+ }
243
+
244
+ @Test
245
+ public void testClose() throws Exception {
246
+ CommonsCompressProvider provider = new CommonsCompressProvider(task, files);
247
+ provider.close();
248
+
249
+ new Verifications() {{
250
+ files.close(); times = 1;
251
+ }};
252
+ }
253
+
254
+ @Test
255
+ public void testCreateArchiveInputStreamFormat() throws Exception {
256
+ try (CommonsCompressProvider provider = new CommonsCompressProvider(task, files)) {
257
+ ArchiveInputStream in = provider.createArchiveInputStream("tar",
258
+ getResourceInputStream("samples.tar"));
259
+ assertNotNull("Verify an instance is returned.", in);
260
+ }
261
+ }
262
+
263
+ // NOTE: This may need to handle instead of null. But, it means that there is no entry.
264
+ // So, it may be ok...
265
+ public void testCreateArchiveInputStreamWrongFormat() throws IOException, ArchiveException {
266
+ try (CommonsCompressProvider provider = new CommonsCompressProvider(task, files)) {
267
+ ArchiveInputStream in = provider.createArchiveInputStream("zip",
268
+ getResourceInputStream("samples.tar"));
269
+ assertNull("Verify a wrong format null in this testcase.", in.getNextEntry());
270
+ }
271
+ }
272
+
273
+ @Test(expected=ArchiveException.class)
274
+ public void testCreateArchiveInputStreamFormatNotFound() throws Exception {
275
+ try (CommonsCompressProvider provider = new CommonsCompressProvider(task, files)) {
276
+ provider.createArchiveInputStream("non-existing-format",
277
+ getResourceInputStream("samples.tar"));
278
+ }
279
+ }
280
+
281
+ @Test
282
+ public void testCreateArchiveInputStreamAutoDetect() throws Exception {
283
+ try (CommonsCompressProvider provider = new CommonsCompressProvider(task, files)) {
284
+ ArchiveInputStream in = provider.createArchiveInputStream("",
285
+ getResourceInputStream("samples.tar"));
286
+ assertNotNull("Verify an instance is returned.", in);
287
+ }
288
+ }
289
+
290
+ @Test(expected=IOException.class)
291
+ public void testCreateArchiveInputStreamAutoDetectIOException() throws Exception {
292
+ try (CommonsCompressProvider provider = new CommonsCompressProvider(task, files)) {
293
+ provider.createArchiveInputStream("", getResourceInputStream("sample_1.csv"));
294
+ }
295
+ }
296
+
297
+ @Test
298
+ public void testCreateCompressorInputStreamFormat() throws Exception {
299
+ try (CommonsCompressProvider provider = new CommonsCompressProvider(task, files)) {
300
+ CompressorInputStream in = provider.createCompressorInputStream("bzip2",
301
+ getResourceInputStream("samples.tar.bz2"));
302
+ assertNotNull("Verify an instance is returned.", in);
303
+ }
304
+ }
305
+
306
+ @Test(expected=CompressorException.class)
307
+ public void testCreateCompressorInputStreamWrongFormat() throws Exception {
308
+ try (CommonsCompressProvider provider = new CommonsCompressProvider(task, files)) {
309
+ provider.createCompressorInputStream("bzip2",
310
+ getResourceInputStream("samples.tar"));
311
+ }
312
+ }
313
+
314
+ @Test(expected=CompressorException.class)
315
+ public void testCreateCompressorInputStreamFormatNotFound() throws Exception {
316
+ try (CommonsCompressProvider provider = new CommonsCompressProvider(task, files)) {
317
+ provider.createCompressorInputStream("no-existing-format",
318
+ getResourceInputStream("samples.tar.bz2"));
319
+ }
320
+ }
321
+
322
+ @Test
323
+ public void testCreateCompressorInputStreamAutoDetect() throws Exception {
324
+ try (CommonsCompressProvider provider = new CommonsCompressProvider(task, files)) {
325
+ CompressorInputStream in = provider.createCompressorInputStream("",
326
+ getResourceInputStream("samples.tar.bz2"));
327
+ assertNotNull("Verify an instance is returned.", in);
328
+ }
329
+ }
330
+
331
+ @Test(expected=IOException.class)
332
+ public void testCreateCompressorInputStreamAutoDetectIOException() throws Exception {
333
+ try (CommonsCompressProvider provider = new CommonsCompressProvider(task, files)) {
334
+ provider.createCompressorInputStream("", getResourceInputStream("sample_1.csv"));
335
+ }
336
+ }
337
+
338
+ private void verifyContents(Iterator<InputStream> it, String ...expected) throws IOException {
339
+ for (String text : expected) {
340
+ assertTrue("There is a contents.", it.hasNext());
341
+ assertEquals("Verify the contents.", text, toString(it.next()).trim());
342
+ }
343
+ assertFalse("There is no contents.", it.hasNext());
344
+ }
345
+
346
+ private String toString(InputStream in) throws IOException {
347
+ return new String(toByteArray(in));
348
+ }
349
+
350
+ private byte[] toByteArray(InputStream in) throws IOException {
351
+ ByteArrayOutputStream bout = new ByteArrayOutputStream();
352
+ byte[] buff = new byte[1024];
353
+ int len = in.read(buff);
354
+ while (len != -1) {
355
+ bout.write(buff, 0, len);
356
+ len = in.read(buff);
357
+ }
358
+ return bout.toByteArray();
359
+ }
360
+
361
+ private InputStream getResourceInputStream(String resource) throws IOException {
362
+ InputStream in = getClass().getResourceAsStream(resource);
363
+ try {
364
+ return new ByteArrayInputStream(toByteArray(in));
365
+ } finally {
366
+ in.close();
367
+ }
368
+ }
369
+ }