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 import static org.junit.Assert.assertThrows; 21 22 import com.google.common.primitives.Bytes; 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 import junit.framework.TestCase; 30 31 /** 32 * Test class for {@link LittleEndianDataInputStream}. 33 * 34 * @author Chris Nokleberg 35 */ 36 public class LittleEndianDataInputStreamTest extends TestCase { 37 38 private byte[] data; 39 40 @Override setUp()41 protected void setUp() throws Exception { 42 super.setUp(); 43 44 ByteArrayOutputStream baos = new ByteArrayOutputStream(); 45 DataOutputStream out = new DataOutputStream(baos); 46 47 initializeData(out); 48 49 data = baos.toByteArray(); 50 } 51 initializeData(DataOutputStream out)52 private void initializeData(DataOutputStream out) throws IOException { 53 /* Write out various test values NORMALLY */ 54 out.write(new byte[] {-100, 100}); 55 out.writeBoolean(true); 56 out.writeBoolean(false); 57 out.writeByte(100); 58 out.writeByte(-100); 59 out.writeByte((byte) 200); 60 out.writeChar('a'); 61 out.writeShort((short) -30000); 62 out.writeShort((short) 50000); 63 out.writeInt(0xCAFEBABE); 64 out.writeLong(0xDEADBEEFCAFEBABEL); 65 out.writeUTF("Herby Derby"); 66 out.writeFloat(Float.intBitsToFloat(0xCAFEBABE)); 67 out.writeDouble(Double.longBitsToDouble(0xDEADBEEFCAFEBABEL)); 68 } 69 testReadFully()70 public void testReadFully() throws IOException { 71 DataInput in = new LittleEndianDataInputStream(new ByteArrayInputStream(data)); 72 byte[] b = new byte[data.length]; 73 in.readFully(b); 74 assertEquals(Bytes.asList(data), Bytes.asList(b)); 75 } 76 testReadUnsignedByte_eof()77 public void testReadUnsignedByte_eof() throws IOException { 78 DataInput in = new LittleEndianDataInputStream(new ByteArrayInputStream(new byte[0])); 79 assertThrows(EOFException.class, () -> in.readUnsignedByte()); 80 } 81 testReadUnsignedShort_eof()82 public void testReadUnsignedShort_eof() throws IOException { 83 byte[] buf = {23}; 84 DataInput in = new LittleEndianDataInputStream(new ByteArrayInputStream(buf)); 85 assertThrows(EOFException.class, () -> in.readUnsignedShort()); 86 } 87 88 @SuppressWarnings("DoNotCall") testReadLine()89 public void testReadLine() throws IOException { 90 DataInput in = new LittleEndianDataInputStream(new ByteArrayInputStream(data)); 91 UnsupportedOperationException expected = 92 assertThrows(UnsupportedOperationException.class, () -> in.readLine()); 93 assertThat(expected).hasMessageThat().isEqualTo("readLine is not supported"); 94 } 95 testReadLittleEndian()96 public void testReadLittleEndian() throws IOException { 97 DataInput in = new LittleEndianDataInputStream(new ByteArrayInputStream(data)); 98 99 /* Read in various values in LITTLE ENDIAN FORMAT */ 100 byte[] b = new byte[2]; 101 in.readFully(b); 102 assertEquals(-100, b[0]); 103 assertEquals(100, b[1]); 104 assertEquals(true, in.readBoolean()); 105 assertEquals(false, in.readBoolean()); 106 assertEquals(100, in.readByte()); 107 assertEquals(-100, in.readByte()); 108 assertEquals(200, in.readUnsignedByte()); 109 assertEquals('\u6100', in.readChar()); 110 assertEquals(-12150, in.readShort()); 111 assertEquals(20675, in.readUnsignedShort()); 112 assertEquals(0xBEBAFECA, in.readInt()); 113 assertEquals(0xBEBAFECAEFBEADDEL, in.readLong()); 114 assertEquals("Herby Derby", in.readUTF()); 115 assertEquals(0xBEBAFECA, Float.floatToIntBits(in.readFloat())); 116 assertEquals(0xBEBAFECAEFBEADDEL, Double.doubleToLongBits(in.readDouble())); 117 } 118 testSkipBytes()119 public void testSkipBytes() throws IOException { 120 ByteArrayOutputStream baos = new ByteArrayOutputStream(); 121 DataOutputStream out = new DataOutputStream(baos); 122 123 /* Write out various test values NORMALLY */ 124 out.write(new byte[] {0, 1, 2, 3, 4, 5, 6, 7, 8, 9}); // 10 bytes of junk to skip 125 initializeData(out); 126 127 byte[] data = baos.toByteArray(); 128 129 DataInput in = new LittleEndianDataInputStream(new ByteArrayInputStream(data)); 130 int bytesSkipped = 0; 131 while (bytesSkipped < 10) { 132 bytesSkipped += in.skipBytes(10 - bytesSkipped); 133 } 134 135 /* Read in various values in LITTLE ENDIAN FORMAT */ 136 byte[] b = new byte[2]; 137 in.readFully(b); 138 assertEquals(-100, b[0]); 139 assertEquals(100, b[1]); 140 assertTrue(in.readBoolean()); 141 assertFalse(in.readBoolean()); 142 } 143 } 144