You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@mina.apache.org by rb...@apache.org on 2013/07/01 14:35:53 UTC

[2/5] git commit: added asInputStream in IoBuffer and some documentation

added asInputStream in IoBuffer and some documentation


Project: http://git-wip-us.apache.org/repos/asf/mina/repo
Commit: http://git-wip-us.apache.org/repos/asf/mina/commit/68ca8151
Tree: http://git-wip-us.apache.org/repos/asf/mina/tree/68ca8151
Diff: http://git-wip-us.apache.org/repos/asf/mina/diff/68ca8151

Branch: refs/heads/trunk
Commit: 68ca8151b70a33e0bc312518d53b5ceb7142cf8d
Parents: fb03fe6
Author: Raphaël P. Barazzutti <rb...@apache.org>
Authored: Sun Jun 30 11:50:02 2013 +0200
Committer: Raphaël P. Barazzutti <rb...@apache.org>
Committed: Sun Jun 30 11:50:02 2013 +0200

----------------------------------------------------------------------
 .../java/org/apache/mina/codec/IoBuffer.java    | 79 +++++++++++++++-----
 1 file changed, 61 insertions(+), 18 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/mina/blob/68ca8151/codec/src/main/java/org/apache/mina/codec/IoBuffer.java
----------------------------------------------------------------------
diff --git a/codec/src/main/java/org/apache/mina/codec/IoBuffer.java b/codec/src/main/java/org/apache/mina/codec/IoBuffer.java
index cf127b7..a768394 100644
--- a/codec/src/main/java/org/apache/mina/codec/IoBuffer.java
+++ b/codec/src/main/java/org/apache/mina/codec/IoBuffer.java
@@ -18,6 +18,8 @@
  */
 package org.apache.mina.codec;
 
+import java.io.IOException;
+import java.io.InputStream;
 import java.nio.BufferUnderflowException;
 import java.nio.ByteBuffer;
 import java.nio.ByteOrder;
@@ -32,6 +34,7 @@ import java.nio.ReadOnlyBufferException;
  * @author <a href="http://mina.apache.org">Apache MINA Project</a>
  */
 public final class IoBuffer {
+
     /**
      * @see ByteBuffer#allocate(int)
      */
@@ -73,6 +76,12 @@ public final class IoBuffer {
         return wrap(ByteBuffer.wrap(array, offset, length));
     }
 
+    /**
+     * Build new IoBuffer containing 
+     * 
+     * @param buffers
+     * @return the new {@link IoBuffer}
+     */
     public static IoBuffer wrap(ByteBuffer... buffers) {
         IoBuffer ioBuffer = new IoBuffer();
         for (ByteBuffer b : buffers) {
@@ -107,8 +116,6 @@ public final class IoBuffer {
         mark = null;
     }
 
-    
-
     /**
      * Add one or more ByteBuffer to the current IoBuffer
      * 
@@ -149,6 +156,25 @@ public final class IoBuffer {
     }
 
     /**
+     * Provides an input stream which is actually reading the {@link IoBuffer}
+     * instance.
+     * <p>
+     * Further reads on the returned inputstream move the reading head of the {@link IoBuffer}
+     * instance used for it's creation</i>
+     *
+     * @return an input stream
+     */
+    public InputStream asInputStream() {
+        return new InputStream() {
+
+            @Override
+            public int read() throws IOException {
+                return hasRemaining() ? get() & 0xff : -1;
+            }
+        };
+    }
+
+    /**
      * @see ByteBuffer#asReadOnlyBuffer()
      */
     public IoBuffer asReadOnlyBuffer() {
@@ -187,6 +213,11 @@ public final class IoBuffer {
         return this;
     }
 
+    /**
+     * Returns a copy of the current {@link IoBuffer}, with an independent copy if the postion, limit and mark.
+     * 
+     * @return the copied {@link IoBuffer}
+     */
     public IoBuffer duplicate() {
         IoBuffer buffer = new IoBuffer();
 
@@ -228,6 +259,9 @@ public final class IoBuffer {
         }
     }
 
+    /**
+     * {@inheritDoc}
+     */
     @Override
     public boolean equals(Object ob) {
         if (this == ob) {
@@ -249,6 +283,12 @@ public final class IoBuffer {
         return true;
     }
 
+    /**
+     * Extends the current IoBuffer capacity.
+     * 
+     * @param size the number of bytes to extend the current IoBuffer 
+     * @return the current {@link IoBuffer}
+     */
     public IoBuffer extend(int size) {
         ByteBuffer extension = isDirect() ? ByteBuffer.allocateDirect(size) : ByteBuffer.allocate(size);
         add(extension);
@@ -486,6 +526,9 @@ public final class IoBuffer {
         return out;
     }
 
+    /**
+     * {@inheritDoc}
+     */
     @Override
     public int hashCode() {
         int hash = 0;
@@ -735,6 +778,17 @@ public final class IoBuffer {
     }
 
     /**
+     * @see ByteBuffer#putLong(int, int)
+     */
+    public IoBuffer putLong(int index, long value) {
+        int oldPos = position();
+        position(index);
+        putLong(value);
+        position(oldPos);
+        return this;
+    }
+
+    /**
      * @see ByteBuffer#putLong(long)
      */
     public IoBuffer putLong(long value) {
@@ -750,12 +804,12 @@ public final class IoBuffer {
     }
 
     /**
-     * @see ByteBuffer#putLong(int, int)
+     * @see ByteBuffer#putShort(int, short)
      */
-    public IoBuffer putLong(int index, long value) {
+    public IoBuffer putShort(int index, short value) {
         int oldPos = position();
         position(index);
-        putLong(value);
+        putShort(value);
         position(oldPos);
         return this;
     }
@@ -775,17 +829,6 @@ public final class IoBuffer {
     }
 
     /**
-     * @see ByteBuffer#putShort(int, short)
-     */
-    public IoBuffer putShort(int index, short value) {
-        int oldPos = position();
-        position(index);
-        putShort(value);
-        position(oldPos);
-        return this;
-    }
-
-    /**
      * @see ByteBuffer#remaining()
      */
     public int remaining() {
@@ -917,11 +960,11 @@ public final class IoBuffer {
             return position - node.getOffset();
         }
 
-        public void setNode(BufferNode node) {
+        private void setNode(BufferNode node) {
             this.node = node;
         }
 
-        public void setPosition(int position) {
+        private void setPosition(int position) {
             this.position = position;
         }