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