Example usage for java.io RandomAccessFile readUnsignedByte

List of usage examples for java.io RandomAccessFile readUnsignedByte

Introduction

In this page you can find the example usage for java.io RandomAccessFile readUnsignedByte.

Prototype

public final int readUnsignedByte() throws IOException 

Source Link

Document

Reads an unsigned eight-bit number from this file.

Usage

From source file:Main.java

public static void main(String[] args) {
    try {// ww w .ja  v a2  s  .com

        RandomAccessFile raf = new RandomAccessFile("c:/test.txt", "rw");

        // write something in the file
        raf.writeUTF("Hello World from java2s.com");

        // set the file pointer at 0 position
        raf.seek(0);

        // print the byte
        System.out.println(raf.readUnsignedByte());

        // set the file pointer at 7 position
        raf.seek(7);

        System.out.println(raf.readUnsignedByte());
        raf.close();
    } catch (IOException ex) {
        ex.printStackTrace();
    }

}

From source file:edu.msu.cme.rdp.readseq.readers.core.SFFCore.java

private LinkedHashMap<String, Long> readIndex() throws IOException {
    if (commonHeader.indexOffset <= commonHeader.headerLength) {
        throw new IOException("Index offset is not set correctly");
    }/*from   w  w  w.  j a  v  a2s.c  o m*/

    RandomAccessFile seqFile = super.getRawFile();

    long seekBackTo = seqFile.getFilePointer();
    seqFile.seek(commonHeader.indexOffset);
    long dataEnd = seqFile.getFilePointer();
    LinkedHashMap<String, Long> seqIndex = new LinkedHashMap(commonHeader.numReads);

    int magicNumber = seqFile.readInt();

    if (magicNumber == mftMagicNumber) {
        int version = seqFile.readInt();

        if (version != v1MagicNumber) {
            throw new IOException("Can only parse .mft v1.0 indices");
        }

        int xmlSize = seqFile.readInt();
        int dataSize = seqFile.readInt();
        dataEnd += dataSize;

        byte[] xml = new byte[xmlSize];
        seqFile.read(xml);
        manifest = new String(xml);

    } else if (magicNumber == srtMagicNumber) {
        int version = seqFile.readInt();

        if (version != v1MagicNumber) {
            throw new IOException("Can only parse .srt v1.0 indices");
        }

        if (seqFile.read() != 0) {
            throw new IOException("Failed to find expected null byte in .srt header");
        }
        dataEnd += commonHeader.indexLength;
    } else {
        throw new IOException("No supported index found");
    }

    List<Integer> currIndex = new ArrayList();
    while (seqFile.getFilePointer() < dataEnd) {
        int b = seqFile.readUnsignedByte();
        if (b == 0xff) {
            byte[] nameArray = new byte[currIndex.size() - 5];
            long indexLoc = 0;
            int[] multipliers = new int[] { 0, 16581375, 65025, 255, 1 };
            for (int i = 0; i < currIndex.size(); i++) {
                if (i < nameArray.length) {
                    nameArray[i] = (byte) (currIndex.get(i) & 0xff);
                } else {
                    int index = i - nameArray.length;
                    indexLoc += currIndex.get(i) * multipliers[index];
                }
            }
            String name = new String(nameArray);

            seqIndex.put(name, indexLoc);

            currIndex.clear();
        } else {
            currIndex.add(b);
        }
    }
    seqFile.seek(seekBackTo);

    return seqIndex;
}