KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > archive > io > GzippedInputStreamTest


1 /* GzippedInputStreamTest
2  *
3  * Created on May 4, 2005
4  *
5  * Copyright (C) 2005 Internet Archive.
6  *
7  * This file is part of the Heritrix web crawler (crawler.archive.org).
8  *
9  * Heritrix is free software; you can redistribute it and/or modify
10  * it under the terms of the GNU Lesser Public License as published by
11  * the Free Software Foundation; either version 2.1 of the License, or
12  * any later version.
13  *
14  * Heritrix is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17  * GNU Lesser Public License for more details.
18  *
19  * You should have received a copy of the GNU Lesser Public License
20  * along with Heritrix; if not, write to the Free Software
21  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
22  */

23 package org.archive.io;
24
25 import it.unimi.dsi.fastutil.io.RepositionableStream;
26
27 import java.io.BufferedOutputStream JavaDoc;
28 import java.io.ByteArrayInputStream JavaDoc;
29 import java.io.File JavaDoc;
30 import java.io.FileOutputStream JavaDoc;
31 import java.io.IOException JavaDoc;
32 import java.io.InputStream JavaDoc;
33 import java.io.OutputStream JavaDoc;
34 import java.util.Iterator JavaDoc;
35
36 import org.archive.util.TmpDirTestCase;
37
38 /**
39  * @author stack
40  * @version $Date: 2006/08/04 00:13:51 $, $Revision: 1.5 $
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 JavaDoc TEXT = "Some old text to compress.";
48     // Create file to use in tests below.
49
private File JavaDoc compressedFile = null;
50     
51     protected void setUp() throws Exception JavaDoc {
52         super.setUp();
53         this.compressedFile = createMultiGzipMembers();
54     }
55     
56     protected void tearDown() throws Exception JavaDoc {
57         if (this.compressedFile != null) {
58             this.compressedFile.delete();
59         }
60         super.tearDown();
61     }
62
63     public static void main(String JavaDoc [] 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 JavaDoc file)
71         throws IOException JavaDoc {
72             super(file);
73         }
74         
75         public RepositionableRandomAccessInputStream(final File JavaDoc file,
76             final long offset)
77         throws IOException JavaDoc {
78             super(file, offset);
79         }
80     }
81
82     protected File JavaDoc createMultiGzipMembers() throws IOException JavaDoc {
83         final File JavaDoc f =
84             new File JavaDoc(getTmpDir(), this.getClass().getName() + ".gz");
85         OutputStream JavaDoc os = new BufferedOutputStream JavaDoc(new FileOutputStream JavaDoc(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 JavaDoc {
95         InputStream JavaDoc is =
96             new RepositionableRandomAccessInputStream(this.compressedFile);
97         GzippedInputStream gis = new GzippedInputStream(is);
98         int records = 0;
99         // Get offset of second record. Will use it later in tests below.
100
long offsetOfSecondRecord = -1;
101         for (Iterator JavaDoc i = gis.iterator(); i.hasNext();) {
102             long offset = gis.position();
103             if (records == 1) {
104                 offsetOfSecondRecord = offset;
105             }
106             is = (InputStream JavaDoc)i.next();
107             records++;
108         }
109         assertTrue("Record count is off " + records,
110             records == GZIPMEMBER_COUNT);
111         gis.close();
112         
113         // Test random record read.
114
is = new RepositionableRandomAccessInputStream(this.compressedFile);
115         gis = new GzippedInputStream(is);
116         byte [] buffer = new byte[TEXT.length()];
117         // Seek to second record, read in gzip header.
118
gis.gzipMemberSeek(offsetOfSecondRecord);
119         gis.read(buffer);
120         String JavaDoc readString = new String JavaDoc(buffer);
121         assertEquals("Failed read", TEXT, readString);
122         gis.close();
123         
124         // Test the count we get makes sense after iterating through
125
// starting at second record.
126
is = new RepositionableRandomAccessInputStream(this.compressedFile,
127             offsetOfSecondRecord);
128         gis = new GzippedInputStream(is);
129         records = 0;
130         for (final Iterator JavaDoc i = gis.iterator(); i.hasNext(); i.next()) {
131             records++;
132         }
133         assertEquals(records,
134             GZIPMEMBER_COUNT - 1 /*We started at 2nd record*/);
135         gis.close();
136     }
137     
138     public void testCompressedStream() throws IOException JavaDoc {
139         byte [] bytes = "test".getBytes();
140         ByteArrayInputStream JavaDoc baos = new ByteArrayInputStream JavaDoc(bytes);
141         assertFalse(GzippedInputStream.isCompressedStream(baos));
142         
143         byte [] gzippedMetaData = GzippedInputStream.gzip(bytes);
144         baos = new ByteArrayInputStream JavaDoc(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 JavaDoc 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 }
Popular Tags