Author: mturk
Date: Fri Aug 19 10:36:49 2011
New Revision: 1159584

URL: http://svn.apache.org/viewvc?rev=1159584&view=rev
Log:
Add position and flip methods

Modified:
    
commons/sandbox/runtime/trunk/src/main/java/org/apache/commons/runtime/io/MemoryStream.java
    commons/sandbox/runtime/trunk/src/main/native/shared/memstream.c

Modified: 
commons/sandbox/runtime/trunk/src/main/java/org/apache/commons/runtime/io/MemoryStream.java
URL: 
http://svn.apache.org/viewvc/commons/sandbox/runtime/trunk/src/main/java/org/apache/commons/runtime/io/MemoryStream.java?rev=1159584&r1=1159583&r2=1159584&view=diff
==============================================================================
--- 
commons/sandbox/runtime/trunk/src/main/java/org/apache/commons/runtime/io/MemoryStream.java
 (original)
+++ 
commons/sandbox/runtime/trunk/src/main/java/org/apache/commons/runtime/io/MemoryStream.java
 Fri Aug 19 10:36:49 2011
@@ -23,14 +23,15 @@ import java.io.SyncFailedException;
 import java.nio.ByteBuffer;
 import org.apache.commons.runtime.InvalidArgumentException;
 import org.apache.commons.runtime.Pointer;
+import org.apache.commons.runtime.OverflowException;
 import org.apache.commons.runtime.util.Utils;
 
 /**
  * Bidirectional Memory Stream.
  * <p>
  * This class uses native heap memory for internal data buffer and its
- * capacity is limited to {@code Integer.MAX_VALUE} bytes. Stream dynamically
- * allocates and extends the memory and as needed.
+ * capacity is limited to {@code Integer.MAX_VALUE - 65536} bytes.
+ * Stream dynamically allocates and extends the memory as needed.
  * </p>
  */
 public final class MemoryStream extends Stream
@@ -47,7 +48,9 @@ public final class MemoryStream extends 
     private static native void          flush0(long sb);
     private static native void          finish0(long sb);
     private static native boolean       finished0(long sb);
-    private static native int           length0(long sb);
+    private static native int           length0(long sb)
+        throws OverflowException;
+    private static native int           setpos0(long sb, int pos);
 
     private static native int           read0(long sb, int pos);
     private static native int           read1(long sb, int pos, byte[] buf, 
int off, int len)
@@ -71,6 +74,12 @@ public final class MemoryStream extends 
     private static native long          write5(long sb, ByteBuffer[] buf, int 
off, int len)
         throws IOException;
 
+
+    private static native Pointer       pointer0(long sb, int off)
+        throws InvalidRangeException;
+    private static native Pointer       pointer1(long sb, int off)
+        throws InvalidRangeException;
+    
     /**
      * Creates a new memory stream instance with default buffer size.
      */
@@ -99,6 +108,7 @@ public final class MemoryStream extends 
      * @throws IOException
      *             if an error occurs in this stream.
      */
