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
17package libcore.java.io;
18
19import java.io.BufferedInputStream;
20import java.io.ByteArrayInputStream;
21import java.io.IOException;
22import java.io.InputStream;
23import junit.framework.Assert;
24import junit.framework.TestCase;
25
26/**
27 * Tests to verify that simple functionality works for BufferedInputStreams.
28 */
29public class OldAndroidBufferedInputStreamTest extends TestCase {
30
31    public void testBufferedInputStream() throws Exception {
32        String str = "AbCdEfGhIjKlM\nOpQrStUvWxYz";
33        ByteArrayInputStream aa = new ByteArrayInputStream(str.getBytes());
34        ByteArrayInputStream ba = new ByteArrayInputStream(str.getBytes());
35        ByteArrayInputStream ca = new ByteArrayInputStream(str.getBytes());
36        ByteArrayInputStream da = new ByteArrayInputStream(str.getBytes());
37        ByteArrayInputStream ea = new ByteArrayInputStream(str.getBytes());
38
39        BufferedInputStream a = new BufferedInputStream(aa, 6);
40        try {
41            Assert.assertEquals(str, read(a));
42        } finally {
43            a.close();
44        }
45
46        BufferedInputStream b = new BufferedInputStream(ba, 7);
47        try {
48            Assert.assertEquals("AbCdEfGhIj", read(b, 10));
49        } finally {
50            b.close();
51        }
52
53        BufferedInputStream c = new BufferedInputStream(ca, 9);
54        try {
55            assertEquals("bdfhjl\nprtvxz", skipRead(c));
56        } finally {
57            c.close();
58        }
59
60        BufferedInputStream d = new BufferedInputStream(da, 9);
61        try {
62            assertEquals('A', d.read());
63            d.mark(15);
64            assertEquals('b', d.read());
65            assertEquals('C', d.read());
66            d.reset();
67            assertEquals('b', d.read());
68        } finally {
69            d.close();
70        }
71
72        BufferedInputStream e = new BufferedInputStream(ea, 11);
73        try {
74            // test that we can ask for more than is present, and that we'll get
75            // back only what is there.
76            assertEquals(str, read(e, 10000));
77        } finally {
78            e.close();
79        }
80    }
81
82    public static String read(InputStream a) throws IOException {
83        int r;
84        StringBuilder builder = new StringBuilder();
85        do {
86            r = a.read();
87            if (r != -1)
88                builder.append((char) r);
89        } while (r != -1);
90        return builder.toString();
91    }
92
93    public static String skipRead(InputStream a) throws IOException {
94        int r;
95        StringBuilder builder = new StringBuilder();
96        do {
97            a.skip(1);
98            r = a.read();
99            if (r != -1)
100                builder.append((char) r);
101        } while (r != -1);
102        return builder.toString();
103    }
104
105    public static String read(InputStream a, int x) throws IOException {
106        byte[] b = new byte[x];
107        int len = a.read(b, 0, x);
108        if (len < 0) {
109            return "";
110        }
111        return new String(b, 0, len);
112    }
113}
114