1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23 package org.archive.io;
24
25 import it.unimi.dsi.fastutil.io.RepositionableStream;
26
27 import java.io.BufferedOutputStream;
28 import java.io.ByteArrayInputStream;
29 import java.io.File;
30 import java.io.FileOutputStream;
31 import java.io.IOException;
32 import java.io.InputStream;
33 import java.io.OutputStream;
34 import java.util.Iterator;
35
36 import org.archive.util.TmpDirTestCase;
37
38 /***
39 * @author stack
40 * @version $Date: 2006-08-04 00:13:51 +0000 (Fri, 04 Aug 2006) $, $Revision: 4431 $
41 */
42 public class GzippedInputStreamTest extends TmpDirTestCase {
43 /***
44 * Number of records in gzip member file.
45 */
46 final static int GZIPMEMBER_COUNT = 4;
47 final static String TEXT = "Some old text to compress.";
48
49 private File compressedFile = null;
50
51 protected void setUp() throws Exception {
52 super.setUp();
53 this.compressedFile = createMultiGzipMembers();
54 }
55
56 protected void tearDown() throws Exception {
57 if (this.compressedFile != null) {
58 this.compressedFile.delete();
59 }
60 super.tearDown();
61 }
62
63 public static void main(String [] args) {
64 junit.textui.TestRunner.run(GzippedInputStreamTest.class);
65 }
66
67 protected class RepositionableRandomAccessInputStream
68 extends RandomAccessInputStream
69 implements RepositionableStream {
70 public RepositionableRandomAccessInputStream(final File file)
71 throws IOException {
72 super(file);
73 }
74
75 public RepositionableRandomAccessInputStream(final File file,
76 final long offset)
77 throws IOException {
78 super(file, offset);
79 }
80 }
81
82 protected File createMultiGzipMembers() throws IOException {
83 final File f =
84 new File(getTmpDir(), this.getClass().getName() + ".gz");
85 OutputStream os = new BufferedOutputStream(new FileOutputStream(f));
86 for (int i = 0; i < GZIPMEMBER_COUNT; i++) {
87 os.write(GzippedInputStream.gzip(TEXT.getBytes()));
88 }
89 os.close();
90 return f;
91 }
92
93 public void testCountOfMembers()
94 throws IOException {
95 InputStream is =
96 new RepositionableRandomAccessInputStream(this.compressedFile);
97 GzippedInputStream gis = new GzippedInputStream(is);
98 int records = 0;
99
100 long offsetOfSecondRecord = -1;
101 for (Iterator i = gis.iterator(); i.hasNext();) {
102 long offset = gis.position();
103 if (records == 1) {
104 offsetOfSecondRecord = offset;
105 }
106 is = (InputStream)i.next();
107 records++;
108 }
109 assertTrue("Record count is off " + records,
110 records == GZIPMEMBER_COUNT);
111 gis.close();
112
113
114 is = new RepositionableRandomAccessInputStream(this.compressedFile);
115 gis = new GzippedInputStream(is);
116 byte [] buffer = new byte[TEXT.length()];
117
118 gis.gzipMemberSeek(offsetOfSecondRecord);
119 gis.read(buffer);
120 String readString = new String(buffer);
121 assertEquals("Failed read", TEXT, readString);
122 gis.close();
123
124
125
126 is = new RepositionableRandomAccessInputStream(this.compressedFile,
127 offsetOfSecondRecord);
128 gis = new GzippedInputStream(is);
129 records = 0;
130 for (final Iterator i = gis.iterator(); i.hasNext(); i.next()) {
131 records++;
132 }
133 assertEquals(records,
134 GZIPMEMBER_COUNT - 1
135 gis.close();
136 }
137
138 public void testCompressedStream() throws IOException {
139 byte [] bytes = "test".getBytes();
140 ByteArrayInputStream baos = new ByteArrayInputStream(bytes);
141 assertFalse(GzippedInputStream.isCompressedStream(baos));
142
143 byte [] gzippedMetaData = GzippedInputStream.gzip(bytes);
144 baos = new ByteArrayInputStream(gzippedMetaData);
145 assertTrue(GzippedInputStream.isCompressedStream(baos));
146
147 gzippedMetaData = GzippedInputStream.gzip(bytes);
148 final RepositionableByteArrayInputStream rbaos =
149 new RepositionableByteArrayInputStream(gzippedMetaData);
150 final int totalBytes = gzippedMetaData.length;
151 assertTrue(GzippedInputStream.isCompressedRepositionableStream(rbaos));
152 long available = rbaos.available();
153 assertEquals(available, totalBytes);
154 assertEquals(rbaos.position(), 0);
155 }
156
157 private class RepositionableByteArrayInputStream
158 extends ByteArrayInputStream implements RepositionableStream {
159 public RepositionableByteArrayInputStream(final byte [] bytes) {
160 super(bytes);
161 }
162
163 public void position(long p) {
164 this.pos = (int)p;
165 }
166 public long position() {
167 return this.pos;
168 }
169 }
170 }