• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 android.core;
18 
19 import junit.framework.TestCase;
20 
21 import java.io.ByteArrayInputStream;
22 import java.io.ByteArrayOutputStream;
23 import java.io.DataInputStream;
24 import java.io.DataOutputStream;
25 import android.test.suitebuilder.annotation.SmallTest;
26 
27 public class DataInputStreamTest extends TestCase {
28 
29     @SmallTest
testDataInputStream()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             assertEquals(str, IOUtil.read(a));
40         } finally {
41             a.close();
42         }
43 
44         DataInputStream b = new DataInputStream(ba);
45         try {
46             assertEquals("AbCdEfGhIj", IOUtil.read(b, 10));
47         } finally {
48             b.close();
49         }
50 
51         DataInputStream c = new DataInputStream(ca);
52         try {
53             assertEquals("bdfhjl\np\rtvxz", IOUtil.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