1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17 package com.google.common.io;
18
19 import com.google.common.primitives.Bytes;
20
21 import junit.framework.TestCase;
22
23 import java.io.ByteArrayInputStream;
24 import java.io.ByteArrayOutputStream;
25 import java.io.DataInput;
26 import java.io.DataOutputStream;
27 import java.io.EOFException;
28 import java.io.IOException;
29
30
31
32
33
34
35 public class LittleEndianDataInputStreamTest extends TestCase {
36
37 private byte[] data;
38
39 @Override
40 protected void setUp() throws Exception {
41 super.setUp();
42
43 ByteArrayOutputStream baos = new ByteArrayOutputStream();
44 DataOutputStream out = new DataOutputStream(baos);
45
46 initializeData(out);
47
48 data = baos.toByteArray();
49 }
50
51 private void initializeData(DataOutputStream out) throws IOException {
52
53 out.write(new byte[] { -100, 100 });
54 out.writeBoolean(true);
55 out.writeBoolean(false);
56 out.writeByte(100);
57 out.writeByte(-100);
58 out.writeByte((byte) 200);
59 out.writeChar('a');
60 out.writeShort((short) -30000);
61 out.writeShort((short) 50000);
62 out.writeInt(0xCAFEBABE);
63 out.writeLong(0xDEADBEEFCAFEBABEL);
64 out.writeUTF("Herby Derby");
65 out.writeFloat(Float.intBitsToFloat(0xCAFEBABE));
66 out.writeDouble(Double.longBitsToDouble(0xDEADBEEFCAFEBABEL));
67 }
68
69 public void testReadFully() throws IOException {
70 DataInput in = new LittleEndianDataInputStream(new ByteArrayInputStream(data));
71 byte[] b = new byte[data.length];
72 in.readFully(b);
73 assertEquals(Bytes.asList(data), Bytes.asList(b));
74 }
75
76 public void testReadUnsignedByte_eof() throws IOException {
77 DataInput in = new LittleEndianDataInputStream(new ByteArrayInputStream(new byte[0]));
78 try {
79 in.readUnsignedByte();
80 fail();
81 } catch (EOFException expected) {
82 }
83 }
84
85 public void testReadUnsignedShort_eof() throws IOException {
86 byte[] buf = {23};
87 DataInput in = new LittleEndianDataInputStream(new ByteArrayInputStream(buf));
88 try {
89 in.readUnsignedShort();
90 fail();
91 } catch (EOFException expected) {}
92 }
93
94 public void testReadLine() throws IOException {
95 DataInput in = new LittleEndianDataInputStream(new ByteArrayInputStream(data));
96 try {
97 in.readLine();
98 fail();
99 } catch (UnsupportedOperationException expected) {
100 assertEquals("readLine is not supported", expected.getMessage());
101 }
102 }
103
104 public void testReadLittleEndian() throws IOException {
105 DataInput in = new LittleEndianDataInputStream(new ByteArrayInputStream(data));
106
107
108 byte[] b = new byte[2];
109 in.readFully(b);
110 assertEquals(-100, b[0]);
111 assertEquals(100, b[1]);
112 assertEquals(true, in.readBoolean());
113 assertEquals(false, in.readBoolean());
114 assertEquals(100, in.readByte());
115 assertEquals(-100, in.readByte());
116 assertEquals(200, in.readUnsignedByte());
117 assertEquals('\u6100', in.readChar());
118 assertEquals(-12150, in.readShort());
119 assertEquals(20675, in.readUnsignedShort());
120 assertEquals(0xBEBAFECA, in.readInt());
121 assertEquals(0xBEBAFECAEFBEADDEL, in.readLong());
122 assertEquals("Herby Derby", in.readUTF());
123 assertEquals(0xBEBAFECA, Float.floatToIntBits(in.readFloat()));
124 assertEquals(0xBEBAFECAEFBEADDEL, Double.doubleToLongBits(in.readDouble()));
125 }
126
127 public void testSkipBytes() throws IOException {
128 ByteArrayOutputStream baos = new ByteArrayOutputStream();
129 DataOutputStream out = new DataOutputStream(baos);
130
131
132 out.write(new byte[] { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9});
133 initializeData(out);
134
135 byte[] data = baos.toByteArray();
136
137 DataInput in = new LittleEndianDataInputStream(new ByteArrayInputStream(data));
138 int bytesSkipped = 0;
139 while (bytesSkipped < 10) {
140 bytesSkipped += in.skipBytes(10 - bytesSkipped);
141 }
142
143
144 byte[] b = new byte[2];
145 in.readFully(b);
146 assertEquals(-100, b[0]);
147 assertEquals(100, b[1]);
148 assertTrue(in.readBoolean());
149 assertFalse(in.readBoolean());
150 }
151 }