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