• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2008 The Android Open Source Project
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 package android.core;
18 
19 import junit.framework.TestCase;
20 
21 import java.io.ByteArrayInputStream;
22 import java.io.ByteArrayOutputStream;
23 import java.io.IOException;
24 import java.util.zip.GZIPInputStream;
25 import java.util.zip.GZIPOutputStream;
26 import android.test.suitebuilder.annotation.MediumTest;
27 
28 /**
29  * Deflates and inflates some test data with GZipStreams
30  */
31 public class GZIPStreamTest extends TestCase {
32 
33     @MediumTest
testGZIPStream()34     public void testGZIPStream() throws Exception {
35         ByteArrayOutputStream bytesOut = new ByteArrayOutputStream();
36         createGZIP(bytesOut);
37 
38         byte[] zipData;
39         zipData = bytesOut.toByteArray();
40 
41         /*
42         FileOutputStream outFile = new FileOutputStream("/tmp/foo.gz");
43         outFile.write(zipData, 0, zipData.length);
44         outFile.close();
45         */
46 
47         /*
48         FileInputStream inFile = new FileInputStream("/tmp/foo.gz");
49         int inputLength = inFile.available();
50         zipData = new byte[inputLength];
51         if (inFile.read(zipData) != inputLength)
52             throw new RuntimeException();
53         inFile.close();
54         */
55 
56         ByteArrayInputStream bytesIn = new ByteArrayInputStream(zipData);
57         scanGZIP(bytesIn);
58     }
59 
60     /*
61      * stepStep == 0 --> >99% compression
62      * stepStep == 1 --> ~30% compression
63      * stepStep == 2 --> no compression
64      */
makeSampleFile(int stepStep)65     static byte[] makeSampleFile(int stepStep) throws IOException {
66         byte[] sample = new byte[128 * 1024];
67         byte val, step;
68         int i, j, offset;
69 
70         val = 0;
71         step = 1;
72         offset = 0;
73         for (i = 0; i < (128 * 1024) / 256; i++) {
74             for (j = 0; j < 256; j++) {
75                 sample[offset++] = val;
76                 val += step;
77             }
78 
79             step += stepStep;
80         }
81 
82         return sample;
83     }
84 
createGZIP(ByteArrayOutputStream bytesOut)85     static void createGZIP(ByteArrayOutputStream bytesOut) throws IOException {
86         GZIPOutputStream out = new GZIPOutputStream(bytesOut);
87         try {
88             byte[] input = makeSampleFile(1);
89             out.write(input, 0, input.length);
90             //out.finish();
91         } finally {
92             out.close();
93         }
94     }
95 
scanGZIP(ByteArrayInputStream bytesIn)96     static void scanGZIP(ByteArrayInputStream bytesIn) throws IOException {
97         GZIPInputStream in = new GZIPInputStream(bytesIn);
98         try {
99             ByteArrayOutputStream contents = new ByteArrayOutputStream();
100             byte[] buf = new byte[4096];
101             int len, totalLen = 0;
102 
103             while ((len = in.read(buf)) > 0) {
104                 contents.write(buf, 0, len);
105                 totalLen += len;
106             }
107 
108             assertEquals(totalLen, 128 * 1024);
109         } finally {
110             in.close();
111         }
112     }
113 }
114 
115