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