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:org.mhisoft.common.util.FileUtils.java

public static String readString(RandomAccessFile raFile) throws IOException {
    int numBytes = FileUtils.readInt(raFile);
    byte[] _byte = new byte[numBytes];
    int readBytes = raFile.read(_byte);
    if (readBytes != numBytes)
        throw new RuntimeException(
                "readString() failed, " + "read " + readBytes + " bytes only, expected to read:" + numBytes);

    return StringUtils.bytesToString(_byte);

}

From source file:jsave.Utils.java

/**
 * Reads a Byte (-128 to 127).// w  w  w .ja  va2  s  . co  m
 *
 * @param raf the file where the 4 Bytes are read
 * @return a Byte
 */
private static short read_UnsignedByte(RandomAccessFile raf) {
    byte[] data = new byte[1];
    byte byteData = -1;
    try {
        raf.read(data);
        ByteBuffer bb = ByteBuffer.allocate(data.length);
        bb.put(data);
        return getUnsignedByte(bb);
    } catch (IOException e) {
    }

    return byteData;
}

From source file:jsave.Utils.java

/**
 * Reads an Integer (-32768 to 32767)/*from  www  . j ava2  s . co m*/
 *
 * @param raf the 4 Bytes where the file is read
 * @return a short Integer
 */
private static short read_int16(RandomAccessFile raf) {
    byte[] data = new byte[2];
    short shortData = -1;
    try {
        raf.read(data);
        shortData = ByteBuffer.wrap(data).getShort();
    } catch (IOException e) {
    }

    return shortData;
}

From source file:jsave.Utils.java

/**
 * Reads a Single precision float (sign bit, 8 bits exponent, 23 bits
 * mantissa)./*from   w w  w .  j  a  va2  s  .c o  m*/
 *
 * @param raf the 4 Bytes where the file is read
 * @return a Single precision float (sign bit, 8 bits exponent, 23 bits
 * mantissa)
 */
private static float read_float32(RandomAccessFile raf) {
    byte[] data = new byte[4];
    float floatData = -1;
    try {
        raf.read(data);
        floatData = ByteBuffer.wrap(data).getFloat();

    } catch (IOException e) {
    }

    return floatData;
}

From source file:jsave.Utils.java

/**
 * Reads a Double precision float: sign bit, 11 bits exponent, 52 bits
 * mantissa// w w  w  . j av a 2  s  .com
 *
 * @param raf the file where the bytes are read
 * @return a Double precision float
 */
private static double read_float64(RandomAccessFile raf) {
    byte[] data = new byte[8];
    double floatData = -1;
    try {
        raf.read(data);
        floatData = ByteBuffer.wrap(data).getDouble();
    } catch (IOException e) {
    }

    return floatData;
}

From source file:eionet.eunis.servlets.DownloadServlet.java

/**
 * Copy the given byte range of the given input to the given output.
 *
 * @param input The input to copy the given range to the given output for.
 * @param output The output to copy the given range from the given input for.
 * @param start Start of the byte range.
 * @param length Length of the byte range.
 * @throws IOException If something fails at I/O level.
 *//*from   ww  w .j a v a2 s  . co m*/
private static void copy(RandomAccessFile input, OutputStream output, long start, long length)
        throws IOException {
    byte[] buffer = new byte[DEFAULT_BUFFER_SIZE];
    int read;

    if (input.length() == length) {
        // Write full range.
        while ((read = input.read(buffer)) > 0) {
            output.write(buffer, 0, read);
        }
    } else {
        // Write partial range.
        input.seek(start);
        long toRead = length;

        while ((read = input.read(buffer)) > 0) {
            toRead = toRead - read;
            if (toRead > 0) {
                output.write(buffer, 0, read);
            } else {
                output.write(buffer, 0, (int) toRead + read);
                break;
            }
        }
    }
}

From source file:jsave.Utils.java

public static String read_string(final RandomAccessFile raf) throws IOException {
    int length = read_long(raf);
    String result;//w w  w. j a v  a 2s . c  om
    if (length > 0) {
        byte[] data = new byte[length];
        raf.read(data);
        align_32(raf);
        result = new String(data, StandardCharsets.UTF_8);
    } else {
        result = "";
    }
    return result;
}

From source file:jsave.Utils.java

public static String read_string_data(final RandomAccessFile raf) throws IOException {
    int length = read_long(raf);
    String result;/*  w  w w . ja  v a2 s . c  om*/
    if (length > 0) {
        length = read_long(raf);
        byte[] data = new byte[length];
        raf.read(data);
        align_32(raf);
        result = new String(data, StandardCharsets.UTF_8);
    } else {
        result = "";
    }
    return result;
}

From source file:com.hdsfed.cometapi.HCPClient.java

/**
 * Copy the given byte range of the given input to the given output.
 * @param input The input to copy the given range to the given output for.
 * @param output The output to copy the given range from the given input for.
 * @param start Start of the byte range.
 * @param length Length of the byte range.
 * @throws IOException If something fails at I/O level.
 *//*from  www . j  a va 2  s .  c om*/
public static void FileToOutputStream(RandomAccessFile input, OutputStream output, long start, long length)
        throws IOException {
    byte[] buffer = new byte[DEFAULT_BUFFER_SIZE];
    int read;

    if (input.length() == length) {
        // Write full range.
        while ((read = input.read(buffer)) > 0) {
            output.write(buffer, 0, read);
        }
    } else {
        // Write partial range.
        input.seek(start);
        long toRead = length;

        while ((read = input.read(buffer)) > 0) {
            if ((toRead -= read) > 0) {
                output.write(buffer, 0, read);
            } else {
                output.write(buffer, 0, (int) toRead + read);
                break;
            }
        }
    }
}

From source file:net.yacy.document.importer.MediawikiImporter.java

public static byte[] read(final File f, final long start, final int len) {
    final byte[] b = new byte[len];
    RandomAccessFile raf = null;
    try {//w  w  w . j  av a2 s  .  c o  m
        raf = new RandomAccessFile(f, "r");
        raf.seek(start);
        raf.read(b);
    } catch (final IOException e) {
        ConcurrentLog.logException(e);
        return null;
    } finally {
        if (raf != null)
            try {
                raf.close();
                try {
                    raf.getChannel().close();
                } catch (final IOException e) {
                }
            } catch (final IOException e) {
            }
    }
    return b;
}