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.ByteArrayInputStream; 20 import java.io.ByteArrayOutputStream; 21 import java.io.DataInputStream; 22 import java.io.DataOutputStream; 23 import java.io.IOException; 24 import java.io.InputStream; 25 import junit.framework.Assert; 26 import junit.framework.TestCase; 27 28 public class OldAndroidDataInputStreamTest extends TestCase { 29 30 public void testDataInputStream() throws Exception { 31 String str = "AbCdEfGhIjKlM\nOpQ\rStUvWxYz"; 32 ByteArrayInputStream aa = new ByteArrayInputStream(str.getBytes()); 33 ByteArrayInputStream ba = new ByteArrayInputStream(str.getBytes()); 34 ByteArrayInputStream ca = new ByteArrayInputStream(str.getBytes()); 35 ByteArrayInputStream da = new ByteArrayInputStream(str.getBytes()); 36 37 DataInputStream a = new DataInputStream(aa); 38 try { 39 Assert.assertEquals(str, read(a)); 40 } finally { 41 a.close(); 42 } 43 44 DataInputStream b = new DataInputStream(ba); 45 try { 46 Assert.assertEquals("AbCdEfGhIj", read(b, 10)); 47 } finally { 48 b.close(); 49 } 50 51 DataInputStream c = new DataInputStream(ca); 52 try { 53 Assert.assertEquals("bdfhjl\np\rtvxz", skipRead(c)); 54 } finally { 55 c.close(); 56 } 57 58 DataInputStream d = new DataInputStream(da); 59 try { 60 assertEquals("AbCdEfGhIjKlM", d.readLine()); 61 assertEquals("OpQ", d.readLine()); 62 assertEquals("StUvWxYz", d.readLine()); 63 } finally { 64 d.close(); 65 } 66 67 ByteArrayOutputStream e = new ByteArrayOutputStream(); 68 DataOutputStream f = new DataOutputStream(e); 69 try { 70 f.writeBoolean(true); 71 f.writeByte('a'); 72 f.writeBytes("BCD"); 73 f.writeChar('e'); 74 f.writeChars("FGH"); 75 f.writeUTF("ijklm"); 76 f.writeDouble(1); 77 f.writeFloat(2); 78 f.writeInt(3); 79 f.writeLong(4); 80 f.writeShort(5); 81 } finally { 82 f.close(); 83 } 84 85 ByteArrayInputStream ga = new ByteArrayInputStream(e.toByteArray()); 86 DataInputStream g = new DataInputStream(ga); 87 88 try { 89 assertTrue(g.readBoolean()); 90 assertEquals('a', g.readByte()); 91 assertEquals(2, g.skipBytes(2)); 92 assertEquals('D', g.readByte()); 93 assertEquals('e', g.readChar()); 94 assertEquals('F', g.readChar()); 95 assertEquals('G', g.readChar()); 96 assertEquals('H', g.readChar()); 97 assertEquals("ijklm", g.readUTF()); 98 assertEquals(1, g.readDouble(), 0); 99 assertEquals(2f, g.readFloat(), 0f); 100 assertEquals(3, g.readInt()); 101 assertEquals(4, g.readLong()); 102 assertEquals(5, g.readShort()); 103 } finally { 104 g.close(); 105 } 106 } 107 108 public static String read(InputStream a) throws IOException { 109 int r; 110 StringBuilder builder = new StringBuilder(); 111 do { 112 r = a.read(); 113 if (r != -1) 114 builder.append((char) r); 115 } while (r != -1); 116 return builder.toString(); 117 } 118 119 public static String read(InputStream a, int x) throws IOException { 120 byte[] b = new byte[x]; 121 int len = a.read(b, 0, x); 122 if (len < 0) { 123 return ""; 124 } 125 return new String(b, 0, len); 126 } 127 128 public static String skipRead(InputStream a) throws IOException { 129 int r; 130 StringBuilder builder = new StringBuilder(); 131 do { 132 a.skip(1); 133 r = a.read(); 134 if (r != -1) 135 builder.append((char) r); 136 } while (r != -1); 137 return builder.toString(); 138 } 139 140 public static String markRead(InputStream a, int x, int y) throws IOException { 141 int m = 0; 142 int r; 143 StringBuilder builder = new StringBuilder(); 144 do { 145 m++; 146 r = a.read(); 147 if (m == x) 148 a.mark((x + y)); 149 if (m == (x + y)) 150 a.reset(); 151 152 if (r != -1) 153 builder.append((char) r); 154 } while (r != -1); 155 return builder.toString(); 156 } 157 } 158