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 2012/09/16 20:44:51 UTC

svn commit: r1385341 [2/2] - in /httpcomponents/httpcore/trunk: httpcore-nio/src/main/java/org/apache/http/impl/nio/ httpcore-nio/src/main/java/org/apache/http/impl/nio/reactor/ httpcore-nio/src/test/java/org/apache/http/impl/nio/codecs/ httpcore-nio/s...

Modified: httpcomponents/httpcore/trunk/httpcore/src/main/java/org/apache/http/impl/io/SessionOutputBufferImpl.java
URL: http://svn.apache.org/viewvc/httpcomponents/httpcore/trunk/httpcore/src/main/java/org/apache/http/impl/io/SessionOutputBufferImpl.java?rev=1385341&r1=1385340&r2=1385341&view=diff
==============================================================================
--- httpcomponents/httpcore/trunk/httpcore/src/main/java/org/apache/http/impl/io/SessionOutputBufferImpl.java (original)
+++ httpcomponents/httpcore/trunk/httpcore/src/main/java/org/apache/http/impl/io/SessionOutputBufferImpl.java Sun Sep 16 18:44:51 2012
@@ -31,12 +31,9 @@ import java.io.IOException;
 import java.io.OutputStream;
 import java.nio.ByteBuffer;
 import java.nio.CharBuffer;
-import java.nio.charset.Charset;
 import java.nio.charset.CharsetEncoder;
 import java.nio.charset.CoderResult;
-import java.nio.charset.CodingErrorAction;
 
-import org.apache.http.Consts;
 import org.apache.http.annotation.NotThreadSafe;
 import org.apache.http.io.BufferInfo;
 import org.apache.http.io.HttpTransportMetrics;
@@ -64,14 +61,10 @@ public class SessionOutputBufferImpl imp
 
     private final HttpTransportMetricsImpl metrics;
     private final ByteArrayBuffer buffer;
-    private final Charset charset;
-    private final boolean ascii;
     private final int minChunkLimit;
-    private final CodingErrorAction onMalformedCharAction;
-    private final CodingErrorAction onUnmappableCharAction;
+    private final CharsetEncoder encoder;
 
     private OutputStream outstream;
