1/* 2 * Copyright (C) 2010 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 17package libcore.java.util.zip; 18 19import java.io.ByteArrayInputStream; 20import java.io.ByteArrayOutputStream; 21import java.io.IOException; 22import java.io.InputStream; 23import java.io.OutputStream; 24import java.util.Arrays; 25import java.util.Random; 26import java.util.zip.GZIPInputStream; 27import java.util.zip.GZIPOutputStream; 28import junit.framework.TestCase; 29 30public final class GZIPInputStreamTest extends TestCase { 31 public void testShortMessage() throws IOException { 32 byte[] data = new byte[] { 33 31, -117, 8, 0, 0, 0, 0, 0, 0, 0, -13, 72, -51, -55, -55, 87, 8, -49, 34 47, -54, 73, 1, 0, 86, -79, 23, 74, 11, 0, 0, 0 35 }; 36 assertEquals("Hello World", new String(gunzip(data), "UTF-8")); 37 } 38 39 public void testLongMessage() throws IOException { 40 byte[] data = new byte[1024 * 1024]; 41 new Random().nextBytes(data); 42 assertTrue(Arrays.equals(data, gunzip(GZIPOutputStreamTest.gzip(data)))); 43 } 44 45 /** http://b/3042574 GzipInputStream.skip() causing CRC failures */ 46 public void testSkip() throws IOException { 47 byte[] data = new byte[1024 * 1024]; 48 byte[] gzipped = GZIPOutputStreamTest.gzip(data); 49 GZIPInputStream in = new GZIPInputStream(new ByteArrayInputStream(gzipped)); 50 long totalSkipped = 0; 51 52 long count; 53 do { 54 count = in.skip(Long.MAX_VALUE); 55 totalSkipped += count; 56 } while (count > 0); 57 58 assertEquals(data.length, totalSkipped); 59 in.close(); 60 } 61 62 public static byte[] gunzip(byte[] bytes) throws IOException { 63 InputStream in = new GZIPInputStream(new ByteArrayInputStream(bytes)); 64 ByteArrayOutputStream out = new ByteArrayOutputStream(); 65 byte[] buffer = new byte[1024]; 66 int count; 67 while ((count = in.read(buffer)) != -1) { 68 out.write(buffer, 0, count); 69 } 70 in.close(); 71 return out.toByteArray(); 72 } 73} 74