1/*
2 * SeekableFileInputStream
3 *
4 * Author: Lasse Collin <lasse.collin@tukaani.org>
5 *
6 * This file has been put into the public domain.
7 * You can do whatever you want with this file.
8 */
9
10package org.tukaani.xz;
11
12import java.io.File;
13import java.io.RandomAccessFile;
14import java.io.IOException;
15import java.io.FileNotFoundException;
16
17/**
18 * Wraps a {@link java.io.RandomAccessFile RandomAccessFile}
19 * in a SeekableInputStream.
20 */
21public class SeekableFileInputStream extends SeekableInputStream {
22    /**
23     * The RandomAccessFile that has been wrapped
24     * into a SeekableFileInputStream.
25     */
26    protected RandomAccessFile randomAccessFile;
27
28    /**
29     * Creates a new seekable input stream that reads from the specified file.
30     */
31    public SeekableFileInputStream(File file) throws FileNotFoundException {
32        randomAccessFile = new RandomAccessFile(file, "r");
33    }
34
35    /**
36     * Creates a new seekable input stream that reads from a file with
37     * the specified name.
38     */
39    public SeekableFileInputStream(String name) throws FileNotFoundException {
40        randomAccessFile = new RandomAccessFile(name, "r");
41    }
42
43    /**
44     * Creates a new seekable input stream from an existing
45     * <code>RandomAccessFile</code> object.
46     */
47    public SeekableFileInputStream(RandomAccessFile randomAccessFile) {
48        this.randomAccessFile = randomAccessFile;
49    }
50
51    /**
52     * Calls {@link RandomAccessFile#read() randomAccessFile.read()}.
53     */
54    public int read() throws IOException {
55        return randomAccessFile.read();
56    }
57
58    /**
59     * Calls {@link RandomAccessFile#read(byte[]) randomAccessFile.read(buf)}.
60     */
61    public int read(byte[] buf) throws IOException {
62        return randomAccessFile.read(buf);
63    }
64
65    /**
66     * Calls
67     * {@link RandomAccessFile#read(byte[],int,int)
68     *        randomAccessFile.read(buf, off, len)}.
69     */
70    public int read(byte[] buf, int off, int len) throws IOException {
71        return randomAccessFile.read(buf, off, len);
72    }
73
74    /**
75     * Calls {@link RandomAccessFile#close() randomAccessFile.close()}.
76     */
77    public void close() throws IOException {
78        randomAccessFile.close();
79    }
80
81    /**
82     * Calls {@link RandomAccessFile#length() randomAccessFile.length()}.
83     */
84    public long length() throws IOException {
85        return randomAccessFile.length();
86    }
87
88    /**
89     * Calls {@link RandomAccessFile#getFilePointer()
90                    randomAccessFile.getFilePointer()}.
91     */
92    public long position() throws IOException {
93        return randomAccessFile.getFilePointer();
94    }
95
96    /**
97     * Calls {@link RandomAccessFile#seek(long) randomAccessFile.seek(long)}.
98     */
99    public void seek(long pos) throws IOException {
100        randomAccessFile.seek(pos);
101    }
102}
103