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:aarddict.Volume.java

Article readArticle(long pointer) throws IOException {
    Article a = articleCache.get(pointer);
    if (a != null)
        return a;
    Header h = this.header;
    long pos = h.articleOffset + pointer;
    RandomAccessFile f = this.file;
    f.seek(pos);//from ww w . jav  a2s  .  c  o  m
    long articleLength = f.readSpec(h.articleLengthSpec);

    byte[] articleBytes = new byte[(int) articleLength];
    f.read(articleBytes);
    String serializedArticle = decompress(articleBytes);
    a = Article.fromJsonStr(serializedArticle);
    a.dictionaryUUID = h.uuid;
    a.volumeId = h.sha1sum;
    a.pointer = pointer;
    articleCache.put(pointer, a);
    return a;
}

From source file:cern.acet.tracing.input.file.tailer.PositionTailer.java

/**
 * Read new lines./*from  ww  w .  j av a2  s .  co  m*/
 *
 * @param reader The file to read
 * @return The new position after the lines have been read
 * @throws java.io.IOException if an I/O error occurs.
 */
private long readLines(RandomAccessFile reader) throws IOException {
    StringBuilder sb = new StringBuilder();

    long pos = reader.getFilePointer();
    long rePos = pos; // position to re-read

    int num;
    boolean seenCR = false;
    while (run && ((num = reader.read(inbuf)) != -1)) {
        for (int i = 0; i < num; i++) {
            byte ch = inbuf[i];
            switch (ch) {
            case '\n':
                seenCR = false; // swallow CR before LF
                listener.handle(sb.toString());
                sb.setLength(0);
                rePos = pos + i + 1;
                break;
            case '\r':
                if (seenCR) {
                    sb.append('\r');
                }
                seenCR = true;
                break;
            default:
                if (seenCR) {
                    seenCR = false; // swallow final CR
                    listener.handle(sb.toString());
                    sb.setLength(0);
                    rePos = pos + i + 1;
                }
                sb.append((char) ch); // add character, not its ascii value
            }
        }

        pos = reader.getFilePointer();
    }

    reader.seek(rePos); // Ensure we can re-read if necessary
    listener.positionUpdated(pos);
    return rePos;
}

From source file:com.aol.advertising.qiao.injector.file.AbstractFileReader.java

