/** * Return a new InputStream representing a subset of the data * from this InputStream, starting at <code>start</code> (inclusive) * up to <code>end</code> (exclusive). <code>start</code> must be * non-negative. If <code>end</code> is -1, the new stream ends * at the same place as this stream. The returned InputStream * will also implement the SharedInputStream interface. * * @param start the starting position * @param end the ending position + 1 * @return the new stream */ public synchronized InputStream newStream(long start, long end) { if (in == null) throw new RuntimeException("Stream closed"); if (start < 0) throw new IllegalArgumentException("start < 0"); if (end == -1) end = datalen; return new SharedFileInputStream(sf, this.start + start, end - start, bufsize); }
public WritableSharedFile(File file) throws IOException { super(file); try { raf = new RandomAccessFile(file, "rw"); } catch (IOException ex) { // if anything goes wrong opening the writable file, // close the readable file too super.close(); } }
/** * Creates a <code>SharedFileInputStream</code> * with the specified buffer size. * * @param file the file * @param size the buffer size. * @exception IOException for errors opening the file * @exception IllegalArgumentException if size ≤ 0. */ public SharedFileInputStream(String file, int size) throws IOException { super(null); // XXX - will it NPE? if (size <= 0) throw new IllegalArgumentException("Buffer size <= 0"); init(new SharedFile(file), size); }
/** * Returns the number of bytes that can be read from this input * stream without blocking. * * @return the number of bytes that can be read from this input * stream without blocking. * @exception IOException if an I/O error occurs. */ public synchronized int available() throws IOException { ensureOpen(); return (count - pos) + in_available(); }
/** * See the general contract of the <code>read</code> * method of <code>InputStream</code>. * * @return the next byte of data, or <code>-1</code> if the end of the * stream is reached. * @exception IOException if an I/O error occurs. */ @Override public synchronized int read() throws IOException { ensureOpen(); if (pos >= count) { fill(); if (pos >= count) return -1; } return buf[pos++] & 0xff; }
throws IOException ensureOpen(); if ((off | len | (off + len) | (b.length - (off + len))) < 0) { throw new IndexOutOfBoundsException(); int n = read1(b, off, len); if (n <= 0) return n; while ((n < len) /* && (in.available() > 0) */) { int n1 = read1(b, off + n, len - n); if (n1 <= 0) break; n += n1;
/** * Read characters into a portion of an array, reading from the underlying * stream at most once if necessary. */ private int read1(byte[] b, int off, int len) throws IOException { int avail = count - pos; if (avail <= 0) { if (false) { /* If the requested length is at least as large as the buffer, and if there is no mark/reset activity, do not bother to copy the bytes into the local buffer. In this way buffered streams will cascade harmlessly. */ if (len >= buf.length && markpos < 0) { // XXX - seek, update bufpos - how? return in.read(b, off, len); } } fill(); avail = count - pos; if (avail <= 0) return -1; } int cnt = (avail < len) ? avail : len; System.arraycopy(buf, pos, b, off, cnt); pos += cnt; return cnt; }
/** * Create a new stream from this stream, using the given * start offset and length. * * @param offset The offset relative to the start of this stream instance. * @param end The end offset of the substream. If -1, the end of the parent stream is used. * * @return A new SharedFileInputStream object sharing the same source * input file. */ public InputStream newStream(long offset, long end) { checkOpenRuntime(); if (offset < 0) { throw new IllegalArgumentException("Start position is less than 0"); } // the default end position is the datalen of the one we're spawning from. if (end == -1) { end = datalen; } // create a new one using the private constructor return new SharedFileInputStream(source, start + (int)offset, (int)(end - offset), bufsize); }
/** * See the general contract of the <code>reset</code> * method of <code>InputStream</code>. * <p> * If <code>markpos</code> is <code>-1</code> * (no mark has been set or the mark has been * invalidated), an <code>IOException</code> * is thrown. Otherwise, <code>pos</code> is * set equal to <code>markpos</code>. * * @exception IOException if this stream has not been marked or * if the mark has been invalidated. * @see #mark(int) */ public synchronized void reset() throws IOException { ensureOpen(); if (markpos < 0) throw new IOException("Resetting to invalid mark"); pos = markpos; }
/** * See the general contract of the <code>read</code> * method of <code>InputStream</code>. * * @return the next byte of data, or <code>-1</code> if the end of the * stream is reached. * @exception IOException if an I/O error occurs. */ public synchronized int read() throws IOException { ensureOpen(); if (pos >= count) { fill(); if (pos >= count) return -1; } return buf[pos++] & 0xff; }
/** * Returns the number of bytes that can be read from this input * stream without blocking. * * @return the number of bytes that can be read from this input * stream without blocking. * @exception IOException if an I/O error occurs. */ @Override public synchronized int available() throws IOException { ensureOpen(); return (count - pos) + in_available(); }
throws IOException ensureOpen(); if ((off | len | (off + len) | (b.length - (off + len))) < 0) { throw new IndexOutOfBoundsException(); int n = read1(b, off, len); if (n <= 0) return n; while ((n < len) /* && (in.available() > 0) */) { int n1 = read1(b, off + n, len - n); if (n1 <= 0) break; n += n1;
/** * Read characters into a portion of an array, reading from the underlying * stream at most once if necessary. */ private int read1(byte[] b, int off, int len) throws IOException { int avail = count - pos; if (avail <= 0) { if (false) { /* If the requested length is at least as large as the buffer, and if there is no mark/reset activity, do not bother to copy the bytes into the local buffer. In this way buffered streams will cascade harmlessly. */ if (len >= buf.length && markpos < 0) { // XXX - seek, update bufpos - how? return in.read(b, off, len); } } fill(); avail = count - pos; if (avail <= 0) return -1; } int cnt = (avail < len) ? avail : len; System.arraycopy(buf, pos, b, off, cnt); pos += cnt; return cnt; }
/** * See the general contract of the <code>reset</code> * method of <code>InputStream</code>. * <p> * If <code>markpos</code> is <code>-1</code> * (no mark has been set or the mark has been * invalidated), an <code>IOException</code> * is thrown. Otherwise, <code>pos</code> is * set equal to <code>markpos</code>. * * @exception IOException if this stream has not been marked or * if the mark has been invalidated. * @see #mark(int) */ @Override public synchronized void reset() throws IOException { ensureOpen(); if (markpos < 0) throw new IOException("Resetting to invalid mark"); pos = markpos; }
ensureOpen(); if (n <= 0) { return 0; fill(); avail = count - pos; if (avail <= 0)
/** * Returns the number of bytes that can be read from this input * stream without blocking. * * @return the number of bytes that can be read from this input * stream without blocking. * @exception IOException if an I/O error occurs. */ public synchronized int available() throws IOException { ensureOpen(); return (count - pos) + in_available(); }
throws IOException ensureOpen(); if ((off | len | (off + len) | (b.length - (off + len))) < 0) { throw new IndexOutOfBoundsException(); int n = read1(b, off, len); if (n <= 0) return n; while ((n < len) /* && (in.available() > 0) */) { int n1 = read1(b, off + n, len - n); if (n1 <= 0) break; n += n1;
/** * Close the readable and writable files. */ public void close() throws IOException { try { super.close(); } finally { raf.close(); } }
/** * Return a new InputStream representing a subset of the data * from this InputStream, starting at <code>start</code> (inclusive) * up to <code>end</code> (exclusive). <code>start</code> must be * non-negative. If <code>end</code> is -1, the new stream ends * at the same place as this stream. The returned InputStream * will also implement the SharedInputStream interface. * * @param start the starting position * @param end the ending position + 1 * @return the new stream */ @Override public synchronized InputStream newStream(long start, long end) { if (in == null) throw new RuntimeException("Stream closed"); if (start < 0) throw new IllegalArgumentException("start < 0"); if (end == -1) end = datalen; return new SharedFileInputStream(sf, this.start + start, end - start, bufsize); }
/** * Creates a <code>SharedFileInputStream</code> * with the specified buffer size. * * @param file the file * @param size the buffer size. * @exception IOException for errors opening the file * @exception IllegalArgumentException if size ≤ 0. */ public SharedFileInputStream(File file, int size) throws IOException { super(null); // XXX - will it NPE? if (size <= 0) throw new IllegalArgumentException("Buffer size <= 0"); init(new SharedFile(file), size); }