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.ByteArrayInputStream;
20import java.io.IOException;
21import java.io.InputStream;
22import junit.framework.Assert;
23import junit.framework.TestCase;
24
25/**
26 * Tests to verify that simple functionality works for ByteArrayInputStreams.
27 */
28public class OldAndroidByteArrayInputStreamTest extends TestCase {
29
30    public void testByteArrayInputStream() throws Exception {
31        String str = "AbCdEfGhIjKlMnOpQrStUvWxYz";
32
33        ByteArrayInputStream a = new ByteArrayInputStream(str.getBytes());
34        ByteArrayInputStream b = new ByteArrayInputStream(str.getBytes());
35        ByteArrayInputStream c = new ByteArrayInputStream(str.getBytes());
36        ByteArrayInputStream d = new ByteArrayInputStream(str.getBytes());
37
38        Assert.assertEquals(str, read(a));
39        Assert.assertEquals("AbCdEfGhIj", read(b, 10));
40        Assert.assertEquals("bdfhjlnprtvxz", skipRead(c));
41        Assert.assertEquals("AbCdEfGdEfGhIjKlMnOpQrStUvWxYz", markRead(d, 3, 4));
42    }
43
44    public static String read(InputStream a) throws IOException {
45        int r;
46        StringBuilder builder = new StringBuilder();
47        do {
48            r = a.read();
49            if (r != -1)
50                builder.append((char) r);
51        } while (r != -1);
52        return builder.toString();
53    }
54
55    public static String read(InputStream a, int x) throws IOException {
56        byte[] b = new byte[x];
57        int len = a.read(b, 0, x);
58        if (len < 0) {
59            return "";
60        }
61        return new String(b, 0, len);
62    }
63
64    public static String skipRead(InputStream a) throws IOException {
65        int r;
66        StringBuilder builder = new StringBuilder();
67        do {
68            a.skip(1);
69            r = a.read();
70            if (r != -1)
71                builder.append((char) r);
72        } while (r != -1);
73        return builder.toString();
74    }
75
76    public static String markRead(InputStream a, int x, int y) throws IOException {
77        int m = 0;
78        int r;
79        StringBuilder builder = new StringBuilder();
80        do {
81            m++;
82            r = a.read();
83            if (m == x)
84                a.mark((x + y));
85            if (m == (x + y))
86                a.reset();
87
88            if (r != -1)
89                builder.append((char) r);
90        } while (r != -1);
91        return builder.toString();
92    }
93}
94