annotate at/ofai/music/util/RandomAccessInputStream.java @ 5:bcb4c9697967 tip

Add README and CITATION files
author Chris Cannam
date Tue, 03 Dec 2013 12:58:05 +0000
parents 4c3f5bc01c97
children
rev   line source
Chris@2 1 /*
Chris@2 2 Copyright (C) 2001, 2006 by Simon Dixon
Chris@2 3
Chris@2 4 This program is free software; you can redistribute it and/or modify
Chris@2 5 it under the terms of the GNU General Public License as published by
Chris@2 6 the Free Software Foundation; either version 2 of the License, or
Chris@2 7 (at your option) any later version.
Chris@2 8
Chris@2 9 This program is distributed in the hope that it will be useful,
Chris@2 10 but WITHOUT ANY WARRANTY; without even the implied warranty of
Chris@2 11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
Chris@2 12 GNU General Public License for more details.
Chris@2 13
Chris@2 14 You should have received a copy of the GNU General Public License along
Chris@2 15 with this program (the file gpl.txt); if not, download it from
Chris@2 16 http://www.gnu.org/licenses/gpl.txt or write to the
Chris@2 17 Free Software Foundation, Inc.,
Chris@2 18 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
Chris@2 19 */
Chris@2 20
Chris@2 21 package at.ofai.music.util;
Chris@2 22 import java.io.*;
Chris@2 23
Chris@2 24 public class RandomAccessInputStream extends InputStream {
Chris@2 25
Chris@2 26 protected RandomAccessFile r;
Chris@2 27 protected long markPosition = 0;
Chris@2 28
Chris@2 29 public RandomAccessInputStream(String name) throws FileNotFoundException {
Chris@2 30 r = new RandomAccessFile(name, "r");
Chris@2 31 } // constructor
Chris@2 32
Chris@2 33 public RandomAccessInputStream(File f) throws FileNotFoundException {
Chris@2 34 r = new RandomAccessFile(f, "r");
Chris@2 35 } // constructor
Chris@2 36
Chris@2 37 /** Returns the number of bytes that can be read (or skipped over) from
Chris@2 38 * this input stream without blocking by the next caller of a method for
Chris@2 39 * this input stream.
Chris@2 40 */
Chris@2 41 public int available() throws IOException {
Chris@2 42 long availableBytes = r.length() - r.getFilePointer();
Chris@2 43 if (availableBytes > Integer.MAX_VALUE)
Chris@2 44 return Integer.MAX_VALUE;
Chris@2 45 else
Chris@2 46 return (int)availableBytes;
Chris@2 47 } // available()
Chris@2 48
Chris@2 49 /** Closes this input stream and releases any system resources associated
Chris@2 50 * with the stream.
Chris@2 51 */
Chris@2 52 public void close() throws IOException {
Chris@2 53 r.close();
Chris@2 54 } // close()
Chris@2 55
Chris@2 56 /** Marks the current position in this input stream.
Chris@2 57 * Warning: Use mark() instead of mark(int).
Chris@2 58 * IOExceptions are caught, because InputStream doesn't allow them to be
Chris@2 59 * thrown. The exception is printed and the mark position invalidated.
Chris@2 60 * @param readlimit Ignored
Chris@2 61 */
Chris@2 62 public void mark(int readlimit) {
Chris@2 63 try {
Chris@2 64 mark();
Chris@2 65 } catch (IOException e) {
Chris@2 66 e.printStackTrace();
Chris@2 67 markPosition = -1;
Chris@2 68 }
Chris@2 69 } // mark()
Chris@2 70
Chris@2 71 /** Marks the current position in this input stream.
Chris@2 72 */
Chris@2 73 public void mark() throws IOException {
Chris@2 74 markPosition = r.getFilePointer();
Chris@2 75 } // mark()
Chris@2 76
Chris@2 77 /** This input stream supports the mark and reset methods.
Chris@2 78 * @return true
Chris@2 79 */
Chris@2 80 public boolean markSupported() {
Chris@2 81 return true;
Chris@2 82 } // markSupported()
Chris@2 83
Chris@2 84 /** Reads the next byte of data from the input stream.
Chris@2 85 */
Chris@2 86 public int read() throws IOException {
Chris@2 87 return r.read();
Chris@2 88 } // read()
Chris@2 89
Chris@2 90 /** Reads some number of bytes from the input stream and stores them into
Chris@2 91 * the buffer array b.
Chris@2 92 */
Chris@2 93 public int read(byte[] b) throws IOException {
Chris@2 94 return r.read(b);
Chris@2 95 } // read()
Chris@2 96
Chris@2 97 /** Reads up to len bytes of data from the input stream into an array of
Chris@2 98 * bytes.
Chris@2 99 */
Chris@2 100 public int read(byte[] b, int off, int len) throws IOException {
Chris@2 101 return r.read(b, off, len);
Chris@2 102 } // read()
Chris@2 103
Chris@2 104 /** Repositions this stream to the position at the time the mark method
Chris@2 105 * was last called on this input stream.
Chris@2 106 */
Chris@2 107 public void reset() throws IOException {
Chris@2 108 if (markPosition < 0)
Chris@2 109 throw new IOException("reset(): invalid mark position");
Chris@2 110 r.seek(markPosition);
Chris@2 111 } // reset()
Chris@2 112
Chris@2 113 /** Skips over and discards n bytes of data from this input stream.
Chris@2 114 */
Chris@2 115 public long skip(long n) throws IOException {
Chris@2 116 long pos = r.getFilePointer();
Chris@2 117 r.seek(n + pos);
Chris@2 118 return r.getFilePointer() - pos;
Chris@2 119 } // skip()
Chris@2 120
Chris@2 121 /** Seek to a position n bytes after the mark.
Chris@2 122 */
Chris@2 123 public long seekFromMark(long n) throws IOException {
Chris@2 124 r.seek(markPosition + n);
Chris@2 125 return r.getFilePointer() - markPosition;
Chris@2 126 } // seekFromMark()
Chris@2 127
Chris@2 128 } // class RandomAccessInputStream