com.norconex.commons.lang.io.ReverseFileInputStream Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of norconex-commons-lang Show documentation
Show all versions of norconex-commons-lang Show documentation
Norconex Commons Lang is a Java library containing utility classes that complements the Java API and are not found in commonly available libraries (such as the great Apache Commons Lang, which it relies on).
/* Copyright 2010-2014 Norconex Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.norconex.commons.lang.io;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.io.RandomAccessFile;
/**
* {@link InputStream} implementation for streaming files in reverse order
* (from the end of file to its beginning).
* @author Pascal Essiembre
*/
@SuppressWarnings("nls")
public class ReverseFileInputStream extends InputStream {
private static final int BUFFER_SIZE = 4096;
private final byte[] buffer = new byte[BUFFER_SIZE];
private final RandomAccessFile raf;
private long currentPositionInFile;
private int currentPositionInBuffer;
/**
* Creates a new ReverseFileInputStream
instance.
* @param file the file to stream
* @throws IOException problem streaming the file
*/
public ReverseFileInputStream(File file) throws IOException {
assertFile(file);
raf = new RandomAccessFile(file, "r");
currentPositionInFile = raf.length();
currentPositionInBuffer = 0;
}
@Override
public int read() throws IOException {
if (currentPositionInFile <= 0) {
return -1;
}
if (--currentPositionInBuffer < 0) {
currentPositionInBuffer = buffer.length;
long startOfBlock = currentPositionInFile - buffer.length;
if (startOfBlock < 0) {
currentPositionInBuffer = buffer.length + (int) startOfBlock;
startOfBlock = 0;
}
raf.seek(startOfBlock);
raf.readFully(buffer, 0, currentPositionInBuffer);
return read();
}
currentPositionInFile--;
return buffer[currentPositionInBuffer];
}
@Override
public void close() throws IOException {
raf.close();
}
private static void assertFile(File file) throws IOException {
if (file == null || !file.exists()
|| !file.isFile() || !file.canRead()) {
throw new IOException("Not a valid file: " + file);
}
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy