• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2010 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 java.nio.charset.StandardCharsets.ISO_8859_1;
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.DataInputStream;
26 import java.io.IOException;
27 import junit.framework.TestCase;
28 
29 /**
30  * Test class for {@link LittleEndianDataOutputStream}.
31  *
32  * @author Keith Bottner
33  */
34 public class LittleEndianDataOutputStreamTest extends TestCase {
35 
36   private ByteArrayOutputStream baos = new ByteArrayOutputStream();
37   private LittleEndianDataOutputStream out = new LittleEndianDataOutputStream(baos);
38 
testWriteLittleEndian()39   public void testWriteLittleEndian() throws IOException {
40 
41     /* Write out various test values in LITTLE ENDIAN FORMAT */
42     out.write(new byte[] {-100, 100});
43     out.writeBoolean(true);
44     out.writeBoolean(false);
45     out.writeByte(100);
46     out.writeByte(-100);
47     out.writeByte((byte) 200);
48     out.writeChar('a');
49     out.writeShort((short) -30000);
50     out.writeShort((short) 50000);
51     out.writeInt(0xCAFEBABE);
52     out.writeLong(0xDEADBEEFCAFEBABEL);
53     out.writeUTF("Herby Derby");
54     out.writeFloat(Float.intBitsToFloat(0xCAFEBABE));
55     out.writeDouble(Double.longBitsToDouble(0xDEADBEEFCAFEBABEL));
56 
57     byte[] data = baos.toByteArray();
58 
59     /* Setup input streams */
60     DataInput in = new DataInputStream(new ByteArrayInputStream(data));
61 
62     /* Read in various values NORMALLY */
63     byte[] b = new byte[2];
64     in.readFully(b);
65     assertEquals(-100, b[0]);
66     assertEquals(100, b[1]);
67     assertEquals(true, in.readBoolean());
68     assertEquals(false, in.readBoolean());
69     assertEquals(100, in.readByte());
70     assertEquals(-100, in.readByte());
71     assertEquals(200, in.readUnsignedByte());
72     assertEquals('\u6100', in.readChar());
73     assertEquals(-12150, in.readShort());
74     assertEquals(20675, in.readUnsignedShort());
75     assertEquals(0xBEBAFECA, in.readInt());
76     assertEquals(0xBEBAFECAEFBEADDEL, in.readLong());
77     assertEquals("Herby Derby", in.readUTF());
78     assertEquals(0xBEBAFECA, Float.floatToIntBits(in.readFloat()));
79     assertEquals(0xBEBAFECAEFBEADDEL, Double.doubleToLongBits(in.readDouble()));
80   }
81 
82   @SuppressWarnings("deprecation") // testing a deprecated method
testWriteBytes()83   public void testWriteBytes() throws IOException {
84 
85     /* Write out various test values in LITTLE ENDIAN FORMAT */
86     out.writeBytes("r\u00C9sum\u00C9");
87 
88     byte[] data = baos.toByteArray();
89 
90     /* Setup input streams */
91     DataInput in = new DataInputStream(new ByteArrayInputStream(data));
92 
93     /* Read in various values NORMALLY */
94     byte[] b = new byte[6];
95     in.readFully(b);
96     assertEquals("r\u00C9sum\u00C9".getBytes(ISO_8859_1), b);
97   }
98 
99   @SuppressWarnings("deprecation") // testing a deprecated method
testWriteBytes_discardHighOrderBytes()100   public void testWriteBytes_discardHighOrderBytes() throws IOException {
101 
102     /* Write out various test values in LITTLE ENDIAN FORMAT */
103     out.writeBytes("\uAAAA\uAABB\uAACC");
104 
105     byte[] data = baos.toByteArray();
106 
107     /* Setup input streams */
108     DataInput in = new DataInputStream(new ByteArrayInputStream(data));
109 
110     /* Read in various values NORMALLY */
111     byte[] b = new byte[3];
112     in.readFully(b);
113     byte[] expected = {(byte) 0xAA, (byte) 0xBB, (byte) 0xCC};
114     assertEquals(expected, b);
115   }
116 
testWriteChars()117   public void testWriteChars() throws IOException {
118 
119     /* Write out various test values in LITTLE ENDIAN FORMAT */
120     out.writeChars("r\u00C9sum\u00C9");
121 
122     byte[] data = baos.toByteArray();
123 
124     /* Setup input streams */
125     DataInput in = new DataInputStream(new ByteArrayInputStream(data));
126 
127     /* Read in various values NORMALLY */
128     byte[] actual = new byte[12];
129     in.readFully(actual);
130     assertEquals('r', actual[0]);
131     assertEquals(0, actual[1]);
132     assertEquals((byte) 0xC9, actual[2]);
133     assertEquals(0, actual[3]);
134     assertEquals('s', actual[4]);
135     assertEquals(0, actual[5]);
136     assertEquals('u', actual[6]);
137     assertEquals(0, actual[7]);
138     assertEquals('m', actual[8]);
139     assertEquals(0, actual[9]);
140     assertEquals((byte) 0xC9, actual[10]);
141     assertEquals(0, actual[11]);
142   }
143 
assertEquals(byte[] expected, byte[] actual)144   private static void assertEquals(byte[] expected, byte[] actual) {
145     assertEquals(Bytes.asList(expected), Bytes.asList(actual));
146   }
147 }
148