+    @Override
     public synchronized int available()
         throws IOException
     {
@@ -114,6 +124,7 @@ public final class MemoryStream extends 
      * @throws IOException
      *              if any error occurs when closing the object.
      */
+    @Override
     public synchronized void close()
         throws IOException
     {
@@ -136,6 +147,7 @@ public final class MemoryStream extends 
      * @throws SyncFailedException when the object cannot be flushed.
      * @throws IOException if an I/O error occurs.
      */
+    @Override
     public void flush()
         throws SyncFailedException, IOException
     {
@@ -158,6 +170,7 @@ public final class MemoryStream extends 
      * @throws SyncFailedException when the object cannot be flushed.
      * @throws IOException if an I/O error occurs.
      */
+    @Override
     public void sync()
         throws SyncFailedException, IOException
     {
@@ -179,6 +192,7 @@ public final class MemoryStream extends 
      *         open file, socket, or other I/O object; {@code false} otherwse.
      *
      */
+    @Override
     public synchronized boolean valid()
     {
         return sb != 0L;
@@ -193,6 +207,7 @@ public final class MemoryStream extends 
      *
      * @throws IOException if an I/O error occurs.
      */
+    @Override
     public boolean isBlocking()
         throws IOException
     {
@@ -222,6 +237,7 @@ public final class MemoryStream extends 
      * @throws IOException
      *          If some other I/O error occurs.
      */
+    @Override
     public synchronized long skip(long count)
         throws IOException
     {
@@ -367,4 +383,46 @@ public final class MemoryStream extends 
         return write5(sb, array, offset, count);
     }
 
+    /**
+     * Return this stream's read position.
+     */
+    public synchronized final int position()
+    {
+        return rdpos;
+    }
+
+    /**
+     * Sets this stream's read position.
+     */
+    public synchronized final MemoryStream position(int newPos)
+        throws IllegalArgumentException, IOException
+    {
+        if (closed())
+            throw new ClosedDescriptorException();
+        if (newPos < 0 || newPos > length0(sb))
+            throw new IllegalArgumentException();
+        rdpos = newPos;
+        return this;
+    }
+
+    /**
+     * Rewinds this stream read position.
+     */
+    public synchronized final void rewind()
+    {
+        rdpos = 0;
+    }
+
+    /**
+     * Flips this stream.
+     */
+    public synchronized final MemoryStream flip()
+    {
+        if (valid()) {
+            setpos0(sb, rdpos);
+            rdpos = 0;
+        }
+        return this;
+    }
+
 }

Modified: commons/sandbox/runtime/trunk/src/main/native/shared/memstream.c
URL: 
http://svn.apache.org/viewvc/commons/sandbox/runtime/trunk/src/main/native/shared/memstream.c?rev=1159584&r1=1159583&r2=1159584&view=diff
==============================================================================
--- commons/sandbox/runtime/trunk/src/main/native/shared/memstream.c (original)
+++ commons/sandbox/runtime/trunk/src/main/native/shared/memstream.c Fri Aug 19 
10:36:49 2011
@@ -40,16 +40,48 @@ ACR_IO_EXPORT(void, MemoryStream, clear0
     AcrSbClear(sb);
 }
 
-ACR_IO_EXPORT(jlong, MemoryStream, detach0)(JNI_STDARGS, jlong sh)
+ACR_IO_EXPORT(jlong, MemoryStream, data0)(JNI_STDARGS, jlong sh)
 {
     acr_sb_t *sb = J2P(sh, acr_sb_t *);
-    return P2J(AcrSbDetach(sb));
+    return P2J(AcrSbData(sb));
 }
 
-ACR_IO_EXPORT(jlong, MemoryStream, data0)(JNI_STDARGS, jlong sh)
+ACR_IO_EXPORT(jobject, MemoryStream, pointer0)(JNI_STDARGS, jlong sh, jint pos)
 {
+    char   *buf;
+    int     len;
     acr_sb_t *sb = J2P(sh, acr_sb_t *);
-    return P2J(AcrSbData(sb));
+
+    len = AcrSbLen(sb);
+    if (pos >= len) {
+        ACR_THROW(ACR_EX_ERANGE, 0);
+        return 0;
+    }
+    buf = AcrSbDetach(sb);
+    AcrSbInit(sb, 0, 32, 0);
+    sb->s_flags |= ACR_SB_DYNSTRUCT;
+    return AcrNewHeapPointer(env, buf + pos, len - pos);
+}
+
+ACR_IO_EXPORT(jobject, MemoryStream, pointer1)(JNI_STDARGS, jlong sh, jint pos)
+{
+    char   *buf;
+    int     len;
+    acr_sb_t *sb = J2P(sh, acr_sb_t *);
+
+    len = AcrSbLen(sb);
+    buf = AcrSbData(sb);
+    if (pos >= len) {
+        ACR_THROW(ACR_EX_ERANGE, 0);
+        return 0;
+    }
+    return AcrNewConstPointer(env, buf + pos, len - pos);
+}
+
+ACR_IO_EXPORT(jlong, MemoryStream, detach0)(JNI_STDARGS, jlong sh)
+{
+    acr_sb_t *sb = J2P(sh, acr_sb_t *);
+    return P2J(AcrSbDetach(sb));
 }
 
 ACR_IO_EXPORT(jlong, MemoryStream, detach1)(JNI_STDARGS, jlong sh, jint len)
@@ -64,8 +96,12 @@ ACR_IO_EXPORT(jlong, MemoryStream, detac
 
 ACR_IO_EXPORT(jint, MemoryStream, length0)(JNI_STDARGS, jlong sh)
 {
+    int len;
     acr_sb_t *sb = J2P(sh, acr_sb_t *);
-    return AcrSbLen(sb);
+
+    if ((len = AcrSbLen(sb)) == -1)
+        ACR_THROW(ACR_EX_EOVERFLOW, 0);
+    return len;
 }
 
 ACR_IO_EXPORT(jint, MemoryStream, capacity0)(JNI_STDARGS, jlong sh)
@@ -138,8 +174,10 @@ ACR_IO_EXPORT(jint, MemoryStream, read1)
         else
             rd = -1;
     }
-    else
+    else {
         rc = ACR_EINVAL;
+        rd = -1;
+    }
     RELEASE_CRITICAL(buf, bb);
     if (rc != 0) {
         rd = -1;
@@ -204,7 +242,7 @@ ACR_IO_EXPORT(jint, MemoryStream, write0
     int wr = 1;
     acr_sb_t *sb = J2P(sh, acr_sb_t *);
 
-    rc = AcrSbPutc(sb, b);
+    rc = AcrSbPutb(sb, b);
     if (rc != 0) {
         wr = -1;
         ACR_THROW_EIO_ERROR(rc);


Reply via email to