• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2007 The Guava Authors
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 com.google.common.io;
18 
19 import static com.google.common.truth.Truth.assertThat;
20 
21 import com.google.common.primitives.Bytes;
22 import java.io.ByteArrayInputStream;
23 import java.io.ByteArrayOutputStream;
24 import java.io.DataInput;
25 import java.io.DataOutputStream;
26 import java.io.EOFException;
27 import java.io.IOException;
28 import junit.framework.TestCase;
29 
30 /**
31  * Test class for {@link LittleEndianDataInputStream}.
32  *
33  * @author Chris Nokleberg
34  */
35 public class LittleEndianDataInputStreamTest extends TestCase {
36 
37   private byte[] data;
38 
39   @Override
setUp()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 
initializeData(DataOutputStream out)51   private void initializeData(DataOutputStream out) throws IOException {
52     /* Write out various test values NORMALLY */
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 
testReadFully()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 
testReadUnsignedByte_eof()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 
testReadUnsignedShort_eof()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 
testReadLine()95   public void testReadLine() throws IOException {
96     DataInput in = new LittleEndianDataInputStream(new ByteArrayInputStream(data));
97     try {
98       in.readLine();
99       fail();
100     } catch (UnsupportedOperationException expected) {
101       assertThat(expected).hasMessageThat().isEqualTo("readLine is not supported");
102     }
103   }
104 
testReadLittleEndian()105   public void testReadLittleEndian() throws IOException {
106     DataInput in = new LittleEndianDataInputStream(new ByteArrayInputStream(data));
107 
108     /* Read in various values in LITTLE ENDIAN FORMAT */
109     byte[] b = new byte[2];
110     in.readFully(b);
111     assertEquals(-100, b[0]);
112     assertEquals(100, b[1]);
113     assertEquals(true, in.readBoolean());
114     assertEquals(false, in.readBoolean());
115     assertEquals(100, in.readByte());
116     assertEquals(-100, in.readByte());
117     assertEquals(200, in.readUnsignedByte());
118     assertEquals('\u6100', in.readChar());
119     assertEquals(-12150, in.readShort());
120     assertEquals(20675, in.readUnsignedShort());
121     assertEquals(0xBEBAFECA, in.readInt());
122     assertEquals(0xBEBAFECAEFBEADDEL, in.readLong());
123     assertEquals("Herby Derby", in.readUTF());
124     assertEquals(0xBEBAFECA, Float.floatToIntBits(in.readFloat()));
125     assertEquals(0xBEBAFECAEFBEADDEL, Double.doubleToLongBits(in.readDouble()));
126   }
127 
testSkipBytes()128   public void testSkipBytes() throws IOException {
129     ByteArrayOutputStream baos = new ByteArrayOutputStream();
130     DataOutputStream out = new DataOutputStream(baos);
131 
132     /* Write out various test values NORMALLY */
133     out.write(new byte[] {0, 1, 2, 3, 4, 5, 6, 7, 8, 9}); // 10 bytes of junk to skip
134     initializeData(out);
135 
136     byte[] data = baos.toByteArray();
137 
138     DataInput in = new LittleEndianDataInputStream(new ByteArrayInputStream(data));
139     int bytesSkipped = 0;
140     while (bytesSkipped < 10) {
141       bytesSkipped += in.skipBytes(10 - bytesSkipped);
142     }
143 
144     /* Read in various values in LITTLE ENDIAN FORMAT */
145     byte[] b = new byte[2];
146     in.readFully(b);
147     assertEquals(-100, b[0]);
148     assertEquals(100, b[1]);
149     assertTrue(in.readBoolean());
150     assertFalse(in.readBoolean());
151   }
152 }
153