Code example for DataInputStream

Methods: readUnsignedByte

0
     */ 
    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.