Code example for DataInputStream

Methods: close, read, skipBytes

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