You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@tika.apache.org by ju...@apache.org on 2011/03/25 16:02:50 UTC

svn commit: r1085419 - in /tika/trunk/tika-core/src: main/java/org/apache/tika/io/LookaheadInputStream.java test/java/org/apache/tika/io/LookaheadInputStreamTest.java

Author: jukka
Date: Fri Mar 25 15:02:50 2011
New Revision: 1085419

URL: http://svn.apache.org/viewvc?rev=1085419&view=rev
Log:
TIKA-160: Support encryption formats

Minor LookaheadInputStream improvements, plus test cases.

Added:
    tika/trunk/tika-core/src/test/java/org/apache/tika/io/LookaheadInputStreamTest.java   (with props)
Modified:
    tika/trunk/tika-core/src/main/java/org/apache/tika/io/LookaheadInputStream.java

Modified: tika/trunk/tika-core/src/main/java/org/apache/tika/io/LookaheadInputStream.java
URL: http://svn.apache.org/viewvc/tika/trunk/tika-core/src/main/java/org/apache/tika/io/LookaheadInputStream.java?rev=1085419&r1=1085418&r2=1085419&view=diff
==============================================================================
--- tika/trunk/tika-core/src/main/java/org/apache/tika/io/LookaheadInputStream.java (original)
+++ tika/trunk/tika-core/src/main/java/org/apache/tika/io/LookaheadInputStream.java Fri Mar 25 15:02:50 2011
@@ -56,10 +56,22 @@ public class LookaheadInputStream extend
 
     private int mark = 0;
 
+    /**
+     * Creates a lookahead wrapper for the given input stream.
+     * The given input stream should support the mark feature,
+     * as otherwise the state of that stream will be undefined
+     * after the lookahead wrapper has been closed. As a special
+     * case a <code>null</code> stream is treated as an empty stream.
+     *
+     * @param stream input stream, can be <code>null</code>
+     * @param n maximum number of bytes to look ahead
+     */
     public LookaheadInputStream(InputStream stream, int n) {
         this.stream = stream;
-        this.buffer = new byte[0];
-        stream.mark(n);
+        this.buffer = new byte[n];
+        if (stream != null) {
+            stream.mark(n);
+        }
     }
 
     @Override
@@ -105,7 +117,8 @@ public class LookaheadInputStream extend
     }
 
     @Override
-    public long skip(long n) {
+    public long skip(long n) throws IOException {
+        fill();
         n = Math.min(n, available());
         position += n;
         return n;

Added: tika/trunk/tika-core/src/test/java/org/apache/tika/io/LookaheadInputStreamTest.java
URL: http://svn.apache.org/viewvc/tika/trunk/tika-core/src/test/java/org/apache/tika/io/LookaheadInputStreamTest.java?rev=1085419&view=auto
==============================================================================
--- tika/trunk/tika-core/src/test/java/org/apache/tika/io/LookaheadInputStreamTest.java (added)
+++ tika/trunk/tika-core/src/test/java/org/apache/tika/io/LookaheadInputStreamTest.java Fri Mar 25 15:02:50 2011
@@ -0,0 +1,105 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ * 
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ * 
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.tika.io;
+
+import java.io.ByteArrayInputStream;
+import java.io.IOException;
+import java.io.InputStream;
+
+import junit.framework.TestCase;
+
+/**
+ * Test cases for the {@link LookaheadInputStream} class.
+ */
+public class LookaheadInputStreamTest extends TestCase {
+
+    public void testNullStream() throws IOException {
+        InputStream lookahead = new LookaheadInputStream(null, 100);
+        assertEquals(-1, lookahead.read());
+    }
+
+    public void testEmptyStream() throws IOException {
+        InputStream stream = new ByteArrayInputStream(new byte[0]);
+        InputStream lookahead = new LookaheadInputStream(stream, 100);
+        assertEquals(-1, lookahead.read());
+        lookahead.close();
+        assertEquals(-1, stream.read());
+    }
+
+    public void testBasicLookahead() throws IOException {
+        InputStream stream =
+            new ByteArrayInputStream(new byte[] { 'a', 'b', 'c' });
+        InputStream lookahead = new LookaheadInputStream(stream, 2);
+        assertEquals('a', lookahead.read());
+        assertEquals('b', lookahead.read());
+        assertEquals(-1, lookahead.read());
+        lookahead.close();
+        assertEquals('a', stream.read());
+        assertEquals('b', stream.read());
+        assertEquals('c', stream.read());
+        assertEquals(-1, stream.read());
+    }
+
+    public void testZeroLookahead() throws IOException {
+        InputStream stream =
+            new ByteArrayInputStream(new byte[] { 'a', 'b', 'c' });
+        InputStream lookahead = new LookaheadInputStream(stream, 0);
+        assertEquals(-1, lookahead.read());
+        lookahead.close();
+        assertEquals('a', stream.read());
+        assertEquals('b', stream.read());
+        assertEquals('c', stream.read());
+        assertEquals(-1, stream.read());
+    }
+
+    public void testMarkLookahead() throws IOException {
+        InputStream stream =
+            new ByteArrayInputStream(new byte[] { 'a', 'b', 'c' });
+        InputStream lookahead = new LookaheadInputStream(stream, 2);
+        lookahead.mark(1);
+        assertEquals('a', lookahead.read());
+        lookahead.reset();
+        assertEquals('a', lookahead.read());
+        lookahead.mark(2);
+        assertEquals('b', lookahead.read());
+        assertEquals(-1, lookahead.read());
+        lookahead.reset();
+        assertEquals('b', lookahead.read());
+        assertEquals(-1, lookahead.read());
+        lookahead.close();
+        assertEquals('a', stream.read());
+        assertEquals('b', stream.read());
+        assertEquals('c', stream.read());
+        assertEquals(-1, stream.read());
+    }
+
+    public void testSkipLookahead() throws IOException {
+        InputStream stream =
+            new ByteArrayInputStream(new byte[] { 'a', 'b', 'c' });
+        InputStream lookahead = new LookaheadInputStream(stream, 2);
+        assertEquals(1, lookahead.skip(1));
+        assertEquals('b', lookahead.read());
+        assertEquals(0, lookahead.skip(1));
+        assertEquals(-1, lookahead.read());
+        lookahead.close();
+        assertEquals('a', stream.read());
+        assertEquals('b', stream.read());
+        assertEquals('c', stream.read());
+        assertEquals(-1, stream.read());
+    }
+
+}

Propchange: tika/trunk/tika-core/src/test/java/org/apache/tika/io/LookaheadInputStreamTest.java
------------------------------------------------------------------------------
    svn:executable = *