All Downloads are FREE. Search and download functionalities are using the official Maven repository.

net.sf.sevenzipjbinding.impl.RandomAccessFileInStream Maven / Gradle / Ivy

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();
	}
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy