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 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 * 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 testReadLine()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 testReadLittleEndian()104 public void testReadLittleEndian() throws IOException { 105 DataInput in = new LittleEndianDataInputStream(new ByteArrayInputStream(data)); 106 107 /* Read in various values in LITTLE ENDIAN FORMAT */ 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 testSkipBytes()127 public void testSkipBytes() throws IOException { 128 ByteArrayOutputStream baos = new ByteArrayOutputStream(); 129 DataOutputStream out = new DataOutputStream(baos); 130 131 /* Write out various test values NORMALLY */ 132 out.write(new byte[] { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9}); // 10 bytes of junk to skip 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 /* Read in various values in LITTLE ENDIAN FORMAT */ 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 } 152