hc-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From ol...@apache.org
Subject svn commit: r159112 - in jakarta/httpclient/trunk/http-common/src: java/org/apache/http/ java/org/apache/http/impl/ test/org/apache/http/impl/
Date Sat, 26 Mar 2005 16:52:47 GMT
Author: olegk
Date: Sat Mar 26 08:52:45 2005
New Revision: 159112

URL: http://svn.apache.org/viewcvs?view=rev&rev=159112
Log:
Added HttpDataReceiver#read() and HttpDataTransmitter#write(int) methods

Modified:
    jakarta/httpclient/trunk/http-common/src/java/org/apache/http/HttpDataReceiver.java
    jakarta/httpclient/trunk/http-common/src/java/org/apache/http/HttpDataTransmitter.java
    jakarta/httpclient/trunk/http-common/src/java/org/apache/http/impl/NIOHttpDataReceiver.java
    jakarta/httpclient/trunk/http-common/src/java/org/apache/http/impl/NIOHttpDataTransmitter.java
    jakarta/httpclient/trunk/http-common/src/test/org/apache/http/impl/TestNIOHttpTransmitterAndReceiver.java

Modified: jakarta/httpclient/trunk/http-common/src/java/org/apache/http/HttpDataReceiver.java
URL: http://svn.apache.org/viewcvs/jakarta/httpclient/trunk/http-common/src/java/org/apache/http/HttpDataReceiver.java?view=diff&r1=159111&r2=159112
==============================================================================
--- jakarta/httpclient/trunk/http-common/src/java/org/apache/http/HttpDataReceiver.java (original)
+++ jakarta/httpclient/trunk/http-common/src/java/org/apache/http/HttpDataReceiver.java Sat
Mar 26 08:52:45 2005
@@ -50,6 +50,8 @@
     
     int read(byte[] b) throws IOException; 
     
+    int read() throws IOException; 
+    
     String readLine() throws IOException;
     
     boolean isDataAvailable(int timeout) throws IOException; 

Modified: jakarta/httpclient/trunk/http-common/src/java/org/apache/http/HttpDataTransmitter.java
URL: http://svn.apache.org/viewcvs/jakarta/httpclient/trunk/http-common/src/java/org/apache/http/HttpDataTransmitter.java?view=diff&r1=159111&r2=159112
==============================================================================
--- jakarta/httpclient/trunk/http-common/src/java/org/apache/http/HttpDataTransmitter.java
(original)
+++ jakarta/httpclient/trunk/http-common/src/java/org/apache/http/HttpDataTransmitter.java
Sat Mar 26 08:52:45 2005
@@ -50,6 +50,8 @@
     
     void write(byte[] b) throws IOException;
     
+    void write(int b) throws IOException;
+    
     void writeLine(String s) throws IOException;
     
     void flush() throws IOException;

Modified: jakarta/httpclient/trunk/http-common/src/java/org/apache/http/impl/NIOHttpDataReceiver.java
URL: http://svn.apache.org/viewcvs/jakarta/httpclient/trunk/http-common/src/java/org/apache/http/impl/NIOHttpDataReceiver.java?view=diff&r1=159111&r2=159112
==============================================================================
--- jakarta/httpclient/trunk/http-common/src/java/org/apache/http/impl/NIOHttpDataReceiver.java
(original)
+++ jakarta/httpclient/trunk/http-common/src/java/org/apache/http/impl/NIOHttpDataReceiver.java
Sat Mar 26 08:52:45 2005
@@ -70,6 +70,7 @@
             buffersize = 2048;
         }
         this.buffer = ByteBuffer.allocateDirect(buffersize);
+        this.buffer.flip();
         
         this.charset = Charset.forName("US-ASCII");
     }
@@ -97,14 +98,6 @@
             return 0;
         }
         int noRead = 0;
-        // prefill the buffer if necessary
-        if (this.buffer.position() == 0) {
-            noRead = fillBuffer();
-            if (noRead == -1) {
-                // end of stream
-                return -1;
-            }
-        }
         if (!this.buffer.hasRemaining()) {
             this.buffer.clear();
             noRead = fillBuffer();
@@ -127,6 +120,18 @@
         return read(b, 0, b.length);
     }
     
