Code example for DataInputStream

Methods: readChar

0
     */ 
    public void test_readChar() throws IOException { 
        os.writeChar('t');
        os.close();
        openDataInputStream(); 
        assertEquals("Incorrect char read", 't', dis.readChar());
    } 
 
    /** 
     * @tests java.io.DataInputStream#readDouble() 
     */ 
    public void test_readDouble() throws IOException { 
        os.writeDouble(2345.76834720202);
        os.close();
        openDataInputStream(); 
        assertEquals("Incorrect double read", 2345.76834720202, dis
                .readDouble());
    } 
 
    /** 
     * @tests java.io.DataInputStream#readFloat() 
     */ 
    public void test_readFloat() throws IOException { 
        os.writeFloat(29.08764f);
        os.close();
        openDataInputStream(); 
        assertTrue("Incorrect float read", dis.readFloat() == 29.08764f);
    } 
 
    /** 
     * @tests java.io.DataInputStream#readFully(byte[]) 
     */ 
    public void test_readFully$B() throws IOException { 
        os.write(fileString.getBytes());
        os.close();
        openDataInputStream(); 
        byte rbytes[] = new byte[fileString.length()];
        dis.readFully(rbytes);
        assertTrue("Incorrect data read", new String(rbytes, 0, fileString
                .length()).equals(fileString));
    } 
 
    /** 
     * @tests java.io.DataInputStream#readFully(byte[], int, int) 
     */ 
    public void test_readFully$BII() throws IOException { 
        os.write(fileString.getBytes());
        os.close();
        openDataInputStream(); 
        byte rbytes[] = new byte[fileString.length()];
        dis.readFully(rbytes, 0, fileString.length());
        assertTrue("Incorrect data read", new String(rbytes, 0, fileString
                .length()).equals(fileString));
    } 
 
    /** 
     * @tests java.io.DataInputStream#readFully(byte[], int, int) 
     */ 
    public void test_readFully$BII_Exception() throws IOException { 
        DataInputStream is = new DataInputStream(new ByteArrayInputStream(
                new byte[fileString.length()]));
 
        byte[] byteArray = new byte[fileString.length()];
 
        try { 
            is.readFully(byteArray, -1, -1);
            fail("should throw IndexOutOfBoundsException");
        } catch (IndexOutOfBoundsException e) {
            // expected 
        } 
 
        try { 
            is.readFully(byteArray, 0, -1);
            fail("should throw IndexOutOfBoundsException");
        } catch (IndexOutOfBoundsException e) {
            // expected 
        } 
 
        try { 
            is.readFully(byteArray, 1, -1);
            fail("should throw IndexOutOfBoundsException");
        } catch (IndexOutOfBoundsException e) {
            // expected 
        } 
 
        is.readFully(byteArray, -1, 0);
        is.readFully(byteArray, 0, 0);
        is.readFully(byteArray, 1, 0);
 
        try { 
            is.readFully(byteArray, -1, 1);
            fail("should throw IndexOutOfBoundsException");
        } catch (IndexOutOfBoundsException e) {
            // expected 
        } 
 
        is.readFully(byteArray, 0, 1);
        is.readFully(byteArray, 1, 1);
        try { 
            is.readFully(byteArray, 0, Integer.MAX_VALUE);
            fail("should throw IndexOutOfBoundsException");
        } catch (IndexOutOfBoundsException e) {
            // expected 
        } 
    } 
 
    /** 
     * @tests java.io.DataInputStream#readFully(byte[], int, int) 
     */ 
    public void test_readFully$BII_NullArray() throws IOException { 
        DataInputStream is = new DataInputStream(new ByteArrayInputStream(
                new byte[fileString.length()]));
 
        byte[] nullByteArray = null;
 
        try { 
            is.readFully(nullByteArray, -1, -1);
            fail();
        } catch (NullPointerException expected) {
        } catch (IndexOutOfBoundsException expected) {
        } 
 
        try { 
            is.readFully(nullByteArray, 0, -1);
            fail();
        } catch (NullPointerException expected) {
        } catch (IndexOutOfBoundsException expected) {
        } 
 
        try { 
            is.readFully(nullByteArray, 1, -1);
            fail();
        } catch (NullPointerException expected) {
        } catch (IndexOutOfBoundsException expected) {
        } 
 
        is.readFully(nullByteArray, -1, 0);
        is.readFully(nullByteArray, 0, 0);
        is.readFully(nullByteArray, 1, 0);
 
        try { 
            is.readFully(nullByteArray, -1, 1);
            fail();
        } catch (NullPointerException expected) {
        } catch (IndexOutOfBoundsException expected) {
        } 
 
        try { 
            is.readFully(nullByteArray, 0, 1);
            fail("should throw NullPointerException");
        } catch (NullPointerException e) {
            // expected 
        } 
 
        try { 
            is.readFully(nullByteArray, 1, 1);
            fail("should throw NullPointerException");
        } catch (NullPointerException e) {
            // expected 
        } 
 
        try { 
            is.readFully(nullByteArray, 0, Integer.MAX_VALUE);
            fail("should throw NullPointerException");
        } catch (NullPointerException e) {
            // expected 
        } 
    } 
 
    /** 
     * @tests java.io.DataInputStream#readFully(byte[], int, int) 
     */ 
    public void test_readFully$BII_NullStream() throws IOException { 
        DataInputStream is = new DataInputStream(null);
        byte[] byteArray = new byte[fileString.length()];
 
        try { 
            is.readFully(byteArray, -1, -1);
            fail();
        } catch (NullPointerException expected) {
        } catch (IndexOutOfBoundsException expected) {
        } 
 
        try { 
            is.readFully(byteArray, 0, -1);
            fail();
        } catch (NullPointerException expected) {
        } catch (IndexOutOfBoundsException expected) {
        } 
 
        try { 
            is.readFully(byteArray, 1, -1);
            fail();
        } catch (NullPointerException expected) {
        } catch (IndexOutOfBoundsException expected) {
        } 
 
        is.readFully(byteArray, -1, 0);
        is.readFully(byteArray, 0, 0);
        is.readFully(byteArray, 1, 0);
 
        try { 
            is.readFully(byteArray, -1, 1);
            fail("should throw NullPointerException");
        } catch (NullPointerException e) {
            // expected 
        } 
 
        try { 
            is.readFully(byteArray, 0, 1);
            fail("should throw NullPointerException");
        } catch (NullPointerException e) {
            // expected 
        } 
 
        try { 
            is.readFully(byteArray, 1, 1);
            fail("should throw NullPointerException");
        } catch (NullPointerException e) {
            // expected 
        } 
 
        try { 
            is.readFully(byteArray, 0, Integer.MAX_VALUE);
            fail("should throw NullPointerException");
        } catch (NullPointerException e) {
            // expected 
        } 
    } 
 
    /** 
     * @tests java.io.DataInputStream#readFully(byte[], int, int) 
     */ 
    public void test_readFully$BII_NullStream_NullArray() throws IOException { 
        DataInputStream is = new DataInputStream(null);
        byte[] nullByteArray = null;
 
        try { 
            is.readFully(nullByteArray, -1, -1);
            fail();
        } catch (NullPointerException expected) {
        } catch (IndexOutOfBoundsException expected) {
        } 
 
        try { 
            is.readFully(nullByteArray, 0, -1);
            fail();
        } catch (NullPointerException expected) {
        } catch (IndexOutOfBoundsException expected) {
        } 
 
        try { 
            is.readFully(nullByteArray, 1, -1);
            fail();
        } catch (NullPointerException expected) {
        } catch (IndexOutOfBoundsException expected) {
        } 
 
        is.readFully(nullByteArray, -1, 0);
        is.readFully(nullByteArray, 0, 0);
        is.readFully(nullByteArray, 1, 0);
 
        try { 
            is.readFully(nullByteArray, -1, 1);
            fail("should throw NullPointerException");
        } catch (NullPointerException e) {
            // expected 
        } 
 
        try { 
            is.readFully(nullByteArray, 0, 1);
            fail("should throw NullPointerException");
        } catch (NullPointerException e) {
            // expected 
        } 
 
        try { 
            is.readFully(nullByteArray, 1, 1);
            fail("should throw NullPointerException");
        } catch (NullPointerException e) {
            // expected 
        } 
 
        try { 
            is.readFully(nullByteArray, 0, Integer.MAX_VALUE);
            fail("should throw NullPointerException");
        } catch (NullPointerException e) {
            // expected 
        } 
    } 
 
    /** 
     * @tests java.io.DataInputStream#readInt() 
     */ 
    public void test_readInt() throws IOException { 
        os.writeInt(768347202);
        os.close();
        openDataInputStream(); 
        assertEquals("Incorrect int read", 768347202, dis.readInt());
    } 
 
    /** 
     * @tests java.io.DataInputStream#readLine() 
     */ 
    @SuppressWarnings("deprecation") 
    public void test_readLine() throws IOException { 
        os.writeBytes("Hello");
        os.close();
        openDataInputStream(); 
        String line = dis.readLine();
        assertTrue("Incorrect line read: " + line, line.equals("Hello"));
    } 
 
    /** 
     * @tests java.io.DataInputStream#readLong() 
     */ 
    public void test_readLong() throws IOException { 
        os.writeLong(9875645283333L);
        os.close();
        openDataInputStream(); 
        assertEquals("Incorrect long read", 9875645283333L, dis.readLong());
    } 
 
    /** 
     * @tests java.io.DataInputStream#readShort() 
     */ 
    public void test_readShort() throws IOException { 
        os.writeShort(9875);
        os.close();
        openDataInputStream(); 
        assertTrue("Incorrect short read", dis.readShort() == (short) 9875);
    } 
 
    /** 
     * @tests java.io.DataInputStream#readUnsignedByte() 
     */ 
    public void test_readUnsignedByte() throws IOException { 
        os.writeByte((byte) -127);
        os.close();
        openDataInputStream(); 
        assertEquals("Incorrect byte read", 129, dis.readUnsignedByte());
    } 
 
    /** 
     * @tests java.io.DataInputStream#readUnsignedShort() 
     */ 
    public void test_readUnsignedShort() throws IOException { 
        os.writeShort(9875);
        os.close();
        openDataInputStream(); 
        assertEquals("Incorrect short read", 9875, dis.readUnsignedShort());
    } 
 
    /** 
     * @tests java.io.DataInputStream#readUTF() 
     */ 
    public void test_readUTF() throws IOException { 
        os.writeUTF(unihw);
        os.close();
        openDataInputStream(); 
        assertTrue("Failed to write string in UTF format",
                dis.available() == unihw.length() + 2);
        assertTrue("Incorrect string read", dis.readUTF().equals(unihw));
    } 
 
    static class TestDataInputStream implements DataInput {
        public boolean readBoolean() throws IOException { 
            return false; 
        } 
 
        public byte readByte() throws IOException { 
            return (byte) 0; 
        } 
 
        public char readChar() throws IOException { 
            return (char) 0; 
        } 
 
        public double readDouble() throws IOException { 
            return 0.0; 
        } 
 
        public float readFloat() throws IOException { 
            return (float) 0.0; 
        } 
 
        public void readFully(byte[] buffer) throws IOException {
        } 
 
        public void readFully(byte[] buffer, int offset, int count)
                throws IOException { 
        } 
 
        public int readInt() throws IOException { 
            return 0; 
        } 
 
        public String readLine() throws IOException {
            return null; 
        } 
 
        public long readLong() throws IOException { 
            return (long) 0; 
        } 
 
        public short readShort() throws IOException { 
            return (short) 0; 
        } 
 
        public int readUnsignedByte() throws IOException { 
            return 0; 
        } 
 
        public int readUnsignedShort() throws IOException { 
            return 0; 
        } 
 
        public String readUTF() throws IOException {
            return DataInputStream.readUTF(this);
        } 
 
        public int skipBytes(int count) throws IOException {
            return 0; 
        } 
    } 
 
    /** 
     * @tests java.io.DataInputStream#readUTF(java.io.DataInput) 
     */ 
    public void test_readUTFLjava_io_DataInput() throws IOException { 
        os.writeUTF(unihw);
        os.close();
        openDataInputStream(); 
        assertTrue("Failed to write string in UTF format",
                dis.available() == unihw.length() + 2);
        assertTrue("Incorrect string read", DataInputStream.readUTF(dis)
                .equals(unihw));
 
        // Regression test for HARMONY-5336 
        new TestDataInputStream().readUTF(); 
    } 
 
    /** 
     * @tests java.io.DataInputStream#skipBytes(int) 
     */ 
    public void test_skipBytesI() throws IOException { 
        byte fileBytes[] = fileString.getBytes();
        os.write(fileBytes);
        os.close();
        openDataInputStream(); 
        dis.skipBytes(100);
        byte rbytes[] = new byte[fileString.length()];
        dis.read(rbytes, 0, 50);
        dis.close();
        assertTrue("Incorrect data read", new String(rbytes, 0, 50)
                .equals(fileString.substring(100, 150)));
 
        int skipped = 0;
        openDataInputStream(); 
        try { 
            skipped = dis.skipBytes(50000);
        } catch (EOFException e) {
        } 
        assertTrue("Skipped should report " + fileString.length() + " not "
                + skipped, skipped == fileString.length());
    } 
 
    private void openDataInputStream() throws IOException { 
        dis = new DataInputStream(new ByteArrayInputStream(bos.toByteArray()));
    } 
 
    /** 
     * Sets up the fixture, for example, open a network connection. This method 
     * is called before a test is executed.