Example usage for java.io RandomAccessFile readShort

List of usage examples for java.io RandomAccessFile readShort

Introduction

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

Prototype

public final short readShort() throws IOException 

Source Link

Document

Reads a signed 16-bit number from this file.

Usage

From source file:Main.java

public static void main(String[] args) {
    try {//from   w  w w.  jav a  2  s  .co  m
        short s = 15;

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

        raf.writeShort(s);

        raf.seek(0);

        System.out.println(raf.readShort());

        raf.seek(0);

        raf.writeShort(20);

        raf.seek(0);

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

}

From source file:Main.java

public static void main(String[] args) {
    try {//  www . j  av a2  s  .  c  o m
        short s = 15000;

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

        // write something in the file
        raf.writeShort(s);

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

        // print the short
        System.out.println(raf.readShort());

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

        // write something in the file
        raf.writeShort(134);

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

        // print the short
        System.out.println(raf.readShort());
        raf.close();
    } catch (IOException ex) {
        ex.printStackTrace();
    }

}

From source file:Main.java

static boolean checkMPEntryTag(RandomAccessFile randomAccessFile) throws IOException {
    if (45058 == (65535 & randomAccessFile.readShort())) {
        return true;
    }//  w  w w .  ja v  a 2 s. c om
    return false;
}

From source file:Main.java

public static short[] fromAudioFile(RandomAccessFile f) {
    short[] samples = null;

    try {/* w  w w .j  ava 2 s .c o m*/
        samples = new short[(int) (f.length() / 2)];
        f.seek(0); //Seek to start point of file

        for (int i = 0; i < f.length() / 2; i++) {
            samples[i] = f.readShort();
        }
        f.close();
    } catch (IOException e) {
        e.printStackTrace();
    }

    return samples;
}

From source file:org.commoncrawl.service.listcrawler.CrawlHistoryManager.java

private ProxyCrawlHistoryItem readItem(RandomAccessFile fileStream) throws IOException {

    try {//from www. java  2s  .  c o m
        // read sync bytes ...
        fileStream.read(_syncByteBuffer);
        // validate ...
        if (!Arrays.equals(_header._sync, _syncByteBuffer)) {
            throw new IOException("Error Reading Sync Bytes for Item In Checkpoint");
        }
        int checksum = fileStream.readInt();
        int payloadSize = fileStream.readShort();

        if (payloadSize == 0) {
            throw new IOException("Invalid Payload Size Reading Item In Checkpoint");
        }
        // read the payload
        _payloadBuffer.setCapacity(payloadSize);

        fileStream.read(_payloadBuffer.get(), 0, payloadSize);

        _crc16in.reset();
        _crc16in.update(_payloadBuffer.get(), 0, payloadSize);

        // if computed checksum does not match file checksum !!!
        if (_crc16in.getValue() != (long) checksum) {
            throw new IOException("Checksum Mismatch Expected:" + checksum + " got:" + _crc16in.getValue()
                    + " while Reading Item");
        }
        _payloadInputStream.reset(_payloadBuffer.get(), 0, payloadSize);

        ProxyCrawlHistoryItem itemOut = new ProxyCrawlHistoryItem();

        itemOut.deserialize(_payloadInputStream, new BinaryProtocol());

        return itemOut;
    } catch (Exception e) {
        LOG.error(CCStringUtils.stringifyException(e));
        throw new IOException(e);
    }
}