Home | History | Annotate | Download | only in zip
      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 libcore.java.util.zip;
     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 
     27 /**
     28  * Deflates and inflates some test data with GZipStreams
     29  */
     30 public class OldAndroidGZIPStreamTest extends TestCase {
     31 
     32     public void testGZIPStream() throws Exception {
     33         ByteArrayOutputStream bytesOut = new ByteArrayOutputStream();
     34         createGZIP(bytesOut);
     35 
     36         byte[] zipData;
     37         zipData = bytesOut.toByteArray();
     38 
     39         /*
     40         FileOutputStream outFile = new FileOutputStream("/tmp/foo.gz");
     41         outFile.write(zipData, 0, zipData.length);
     42         outFile.close();
     43         */
     44 
     45         /*
     46         FileInputStream inFile = new FileInputStream("/tmp/foo.gz");
     47         int inputLength = inFile.available();
     48         zipData = new byte[inputLength];
     49         if (inFile.read(zipData) != inputLength)
     50             throw new RuntimeException();
     51         inFile.close();
     52         */
     53 
     54         ByteArrayInputStream bytesIn = new ByteArrayInputStream(zipData);
     55         scanGZIP(bytesIn);
     56     }
     57 
     58     /*
     59      * stepStep == 0 --> >99% compression
     60      * stepStep == 1 --> ~30% compression
     61      * stepStep == 2 --> no compression
     62      */
     63     static byte[] makeSampleFile(int stepStep) throws IOException {
     64         byte[] sample = new byte[128 * 1024];
     65         byte val, step;
     66         int i, j, offset;
     67 
     68         val = 0;
     69         step = 1;
     70         offset = 0;
     71         for (i = 0; i < (128 * 1024) / 256; i++) {
     72             for (j = 0; j < 256; j++) {
     73                 sample[offset++] = val;
     74                 val += step;
     75             }
     76 
     77             step += stepStep;
     78         }
     79 
     80         return sample;
     81     }
     82 
     83     static void createGZIP(ByteArrayOutputStream bytesOut) throws IOException {
     84         GZIPOutputStream out = new GZIPOutputStream(bytesOut);
     85         try {
     86             byte[] input = makeSampleFile(1);
     87             out.write(input, 0, input.length);
     88             //out.finish();
     89         } finally {
     90             out.close();
     91         }
     92     }
     93 
     94     static void scanGZIP(ByteArrayInputStream bytesIn) throws IOException {
     95         GZIPInputStream in = new GZIPInputStream(bytesIn);
     96         try {
     97             ByteArrayOutputStream contents = new ByteArrayOutputStream();
     98             byte[] buf = new byte[4096];
     99             int len, totalLen = 0;
    100 
    101             while ((len = in.read(buf)) > 0) {
    102                 contents.write(buf, 0, len);
    103                 totalLen += len;
    104             }
    105 
    106             assertEquals(totalLen, 128 * 1024);
    107         } finally {
    108             in.close();
    109         }
    110     }
    111 }
    112 
    113