-    private CharsetEncoder encoder;
     private ByteBuffer bbuf;
 
     /**
@@ -79,38 +72,26 @@ public class SessionOutputBufferImpl imp
      *
      * @param metrics HTTP transport metrics.
      * @param buffersize buffer size. Must be a positive number.
-     * @param charset charset to be used for encoding HTTP protocol elements.
-     *   If <code>null</code> US-ASCII will be used.
      * @param minChunkLimit size limit below which data chunks should be buffered in memory
      *   in order to minimize native method invocations on the underlying network socket.
      *   The optimal value of this parameter can be platform specific and defines a trade-off
      *   between performance of memory copy operations and that of native method invocation.
      *   If negative default chunk limited will be used.
-     * @param malformedCharAction action to perform upon receiving a malformed input.
-     *   If <code>null</code> {@link CodingErrorAction#REPORT} will be used.
-     * @param unmappableCharAction action to perform upon receiving an unmappable input.
-     *   If <code>null</code> {@link CodingErrorAction#REPORT}  will be used.
+     * @param charencoder charencoder to be used for encoding HTTP protocol elements.
+     *   If <code>null</code> simple type cast will be used for char to byte conversion.
      */
     public SessionOutputBufferImpl(
             final HttpTransportMetricsImpl metrics,
             int buffersize,
             int minChunkLimit,
-            final Charset charset,
-            final CodingErrorAction malformedCharAction,
-            final CodingErrorAction unmappableCharAction) {
+            final CharsetEncoder charencoder) {
         super();
         Args.positive(buffersize, "Buffer size");
         Args.notNull(metrics, "HTTP transport metrcis");
         this.metrics = metrics;
         this.buffer = new ByteArrayBuffer(buffersize);
-        this.charset = charset != null ? charset : Consts.ASCII;
-        this.ascii = this.charset.equals(Consts.ASCII);
-        this.encoder = null;
         this.minChunkLimit = minChunkLimit >= 0 ? minChunkLimit : 512;
-        this.onMalformedCharAction = malformedCharAction != null ? malformedCharAction :
-            CodingErrorAction.REPORT;
-        this.onUnmappableCharAction = unmappableCharAction != null? unmappableCharAction :
-            CodingErrorAction.REPORT;
+        this.encoder = charencoder;
     }
 
     public void bind(final OutputStream outstream) {
@@ -206,7 +187,7 @@ public class SessionOutputBufferImpl imp
             return;
         }
         if (s.length() > 0) {
-            if (this.ascii) {
+            if (this.encoder == null) {
                 for (int i = 0; i < s.length(); i++) {
                     write(s.charAt(i));
                 }
@@ -231,7 +212,7 @@ public class SessionOutputBufferImpl imp
         if (charbuffer == null) {
             return;
         }
-        if (this.ascii) {
+        if (this.encoder == null) {
             int off = 0;
             int remaining = charbuffer.length();
             while (remaining > 0) {
@@ -257,11 +238,6 @@ public class SessionOutputBufferImpl imp
         if (!cbuf.hasRemaining()) {
             return;
         }
-        if (this.encoder == null) {
-            this.encoder = this.charset.newEncoder();
-            this.encoder.onMalformedInput(this.onMalformedCharAction);
-            this.encoder.onUnmappableCharacter(this.onUnmappableCharAction);
-        }
         if (this.bbuf == null) {
             this.bbuf = ByteBuffer.allocate(1024);
         }

Modified: httpcomponents/httpcore/trunk/httpcore/src/test/java/org/apache/http/impl/SessionInputBufferMock.java
URL: http://svn.apache.org/viewvc/httpcomponents/httpcore/trunk/httpcore/src/test/java/org/apache/http/impl/SessionInputBufferMock.java?rev=1385341&r1=1385340&r2=1385341&view=diff
==============================================================================
--- httpcomponents/httpcore/trunk/httpcore/src/test/java/org/apache/http/impl/SessionInputBufferMock.java (original)
+++ httpcomponents/httpcore/trunk/httpcore/src/test/java/org/apache/http/impl/SessionInputBufferMock.java Sun Sep 16 18:44:51 2012
@@ -32,9 +32,8 @@ import java.io.IOException;
 import java.io.InputStream;
 import java.io.UnsupportedEncodingException;
 import java.nio.charset.Charset;
-import java.nio.charset.CodingErrorAction;
+import java.nio.charset.CharsetDecoder;
 
-import org.apache.http.Consts;
 import org.apache.http.impl.io.HttpTransportMetricsImpl;
 import org.apache.http.impl.io.SessionInputBufferImpl;
 
@@ -50,37 +49,31 @@ public class SessionInputBufferMock exte
             int buffersize, 
             int maxLineLen,
             int minChunkLimit,
-            final Charset charset,
-            final CodingErrorAction malformedInputAction,
-            final CodingErrorAction unmappableInputAction) {
-        super(new HttpTransportMetricsImpl(), buffersize, maxLineLen, minChunkLimit, 
-                charset, malformedInputAction, unmappableInputAction);
+            final CharsetDecoder decoder) {
+        super(new HttpTransportMetricsImpl(), buffersize, maxLineLen, minChunkLimit, decoder);
         bind(instream);
     }
 
     public SessionInputBufferMock(
             final InputStream instream,
             int buffersize) {
-        this(instream, buffersize, -1, -1, null, null, null);
+        this(instream, buffersize, -1, -1, null);
     }
 
     public SessionInputBufferMock(
             final byte[] bytes,
             int buffersize,
-            final Charset charset,
             int maxLineLen,
             int minChunkLimit,
-            final CodingErrorAction malformedInputAction,
-            final CodingErrorAction unmappableInputAction) {
-        this(new ByteArrayInputStream(bytes), buffersize, maxLineLen, minChunkLimit, 
-                charset, malformedInputAction, unmappableInputAction);
+            final CharsetDecoder decoder) {
+        this(new ByteArrayInputStream(bytes), buffersize, maxLineLen, minChunkLimit, decoder);
     }
 
     public SessionInputBufferMock(
             final byte[] bytes,
             int buffersize,
             int maxLineLen) {
-        this(new ByteArrayInputStream(bytes), buffersize, maxLineLen, -1, Consts.ASCII, null, null);
+        this(new ByteArrayInputStream(bytes), buffersize, maxLineLen, -1, null);
     }
 
     public SessionInputBufferMock(
@@ -96,23 +89,19 @@ public class SessionInputBufferMock exte
 
     public SessionInputBufferMock(
             final byte[] bytes, final Charset charset) {
-        this(bytes, BUFFER_SIZE, charset, -1, -1, null, null);
+        this(bytes, BUFFER_SIZE, -1, -1, charset != null ? charset.newDecoder() : null);
     }
 
     public SessionInputBufferMock(
             final byte[] bytes, 
-            final Charset charset,
-            final CodingErrorAction malformedInputAction,
-            final CodingErrorAction unmappableInputAction) {
-        this(bytes, BUFFER_SIZE, charset, -1, -1, malformedInputAction, unmappableInputAction);
+            final CharsetDecoder decoder) {
+        this(bytes, BUFFER_SIZE, -1, -1, decoder);
     }
 
     public SessionInputBufferMock(
             final String s,
-            final Charset charset)
-        throws UnsupportedEncodingException {
-        this(s.getBytes(charset.name()), BUFFER_SIZE, charset, -1, -1, null, null);
-
+            final Charset charset) throws UnsupportedEncodingException {
+        this(s.getBytes(charset.name()), charset);
     }
 
     public boolean isDataAvailable(int timeout) throws IOException {

Modified: httpcomponents/httpcore/trunk/httpcore/src/test/java/org/apache/http/impl/SessionOutputBufferMock.java
URL: http://svn.apache.org/viewvc/httpcomponents/httpcore/trunk/httpcore/src/test/java/org/apache/http/impl/SessionOutputBufferMock.java?rev=1385341&r1=1385340&r2=1385341&view=diff
==============================================================================
--- httpcomponents/httpcore/trunk/httpcore/src/test/java/org/apache/http/impl/SessionOutputBufferMock.java (original)
+++ httpcomponents/httpcore/trunk/httpcore/src/test/java/org/apache/http/impl/SessionOutputBufferMock.java Sun Sep 16 18:44:51 2012
@@ -29,7 +29,7 @@ package org.apache.http.impl;
 
 import java.io.ByteArrayOutputStream;
 import java.nio.charset.Charset;
-import java.nio.charset.CodingErrorAction;
+import java.nio.charset.CharsetEncoder;
 
 import org.apache.http.impl.io.HttpTransportMetricsImpl;
 import org.apache.http.impl.io.SessionOutputBufferImpl;
@@ -48,11 +48,8 @@ public class SessionOutputBufferMock ext
             final ByteArrayOutputStream buffer, 
             int buffersize, 
             int minChunkLimit,
-            final Charset charset,
-            final CodingErrorAction malformedInputAction,
-            final CodingErrorAction unmappableInputAction) {
-        super(new HttpTransportMetricsImpl(), buffersize, minChunkLimit, 
-                charset, malformedInputAction, unmappableInputAction);
+            final CharsetEncoder encoder) {
+        super(new HttpTransportMetricsImpl(), buffersize, minChunkLimit, encoder);
         bind(buffer);
         this.buffer = buffer;
     }
@@ -60,24 +57,21 @@ public class SessionOutputBufferMock ext
     public SessionOutputBufferMock(
             final ByteArrayOutputStream buffer,
             int buffersize) {
-        this(buffer, buffersize, -1, null, null, null);
+        this(buffer, buffersize, -1, null);
     }
 
     public SessionOutputBufferMock(
-            final Charset charset,
-            final CodingErrorAction malformedInputAction,
-            final CodingErrorAction unmappableInputAction) {
-        this(new ByteArrayOutputStream(), BUFFER_SIZE, -1, 
-                charset, malformedInputAction, unmappableInputAction);
+            final CharsetEncoder encoder) {
+        this(new ByteArrayOutputStream(), BUFFER_SIZE, -1, encoder);
     }
     
     public SessionOutputBufferMock(
             final Charset charset) {
-        this(new ByteArrayOutputStream(), BUFFER_SIZE, -1, charset, null, null);
+        this(new ByteArrayOutputStream(), BUFFER_SIZE, -1, charset != null ? charset.newEncoder() : null);
     }
 
     public SessionOutputBufferMock(final ByteArrayOutputStream buffer) {
-        this(buffer, BUFFER_SIZE, -1, null, null, null);
+        this(buffer, BUFFER_SIZE, -1, null);
     }
 
     public SessionOutputBufferMock() {

Modified: httpcomponents/httpcore/trunk/httpcore/src/test/java/org/apache/http/impl/io/TestSessionInOutBuffers.java
URL: http://svn.apache.org/viewvc/httpcomponents/httpcore/trunk/httpcore/src/test/java/org/apache/http/impl/io/TestSessionInOutBuffers.java?rev=1385341&r1=1385340&r2=1385341&view=diff
==============================================================================
--- httpcomponents/httpcore/trunk/httpcore/src/test/java/org/apache/http/impl/io/TestSessionInOutBuffers.java (original)
+++ httpcomponents/httpcore/trunk/httpcore/src/test/java/org/apache/http/impl/io/TestSessionInOutBuffers.java Sun Sep 16 18:44:51 2012
@@ -29,6 +29,8 @@ package org.apache.http.impl.io;
 
 import java.io.IOException;
 import java.nio.charset.CharacterCodingException;
+import java.nio.charset.CharsetDecoder;
+import java.nio.charset.CharsetEncoder;
 import java.nio.charset.CodingErrorAction;
 
 import org.apache.http.Consts;
@@ -485,16 +487,20 @@ public class TestSessionInOutBuffers {
     @Test(expected=CharacterCodingException.class)
     public void testUnmappableInputActionReport() throws Exception {
         String s = "This text contains a circumflex \u0302 !!!";
-        SessionOutputBufferMock outbuf = new SessionOutputBufferMock(Consts.ISO_8859_1,
-                CodingErrorAction.IGNORE, CodingErrorAction.REPORT);
+        CharsetEncoder encoder = Consts.ISO_8859_1.newEncoder();
+        encoder.onMalformedInput(CodingErrorAction.IGNORE);
+        encoder.onUnmappableCharacter(CodingErrorAction.REPORT);
+        SessionOutputBufferMock outbuf = new SessionOutputBufferMock(encoder);
         outbuf.writeLine(s);
     }
 
     @Test
     public void testUnmappableInputActionReplace() throws Exception {
         String s = "This text contains a circumflex \u0302 !!!";
-        SessionOutputBufferMock outbuf = new SessionOutputBufferMock(Consts.ISO_8859_1,
-                CodingErrorAction.IGNORE, CodingErrorAction.REPLACE);
+        CharsetEncoder encoder = Consts.ISO_8859_1.newEncoder();
+        encoder.onMalformedInput(CodingErrorAction.IGNORE);
+        encoder.onUnmappableCharacter(CodingErrorAction.REPLACE);
+        SessionOutputBufferMock outbuf = new SessionOutputBufferMock(encoder);
         outbuf.writeLine(s);
         outbuf.flush();
         String result = new String(outbuf.getData(), "ISO-8859-1");
@@ -504,8 +510,10 @@ public class TestSessionInOutBuffers {
     @Test
     public void testUnmappableInputActionIgnore() throws Exception {
         String s = "This text contains a circumflex \u0302 !!!";
-        SessionOutputBufferMock outbuf = new SessionOutputBufferMock(Consts.ISO_8859_1,
-                CodingErrorAction.IGNORE, CodingErrorAction.IGNORE);
+        CharsetEncoder encoder = Consts.ISO_8859_1.newEncoder();
+        encoder.onMalformedInput(CodingErrorAction.IGNORE);
+        encoder.onUnmappableCharacter(CodingErrorAction.IGNORE);
+        SessionOutputBufferMock outbuf = new SessionOutputBufferMock(encoder);
         outbuf.writeLine(s);
         outbuf.flush();
         String result = new String(outbuf.getData(), "ISO-8859-1");
@@ -515,16 +523,20 @@ public class TestSessionInOutBuffers {
     @Test(expected=CharacterCodingException.class)
     public void testMalformedInputActionReport() throws Exception {
         byte[] tmp = constructString(SWISS_GERMAN_HELLO).getBytes(Consts.ISO_8859_1.name());
-        SessionInputBufferMock inbuffer = new SessionInputBufferMock(tmp, Consts.UTF_8,
-                CodingErrorAction.REPORT, CodingErrorAction.IGNORE);
+        CharsetDecoder decoder = Consts.UTF_8.newDecoder();
+        decoder.onMalformedInput(CodingErrorAction.REPORT);
+        decoder.onUnmappableCharacter(CodingErrorAction.IGNORE);
+        SessionInputBufferMock inbuffer = new SessionInputBufferMock(tmp, decoder);
         inbuffer.readLine();
     }
 
     @Test
     public void testMalformedInputActionReplace() throws Exception {
         byte[] tmp = constructString(SWISS_GERMAN_HELLO).getBytes(Consts.ISO_8859_1.name());
-        SessionInputBufferMock inbuffer = new SessionInputBufferMock(tmp, Consts.UTF_8,
-                CodingErrorAction.REPLACE, CodingErrorAction.IGNORE);
+        CharsetDecoder decoder = Consts.UTF_8.newDecoder();
+        decoder.onMalformedInput(CodingErrorAction.REPLACE);
+        decoder.onUnmappableCharacter(CodingErrorAction.IGNORE);
+        SessionInputBufferMock inbuffer = new SessionInputBufferMock(tmp, decoder);
         String s = inbuffer.readLine();
         Assert.assertEquals("Gr\ufffdezi_z\ufffdm\ufffd", s);
     }
@@ -532,8 +544,10 @@ public class TestSessionInOutBuffers {
     @Test
     public void testMalformedInputActionIgnore() throws Exception {
         byte[] tmp = constructString(SWISS_GERMAN_HELLO).getBytes(Consts.ISO_8859_1.name());
-        SessionInputBufferMock inbuffer = new SessionInputBufferMock(tmp, Consts.UTF_8,
-                CodingErrorAction.IGNORE, CodingErrorAction.IGNORE);
+        CharsetDecoder decoder = Consts.UTF_8.newDecoder();
+        decoder.onMalformedInput(CodingErrorAction.IGNORE);
+        decoder.onUnmappableCharacter(CodingErrorAction.IGNORE);
+        SessionInputBufferMock inbuffer = new SessionInputBufferMock(tmp, decoder);
         String s = inbuffer.readLine();
         Assert.assertEquals("Grezi_zm", s);
     }