Example usage for java.io RandomAccessFile read

List of usage examples for java.io RandomAccessFile read

Introduction

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

Prototype

public int read(byte b[]) throws IOException 

Source Link

Document

Reads up to b.length bytes of data from this file into an array of bytes.

Usage

From source file:de.erdesignerng.dialect.msaccess.MSAccessFileFormat.java

private static int findInFile(String aFileName, String aSearchFor) {
    int theBufferSize = 5242880; // 5MB
    boolean theSearchOn = true;
    String theStringBuffer;/* w w  w  . j  a v  a2 s  . c  om*/
    int theOffset = 0;
    int theRead = theBufferSize;
    int thePosition;
    int theOverhead = aSearchFor.length() - 1;
    int theResult = -1;

    if (theBufferSize >= aSearchFor.length()) {
        try {
            File file = new File(aFileName);
            RandomAccessFile ra = new RandomAccessFile(aFileName, "r");
            byte[] theByteBuffer = new byte[theBufferSize];

            while ((theOffset < file.length()) && (theSearchOn) && (theRead == theBufferSize)) {
                theRead = ra.read(theByteBuffer);

                if (theRead >= 0) {
                    theStringBuffer = new String(theByteBuffer, 0, theRead);

                    thePosition = theStringBuffer.indexOf(aSearchFor);

                    if (thePosition >= 0) {
                        theResult = theOffset + thePosition;
                        theSearchOn = false;
                        LOGGER.debug(
                                "Found '" + aSearchFor + "' in '" + aFileName + "' at position " + theResult);
                    } else {
                        if (theRead == theBufferSize) {
                            theOffset += (theRead - theOverhead);
                            ra.seek(theOffset);
                        }
                    }
                }
            }

            ra.close();
        } catch (FileNotFoundException ex) {
            LOGGER.error("Cannot find database file " + aFileName, ex);
        } catch (IOException ex) {
            LOGGER.error("Cannot read database file " + aFileName, ex);
        }
    } else {
        throw new RuntimeException("The string to find is too long. Only strings of lenght up to "
                + theBufferSize + " can be found!");
    }

    return theResult;
}

From source file:com.aol.advertising.qiao.util.CommonUtils.java

public static long checksum(RandomAccessFile raFile, int numBytes)
        throws IOException, InsufficientFileLengthException {
    CRC32 _crc = new CRC32();

    long pos = raFile.getFilePointer();
    try {/*from   ww  w . j  a va 2 s  .  c  o m*/
        byte[] buffer = new byte[numBytes];
        raFile.seek(0);
        int n = raFile.read(buffer);
        if (n < numBytes) {
            String s;
            logger.warn(s = ("not enough data for checksum: current file size=" + n));
            throw new InsufficientFileLengthException(s);
        }

        synchronized (_crc) {
            _crc.reset();
            _crc.update(buffer);

            return _crc.getValue();
        }
    } finally {
        raFile.seek(pos);
    }

}

From source file:org.mhisoft.common.util.FileUtils.java

public static int readInt(RandomAccessFile fileInputStream) throws IOException {
    byte[] bytesInt = new byte[4];
    int readBytes = fileInputStream.read(bytesInt);
    if (readBytes != 4)
        throw new RuntimeException("didn't read 4 bytes for a integer");

    return ByteArrayHelper.bytesToInt(bytesInt);
}

From source file:jsave.Utils.java

private static BigInteger read_uint64(final RandomAccessFile raf) throws IOException {
    byte[] data = new byte[8];
    raf.read(data);
    return new BigInteger(1, data);
}

From source file:jsave.Utils.java

public static int read_long(final RandomAccessFile raf) throws IOException {
    byte[] data = new byte[4];
    raf.read(data);
    ByteBuffer bb = ByteBuffer.allocate(data.length);
    bb.put(data);/*from   w  w w .ja  v a 2s  .com*/
    return bb.getInt(0);
}

From source file:jsave.Utils.java

public static int read_uint16(final RandomAccessFile raf) throws IOException {
    byte[] data = new byte[2];
    raf.read(data);
    ByteBuffer bb = ByteBuffer.allocate(data.length);
    bb.put(data);//w ww.  j ava  2  s.  c  o  m
    return getUnsignedShort(bb);
}

From source file:jsave.Utils.java

public static long read_uint32(final RandomAccessFile raf) throws IOException {
    byte[] data = new byte[4];
    raf.read(data);
    ByteBuffer bb = ByteBuffer.allocate(data.length);
    bb.put(data);// w  ww. j  av a  2s  .  c o m
    return getUnsignedInt(bb);
}

From source file:jsave.Utils.java

/**
 * Skip length bytes./*  w ww.j a v  a2s.c om*/
 *
 * @param raf the file where the bytes are read
 * @param length the length to skip
 * @throws IOException
 */
public static void skip_bytes(final RandomAccessFile raf, int length) throws IOException {
    byte[] data = new byte[length];
    raf.read(data);
}

From source file:jsave.Utils.java

/**
 * Reads an Integer (-2147483648 to 2147483647).
 *
 * @param raf the 4 Bytes where the file is read
 * @return an Integer/*w ww.j a  va2s.  co m*/
 */
private static int read_int32(RandomAccessFile raf) {
    byte[] data = new byte[4];
    int intData = -1;
    try {
        raf.read(data);
        intData = ByteBuffer.wrap(data).getInt();
    } catch (IOException e) {
    }
    return intData;
}

From source file:jsave.Utils.java

/**
 * Reads a Integer (-9223372036854775808 to 9223372036854775807).
 *
 * @param raf the 4 Bytes where the file is read
 * @return an long integer//from   w w  w. j a v a  2 s . c  om
 */
private static long read_int64(RandomAccessFile raf) {
    byte[] data = new byte[8];
    long intData = -1;
    try {
        raf.read(data);
        intData = ByteBuffer.wrap(data).getLong();
    } catch (IOException e) {
    }
    return intData;
}