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