net.sf.sevenzipjbinding.impl.RandomAccessFileInStream Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of jwrapper-7zip-jbinding Show documentation
Show all versions of jwrapper-7zip-jbinding Show documentation
${project.organization.name} JWrapper 7zip Jbinding
The newest version!
package net.sf.sevenzipjbinding.impl;
import java.io.IOException;
import java.io.RandomAccessFile;
import net.sf.sevenzipjbinding.IInStream;
import net.sf.sevenzipjbinding.SevenZipException;
/**
* Implementation of {@link IInStream} using {@link RandomAccessFile}.
*
* @author Boris Brodski
* @version 4.65-1
*/
public class RandomAccessFileInStream implements IInStream {
private final RandomAccessFile randomAccessFile;
/**
* Constructs instance of the class from random access file.
*
* @param randomAccessFile
* random access file to use
*/
public RandomAccessFileInStream(RandomAccessFile randomAccessFile) {
this.randomAccessFile = randomAccessFile;
}
/**
* {@inheritDoc}
*/
public long seek(long offset, int seekOrigin) throws SevenZipException {
try {
switch (seekOrigin) {
case SEEK_SET:
randomAccessFile.seek(offset);
break;
case SEEK_CUR:
randomAccessFile.seek(randomAccessFile.getFilePointer() + offset);
break;
case SEEK_END:
randomAccessFile.seek(randomAccessFile.length() + offset);
break;
default:
throw new RuntimeException("Seek: unknown origin: " + seekOrigin);
}
return randomAccessFile.getFilePointer();
} catch (IOException e) {
throw new SevenZipException("Error while seek operation", e);
}
}
/**
* {@inheritDoc}
*/
public int read(byte[] data) throws SevenZipException {
try {
int read = randomAccessFile.read(data);
if (read == -1) {
return 0;
} else {
return read;
}
} catch (IOException e) {
throw new SevenZipException("Error reading random access file", e);
}
}
/**
* Closes random access file. After this call no more methods should be called.
*
* @throws IOException
*/
public void close() throws IOException {
randomAccessFile.close();
}
}