Return-Path: Delivered-To: apmail-incubator-jackrabbit-commits-archive@www.apache.org Received: (qmail 19072 invoked from network); 5 Nov 2004 14:47:00 -0000 Received: from hermes.apache.org (HELO mail.apache.org) (209.237.227.199) by minotaur-2.apache.org with SMTP; 5 Nov 2004 14:47:00 -0000 Received: (qmail 26325 invoked by uid 500); 5 Nov 2004 14:46:59 -0000 Mailing-List: contact jackrabbit-commits-help@incubator.apache.org; run by ezmlm Precedence: bulk List-Help: List-Unsubscribe: List-Post: List-Id: Reply-To: jackrabbit-dev@incubator.apache.org Delivered-To: mailing list jackrabbit-commits@incubator.apache.org Received: (qmail 26312 invoked by uid 500); 5 Nov 2004 14:46:59 -0000 Delivered-To: apmail-incubator-jackrabbit-cvs@incubator.apache.org Received: (qmail 26309 invoked by uid 99); 5 Nov 2004 14:46:59 -0000 X-ASF-Spam-Status: No, hits=-10.0 required=10.0 tests=ALL_TRUSTED,NO_REAL_NAME X-Spam-Check-By: apache.org Received: from [209.237.227.194] (HELO minotaur.apache.org) (209.237.227.194) by apache.org (qpsmtpd/0.28) with SMTP; Fri, 05 Nov 2004 06:46:58 -0800 Received: (qmail 18986 invoked by uid 65534); 5 Nov 2004 14:46:57 -0000 Date: 5 Nov 2004 14:46:57 -0000 Message-ID: <20041105144657.18983.qmail@minotaur.apache.org> From: mreutegg@apache.org To: jackrabbit-cvs@incubator.apache.org Subject: svn commit: rev 56670 - incubator/jackrabbit/trunk/src/java/org/apache/jackrabbit/core/search/lucene X-Virus-Checked: Checked X-Spam-Rating: minotaur-2.apache.org 1.6.2 0/1000/N Author: mreutegg Date: Fri Nov 5 06:46:56 2004 New Revision: 56670 Modified: incubator/jackrabbit/trunk/src/java/org/apache/jackrabbit/core/search/lucene/FileSystemInputStream.java Log: - FileSystemInputStream implementation is not stable when used on top of a BufferedInputStream Modified: incubator/jackrabbit/trunk/src/java/org/apache/jackrabbit/core/search/lucene/FileSystemInputStream.java ============================================================================== --- incubator/jackrabbit/trunk/src/java/org/apache/jackrabbit/core/search/lucene/FileSystemInputStream.java (original) +++ incubator/jackrabbit/trunk/src/java/org/apache/jackrabbit/core/search/lucene/FileSystemInputStream.java Fri Nov 5 06:46:56 2004 @@ -27,12 +27,22 @@ */ class FileSystemInputStream extends InputStream { + /** The underlying resource. */ private final FileSystemResource res; + /** The underlying input stream of the FileSystemResource. */ private java.io.InputStream in; + /** Current position in the stream. */ private long position; + /** + * Creates a new FileSystemInputStream based on the + * {@link org.apache.jackrabbit.core.fs.FileSystemResource} res. + * @param res the resource this stream is based on. + * @throws IOException if an error occurs creating the stream on the + * resource. + */ FileSystemInputStream(FileSystemResource res) throws IOException { this.res = res; try { @@ -42,22 +52,55 @@ } } + /** + * Reads length bytes into the array b starting + * at offset. + * @param b the byte array to write the date into. + * @param offset the offset where to start writing the data. + * @param length number of bytes to read in / write to b + * @throws IOException if an error occurs reading from the stream or + * if the stream is unable to read length bytes. + */ protected void readInternal(byte[] b, int offset, int length) throws IOException { checkOpen(); - position += in.read(b, offset, length); + int total = 0; + int read; + while ((read = in.read(b, offset, length)) > 0) { + total += read; + offset += read; + length -= read; + position += read; + } + if (length > 0) { + throw new IOException("readInternal: Unable to read " + (total + length) + + " bytes. Only read " + total); + } } + /** + * Closes this FileSystemInputStream and also the underlying + * InputStream. + * @throws IOException if an error occurs while closing the underlying + * InputStream. + */ public void close() throws IOException { if (in != null) { in.close(); } } + /** + * Sets the current position to pos. The next read operation + * will occur at the position pos. + * @param pos the position where to seek to. + * @throws IOException if an error occurs while seeking, or if pos > + * {@link #getFilePointer()}. + */ protected void seekInternal(long pos) throws IOException { checkOpen(); + long skip; if (pos >= position) { - in.skip(pos - position); - position = pos; + skip = pos - position; } else { // seeking backwards in.close(); @@ -66,26 +109,50 @@ } catch (FileSystemException e) { throw new IOException(e.getMessage()); } - in.skip(pos); - position = pos; + skip = pos; + } + while (skip > 0) { + long skipped = in.skip(skip); + if (skipped == 0) { + throw new IOException("seekInternal: Unable to skip " + skip + " bytes."); + } + skip -= skipped; } + position = pos; } + /** + * Clones this FileSystemInputStream. + * @return a clone of this FileSystemInputStream. + */ public Object clone() { FileSystemInputStream clone = (FileSystemInputStream) super.clone(); // decouple from this clone.in = null; - clone.position = 0; return clone; } //----------------------------< internal >---------------------------------- + /** + * Opens a new InputStream on the underlying resource if + * necessary. + * @throws IOException if an error occurs creating a new + * InputStream. + */ private void checkOpen() throws IOException { if (in == null) { try { in = res.getInputStream(); length = res.length(); + long skip = position; + while (skip > 0) { + long skipped = in.skip(skip); + if (skipped == 0) { + throw new IOException("checkOpen: Unable to skip " + position + " bytes."); + } + skip -= skipped; + } } catch (FileSystemException e) { throw new IOException(e.getMessage()); }