protected long checksum(RandomAccessFile raFile) throws IOException, IllegalStateException {
    long pos = raFile.getFilePointer();
    try {/*from   ww  w .j a  v a2 s .  c  o m*/
        byte[] buffer = new byte[checksumByteLength];
        raFile.seek(0);
        int n = raFile.read(buffer);
        if (n < checksumByteLength) {
            String s;
            logger.warn(s = ("not enough data for checksum: current file size=" + n));
            throw new IllegalStateException(s);
        }

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

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

}

From source file:at.tuwien.minimee.util.TopParser.java

/**
 * The process ID is in the last line of the file and looks like follows:
 * monitored_pid= 6738 /*from  www.j  a v a2 s .  com*/
 * 
 * @param input
 * @return
 * @throws Exception
 */
private Integer findPid() throws Exception {

    Integer pid = new Integer(0);

    // we open the file
    RandomAccessFile f = new RandomAccessFile(file, "r");

    try {

        long size = f.length();

        f.seek(size - 2);

        // we search the file reverse for '='
        byte[] b = new byte[1];
        for (long i = size - 2; i >= 0; i--) {
            f.seek(i);
            f.read(b);
            if (b[0] == '=') {
                break;
            }
        }

        String line = f.readLine().trim();

        pid = new Integer(line);

    } finally {
        // this is important, RandomAccessFile doesn't close the file handle by default!
        // if close isn't called, you'll get very soon 'too many open files'
        f.close();
    }

    return pid;
}

From source file:org.everit.osgi.dev.maven.util.FileManager.java

public byte[] tryReadingAmount(final RandomAccessFile is, final int amount) throws IOException {
    ByteArrayOutputStream bout = new ByteArrayOutputStream(amount);
    byte[] buffer = new byte[amount];
    int r = is.read(buffer);
    while ((r > -1) && (bout.size() < amount)) {
        bout.write(buffer, 0, r);/* w  w w  . java 2s. com*/
        r = is.read(buffer, 0, amount - bout.size());
    }
    return bout.toByteArray();
}

From source file:org.kawanfw.file.servlet.util.FileTransferManager.java

/**
 * Copy the content of the random access file to the output stream up to a
 * chubk length// w  w w. j  a  va2 s  . c om
 * 
 * @param raf
 * @param out
 * @param chunkLength
 * @return the total amount read
 * 
 * @throws IOException
 */
private long copy(RandomAccessFile raf, OutputStream out, long chunkLength) throws IOException {
    int writeBufferSize = DefaultParms.DEFAULT_WRITE_BUFFER_SIZE;

    // For Chunk Length comparison. If buffer read > chunk
    // length ==> exit
    long totalRead = 0;

    byte[] tmp = new byte[writeBufferSize];
    int len;

    while ((len = raf.read(tmp)) >= 0) {
        totalRead += len;
        out.write(tmp, 0, len);

        // Exit if chunk length is reached. We will just send
        // the chunck,InputStream will be reused
        if (chunkLength > 0 && totalRead > chunkLength - writeBufferSize) {
            return totalRead;
        }
    }

    return totalRead;
}

From source file:com.polyvi.xface.extension.advancedfiletransfer.FileUploader.java

/**
 * ?buffer?,??//from ww w  . java2s  .com
 *
 * @param buffer
 *            :??
 */
private int readFileData(byte[] buffer) {
    int len = READ_FILE_END;
    RandomAccessFile accessFile = null;
    try {
        accessFile = new RandomAccessFile(mUploadFile, "r");
        accessFile.seek(mStartedPosition);
        len = accessFile.read(buffer);
        if (mStartedPosition != mAlreadyUploadLength) {
            mAlreadyUploadLength = mStartedPosition;
        }
        accessFile.close();
    } catch (FileNotFoundException e) {
        len = READ_FILE_END;
        onError(FILE_NOT_FOUND_ERR);
    } catch (IOException e) {
        len = READ_FILE_END;
        onError(FILE_NOT_FOUND_ERR);
    }
    return len;
}

From source file:com.excuseme.rocketleaguelivestats.scanner.tailer.Tailer.java

/**
 * Read new lines./*from   w  w  w  . java2  s  .c o m*/
 *
 * @param reader The file to read
 * @return The new position after the lines have been read
 * @throws java.io.IOException if an I/O error occurs.
 */
private long readLines(RandomAccessFile reader) throws IOException {
    StringBuilder sb = new StringBuilder();

    long pos = reader.getFilePointer();
    long rePos = pos; // position to re-read

    int num;
    boolean seenCR = false;
    while (run && ((num = reader.read(inbuf)) != -1)) {
        for (int i = 0; i < num; i++) {
            byte ch = inbuf[i];
            switch (ch) {
            case '\n':
                seenCR = false; // swallow CR before LF
                listener.handle(sb.toString());
                sb.setLength(0);
                rePos = pos + i + 1;
                break;
            case '\r':
                if (seenCR) {
                    sb.append('\r');
                }
                seenCR = true;
                break;
            default:
                if (seenCR) {
                    seenCR = false; // swallow final CR
                    listener.handle(sb.toString());
                    sb.setLength(0);
                    rePos = pos + i + 1;
                }
                sb.append((char) ch); // add character, not its ascii value
            }
        }

        pos = reader.getFilePointer();
    }
    if (reader.read(inbuf) == -1) {
        listener.endOfFile();
    }

    reader.seek(rePos); // Ensure we can re-read if necessary
    return rePos;
}

From source file:org.pentaho.platform.web.servlet.UploadFileUtils.java

/**
 * Gets the uncompressed file size of a .gz file by reading the last four bytes of the file
 * //from w  w w.ja  va  2s . co m
 * @param file
 * @return long uncompressed original file size
 * @throws IOException
 *           mbatchelor
 */
private long getUncompressedGZipFileSize(File file) throws IOException {
    long rtn = 0;
    RandomAccessFile gzipFile = new RandomAccessFile(file, "r");
    try {
        // go 4 bytes from end of file - the original uncompressed file size is there
        gzipFile.seek(gzipFile.length() - 4);
        byte[] intelSize = new byte[4];
        gzipFile.read(intelSize); // read the size ....
        // rfc1952; ISIZE is the input size modulo 2^32
        // 00F01E69 is really 691EF000
        // The &0xFF turns signed byte into unsigned.
        rtn = (((intelSize[3] & 0xFF) << 24)
                | ((intelSize[2] & 0xFF) << 16) + ((intelSize[1] & 0xFF) << 8) + (intelSize[0] & 0xFF))
                & 0xffffffffL;
    } finally {
        try {
            gzipFile.close();
        } catch (Exception ignored) {
            //ignored
        }
    }
    return rtn;
}

From source file:org.scriptbox.util.common.io.Tailer.java

/**
 * Read new lines.// w  ww. j a  va2s .co  m
 *
 * @param reader The file to read
 * @return The new position after the lines have been read
 * @throws java.io.IOException if an I/O error occurs.
 */
private long readLines(RandomAccessFile reader) throws IOException {
    StringBuilder sb = new StringBuilder();

    long pos = reader.getFilePointer();
    long rePos = pos; // position to re-read

    int num;
    boolean seenCR = false;
    while (run && ((num = reader.read(inbuf)) != -1)) {
        for (int i = 0; i < num; i++) {
            byte ch = inbuf[i];
            switch (ch) {
            case '\n':
                seenCR = false; // swallow CR before LF
                listener.handle(sb.toString());
                sb.setLength(0);
                rePos = pos + i + 1;
                break;
            case '\r':
                if (seenCR) {
                    sb.append('\r');
                }
                seenCR = true;
                break;
            default:
                if (seenCR) {
                    seenCR = false; // swallow final CR
                    listener.handle(sb.toString());
                    sb.setLength(0);
                    rePos = pos + i + 1;
                }
                sb.append((char) ch); // add character, not its ascii value
            }
        }

        pos = reader.getFilePointer();
    }

    reader.seek(rePos); // Ensure we can re-read if necessary
    return rePos;
}