+    public int read() throws IOException {
+        int noRead = 0;
+        if (!this.buffer.hasRemaining()) {
+            this.buffer.clear();
+            noRead = fillBuffer();
+            if (noRead == -1) {
+                return -1; 
+            }
+        }
+        return this.buffer.get();
+    }
+    
     private int locateLF() {
         for (int i = this.buffer.position(); i < this.buffer.limit(); i++) {
             int b = this.buffer.get(i);
@@ -139,14 +144,6 @@
     
     public String readLine() throws IOException {
         int noRead = 0;
-        // prefill the buffer if necessary
-        if (this.buffer.position() == 0) {
-            noRead = fillBuffer();
-            if (noRead == -1) {
-                // end of stream
-                return null;
-            }
-        }
         CharsetDecoder chardecoder = createCharDecoder();
         StringBuffer line = new StringBuffer(); 
         CharBuffer tmp = CharBuffer.allocate(128);
@@ -175,7 +172,6 @@
                     retry = false;
                     // terminate the decoding process
                     chardecoder.decode(this.buffer, tmp, true);
-                    this.buffer.clear();
                 }
             }
             // append the decoded content to the line buffer

Modified: jakarta/httpclient/trunk/http-common/src/java/org/apache/http/impl/NIOHttpDataTransmitter.java
URL: http://svn.apache.org/viewcvs/jakarta/httpclient/trunk/http-common/src/java/org/apache/http/impl/NIOHttpDataTransmitter.java?view=diff&r1=159111&r2=159112
==============================================================================
--- jakarta/httpclient/trunk/http-common/src/java/org/apache/http/impl/NIOHttpDataTransmitter.java
(original)
+++ jakarta/httpclient/trunk/http-common/src/java/org/apache/http/impl/NIOHttpDataTransmitter.java
Sat Mar 26 08:52:45 2005
@@ -150,6 +150,13 @@
         write(CRLF);
     }
 
+    public void write(int b) throws IOException {
+        if (!this.buffer.hasRemaining()) {
+            flushBuffer();
+        }
+        this.buffer.put((byte)b);
+    }
+
     private void flushCharEncoder(final CharsetEncoder charencoder) throws IOException {
         boolean retry = true;
         while (retry) {

Modified: jakarta/httpclient/trunk/http-common/src/test/org/apache/http/impl/TestNIOHttpTransmitterAndReceiver.java
URL: http://svn.apache.org/viewcvs/jakarta/httpclient/trunk/http-common/src/test/org/apache/http/impl/TestNIOHttpTransmitterAndReceiver.java?view=diff&r1=159111&r2=159112
==============================================================================
--- jakarta/httpclient/trunk/http-common/src/test/org/apache/http/impl/TestNIOHttpTransmitterAndReceiver.java
(original)
+++ jakarta/httpclient/trunk/http-common/src/test/org/apache/http/impl/TestNIOHttpTransmitterAndReceiver.java
Sat Mar 26 08:52:45 2005
@@ -244,4 +244,40 @@
         assertEquals(-1, receiver.read(tmp));
         assertEquals(-1, receiver.read(tmp));
     }
+    
+    public void testReadWriteByte() throws Exception {
+        // make the buffer larger than that of transmitter
+        byte[] out = new byte[40];
+        for (int i = 0; i < out.length; i++) {
+            out[i] = (byte)('0' + i);
+        }
+        ByteArrayOutputStream outstream = new ByteArrayOutputStream();
+        HttpDataTransmitter transmitter = 
+            new NIOHttpDataTransmitter(Channels.newChannel(outstream), 16);
+        for (int i = 0; i < out.length; i++) {
+            transmitter.write(out[i]);
+        }
+        transmitter.flush();
+
+        byte[] tmp = outstream.toByteArray();
+        assertEquals(out.length, tmp.length);
+        for (int i = 0; i < out.length; i++) {
+            assertEquals(out[i], tmp[i]);
+        }
+        
+        ByteArrayInputStream instream = new ByteArrayInputStream(tmp);
+        HttpDataReceiver receiver = 
+            new NIOHttpDataReceiver(Channels.newChannel(instream), 16);
+
+        byte[] in = new byte[40];
+        for (int i = 0; i < in.length; i++) {
+            in[i] = (byte)receiver.read();
+        }
+        for (int i = 0; i < out.length; i++) {
+            assertEquals(out[i], in[i]);
+        }
+        assertEquals(-1, receiver.read());
+        assertEquals(-1, receiver.read());
+    }
+    
 }



Mime
View raw message