hc-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From ol...@apache.org
Subject svn commit: r1371968 [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...
Date Sat, 11 Aug 2012 16:08:55 GMT
Modified: httpcomponents/httpcore/trunk/httpcore/src/main/java/org/apache/http/impl/SocketHttpServerConnection.java
URL: http://svn.apache.org/viewvc/httpcomponents/httpcore/trunk/httpcore/src/main/java/org/apache/http/impl/SocketHttpServerConnection.java?rev=1371968&r1=1371967&r2=1371968&view=diff
==============================================================================
--- httpcomponents/httpcore/trunk/httpcore/src/main/java/org/apache/http/impl/SocketHttpServerConnection.java (original)
+++ httpcomponents/httpcore/trunk/httpcore/src/main/java/org/apache/http/impl/SocketHttpServerConnection.java Sat Aug 11 16:08:54 2012
@@ -33,7 +33,10 @@ import java.net.InetSocketAddress;
 import java.net.Socket;
 import java.net.SocketAddress;
 import java.net.SocketException;
+import java.nio.charset.Charset;
+import java.nio.charset.CodingErrorAction;
 
+import org.apache.http.Consts;
 import org.apache.http.HttpInetConnection;
 import org.apache.http.annotation.NotThreadSafe;
 import org.apache.http.impl.io.SocketInputBuffer;
@@ -41,8 +44,10 @@ import org.apache.http.impl.io.SocketOut
 import org.apache.http.io.SessionInputBuffer;
 import org.apache.http.io.SessionOutputBuffer;
 import org.apache.http.params.CoreConnectionPNames;
+import org.apache.http.params.CoreProtocolPNames;
 import org.apache.http.params.HttpParams;
 import org.apache.http.util.Args;
+import org.apache.http.util.CharsetUtils;
 
 /**
  * Implementation of a server-side HTTP connection that can be bound to a
@@ -104,7 +109,19 @@ public class SocketHttpServerConnection 
             final Socket socket,
             int buffersize,
             final HttpParams params) throws IOException {
-        return new SocketInputBuffer(socket, buffersize, params);
+        Charset charset = CharsetUtils.get(
+                (String) params.getParameter(CoreProtocolPNames.HTTP_ELEMENT_CHARSET));
+        if (charset == null) {
+            charset = Consts.ASCII;
+        }
+        int maxLineLen = params.getIntParameter(CoreConnectionPNames.MAX_LINE_LENGTH, -1);
+        int minChunkLimit = params.getIntParameter(CoreConnectionPNames.MIN_CHUNK_LIMIT, -1);
+        CodingErrorAction malformedCharAction = (CodingErrorAction) params.getParameter(
+                CoreProtocolPNames.HTTP_MALFORMED_INPUT_ACTION);
+        CodingErrorAction unmappableCharAction = (CodingErrorAction) params.getParameter(
+                CoreProtocolPNames.HTTP_UNMAPPABLE_INPUT_ACTION);
+        return SocketInputBuffer.create(socket, buffersize, charset, maxLineLen, minChunkLimit,
+                malformedCharAction, unmappableCharAction);
     }
 
     /**
@@ -126,7 +143,18 @@ public class SocketHttpServerConnection 
             final Socket socket,
             int buffersize,
             final HttpParams params) throws IOException {
-        return new SocketOutputBuffer(socket, buffersize, params);
+        Charset charset = CharsetUtils.get(
+                (String) params.getParameter(CoreProtocolPNames.HTTP_ELEMENT_CHARSET));
+        if (charset == null) {
+            charset = Consts.ASCII;
+        }
+        int minChunkLimit = params.getIntParameter(CoreConnectionPNames.MIN_CHUNK_LIMIT, -1);
+        CodingErrorAction malformedCharAction = (CodingErrorAction) params.getParameter(
+                CoreProtocolPNames.HTTP_MALFORMED_INPUT_ACTION);
+        CodingErrorAction unmappableCharAction = (CodingErrorAction) params.getParameter(
+                CoreProtocolPNames.HTTP_UNMAPPABLE_INPUT_ACTION);
+        return SocketOutputBuffer.create(socket, buffersize, charset, minChunkLimit,
+                malformedCharAction, unmappableCharAction);
     }
 
     /**

Modified: httpcomponents/httpcore/trunk/httpcore/src/main/java/org/apache/http/impl/io/AbstractSessionInputBuffer.java
URL: http://svn.apache.org/viewvc/httpcomponents/httpcore/trunk/httpcore/src/main/java/org/apache/http/impl/io/AbstractSessionInputBuffer.java?rev=1371968&r1=1371967&r2=1371968&view=diff
==============================================================================
--- httpcomponents/httpcore/trunk/httpcore/src/main/java/org/apache/http/impl/io/AbstractSessionInputBuffer.java (original)
+++ httpcomponents/httpcore/trunk/httpcore/src/main/java/org/apache/http/impl/io/AbstractSessionInputBuffer.java Sat Aug 11 16:08:54 2012
@@ -58,39 +58,82 @@ import org.apache.http.util.CharArrayBuf
  * class treat a lone LF as valid line delimiters in addition to CR-LF required
  * by the HTTP specification.
  *
- * <p>
- * The following parameters can be used to customize the behavior of this
- * class:
- * <ul>
- *  <li>{@link org.apache.http.params.CoreProtocolPNames#HTTP_ELEMENT_CHARSET}</li>
- *  <li>{@link org.apache.http.params.CoreConnectionPNames#MAX_LINE_LENGTH}</li>
- *  <li>{@link org.apache.http.params.CoreConnectionPNames#MIN_CHUNK_LIMIT}</li>
- * </ul>
  * @since 4.0
  */
 @NotThreadSafe
 public abstract class AbstractSessionInputBuffer implements SessionInputBuffer, BufferInfo {
 
-    private static final Charset ASCII = Charset.forName("US-ASCII");
-
+    // TODO: make final
     private InputStream instream;
     private byte[] buffer;
+    private ByteArrayBuffer linebuffer;
+    private Charset charset;
+    private boolean ascii;
+    private int maxLineLen;
+    private int minChunkLimit;
+    private HttpTransportMetricsImpl metrics;
+    private CodingErrorAction onMalformedCharAction;
+    private CodingErrorAction onUnmappableCharAction;
+
     private int bufferpos;
     private int bufferlen;
-
-    private ByteArrayBuffer linebuffer = null;
-
-    private Charset charset;
     private CharsetDecoder decoder;
     private CharBuffer cbuf;
-    private boolean ascii = true;
-    private int maxLineLen = -1;
-    private int minChunkLimit = 512;
 
-    private HttpTransportMetricsImpl metrics;
+    /**
+     * Creates new instance of AbstractSessionInputBuffer.
+     *
+     * @param instream input stream.
+     * @param buffersize buffer size. Must be a positive number.
+     * @param charset charset to be used for decoding HTTP protocol elements.
+     *   If <code>null</code> US-ASCII will be used.
+     * @param maxLineLen maximum line length limit. If set to a positive value, any line exceeding
+     *   this limit will cause an I/O error. A negative value will disable the check.
+     * @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.
+     *
+     * @since 4.3
+     */
+    protected AbstractSessionInputBuffer(
+            final InputStream instream,
+            int buffersize,
+            final Charset charset,
+            int maxLineLen,
+            int minChunkLimit,
+            final CodingErrorAction malformedCharAction,
+            final CodingErrorAction unmappableCharAction) {
+        Args.notNull(instream, "Input stream");
+        Args.positive(buffersize, "Buffer size");
+        this.instream = instream;
+        this.buffer = new byte[buffersize];
+        this.bufferpos = 0;
+        this.bufferlen = 0;
+        this.linebuffer = new ByteArrayBuffer(buffersize);
+        this.charset = charset != null ? charset : Consts.ASCII;
+        this.ascii = this.charset.equals(Consts.ASCII);
+        this.decoder = null;
+        this.maxLineLen = maxLineLen >= 0 ? maxLineLen : -1;
+        this.minChunkLimit = minChunkLimit >= 0 ? minChunkLimit : 512;
+        this.metrics = createTransportMetrics();
+        this.onMalformedCharAction = malformedCharAction != null ? malformedCharAction :
+            CodingErrorAction.REPORT;
+        this.onUnmappableCharAction = unmappableCharAction != null? unmappableCharAction :
+            CodingErrorAction.REPORT;
+    }
 
-    private CodingErrorAction onMalformedInputAction;
-    private CodingErrorAction onUnMappableInputAction;
+    /**
+     * @deprecated (4.3)
+     */
+    @Deprecated
+    protected AbstractSessionInputBuffer() {
+    }
 
     /**
      * Initializes this session input buffer.
@@ -98,7 +141,11 @@ public abstract class AbstractSessionInp
      * @param instream the source input stream.
      * @param buffersize the size of the internal buffer.
      * @param params HTTP parameters.
+     *
+     * @deprecated (4.3) use constructor
+     *     {@link AbstractSessionInputBuffer#AbstractSessionInputBuffer(InputStream, int, HttpParams)}
      */
+    @Deprecated
     protected void init(final InputStream instream, int buffersize, final HttpParams params) {
         Args.notNull(instream, "Input stream");
         Args.notNegative(buffersize, "Buffer size");
@@ -110,17 +157,17 @@ public abstract class AbstractSessionInp
         this.linebuffer = new ByteArrayBuffer(buffersize);
         String charset = (String) params.getParameter(CoreProtocolPNames.HTTP_ELEMENT_CHARSET);
         this.charset = charset != null ? Charset.forName(charset) : Consts.ASCII;
-        this.ascii = this.charset.equals(ASCII);
+        this.ascii = this.charset.equals(Consts.ASCII);
         this.decoder = null;
         this.maxLineLen = params.getIntParameter(CoreConnectionPNames.MAX_LINE_LENGTH, -1);
         this.minChunkLimit = params.getIntParameter(CoreConnectionPNames.MIN_CHUNK_LIMIT, 512);
         this.metrics = createTransportMetrics();
         CodingErrorAction a1 = (CodingErrorAction) params.getParameter(
                 CoreProtocolPNames.HTTP_MALFORMED_INPUT_ACTION);
-        this.onMalformedInputAction = a1 != null ? a1 : CodingErrorAction.REPORT;
+        this.onMalformedCharAction = a1 != null ? a1 : CodingErrorAction.REPORT;
         CodingErrorAction a2 = (CodingErrorAction) params.getParameter(
                 CoreProtocolPNames.HTTP_UNMAPPABLE_INPUT_ACTION);
-        this.onUnMappableInputAction = a2 != null? a2 : CodingErrorAction.REPORT;
+        this.onUnmappableCharAction = a2 != null? a2 : CodingErrorAction.REPORT;
     }
 
     /**
@@ -357,8 +404,8 @@ public abstract class AbstractSessionInp
         }
         if (this.decoder == null) {
             this.decoder = this.charset.newDecoder();
-            this.decoder.onMalformedInput(this.onMalformedInputAction);
-            this.decoder.onUnmappableCharacter(this.onUnMappableInputAction);
+            this.decoder.onMalformedInput(this.onMalformedCharAction);
+            this.decoder.onUnmappableCharacter(this.onUnmappableCharAction);
         }
         if (this.cbuf == null) {
             this.cbuf = CharBuffer.allocate(1024);

Modified: httpcomponents/httpcore/trunk/httpcore/src/main/java/org/apache/http/impl/io/AbstractSessionOutputBuffer.java
URL: http://svn.apache.org/viewvc/httpcomponents/httpcore/trunk/httpcore/src/main/java/org/apache/http/impl/io/AbstractSessionOutputBuffer.java?rev=1371968&r1=1371967&r2=1371968&view=diff
==============================================================================
--- httpcomponents/httpcore/trunk/httpcore/src/main/java/org/apache/http/impl/io/AbstractSessionOutputBuffer.java (original)
+++ httpcomponents/httpcore/trunk/httpcore/src/main/java/org/apache/http/impl/io/AbstractSessionOutputBuffer.java Sat Aug 11 16:08:54 2012
@@ -53,39 +53,78 @@ import org.apache.http.util.CharArrayBuf
  * Abstract base class for session output buffers that stream data to
  * an arbitrary {@link OutputStream}. This class buffers small chunks of
  * output data in an internal byte array for optimal output performance.
- * <p>
+ * </p>
  * {@link #writeLine(CharArrayBuffer)} and {@link #writeLine(String)} methods
  * of this class use CR-LF as a line delimiter.
- * <p>
- * The following parameters can be used to customize the behavior of this
- * class:
- * <ul>
- *  <li>{@link org.apache.http.params.CoreProtocolPNames#HTTP_ELEMENT_CHARSET}</li>
- *  <li>{@link org.apache.http.params.CoreConnectionPNames#MIN_CHUNK_LIMIT}</li>
- * </ul>
- * <p>
  *
  * @since 4.0
  */
 @NotThreadSafe
 public abstract class AbstractSessionOutputBuffer implements SessionOutputBuffer, BufferInfo {
 
-    private static final Charset ASCII = Charset.forName("US-ASCII");
     private static final byte[] CRLF = new byte[] {HTTP.CR, HTTP.LF};
 
+    // TODO: make final
     private OutputStream outstream;
     private ByteArrayBuffer buffer;
-
     private Charset charset;
+    private boolean ascii;
+    private int minChunkLimit;
+    private HttpTransportMetricsImpl metrics;
+    private CodingErrorAction onMalformedCharAction;
+    private CodingErrorAction onUnmappableCharAction;
+
     private CharsetEncoder encoder;
     private ByteBuffer bbuf;
-    private boolean ascii = true;
-    private int minChunkLimit = 512;
 
-    private HttpTransportMetricsImpl metrics;
+    /**
+     * Creates new instance of AbstractSessionOutputBuffer.
+     *
+     * @param outstream output stream.
+     * @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.
+     *
+     * @since 4.3
+     */
+    protected AbstractSessionOutputBuffer(
+            final OutputStream outstream,
+            int buffersize,
+            final Charset charset,
+            int minChunkLimit,
+            final CodingErrorAction malformedCharAction,
+            final CodingErrorAction unmappableCharAction) {
+        super();
+        Args.notNull(outstream, "Input stream");
+        Args.notNegative(buffersize, "Buffer size");
+        this.outstream = outstream;
+        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.metrics = createTransportMetrics();
+        this.onMalformedCharAction = malformedCharAction != null ? malformedCharAction :
+            CodingErrorAction.REPORT;
+        this.onUnmappableCharAction = unmappableCharAction != null? unmappableCharAction :
+            CodingErrorAction.REPORT;
+    }
 
-    private CodingErrorAction onMalformedInputAction;
-    private CodingErrorAction onUnMappableInputAction;
+    /**
+     * @deprecated (4.3)
+     */
+    @Deprecated
+    protected AbstractSessionOutputBuffer() {
+    }
 
     /**
      * Initializes this session output buffer.
@@ -93,7 +132,11 @@ public abstract class AbstractSessionOut
      * @param outstream the destination output stream.
      * @param buffersize the size of the internal buffer.
      * @param params HTTP parameters.
+     *
+     * @deprecated (4.3) use constructor
+     *   {@link AbstractSessionOutputBuffer#AbstractSessionOutputBuffer(OutputStream, int, HttpParams)}
      */
+    @Deprecated
     protected void init(final OutputStream outstream, int buffersize, final HttpParams params) {
         Args.notNull(outstream, "Input stream");
         Args.notNegative(buffersize, "Buffer size");
@@ -102,16 +145,16 @@ public abstract class AbstractSessionOut
         this.buffer = new ByteArrayBuffer(buffersize);
         String charset = (String) params.getParameter(CoreProtocolPNames.HTTP_ELEMENT_CHARSET);
         this.charset = charset != null ? Charset.forName(charset) : Consts.ASCII;
-        this.ascii = this.charset.equals(ASCII);
+        this.ascii = this.charset.equals(Consts.ASCII);
         this.encoder = null;
         this.minChunkLimit = params.getIntParameter(CoreConnectionPNames.MIN_CHUNK_LIMIT, 512);
         this.metrics = createTransportMetrics();
         CodingErrorAction a1 = (CodingErrorAction) params.getParameter(
                 CoreProtocolPNames.HTTP_MALFORMED_INPUT_ACTION);
-        this.onMalformedInputAction = a1 != null ? a1 : CodingErrorAction.REPORT;
+        this.onMalformedCharAction = a1 != null ? a1 : CodingErrorAction.REPORT;
         CodingErrorAction a2 = (CodingErrorAction) params.getParameter(
                 CoreProtocolPNames.HTTP_UNMAPPABLE_INPUT_ACTION);
-        this.onUnMappableInputAction = a2 != null? a2 : CodingErrorAction.REPORT;
+        this.onUnmappableCharAction = a2 != null? a2 : CodingErrorAction.REPORT;
     }
 
     /**
@@ -262,8 +305,8 @@ public abstract class AbstractSessionOut
         }
         if (this.encoder == null) {
             this.encoder = this.charset.newEncoder();
-            this.encoder.onMalformedInput(this.onMalformedInputAction);
-            this.encoder.onUnmappableCharacter(this.onUnMappableInputAction);
+            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/main/java/org/apache/http/impl/io/SocketInputBuffer.java
URL: http://svn.apache.org/viewvc/httpcomponents/httpcore/trunk/httpcore/src/main/java/org/apache/http/impl/io/SocketInputBuffer.java?rev=1371968&r1=1371967&r2=1371968&view=diff
==============================================================================
--- httpcomponents/httpcore/trunk/httpcore/src/main/java/org/apache/http/impl/io/SocketInputBuffer.java (original)
+++ httpcomponents/httpcore/trunk/httpcore/src/main/java/org/apache/http/impl/io/SocketInputBuffer.java Sat Aug 11 16:08:54 2012
@@ -30,6 +30,8 @@ package org.apache.http.impl.io;
 import java.io.IOException;
 import java.net.Socket;
 import java.net.SocketTimeoutException;
+import java.nio.charset.Charset;
+import java.nio.charset.CodingErrorAction;
 
 import org.apache.http.annotation.NotThreadSafe;
 import org.apache.http.io.EofSensor;
@@ -39,14 +41,6 @@ import org.apache.http.util.Args;
 
 /**
  * {@link SessionInputBuffer} implementation bound to a {@link Socket}.
- * <p>
- * The following parameters can be used to customize the behavior of this
- * class:
- * <ul>
- *  <li>{@link org.apache.http.params.CoreProtocolPNames#HTTP_ELEMENT_CHARSET}</li>
- *  <li>{@link org.apache.http.params.CoreConnectionPNames#MAX_LINE_LENGTH}</li>
- *  <li>{@link org.apache.http.params.CoreConnectionPNames#MIN_CHUNK_LIMIT}</li>
- * </ul>
  *
  * @since 4.0
  */
@@ -66,7 +60,10 @@ public class SocketInputBuffer extends A
      *   {@link Socket#getReceiveBufferSize()}. If resultant number is less
      *   than <code>1024</code> it is set to <code>1024</code>.
      * @param params HTTP parameters.
+     *
+     * @deprecated (4.3) use {@link SocketInputBuffer#create(Socket, int, Charset, int, int, CodingErrorAction, CodingErrorAction)}
      */
+    @Deprecated
     public SocketInputBuffer(
             final Socket socket,
             int buffersize,
@@ -84,6 +81,78 @@ public class SocketInputBuffer extends A
         init(socket.getInputStream(), buffersize, params);
     }
 
+    SocketInputBuffer(
+            final Socket socket,
+            int buffersize,
+            final Charset charset,
+            int maxLineLen,
+            int minChunkLimit,
+            final CodingErrorAction malformedCharAction,
+            final CodingErrorAction unmappableCharAction) throws IOException {
+        super(socket.getInputStream(), buffersize, charset, maxLineLen, minChunkLimit,
+                malformedCharAction, unmappableCharAction);
+        this.socket = socket;
+        this.eof = false;
+    }
+
+    /**
+     * Creates SocketInputBuffer instance.
+     *
+     * @param socket socket
+     * @param buffersize buffer size. If this number is negative it is set to the value of
+     *   {@link Socket#getReceiveBufferSize()}. If resultant number is less than
+     *   <code>1024</code> it is set to <code>1024</code>.
+     * @param charset charset to be used for decoding HTTP protocol elements.
+     *   If <code>null</code> US-ASCII will be used.
+     * @param maxLineLen maximum line length limit. If set to a positive value, any line exceeding
+     *   this limit will cause an I/O error. A negative value will disable the check.
+     * @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.
+     *
+     * @since 4.3
+     */
+    public static SocketInputBuffer create(
+            final Socket socket,
+            int buffersize,
+            final Charset charset,
+            int maxLineLen,
+            int minChunkLimit,
+            final CodingErrorAction malformedCharAction,
+            final CodingErrorAction unmappableCharAction) throws IOException {
+        Args.notNull(socket, "Socket");
+        if (buffersize < 0) {
+            buffersize = socket.getReceiveBufferSize();
+        }
+        if (buffersize < 1024) {
+            buffersize = 1024;
+        }
+        return new SocketInputBuffer(socket, buffersize, charset, maxLineLen, minChunkLimit,
+                malformedCharAction, unmappableCharAction);
+    }
+
+    /**
+     * Creates SocketInputBuffer instance.
+     *
+     * @param socket socket
+     * @param buffersize buffer size. If this number is negative it is set to the value of
+     *   {@link Socket#getReceiveBufferSize()}. If resultant number is less than
+     *   <code>1024</code> it is set to <code>1024</code>.
+     *
+     * @since 4.3
+     */
+    public static SocketInputBuffer create(
+            final Socket socket,
+            int buffersize) throws IOException {
+        return create(socket, buffersize, null, -1, -1, null, null);
+    }
+
     @Override
     protected int fillBuffer() throws IOException {
         int i = super.fillBuffer();

Modified: httpcomponents/httpcore/trunk/httpcore/src/main/java/org/apache/http/impl/io/SocketOutputBuffer.java
URL: http://svn.apache.org/viewvc/httpcomponents/httpcore/trunk/httpcore/src/main/java/org/apache/http/impl/io/SocketOutputBuffer.java?rev=1371968&r1=1371967&r2=1371968&view=diff
==============================================================================
--- httpcomponents/httpcore/trunk/httpcore/src/main/java/org/apache/http/impl/io/SocketOutputBuffer.java (original)
+++ httpcomponents/httpcore/trunk/httpcore/src/main/java/org/apache/http/impl/io/SocketOutputBuffer.java Sat Aug 11 16:08:54 2012
@@ -29,6 +29,8 @@ package org.apache.http.impl.io;
 
 import java.io.IOException;
 import java.net.Socket;
+import java.nio.charset.Charset;
+import java.nio.charset.CodingErrorAction;
 
 import org.apache.http.annotation.NotThreadSafe;
 import org.apache.http.io.SessionOutputBuffer;
@@ -37,13 +39,6 @@ import org.apache.http.util.Args;
 
 /**
  * {@link SessionOutputBuffer} implementation bound to a {@link Socket}.
- * <p>
- * The following parameters can be used to customize the behavior of this
- * class:
- * <ul>
- *  <li>{@link org.apache.http.params.CoreProtocolPNames#HTTP_ELEMENT_CHARSET}</li>
- *  <li>{@link org.apache.http.params.CoreConnectionPNames#MIN_CHUNK_LIMIT}</li>
- * </ul>
  *
  * @since 4.0
  */
@@ -59,7 +54,10 @@ public class SocketOutputBuffer extends 
      *   {@link Socket#getSendBufferSize()}. If resultant number is less
      *   than <code>1024</code> it is set to <code>1024</code>.
      * @param params HTTP parameters.
+     *
+     * @deprecated (4.3) use {@link SocketOutputBuffer#create(Socket, int, Charset, int, CodingErrorAction, CodingErrorAction)}
      */
+    @Deprecated
     public SocketOutputBuffer(
             final Socket socket,
             int buffersize,
@@ -75,4 +73,70 @@ public class SocketOutputBuffer extends 
         init(socket.getOutputStream(), buffersize, params);
     }
 
+    SocketOutputBuffer(
+            final Socket socket,
+            int buffersize,
+            final Charset charset,
+            int minChunkLimit,
+            final CodingErrorAction malformedCharAction,
+            final CodingErrorAction unmappableCharAction) throws IOException {
+        super(socket.getOutputStream(), buffersize, charset, minChunkLimit,
+                malformedCharAction, unmappableCharAction);
+    }
+
+    /**
+     * Creates SocketOutputBuffer instance.
+     *
+     * @param socket socket
+     * @param buffersize buffer size. If this number is negative it is set to the value of
+     *   {@link Socket#getSendBufferSize()}. If resultant number is less than
+     *   <code>1024</code> it is set to <code>1024</code>.
+     * @param charset charset to be used for decoding 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.
+     *
+     * @since 4.3
+     */
+    public static SocketOutputBuffer create(
+            final Socket socket,
+            int buffersize,
+            final Charset charset,
+            int minChunkLimit,
+            final CodingErrorAction malformedCharAction,
+            final CodingErrorAction unmappableCharAction) throws IOException {
+        Args.notNull(socket, "Socket");
+        if (buffersize < 0) {
+            buffersize = socket.getSendBufferSize();
+        }
+        if (buffersize < 1024) {
+            buffersize = 1024;
+        }
+        return new SocketOutputBuffer(socket, buffersize, charset, minChunkLimit,
+                malformedCharAction, unmappableCharAction);
+    }
+
+    /**
+     * Creates SocketOutputBuffer instance.
+     *
+     * @param socket socket
+     * @param buffersize buffer size. If this number is negative it is set to the value of
+     *   {@link Socket#getSendBufferSize()}. If resultant number is less than
+     *   <code>1024</code> it is set to <code>1024</code>.
+     *
+     * @since 4.3
+     */
+    public static SocketOutputBuffer create(
+            final Socket socket,
+            int buffersize) throws IOException {
+        return create(socket, buffersize, null, -1, null, null);
+    }
+
 }

Added: httpcomponents/httpcore/trunk/httpcore/src/main/java/org/apache/http/util/CharsetUtils.java
URL: http://svn.apache.org/viewvc/httpcomponents/httpcore/trunk/httpcore/src/main/java/org/apache/http/util/CharsetUtils.java?rev=1371968&view=auto
==============================================================================
--- httpcomponents/httpcore/trunk/httpcore/src/main/java/org/apache/http/util/CharsetUtils.java (added)
+++ httpcomponents/httpcore/trunk/httpcore/src/main/java/org/apache/http/util/CharsetUtils.java Sat Aug 11 16:08:54 2012
@@ -0,0 +1,58 @@
+/*
+ * ====================================================================
+ * 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.
+ * ====================================================================
+ *
+ * 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.util;
+
+import java.io.UnsupportedEncodingException;
+import java.nio.charset.Charset;
+import java.nio.charset.UnsupportedCharsetException;
+
+public class CharsetUtils {
+
+    public static Charset lookup(final String name) {
+        if (name == null) {
+            return null;
+        }
+        try {
+            return Charset.forName(name);
+        } catch (UnsupportedCharsetException ex) {
+            return null;
+        }
+    }
+
+    public static Charset get(final String name) throws UnsupportedEncodingException {
+        if (name == null) {
+            return null;
+        }
+        try {
+            return Charset.forName(name);
+        } catch (UnsupportedCharsetException ex) {
+            throw new UnsupportedEncodingException(name);
+        }
+    }
+
+}

Propchange: httpcomponents/httpcore/trunk/httpcore/src/main/java/org/apache/http/util/CharsetUtils.java
------------------------------------------------------------------------------
    svn:eol-style = native

Propchange: httpcomponents/httpcore/trunk/httpcore/src/main/java/org/apache/http/util/CharsetUtils.java
------------------------------------------------------------------------------
    svn:keywords = Date Revision

Propchange: httpcomponents/httpcore/trunk/httpcore/src/main/java/org/apache/http/util/CharsetUtils.java
------------------------------------------------------------------------------
    svn:mime-type = text/plain

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=1371968&r1=1371967&r2=1371968&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 Sat Aug 11 16:08:54 2012
@@ -31,10 +31,11 @@ import java.io.ByteArrayInputStream;
 import java.io.IOException;
 import java.io.InputStream;
 import java.io.UnsupportedEncodingException;
+import java.nio.charset.Charset;
+import java.nio.charset.CodingErrorAction;
 
+import org.apache.http.Consts;
 import org.apache.http.impl.io.AbstractSessionInputBuffer;
-import org.apache.http.params.BasicHttpParams;
-import org.apache.http.params.HttpParams;
 
 /**
  * {@link org.apache.http.io.SessionInputBuffer} mockup implementation.
@@ -44,73 +45,71 @@ public class SessionInputBufferMock exte
     public static final int BUFFER_SIZE = 16;
 
     public SessionInputBufferMock(
-            final InputStream instream,
-            int buffersize,
-            final HttpParams params) {
-        super();
-        init(instream, buffersize, params);
+            final InputStream instream, 
+            int buffersize, 
+            final Charset charset,
+            int maxLineLen,
+            int minChunkLimit,
+            final CodingErrorAction malformedInputAction,
+            final CodingErrorAction unmappableInputAction) {
+        super(instream, buffersize, charset, maxLineLen, minChunkLimit, 
+                malformedInputAction, unmappableInputAction);
     }
 
     public SessionInputBufferMock(
             final InputStream instream,
             int buffersize) {
-        this(instream, buffersize, new BasicHttpParams());
+        this(instream, buffersize, null, -1, -1, null, null);
     }
 
     public SessionInputBufferMock(
             final byte[] bytes,
-            final HttpParams params) {
-        this(bytes, BUFFER_SIZE, params);
-    }
-
-    public SessionInputBufferMock(
-            final byte[] bytes) {
-        this(bytes, BUFFER_SIZE, new BasicHttpParams());
+            int buffersize,
+            final Charset charset,
+            int maxLineLen,
+            int minChunkLimit,
+            final CodingErrorAction malformedInputAction,
+            final CodingErrorAction unmappableInputAction) {
+        this(new ByteArrayInputStream(bytes), buffersize, charset, maxLineLen, minChunkLimit, 
+                malformedInputAction, unmappableInputAction);
     }
 
     public SessionInputBufferMock(
             final byte[] bytes,
             int buffersize,
-            final HttpParams params) {
-        this(new ByteArrayInputStream(bytes), buffersize, params);
+            int maxLineLen) {
+        this(new ByteArrayInputStream(bytes), buffersize, Consts.ASCII, maxLineLen, -1, null, null);
     }
 
     public SessionInputBufferMock(
             final byte[] bytes,
             int buffersize) {
-        this(new ByteArrayInputStream(bytes), buffersize, new BasicHttpParams());
+        this(new ByteArrayInputStream(bytes), buffersize);
     }
 
     public SessionInputBufferMock(
-            final String s,
-            final String charset,
-            int buffersize,
-            final HttpParams params)
-        throws UnsupportedEncodingException {
-        this(s.getBytes(charset), buffersize, params);
+            final byte[] bytes) {
+        this(bytes, BUFFER_SIZE);
     }
 
     public SessionInputBufferMock(
-            final String s,
-            final String charset,
-            int buffersize)
-        throws UnsupportedEncodingException {
-        this(s.getBytes(charset), buffersize, new BasicHttpParams());
+            final byte[] bytes, final Charset charset) {
+        this(bytes, BUFFER_SIZE, charset, -1, -1, null, null);
     }
 
     public SessionInputBufferMock(
-            final String s,
-            final String charset,
-            final HttpParams params)
-        throws UnsupportedEncodingException {
-        this(s.getBytes(charset), params);
+            final byte[] bytes, 
+            final Charset charset,
+            final CodingErrorAction malformedInputAction,
+            final CodingErrorAction unmappableInputAction) {
+        this(bytes, BUFFER_SIZE, charset, -1, -1, malformedInputAction, unmappableInputAction);
     }
 
     public SessionInputBufferMock(
             final String s,
-            final String charset)
+            final Charset charset)
         throws UnsupportedEncodingException {
-        this(s.getBytes(charset), new BasicHttpParams());
+        this(s.getBytes(charset.name()), BUFFER_SIZE, charset, -1, -1, null, null);
 
     }
 

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=1371968&r1=1371967&r2=1371968&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 Sat Aug 11 16:08:54 2012
@@ -28,11 +28,10 @@
 package org.apache.http.impl;
 
 import java.io.ByteArrayOutputStream;
-import java.io.OutputStream;
+import java.nio.charset.Charset;
+import java.nio.charset.CodingErrorAction;
 
 import org.apache.http.impl.io.AbstractSessionOutputBuffer;
-import org.apache.http.params.BasicHttpParams;
-import org.apache.http.params.HttpParams;
 
 /**
  * {@link org.apache.http.io.SessionOutputBuffer} mockup implementation.
@@ -40,42 +39,47 @@ import org.apache.http.params.HttpParams
  */
 public class SessionOutputBufferMock extends AbstractSessionOutputBuffer {
 
-    private ByteArrayOutputStream buffer = new ByteArrayOutputStream();
     public static final int BUFFER_SIZE = 16;
 
+    private final ByteArrayOutputStream buffer;
+    
     public SessionOutputBufferMock(
-            final OutputStream outstream,
-            int buffersize,
-            final HttpParams params) {
-        super();
-        init(outstream, buffersize, params);
+            final ByteArrayOutputStream buffer, 
+            int buffersize, 
+            final Charset charset,
+            int minChunkLimit,
+            final CodingErrorAction malformedInputAction,
+            final CodingErrorAction unmappableInputAction) {
+        super(buffer, buffersize, charset, 
+                minChunkLimit, malformedInputAction, unmappableInputAction);
+        this.buffer = buffer;
     }
 
     public SessionOutputBufferMock(
-            final OutputStream outstream,
+            final ByteArrayOutputStream buffer,
             int buffersize) {
-        this(outstream, buffersize, new BasicHttpParams());
+        this(buffer, buffersize, null, -1, null, null);
     }
 
     public SessionOutputBufferMock(
-            final ByteArrayOutputStream buffer,
-            final HttpParams params) {
-        this(buffer, BUFFER_SIZE, params);
-        this.buffer = buffer;
+            final Charset charset,
+            final CodingErrorAction malformedInputAction,
+            final CodingErrorAction unmappableInputAction) {
+        this(new ByteArrayOutputStream(), BUFFER_SIZE, charset, -1, 
+                malformedInputAction, unmappableInputAction);
     }
-
+    
     public SessionOutputBufferMock(
-            final ByteArrayOutputStream buffer) {
-        this(buffer, BUFFER_SIZE, new BasicHttpParams());
-        this.buffer = buffer;
+            final Charset charset) {
+        this(new ByteArrayOutputStream(), BUFFER_SIZE, charset, -1, null, null);
     }
 
-    public SessionOutputBufferMock(final HttpParams params) {
-        this(new ByteArrayOutputStream(), params);
+    public SessionOutputBufferMock(final ByteArrayOutputStream buffer) {
+        this(buffer, BUFFER_SIZE, null, -1, null, null);
     }
 
     public SessionOutputBufferMock() {
-        this(new ByteArrayOutputStream(), new BasicHttpParams());
+        this(new ByteArrayOutputStream());
     }
 
     public byte[] getData() {

Modified: httpcomponents/httpcore/trunk/httpcore/src/test/java/org/apache/http/impl/entity/TestEntityDeserializer.java
URL: http://svn.apache.org/viewvc/httpcomponents/httpcore/trunk/httpcore/src/test/java/org/apache/http/impl/entity/TestEntityDeserializer.java?rev=1371968&r1=1371967&r2=1371968&view=diff
==============================================================================
--- httpcomponents/httpcore/trunk/httpcore/src/test/java/org/apache/http/impl/entity/TestEntityDeserializer.java (original)
+++ httpcomponents/httpcore/trunk/httpcore/src/test/java/org/apache/http/impl/entity/TestEntityDeserializer.java Sat Aug 11 16:08:54 2012
@@ -29,6 +29,7 @@ package org.apache.http.impl.entity;
 
 import java.io.InputStream;
 
+import org.apache.http.Consts;
 import org.apache.http.HttpEntity;
 import org.apache.http.HttpMessage;
 import org.apache.http.ProtocolException;
@@ -63,7 +64,7 @@ public class TestEntityDeserializer {
 
     @Test
     public void testEntityWithTransferEncoding() throws Exception {
-        SessionInputBuffer inbuffer = new SessionInputBufferMock("0\r\n", "US-ASCII");
+        SessionInputBuffer inbuffer = new SessionInputBufferMock("0\r\n", Consts.ASCII);
         HttpMessage message = new DummyHttpMessage();
 
         // lenient mode
@@ -109,7 +110,7 @@ public class TestEntityDeserializer {
 
     @Test
     public void testEntityWithUnsupportedTransferEncoding() throws Exception {
-        SessionInputBuffer inbuffer = new SessionInputBufferMock("0\r\n", "US-ASCII");
+        SessionInputBuffer inbuffer = new SessionInputBufferMock("0\r\n", Consts.ASCII);
         HttpMessage message = new DummyHttpMessage();
 
         // lenient mode
@@ -137,7 +138,7 @@ public class TestEntityDeserializer {
 
     @Test
     public void testChunkedTransferEncodingMustBeLast() throws Exception {
-        SessionInputBuffer inbuffer = new SessionInputBufferMock("0\r\n", "US-ASCII");
+        SessionInputBuffer inbuffer = new SessionInputBufferMock("0\r\n", Consts.ASCII);
         HttpMessage message = new DummyHttpMessage();
 
         // lenient mode

Modified: httpcomponents/httpcore/trunk/httpcore/src/test/java/org/apache/http/impl/io/TestMessageParser.java
URL: http://svn.apache.org/viewvc/httpcomponents/httpcore/trunk/httpcore/src/test/java/org/apache/http/impl/io/TestMessageParser.java?rev=1371968&r1=1371967&r2=1371968&view=diff
==============================================================================
--- httpcomponents/httpcore/trunk/httpcore/src/test/java/org/apache/http/impl/io/TestMessageParser.java (original)
+++ httpcomponents/httpcore/trunk/httpcore/src/test/java/org/apache/http/impl/io/TestMessageParser.java Sat Aug 11 16:08:54 2012
@@ -29,6 +29,7 @@ package org.apache.http.impl.io;
 
 import java.io.IOException;
 
+import org.apache.http.Consts;
 import org.apache.http.Header;
 import org.apache.http.HeaderElement;
 import org.apache.http.NameValuePair;
@@ -71,7 +72,7 @@ public class TestMessageParser {
             "\t and even more stuff\r\n" +
             "     \r\n" +
             "\r\n";
-        SessionInputBuffer receiver = new SessionInputBufferMock(s, "US-ASCII");
+        SessionInputBuffer receiver = new SessionInputBufferMock(s, Consts.ASCII);
         Header[] headers = AbstractMessageParser.parseHeaders
             (receiver, -1, -1, null);
         Assert.assertNotNull(headers);
@@ -96,7 +97,7 @@ public class TestMessageParser {
         String s =
             "header1  : stuff; param1 = value1; param2 = \"value 2\" \r\n" +
             "\r\n";
-        SessionInputBuffer receiver = new SessionInputBufferMock(s, "US-ASCII");
+        SessionInputBuffer receiver = new SessionInputBufferMock(s, Consts.ASCII);
         Header[] headers = AbstractMessageParser.parseHeaders
             (receiver, -1, -1, null);
         Assert.assertNotNull(headers);
@@ -121,7 +122,7 @@ public class TestMessageParser {
         String s = "    stuff\r\n" +
             "header1: stuff\r\n" +
             "\r\n";
-        SessionInputBuffer receiver = new SessionInputBufferMock(s, "US-ASCII");
+        SessionInputBuffer receiver = new SessionInputBufferMock(s, Consts.ASCII);
         try {
             AbstractMessageParser.parseHeaders(receiver, -1, -1, null);
             Assert.fail("ProtocolException should have been thrown");
@@ -131,7 +132,7 @@ public class TestMessageParser {
         s = "  :  stuff\r\n" +
             "header1: stuff\r\n" +
             "\r\n";
-        receiver = new SessionInputBufferMock(s, "US-ASCII");
+        receiver = new SessionInputBufferMock(s, Consts.ASCII);
         try {
             AbstractMessageParser.parseHeaders(receiver, -1, -1, null);
             Assert.fail("ProtocolException should have been thrown");
@@ -145,7 +146,7 @@ public class TestMessageParser {
         String s =
             "    header1: stuff\r\n" +
             "header2  : stuff \r\n";
-        SessionInputBuffer receiver = new SessionInputBufferMock(s, "US-ASCII");
+        SessionInputBuffer receiver = new SessionInputBufferMock(s, Consts.ASCII);
         Header[] headers = AbstractMessageParser.parseHeaders
             (receiver, -1, -1, null);
         Assert.assertNotNull(headers);
@@ -159,7 +160,7 @@ public class TestMessageParser {
     @Test
     public void testEmptyDataStream() throws Exception {
         String s = "";
-        SessionInputBuffer receiver = new SessionInputBufferMock(s, "US-ASCII");
+        SessionInputBuffer receiver = new SessionInputBufferMock(s, Consts.ASCII);
         Header[] headers = AbstractMessageParser.parseHeaders
             (receiver, -1, -1, null);
         Assert.assertNotNull(headers);
@@ -173,7 +174,7 @@ public class TestMessageParser {
             "header2: stuff \r\n" +
             "header3: stuff\r\n" +
             "\r\n";
-        SessionInputBuffer receiver = new SessionInputBufferMock(s, "US-ASCII");
+        SessionInputBuffer receiver = new SessionInputBufferMock(s, Consts.ASCII);
         try {
             AbstractMessageParser.parseHeaders(receiver, 2, -1, null);
             Assert.fail("IOException should have been thrown");
@@ -189,7 +190,7 @@ public class TestMessageParser {
             " stuff \r\n" +
             " stuff\r\n" +
             "\r\n";
-        SessionInputBuffer receiver = new SessionInputBufferMock(s, "US-ASCII");
+        SessionInputBuffer receiver = new SessionInputBufferMock(s, Consts.ASCII);
         try {
             AbstractMessageParser.parseHeaders(receiver, 2, 15, null);
             Assert.fail("IOException should have been thrown");

Modified: httpcomponents/httpcore/trunk/httpcore/src/test/java/org/apache/http/impl/io/TestRequestParser.java
URL: http://svn.apache.org/viewvc/httpcomponents/httpcore/trunk/httpcore/src/test/java/org/apache/http/impl/io/TestRequestParser.java?rev=1371968&r1=1371967&r2=1371968&view=diff
==============================================================================
--- httpcomponents/httpcore/trunk/httpcore/src/test/java/org/apache/http/impl/io/TestRequestParser.java (original)
+++ httpcomponents/httpcore/trunk/httpcore/src/test/java/org/apache/http/impl/io/TestRequestParser.java Sat Aug 11 16:08:54 2012
@@ -34,6 +34,7 @@ package org.apache.http.impl.io;
 import java.io.InterruptedIOException;
 
 import org.apache.http.ConnectionClosedException;
+import org.apache.http.Consts;
 import org.apache.http.Header;
 import org.apache.http.HttpRequest;
 import org.apache.http.HttpVersion;
@@ -95,7 +96,7 @@ public class TestRequestParser {
             "User-Agent: whatever\r\n" +
             "Cookie: c1=stuff\r\n" +
             "\r\n";
-        SessionInputBuffer inbuffer = new SessionInputBufferMock(s, "US-ASCII");
+        SessionInputBuffer inbuffer = new SessionInputBufferMock(s, Consts.ASCII);
 
         DefaultHttpRequestParser parser = new DefaultHttpRequestParser(
                 inbuffer,

Modified: httpcomponents/httpcore/trunk/httpcore/src/test/java/org/apache/http/impl/io/TestResponseParser.java
URL: http://svn.apache.org/viewvc/httpcomponents/httpcore/trunk/httpcore/src/test/java/org/apache/http/impl/io/TestResponseParser.java?rev=1371968&r1=1371967&r2=1371968&view=diff
==============================================================================
--- httpcomponents/httpcore/trunk/httpcore/src/test/java/org/apache/http/impl/io/TestResponseParser.java (original)
+++ httpcomponents/httpcore/trunk/httpcore/src/test/java/org/apache/http/impl/io/TestResponseParser.java Sat Aug 11 16:08:54 2012
@@ -29,6 +29,7 @@ package org.apache.http.impl.io;
 
 import java.io.InterruptedIOException;
 
+import org.apache.http.Consts;
 import org.apache.http.Header;
 import org.apache.http.HttpResponse;
 import org.apache.http.HttpVersion;
@@ -91,7 +92,7 @@ public class TestResponseParser {
             "Date: some date\r\n" +
             "Set-Cookie: c1=stuff\r\n" +
             "\r\n";
-        SessionInputBuffer inbuffer = new SessionInputBufferMock(s, "US-ASCII");
+        SessionInputBuffer inbuffer = new SessionInputBufferMock(s, Consts.ASCII);
 
         DefaultHttpResponseParser parser = new DefaultHttpResponseParser(
                 inbuffer,

Copied: httpcomponents/httpcore/trunk/httpcore/src/test/java/org/apache/http/impl/io/TestSessionInOutBuffers.java (from r1371358, httpcomponents/httpcore/trunk/httpcore/src/test/java/org/apache/http/impl/io/TestSessionBuffers.java)
URL: http://svn.apache.org/viewvc/httpcomponents/httpcore/trunk/httpcore/src/test/java/org/apache/http/impl/io/TestSessionInOutBuffers.java?p2=httpcomponents/httpcore/trunk/httpcore/src/test/java/org/apache/http/impl/io/TestSessionInOutBuffers.java&p1=httpcomponents/httpcore/trunk/httpcore/src/test/java/org/apache/http/impl/io/TestSessionBuffers.java&r1=1371358&r2=1371968&rev=1371968&view=diff
==============================================================================
--- httpcomponents/httpcore/trunk/httpcore/src/test/java/org/apache/http/impl/io/TestSessionBuffers.java (original)
+++ httpcomponents/httpcore/trunk/httpcore/src/test/java/org/apache/http/impl/io/TestSessionInOutBuffers.java Sat Aug 11 16:08:54 2012
@@ -27,10 +27,7 @@
 
 package org.apache.http.impl.io;
 
-import java.io.ByteArrayInputStream;
-import java.io.ByteArrayOutputStream;
 import java.io.IOException;
-import java.io.InputStream;
 import java.nio.charset.CharacterCodingException;
 import java.nio.charset.CodingErrorAction;
 
@@ -38,59 +35,11 @@ import org.apache.http.Consts;
 import org.apache.http.impl.SessionInputBufferMock;
 import org.apache.http.impl.SessionOutputBufferMock;
 import org.apache.http.io.HttpTransportMetrics;
-import org.apache.http.params.BasicHttpParams;
-import org.apache.http.params.CoreConnectionPNames;
-import org.apache.http.params.HttpCoreConfigBuilder;
-import org.apache.http.params.HttpParams;
 import org.apache.http.util.CharArrayBuffer;
 import org.junit.Assert;
 import org.junit.Test;
 
-public class TestSessionBuffers {
-
-    @Test
-    public void testInit() throws Exception {
-        ByteArrayOutputStream out = new ByteArrayOutputStream();
-        new SessionOutputBufferMock(out);
-        try {
-            new SessionOutputBufferMock(null, new BasicHttpParams());
-            Assert.fail("IllegalArgumentException should have been thrown");
-        } catch (IllegalArgumentException ex) {
-            //expected
-        }
-        try {
-            new SessionOutputBufferMock(out, null);
-            Assert.fail("IllegalArgumentException should have been thrown");
-        } catch (IllegalArgumentException ex) {
-            //expected
-        }
-        ByteArrayInputStream in = new ByteArrayInputStream(out.toByteArray());
-        new SessionInputBufferMock(in, 10);
-        try {
-            new SessionInputBufferMock(in, -10);
-            Assert.fail("IllegalArgumentException should have been thrown");
-        } catch (IllegalArgumentException ex) {
-            //expected
-        }
-        try {
-            new SessionOutputBufferMock(out, -10);
-            Assert.fail("IllegalArgumentException should have been thrown");
-        } catch (IllegalArgumentException ex) {
-            //expected
-        }
-        try {
-            new SessionInputBufferMock((InputStream)null, 1024);
-            Assert.fail("IllegalArgumentException should have been thrown");
-        } catch (IllegalArgumentException ex) {
-            //expected
-        }
-        try {
-            new SessionInputBufferMock(in, 10, null);
-            Assert.fail("IllegalArgumentException should have been thrown");
-        } catch (IllegalArgumentException ex) {
-            //expected
-        }
-    }
+public class TestSessionInOutBuffers {
 
     @Test
     public void testBasicBufferProperties() throws Exception {
@@ -377,19 +326,16 @@ public class TestSessionBuffers {
 
     @Test
     public void testLineLimit() throws Exception {
-        HttpParams params = new BasicHttpParams();
         String s = "a very looooooooooooooooooooooooooooooooooooooong line\r\n     ";
         byte[] tmp = s.getBytes("US-ASCII");
         // no limit
-        params.setIntParameter(CoreConnectionPNames.MAX_LINE_LENGTH, 0);
-        SessionInputBufferMock inbuffer1 = new SessionInputBufferMock(tmp, 5, params);
+        SessionInputBufferMock inbuffer1 = new SessionInputBufferMock(tmp, 5, 0);
         Assert.assertNotNull(inbuffer1.readLine());
         long bytesRead = inbuffer1.getMetrics().getBytesTransferred();
         Assert.assertEquals(60, bytesRead);
 
         // 15 char limit
-        params.setIntParameter(CoreConnectionPNames.MAX_LINE_LENGTH, 15);
-        SessionInputBufferMock inbuffer2 = new SessionInputBufferMock(tmp, 5, params);
+        SessionInputBufferMock inbuffer2 = new SessionInputBufferMock(tmp, 5, 15);
         try {
             inbuffer2.readLine();
             Assert.fail("IOException should have been thrown");
@@ -402,10 +348,9 @@ public class TestSessionBuffers {
 
     @Test
     public void testReadLineFringeCase1() throws Exception {
-        HttpParams params = new BasicHttpParams();
         String s = "abc\r\n";
         byte[] tmp = s.getBytes("US-ASCII");
-        SessionInputBufferMock inbuffer1 = new SessionInputBufferMock(tmp, 128, params);
+        SessionInputBufferMock inbuffer1 = new SessionInputBufferMock(tmp, 128);
         Assert.assertEquals('a', inbuffer1.read());
         Assert.assertEquals('b', inbuffer1.read());
         Assert.assertEquals('c', inbuffer1.read());
@@ -438,9 +383,7 @@ public class TestSessionBuffers {
         String s2 = constructString(RUSSIAN_HELLO);
         String s3 = "Like hello and stuff";
 
-        HttpParams params = new HttpCoreConfigBuilder().setHttpElementCharset("UTF-8").build();
-
-        SessionOutputBufferMock outbuffer = new SessionOutputBufferMock(params);
+        SessionOutputBufferMock outbuffer = new SessionOutputBufferMock(Consts.UTF_8);
 
         CharArrayBuffer chbuffer = new CharArrayBuffer(16);
         for (int i = 0; i < 10; i++) {
@@ -462,7 +405,7 @@ public class TestSessionBuffers {
         Assert.assertEquals(expected, bytesWritten);
 
         SessionInputBufferMock inbuffer = new SessionInputBufferMock(
-                outbuffer.getData(), params);
+                outbuffer.getData(), Consts.UTF_8);
 
         for (int i = 0; i < 10; i++) {
             Assert.assertEquals(s1, inbuffer.readLine());
@@ -486,9 +429,7 @@ public class TestSessionBuffers {
         }
         String s = buf.toString();
 
-        HttpParams params = new HttpCoreConfigBuilder().setHttpElementCharset("UTF-8").build();
-
-        SessionOutputBufferMock outbuffer = new SessionOutputBufferMock(params);
+        SessionOutputBufferMock outbuffer = new SessionOutputBufferMock(Consts.UTF_8);
 
         CharArrayBuffer chbuffer = new CharArrayBuffer(16);
         chbuffer.append(s);
@@ -496,7 +437,7 @@ public class TestSessionBuffers {
         outbuffer.flush();
 
         SessionInputBufferMock inbuffer = new SessionInputBufferMock(
-                outbuffer.getData(), params);
+                outbuffer.getData(), Consts.UTF_8);
 
         Assert.assertEquals(s, inbuffer.readLine());
     }
@@ -505,9 +446,7 @@ public class TestSessionBuffers {
     public void testNonAsciiReadWriteLine() throws Exception {
         String s1 = constructString(SWISS_GERMAN_HELLO);
 
-        HttpParams params = new HttpCoreConfigBuilder().setHttpElementCharset("ISO-8859-1").build();
-
-        SessionOutputBufferMock outbuffer = new SessionOutputBufferMock(params);
+        SessionOutputBufferMock outbuffer = new SessionOutputBufferMock(Consts.ISO_8859_1);
 
         CharArrayBuffer chbuffer = new CharArrayBuffer(16);
         for (int i = 0; i < 5; i++) {
@@ -526,8 +465,7 @@ public class TestSessionBuffers {
         Assert.assertEquals(expected, bytesWritten);
 
         SessionInputBufferMock inbuffer = new SessionInputBufferMock(
-                outbuffer.getData(),
-                params);
+                outbuffer.getData(), Consts.ISO_8859_1);
 
         CharArrayBuffer buf = new CharArrayBuffer(64);
         for (int i = 0; i < 10; i++) {
@@ -544,88 +482,60 @@ public class TestSessionBuffers {
         Assert.assertEquals(expected, bytesRead);
     }
 
-    @Test
-    public void testUnmappableInputAction() throws Exception {
-        String s = "In valid ISO-8859-1 character string because  of Ŵ and ŵ";
-        
-        // Action with report
-        HttpParams params = new HttpCoreConfigBuilder()
-            .setHttpElementCharset("ISO-8859-1")
-            .setUnmappableInputAction(CodingErrorAction.REPORT)
-            .build();
-
-        SessionOutputBufferMock outbuf = new SessionOutputBufferMock(params);
-        try {
-            outbuf.writeLine(s);
-            Assert.fail("Expected CharacterCodingException");
-        } catch (CharacterCodingException expected) {
-        }
-
-        // Action with ignore
-        params = new HttpCoreConfigBuilder()
-            .setHttpElementCharset("ISO-8859-1")
-            .setUnmappableInputAction(CodingErrorAction.IGNORE)
-            .build();
-        outbuf = new SessionOutputBufferMock(params);
-        try {
-            outbuf.writeLine(s);
-        } catch (CharacterCodingException e) {
-            Assert.fail("Unexpected CharacterCodingException");
-        }
+    @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);
+        outbuf.writeLine(s);
+    }
 
-        // Action with replace
-        params = new HttpCoreConfigBuilder()
-            .setHttpElementCharset("ISO-8859-1")
-            .setUnmappableInputAction(CodingErrorAction.REPLACE)
-            .build();
-        outbuf = new SessionOutputBufferMock(params);
-        try {
-            outbuf.writeLine(s);
-        } catch (IOException e) {
-            Assert.fail("Unexpected CharacterCodingException");
-        }
+    @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);
+        outbuf.writeLine(s);
+        outbuf.flush();
+        String result = new String(outbuf.getData(), "ISO-8859-1");
+        Assert.assertEquals("This text contains a circumflex ? !!!\r\n", result);
     }
 
     @Test
-    public void testMalformedInputAction() throws Exception {
-        byte[] tmp = constructString(SWISS_GERMAN_HELLO).getBytes("UTF-16");
-        CharArrayBuffer buf = new CharArrayBuffer(1);
+    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);
+        outbuf.writeLine(s);
+        outbuf.flush();
+        String result = new String(outbuf.getData(), "ISO-8859-1");
+        Assert.assertEquals("This text contains a circumflex  !!!\r\n", result);
+    }
 
-        // Action with report
-        HttpParams params = new HttpCoreConfigBuilder()
-            .setHttpElementCharset("UTF-8")
-            .setMalformedInputAction(CodingErrorAction.REPORT)
-            .build();
-        SessionInputBufferMock inbuffer = new SessionInputBufferMock(tmp, params);
-        try {
-            inbuffer.readLine(buf);
-            Assert.fail("Expected CharacterCodingException");
-        } catch (CharacterCodingException e) {
-        }
+    @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);
+        inbuffer.readLine();
+    }
 
-        // Action with replace
-        params = new HttpCoreConfigBuilder()
-            .setHttpElementCharset("UTF-8")
-            .setMalformedInputAction(CodingErrorAction.REPLACE)
-            .build();
-        inbuffer = new SessionInputBufferMock(tmp, params);
-        try {
-            inbuffer.readLine(buf);
-        } catch (CharacterCodingException e) {
-            Assert.fail("Unexpected CharacterCodingException");
-        }
+    @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);
+        String s = inbuffer.readLine();
+        Assert.assertEquals("Gr\ufffdezi_z\ufffdm\ufffd", s);
+    }
 
-        // Action with ignore
-        params = new HttpCoreConfigBuilder()
-            .setHttpElementCharset("UTF-8")
-            .setMalformedInputAction(CodingErrorAction.IGNORE)
-            .build();
-        inbuffer = new SessionInputBufferMock(tmp, params);
-        try {
-            inbuffer.readLine();
-        } catch (IOException e) {
-            Assert.fail("Unexpected CharacterCodingException");
-        }
+    @Test
+    public void testBufferMalformedInputActionIgnore() 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);
+        String s = inbuffer.readLine();
+        Assert.assertEquals("Grezi_zm", s);
     }
 
     @Test

Propchange: httpcomponents/httpcore/trunk/httpcore/src/test/java/org/apache/http/impl/io/TestSessionInOutBuffers.java
------------------------------------------------------------------------------
    svn:eol-style = native

Propchange: httpcomponents/httpcore/trunk/httpcore/src/test/java/org/apache/http/impl/io/TestSessionInOutBuffers.java
------------------------------------------------------------------------------
    svn:keywords = Date Revision

Propchange: httpcomponents/httpcore/trunk/httpcore/src/test/java/org/apache/http/impl/io/TestSessionInOutBuffers.java
------------------------------------------------------------------------------
    svn:mime-type = text/plain

Added: httpcomponents/httpcore/trunk/httpcore/src/test/java/org/apache/http/impl/io/TestSocketInputBuffer.java
URL: http://svn.apache.org/viewvc/httpcomponents/httpcore/trunk/httpcore/src/test/java/org/apache/http/impl/io/TestSocketInputBuffer.java?rev=1371968&view=auto
==============================================================================
--- httpcomponents/httpcore/trunk/httpcore/src/test/java/org/apache/http/impl/io/TestSocketInputBuffer.java (added)
+++ httpcomponents/httpcore/trunk/httpcore/src/test/java/org/apache/http/impl/io/TestSocketInputBuffer.java Sat Aug 11 16:08:54 2012
@@ -0,0 +1,44 @@
+/*
+ * ====================================================================
+ * 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.
+ * ====================================================================
+ *
+ * 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.io;
+
+import org.junit.Assert;
+import org.junit.Test;
+
+public class TestSocketInputBuffer {
+
+    @Test
+    public void testInvalidConstruction() throws Exception {
+        try {
+            SocketInputBuffer.create(null, 0);
+            Assert.fail("IllegalArgumentException expected");
+        } catch (IllegalArgumentException ex) {
+        }
+    }
+
+}

Propchange: httpcomponents/httpcore/trunk/httpcore/src/test/java/org/apache/http/impl/io/TestSocketInputBuffer.java
------------------------------------------------------------------------------
    svn:eol-style = native

Propchange: httpcomponents/httpcore/trunk/httpcore/src/test/java/org/apache/http/impl/io/TestSocketInputBuffer.java
------------------------------------------------------------------------------
    svn:keywords = Date Revision

Propchange: httpcomponents/httpcore/trunk/httpcore/src/test/java/org/apache/http/impl/io/TestSocketInputBuffer.java
------------------------------------------------------------------------------
    svn:mime-type = text/plain

Modified: httpcomponents/httpcore/trunk/httpcore/src/test/java/org/apache/http/impl/io/TestSocketOutputBuffer.java
URL: http://svn.apache.org/viewvc/httpcomponents/httpcore/trunk/httpcore/src/test/java/org/apache/http/impl/io/TestSocketOutputBuffer.java?rev=1371968&r1=1371967&r2=1371968&view=diff
==============================================================================
--- httpcomponents/httpcore/trunk/httpcore/src/test/java/org/apache/http/impl/io/TestSocketOutputBuffer.java (original)
+++ httpcomponents/httpcore/trunk/httpcore/src/test/java/org/apache/http/impl/io/TestSocketOutputBuffer.java Sat Aug 11 16:08:54 2012
@@ -35,11 +35,11 @@ import static org.mockito.Mockito.when;
 
 import java.io.OutputStream;
 import java.net.Socket;
+import java.nio.charset.Charset;
 
-import org.apache.http.params.CoreConnectionPNames;
-import org.apache.http.params.CoreProtocolPNames;
-import org.apache.http.params.HttpParams;
+import org.apache.http.Consts;
 import org.apache.http.util.CharArrayBuffer;
+import org.junit.Assert;
 import org.junit.Before;
 import org.junit.Test;
 import org.mockito.Mock;
@@ -51,7 +51,6 @@ public class TestSocketOutputBuffer {
 
     @Mock private Socket socket;
     @Mock private OutputStream os;
-    @Mock private HttpParams params;
     private byte[] b;
     private CharArrayBuffer cb;
 
@@ -62,13 +61,24 @@ public class TestSocketOutputBuffer {
         when(socket.getOutputStream()).thenReturn(os);
     }
 
-    private void create(int buffSize, int arraySize, int minChunkLimit) throws Exception {
+    private void create(int buffSize, int arraySize, Charset charset, int minChunkLimit) throws Exception {
         b = new byte[arraySize];
         cb = new CharArrayBuffer(arraySize);
 
-        when(params.getIntParameter(CoreConnectionPNames.MIN_CHUNK_LIMIT, 512)).thenReturn(minChunkLimit);
+        sob = SocketOutputBuffer.create(socket, buffSize, charset, minChunkLimit, null, null);
+    }
+
+    private void create(int buffSize, int arraySize, int minChunkLimit) throws Exception {
+        create(buffSize, arraySize, null, minChunkLimit);
+    }
 
-        sob = new SocketOutputBuffer(socket, buffSize, params);
+    @Test
+    public void testInvalidConstruction() throws Exception {
+        try {
+            SocketOutputBuffer.create(null, 0);
+            Assert.fail("IllegalArgumentException expected");
+        } catch (IllegalArgumentException ex) {
+        }
     }
 
     @Test
@@ -128,8 +138,7 @@ public class TestSocketOutputBuffer {
 
     @Test
     public void testWriteLineStringEncode() throws Exception {
-        when(params.getParameter(CoreProtocolPNames.HTTP_ELEMENT_CHARSET)).thenReturn("UTF-8");
-        create(2048, 2048, 2048);
+        create(2048, 2048, Consts.UTF_8, 2048);
 
         sob.writeLine("test");
     }
@@ -143,8 +152,7 @@ public class TestSocketOutputBuffer {
 
     @Test
     public void testWriteLineEmptyStringEncode() throws Exception {
-        when(params.getParameter(CoreProtocolPNames.HTTP_ELEMENT_CHARSET)).thenReturn("UTF-8");
-        create(2048, 2048, 2048);
+        create(2048, 2048, Consts.UTF_8, 2048);
 
         sob.writeLine("");
     }
@@ -158,8 +166,7 @@ public class TestSocketOutputBuffer {
 
     @Test
     public void testWriteLineNullStringEncode() throws Exception {
-        when(params.getParameter(CoreProtocolPNames.HTTP_ELEMENT_CHARSET)).thenReturn("UTF-8");
-        create(2048, 2048, 2048);
+        create(2048, 2048, Consts.UTF_8, 2048);
 
         sob.writeLine((String)null);
     }
@@ -173,8 +180,7 @@ public class TestSocketOutputBuffer {
 
     @Test
     public void testWriteLineCharArrayBufferEncode() throws Exception {
-        when(params.getParameter(CoreProtocolPNames.HTTP_ELEMENT_CHARSET)).thenReturn("UTF-8");
-        create(2048, 2048, 2048);
+        create(2048, 2048, Consts.UTF_8, 2048);
 
         sob.writeLine(cb);
     }
@@ -188,8 +194,7 @@ public class TestSocketOutputBuffer {
 
     @Test
     public void testWriteLineEmptyCharArrayBufferEncode() throws Exception {
-        when(params.getParameter(CoreProtocolPNames.HTTP_ELEMENT_CHARSET)).thenReturn("UTF-8");
-        create(2048, 0, 2048);
+        create(2048, 0, Consts.UTF_8, 2048);
 
         sob.writeLine(cb);
     }



Mime
View raw message