Code example for RandomAccessFile

Methods: lengthreadreadIntskipBytes

0
    protected int readHeader(final RandomAccessFile file) throws IOException, InvalidTagException {
        final int size;
        final byte[] buffer = new byte[3];
        if (has6ByteHeader()) { 
            // read the 3 byte size 
            file.read(buffer, 0, 3);
            size = (int) (buffer[0] * Math.pow(2, 16) + buffer[1] * Math.pow(2, 8) + buffer[2]);
        } else { 
            // read the 4 byte size 
            size = file.readInt();
 
            // we need to skip the flag bytes; 
            file.skipBytes(2);
        } 
        if (size == 0) {
            throw new InvalidTagException("Found empty frame"); 
        } 
        if (size <= 0 || size > file.length()) {
            throw new InvalidTagException("Invalid size for Frame Body"); 
        } 
        return size;
    } 
 
Stop searching for code, let great code find you!  Add Codota to your java IDE