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 2008/02/25 11:44:27 UTC

svn commit: r630797 - in /httpcomponents/httpcore/trunk/module-nio/src: main/java/org/apache/http/nio/util/BufferInfo.java main/java/org/apache/http/nio/util/ExpandableBuffer.java test/java/org/apache/http/nio/util/TestBuffers.java

Author: olegk
Date: Mon Feb 25 02:44:25 2008
New Revision: 630797

URL: http://svn.apache.org/viewvc?rev=630797&view=rev
Log:
HTTPCORE-92: Added optional BufferInfo interface intended to obtain information about buffer capacity, content length and available space

Added:
    httpcomponents/httpcore/trunk/module-nio/src/main/java/org/apache/http/nio/util/BufferInfo.java   (with props)
Modified:
    httpcomponents/httpcore/trunk/module-nio/src/main/java/org/apache/http/nio/util/ExpandableBuffer.java
    httpcomponents/httpcore/trunk/module-nio/src/test/java/org/apache/http/nio/util/TestBuffers.java

Added: httpcomponents/httpcore/trunk/module-nio/src/main/java/org/apache/http/nio/util/BufferInfo.java
URL: http://svn.apache.org/viewvc/httpcomponents/httpcore/trunk/module-nio/src/main/java/org/apache/http/nio/util/BufferInfo.java?rev=630797&view=auto
==============================================================================
--- httpcomponents/httpcore/trunk/module-nio/src/main/java/org/apache/http/nio/util/BufferInfo.java (added)
+++ httpcomponents/httpcore/trunk/module-nio/src/main/java/org/apache/http/nio/util/BufferInfo.java Mon Feb 25 02:44:25 2008
@@ -0,0 +1,49 @@
+/*
+ * $HeadURL$
+ * $Revision$
+ * $Date$
+ *
+ * ====================================================================
+ * 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.nio.util;
+
+/**
+ * Basic buffer properties.
+ *
+ * @author <a href="mailto:oleg at ural.ru">Oleg Kalnichevski</a>
+ *
+ * @since 4.0-beta2
+ */
+public interface BufferInfo {
+
+    int length();
+
+    int capacity();
+
+    int available();
+
+}

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

Propchange: httpcomponents/httpcore/trunk/module-nio/src/main/java/org/apache/http/nio/util/BufferInfo.java
------------------------------------------------------------------------------
    svn:keywords = Date Author Id Revision HeadURL

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

Modified: httpcomponents/httpcore/trunk/module-nio/src/main/java/org/apache/http/nio/util/ExpandableBuffer.java
URL: http://svn.apache.org/viewvc/httpcomponents/httpcore/trunk/module-nio/src/main/java/org/apache/http/nio/util/ExpandableBuffer.java?rev=630797&r1=630796&r2=630797&view=diff
==============================================================================
--- httpcomponents/httpcore/trunk/module-nio/src/main/java/org/apache/http/nio/util/ExpandableBuffer.java (original)
+++ httpcomponents/httpcore/trunk/module-nio/src/main/java/org/apache/http/nio/util/ExpandableBuffer.java Mon Feb 25 02:44:25 2008
@@ -33,7 +33,7 @@
 
 import java.nio.ByteBuffer;
 
-public class ExpandableBuffer {
+public class ExpandableBuffer implements BufferInfo {
     
     public final static int INPUT_MODE = 0;
     public final static int OUTPUT_MODE = 1;
@@ -106,6 +106,11 @@
     
     public int length() {
         setOutputMode();
+        return this.buffer.remaining();
+    }
+    
+    public int available() {
+        setInputMode();
         return this.buffer.remaining();
     }
     

Modified: httpcomponents/httpcore/trunk/module-nio/src/test/java/org/apache/http/nio/util/TestBuffers.java
URL: http://svn.apache.org/viewvc/httpcomponents/httpcore/trunk/module-nio/src/test/java/org/apache/http/nio/util/TestBuffers.java?rev=630797&r1=630796&r2=630797&view=diff
==============================================================================
--- httpcomponents/httpcore/trunk/module-nio/src/test/java/org/apache/http/nio/util/TestBuffers.java (original)
+++ httpcomponents/httpcore/trunk/module-nio/src/test/java/org/apache/http/nio/util/TestBuffers.java Mon Feb 25 02:44:25 2008
@@ -135,6 +135,20 @@
         assertEquals("stuff;more stuff", EncodingUtils.getAsciiString(content));
     }
 
+    public void testBufferInfo() throws Exception {
+        SimpleOutputBuffer buffer = new SimpleOutputBuffer(8, new DirectByteBufferAllocator());
+        BufferInfo bufferinfo = (BufferInfo) buffer; 
+        
+        assertEquals(0, bufferinfo.length());
+        assertEquals(8, bufferinfo.available());
+        buffer.write(new byte[] {'1', '2', '3', '4'});
+        assertEquals(4, bufferinfo.length());
+        assertEquals(4, bufferinfo.available());
+        buffer.write(new byte[] {'1', '2', '3', '4', '5', '6', '7', '8'});
+        assertEquals(12, bufferinfo.length());
+        assertEquals(0, bufferinfo.available());
+    }
+    
     public void testInputBufferNullInput() throws IOException {
         SimpleInputBuffer buffer = new SimpleInputBuffer(4, new DirectByteBufferAllocator());
         assertEquals(0, buffer.read(null));