You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@hc.apache.org by ol...@apache.org on 2005/04/04 20:50:07 UTC

svn commit: r160082 - in jakarta/httpclient/trunk/http-common/src: java/org/apache/http/impl/AutoCloseInputStream.java test/org/apache/http/impl/TestAutoCloseInputStream.java

Author: olegk
Date: Mon Apr  4 11:50:07 2005
New Revision: 160082

URL: http://svn.apache.org/viewcvs?view=rev&rev=160082
Log:
Simplified implementation + 100% test coverage

Added:
    jakarta/httpclient/trunk/http-common/src/test/org/apache/http/impl/TestAutoCloseInputStream.java   (with props)
Modified:
    jakarta/httpclient/trunk/http-common/src/java/org/apache/http/impl/AutoCloseInputStream.java

Modified: jakarta/httpclient/trunk/http-common/src/java/org/apache/http/impl/AutoCloseInputStream.java
URL: http://svn.apache.org/viewcvs/jakarta/httpclient/trunk/http-common/src/java/org/apache/http/impl/AutoCloseInputStream.java?view=diff&r1=160081&r2=160082
==============================================================================
--- jakarta/httpclient/trunk/http-common/src/java/org/apache/http/impl/AutoCloseInputStream.java (original)
+++ jakarta/httpclient/trunk/http-common/src/java/org/apache/http/impl/AutoCloseInputStream.java Mon Apr  4 11:50:07 2005
@@ -46,20 +46,11 @@
 class AutoCloseInputStream extends FilterInputStream {
 
     /** 
-     * True if this stream is open.  Assume that the underlying stream 
-     * is open until we get an EOF indication.
-     */
-    private boolean streamOpen = true;
-
-    /** True if the stream closed itself. */
-    private boolean selfClosed = false;
-
-    /** 
      * The watcher is notified when the contents of the stream have
      * been  exhausted
      */ 
     private ResponseConsumedWatcher watcher = null;
-
+    private boolean watcherNotified = false;
     /**
      * Create a new auto closing stream for the provided connection
      *
@@ -80,14 +71,8 @@
      * @return the character read, or -1 for EOF
      */
     public int read() throws IOException {
-        int l = -1;
-
-        if (isReadAllowed()) {
-            // underlying stream not closed, go ahead and read.
-            l = super.read();
-            checkClose(l);
-        }
-
+        int l = super.read();
+        checkEndOfStream(l);
         return l;
     }
 
@@ -101,13 +86,8 @@
      * @throws IOException if there are errors reading
      */
     public int read(byte[] b, int off, int len) throws IOException {
-        int l = -1;
-
-        if (isReadAllowed()) {
-            l = super.read(b,  off,  len);
-            checkClose(l);
-        }
-
+        int l = super.read(b,  off,  len);
+        checkEndOfStream(l);
         return l;
     }
 
@@ -120,12 +100,8 @@
      * @throws IOException if there are errors reading
      */
     public int read(byte[] b) throws IOException {
-        int l = -1;
-
-        if (isReadAllowed()) {
-            l = super.read(b);
-            checkClose(l);
-        }
+        int l = super.read(b);
+        checkEndOfStream(l);
         return l;
     }
 
@@ -135,10 +111,8 @@
      * @throws IOException If an IO problem occurs.
      */
     public void close() throws IOException {
-        if (!selfClosed) {
-            selfClosed = true;
-            notifyWatcher();
-        }
+        super.close();
+        ensureWatcherNotified();
     }
 
     /**
@@ -147,35 +121,19 @@
      * @param readResult    The result of the read operation to check.
      * @throws IOException If an IO problem occurs.
      */
-    private void checkClose(int readResult) throws IOException {
+    private void checkEndOfStream(int readResult) throws IOException {
         if (readResult == -1) {
-            notifyWatcher();
-        }
-    }
-
-    /**
-     * See whether a read of the underlying stream should be allowed, and if
-     * not, check to see whether our stream has already been closed!
-     *
-     * @return <code>true</code> if it is still OK to read from the stream.
-     * @throws IOException If an IO problem occurs.
-     */
-    private boolean isReadAllowed() throws IOException {
-        if (!streamOpen && selfClosed) {
-            throw new IOException("Attempted read on closed stream.");
+            close();
         }
-        return streamOpen;
     }
 
     /**
      * Notify the watcher that the contents have been consumed.
      * @throws IOException If an IO problem occurs.
      */
-    private void notifyWatcher() throws IOException {
-        if (streamOpen) {
-            super.close();
-            streamOpen = false;
-
+    private void ensureWatcherNotified() throws IOException {
+        if (!this.watcherNotified) {
+            this.watcherNotified = true;
             if (watcher != null) {
                 watcher.responseConsumed();
             }

Added: jakarta/httpclient/trunk/http-common/src/test/org/apache/http/impl/TestAutoCloseInputStream.java
URL: http://svn.apache.org/viewcvs/jakarta/httpclient/trunk/http-common/src/test/org/apache/http/impl/TestAutoCloseInputStream.java?view=auto&rev=160082
==============================================================================
--- jakarta/httpclient/trunk/http-common/src/test/org/apache/http/impl/TestAutoCloseInputStream.java (added)
+++ jakarta/httpclient/trunk/http-common/src/test/org/apache/http/impl/TestAutoCloseInputStream.java Mon Apr  4 11:50:07 2005
@@ -0,0 +1,113 @@
+/*
+ * $HeadURL$
+ * $Revision$
+ * $Date$
+ * ====================================================================
+ *
+ *  Copyright 2002-2004 The Apache Software Foundation
+ *
+ *  Licensed 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.
+ * ====================================================================
+ *
+ * This software consists of voluntary contributions made by many
+ * individuals on behalf of the Apache Software Foundation.  For more
+ * information on the Apache Software Foundation, please see
+ * <http://www.apache.org/>.
+ *
+ */
+
+package org.apache.http.impl;
+
+import java.io.ByteArrayInputStream;
+import java.io.IOException;
+import java.io.InputStream;
+
+import junit.framework.Test;
+import junit.framework.TestCase;
+import junit.framework.TestSuite;
+
+public class TestAutoCloseInputStream extends TestCase {
+
+    public TestAutoCloseInputStream(String testName) {
+        super(testName);
+    }
+
+    // ------------------------------------------------------- TestCase Methods
+
+    public static Test suite() {
+        return new TestSuite(TestAutoCloseInputStream.class);
+    }
+
+    // ------------------------------------------------------------------- Main
+    public static void main(String args[]) {
+        String[] testCaseName = { TestAutoCloseInputStream.class.getName() };
+        junit.textui.TestRunner.main(testCaseName);
+    }
+
+    class TestResponseConsumedWatcher implements ResponseConsumedWatcher {
+        
+        private int count;
+        
+        public TestResponseConsumedWatcher() {
+            super();
+            count = 0;
+        }
+        
+        public void responseConsumed() {
+            count++;
+        }
+        
+        public int getCount() {
+            return this.count;
+        }
+    };
+    
+    
+    public void testConstructors() throws Exception {
+        InputStream instream = new ByteArrayInputStream(new byte[] {});
+        ResponseConsumedWatcher watcher = new TestResponseConsumedWatcher();
+        new AutoCloseInputStream(instream, watcher);
+        new AutoCloseInputStream(instream, null);
+    }
+
+    public void testBasics() throws IOException {
+        byte[] input = "0123456789ABCDEF".getBytes("US-ASCII");
+        InputStream instream = new ByteArrayInputStream(input);
+        TestResponseConsumedWatcher watcher = new TestResponseConsumedWatcher();
+        instream = new AutoCloseInputStream(instream, watcher);
+        byte[] tmp = new byte[input.length];
+        int ch = instream.read();
+        assertTrue(ch != -1);
+        tmp[0] = (byte)ch;
+        assertTrue(instream.read(tmp, 1, tmp.length - 1) != -1);
+        assertTrue(instream.read(tmp) == -1);
+        for (int i = 0; i < input.length; i++) {
+            assertEquals(input[i], tmp[i]);
+        }        
+        assertTrue(instream.read() == -1);
+        instream.close();
+        instream.close();
+        // Has been triggered once only
+        assertEquals(1, watcher.getCount());
+    }
+
+    public void testNullWatcher() throws IOException {
+        byte[] input = "0".getBytes("US-ASCII");
+        InputStream instream = new ByteArrayInputStream(input);
+        instream = new AutoCloseInputStream(instream, null);
+        assertTrue(instream.read() != -1);
+        assertTrue(instream.read() == -1);
+        assertTrue(instream.read() == -1);
+    }
+}
+

Propchange: jakarta/httpclient/trunk/http-common/src/test/org/apache/http/impl/TestAutoCloseInputStream.java
------------------------------------------------------------------------------
    svn:eol-style = native

Propchange: jakarta/httpclient/trunk/http-common/src/test/org/apache/http/impl/TestAutoCloseInputStream.java
------------------------------------------------------------------------------
    svn:keywords = Date Author Id Revision HeadURL

Propchange: jakarta/httpclient/trunk/http-common/src/test/org/apache/http/impl/TestAutoCloseInputStream.java
------------------------------------------------------------------------------
    svn:mime-type = text/plain