Example usage for java.io FileReader skip

List of usage examples for java.io FileReader skip

Introduction

In this page you can find the example usage for java.io FileReader skip.

Prototype

public long skip(long n) throws IOException 

Source Link

Document

Skips characters.

Usage

From source file:org.apache.hadoop.mapred.TaskLogsMonitor.java

/**
 * Truncate the log file of this task-attempt so that only the last retainSize
 * many bytes of each log file is retained and the log file is reduced in size
 * saving disk space./* w ww . j av  a  2  s.  c om*/
 * 
 * @param taskID Task whose logs need to be truncated
 * @param oldLogFileDetail contains the original log details for the attempt
 * @param taskRetainSize retain-size
 * @param tmpFileWriter New log file to write to. Already opened in append
 *          mode.
 * @param logFileReader Original log file to read from.
 * @return
 * @throws IOException
 */
private LogFileDetail truncateALogFileOfAnAttempt(final TaskAttemptID taskID,
        final LogFileDetail oldLogFileDetail, final long taskRetainSize, final FileWriter tmpFileWriter,
        final FileReader logFileReader) throws IOException {
    LogFileDetail newLogFileDetail = new LogFileDetail();

    // ///////////// Truncate log file ///////////////////////

    // New location of log file is same as the old
    newLogFileDetail.location = oldLogFileDetail.location;
    if (taskRetainSize > MINIMUM_RETAIN_SIZE_FOR_TRUNCATION && oldLogFileDetail.length > taskRetainSize) {
        LOG.info("Truncating logs for " + taskID + " from " + oldLogFileDetail.length + "bytes to "
                + taskRetainSize + "bytes.");
        newLogFileDetail.length = taskRetainSize;
    } else {
        LOG.info("No truncation needed for " + taskID + " length is " + oldLogFileDetail.length
                + " retain size " + taskRetainSize + "bytes.");
        newLogFileDetail.length = oldLogFileDetail.length;
    }
    long charsSkipped = logFileReader.skip(oldLogFileDetail.length - newLogFileDetail.length);
    if (charsSkipped != oldLogFileDetail.length - newLogFileDetail.length) {
        throw new IOException("Erroneously skipped " + charsSkipped + " instead of the expected "
                + (oldLogFileDetail.length - newLogFileDetail.length));
    }
    long alreadyRead = 0;
    while (alreadyRead < newLogFileDetail.length) {
        char tmpBuf[]; // Temporary buffer to read logs
        if (newLogFileDetail.length - alreadyRead >= DEFAULT_BUFFER_SIZE) {
            tmpBuf = new char[DEFAULT_BUFFER_SIZE];
        } else {
            tmpBuf = new char[(int) (newLogFileDetail.length - alreadyRead)];
        }
        int bytesRead = logFileReader.read(tmpBuf);
        if (bytesRead < 0) {
            break;
        } else {
            alreadyRead += bytesRead;
        }
        tmpFileWriter.write(tmpBuf);
    }
    // ////// End of truncating log file ///////////////////////

    return newLogFileDetail;
}