Package net.sf.sevenzipjbinding.impl

Source Code of net.sf.sevenzipjbinding.impl.RandomAccessFileInStream

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

Related Classes of net.sf.sevenzipjbinding.impl.RandomAccessFileInStream

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.