blob: 938b16e9b3305b125638eac61d17d2273f62a084 [file] [log] [blame]
The Android Open Source Projectb5de22c2012-04-01 00:00:00 -07001/*
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.util.Arrays;
24import java.util.zip.DeflaterInputStream;
25import java.util.zip.InflaterInputStream;
26import junit.framework.TestCase;
27
28public final class DeflaterInputStreamTest extends TestCase {
29
30 public void testReadByteByByte() throws IOException {
31 byte[] data = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 };
32 InputStream in = new DeflaterInputStream(new ByteArrayInputStream(data));
33 ByteArrayOutputStream out = new ByteArrayOutputStream();
34
35 assertEquals(1, in.available());
36 int b;
37 while ((b = in.read()) != -1) {
38 out.write(b);
39 }
40 assertEquals(0, in.available());
41
42 assertEquals(Arrays.toString(data), Arrays.toString(inflate(out.toByteArray())));
43
44 in.close();
45 try {
46 in.available();
47 fail();
48 } catch (IOException expected) {
49 }
50 }
51
52 public byte[] inflate(byte[] bytes) throws IOException {
53 java.io.InputStream in = new InflaterInputStream(new ByteArrayInputStream(bytes));
54 ByteArrayOutputStream out = new ByteArrayOutputStream();
55 byte[] buffer = new byte[1024];
56 int count;
57 while ((count = in.read(buffer)) != -1) {
58 out.write(buffer, 0, count);
59 }
60 return out.toByteArray();
61 }
62
63 public void testReadWithBuffer() throws IOException {
64 byte[] data = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 };
65 byte[] buffer = new byte[8];
66 InputStream in = new DeflaterInputStream(new ByteArrayInputStream(data));
67 ByteArrayOutputStream out = new ByteArrayOutputStream();
68 assertEquals(1, in.available());
69 int count;
70 while ((count = in.read(buffer, 0, 5)) != -1) {
71 assertTrue(count <= 5);
72 out.write(buffer, 0, count);
73 }
74 assertEquals(0, in.available());
75
76 assertEquals(Arrays.toString(data), Arrays.toString(inflate(out.toByteArray())));
77
78 in.close();
79 try {
80 in.available();
81 fail();
82 } catch (IOException expected) {
83 }
84 }
85
86 public void testReadExceptions() throws IOException {
87 byte[] data = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 };
88 byte[] buffer = new byte[8];
89 InputStream in = new DeflaterInputStream(new ByteArrayInputStream(data));
90 try {
91 in.read(buffer, 0, 10);
92 fail();
93 } catch (IndexOutOfBoundsException expected) {
94 }
95 try {
96 in.read(null, 0, 5);
97 fail();
98 } catch (NullPointerException expected) {
99 }
100 try {
101 in.read(buffer, -1, 5);
102 fail();
103 } catch (IndexOutOfBoundsException expected) {
104 }
105 in.close();
106 try {
107 in.read(buffer, 0, 5);
108 fail();
109 } catch (IOException expected) {
110 }
111 }
112}