Author: mturk
Date: Thu Aug 18 17:49:44 2011
New Revision: 1159327
URL: http://svn.apache.org/viewvc?rev=1159327&view=rev
Log:
Implement memory stream read api
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
commons/sandbox/runtime/trunk/src/main/test/org/apache/commons/runtime/TestMemoryStream.java
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=1159327&r1=1159326&r2=1159327&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
Thu Aug 18 17:49:44 2011
@@ -50,6 +50,12 @@ public final class MemoryStream extends
private static native int length0(long sb);
private static native int read0(long sb, int pos);
+ private static native int read1(long sb, int pos, byte[] buf,
int off, int len)
+ throws IOException;
+ private static native int read2(long sb, int pos, long ptr, int
len)
+ throws IOException;
+ private static native int read3(long sb, int pos, ByteBuffer
buf, int off, int len)
+ throws IOException;
// Native write methods
private static native int write0(long sb, int ch)
@@ -96,6 +102,8 @@ public final class MemoryStream extends
public synchronized int available()
throws IOException
{
+ if (closed())
+ throw new ClosedDescriptorException();
return length0(sb) - rdpos;
}
@@ -131,6 +139,8 @@ public final class MemoryStream extends
public void flush()
throws SyncFailedException, IOException
{
+ if (closed())
+ throw new ClosedDescriptorException();
flush0(sb);
}
@@ -151,6 +161,8 @@ public final class MemoryStream extends
public void sync()
throws SyncFailedException, IOException
{
+ if (closed())
+ throw new ClosedDescriptorException();
flush0(sb);
}
@@ -213,6 +225,8 @@ public final class MemoryStream extends
public synchronized long skip(long count)
throws IOException
{
+ if (closed())
+ throw new ClosedDescriptorException();
if (count < 0L || count > Integer.MAX_VALUE)
throw new InvalidArgumentException();
long len = length0(sb);
@@ -244,6 +258,8 @@ public final class MemoryStream extends
public synchronized boolean eof()
throws IOException
{
+ if (closed())
+ throw new ClosedDescriptorException();
return rdpos >= length0(sb);
}
@@ -251,6 +267,8 @@ public final class MemoryStream extends
public synchronized int read()
throws IOException
{
+ if (closed())
+ throw new ClosedDescriptorException();
int rv = read0(sb, rdpos);
if (rv != -1)
rdpos++;
@@ -261,21 +279,36 @@ public final class MemoryStream extends
public synchronized int read(byte[] buffer, int offset, int count)
throws IndexOutOfBoundsException, IOException
{
- return 0;
+ if (closed())
+ throw new ClosedDescriptorException();
+ int rd = read1(sb, rdpos, buffer, offset, count);
+ if (rd > 0)
+ rdpos += rd;
+ return rd;
}
@Override
public synchronized int read(long address, int count)
throws NullPointerException, IndexOutOfBoundsException, IOException
{
- return 0;
+ if (closed())
+ throw new ClosedDescriptorException();
+ int rd = read2(sb, rdpos, address, count);
+ if (rd > 0)
+ rdpos += rd;
+ return rd;
}
@Override
public synchronized int read(ByteBuffer buffer)
throws IndexOutOfBoundsException, IOException
{
- return 0;
+ if (closed())
+ throw new ClosedDescriptorException();
+ int rd = read3(sb, rdpos, buffer, buffer.position(),
buffer.remaining());
+ if (rd > 0)
+ rdpos += rd;
+ return rd;
}
// === Writer methods
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=1159327&r1=1159326&r2=1159327&view=diff
==============================================================================
--- commons/sandbox/runtime/trunk/src/main/native/shared/memstream.c (original)
+++ commons/sandbox/runtime/trunk/src/main/native/shared/memstream.c Thu Aug 18
17:49:44 2011
@@ -119,6 +119,85 @@ ACR_IO_EXPORT(jint, MemoryStream, read0)
return (unsigned char)sb->s_buf[pos];
}
+ACR_IO_EXPORT(jint, MemoryStream, read1)(JNI_STDARGS, jlong sh, jint pos,
+ jbyteArray buf,
+ jint off, jint len)
+{
+ int rd, rc = 0;
+ char *bb = 0;
+ acr_sb_t *sb = J2P(sh, acr_sb_t *);
+
+ bb = JARRAY_CRITICAL(char, buf);
+ if (bb != 0) {
+ if (pos + len > sb->s_len)
+ rd = sb->s_len - pos;
+ else
+ rd = len;
+ if (rd > 0)
+ memcpy(bb + off, sb->s_buf + pos, rd);
+ else
+ rd = -1;
+ }
+ else
+ rc = ACR_EINVAL;
+ RELEASE_CRITICAL(buf, bb);
+ if (rc != 0) {
+ rd = -1;
+ ACR_THROW_EIO_ERROR(rc);
+ }
+ return rd;
+}
+
+ACR_IO_EXPORT(jint, MemoryStream, read2)(JNI_STDARGS, jlong sh, jint pos,
+ jlong bp, jint len)
+{
+ int rd;
+ char *bb = J2P(bp, char *);
+ acr_sb_t *sb = J2P(sh, acr_sb_t *);
+
+ if (bb != 0) {
+ if (pos + len > sb->s_len)
+ rd = sb->s_len - pos;
+ else
+ rd = len;
+ if (rd > 0)
+ memcpy(bb, sb->s_buf + pos, rd);
+ else
+ rd = -1;
+ }
+ else {
+ rd = -1;
+ ACR_THROW_EIO_ERROR(ACR_EINVAL);
+ }
+ return rd;
+}
+
+ACR_IO_EXPORT(jint, MemoryStream, read3)(JNI_STDARGS, jlong sh, jint pos,
+ jobject buf,
+ jint off, jint len)
+{
+ int rd;
+ char *bb = 0;
+ acr_sb_t *sb = J2P(sh, acr_sb_t *);
+
+ bb = (char *)(*env)->GetDirectBufferAddress(env, buf);
+ if (bb != 0) {
+ if (pos + len > sb->s_len)
+ rd = sb->s_len - pos;
+ else
+ rd = len;
+ if (rd > 0)
+ memcpy(bb + off, sb->s_buf + pos, rd);
+ else
+ rd = -1;
+ }
+ else {
+ rd = -1;
+ ACR_THROW_EIO_ERROR(ACR_EINVAL);
+ }
+ return rd;
+}
+
ACR_IO_EXPORT(jint, MemoryStream, write0)(JNI_STDARGS, jlong sh, jint b)
{
int rc;
@@ -128,7 +207,7 @@ ACR_IO_EXPORT(jint, MemoryStream, write0
rc = AcrSbPutc(sb, b);
if (rc != 0) {
wr = -1;
- ACR_THROW_NET_ERROR(rc);
+ ACR_THROW_EIO_ERROR(rc);
}
return wr;
}
@@ -150,7 +229,7 @@ ACR_IO_EXPORT(jint, MemoryStream, write1
RELEASE_CRITICAL(buf, bb);
if (rc != 0) {
wr = -1;
- ACR_THROW_NET_ERROR(rc);
+ ACR_THROW_EIO_ERROR(rc);
}
return wr;
}
@@ -169,7 +248,7 @@ ACR_IO_EXPORT(jint, MemoryStream, write2
rc = AcrSbCatb(sb, bb, len);
if (rc != 0) {
wr = -1;
- ACR_THROW_NET_ERROR(rc);
+ ACR_THROW_EIO_ERROR(rc);
}
return wr;
}
@@ -190,7 +269,7 @@ ACR_IO_EXPORT(jint, MemoryStream, write3
rc = AcrSbCatb(sb, bb + off, len);
if (rc != 0) {
wr = -1;
- ACR_THROW_NET_ERROR(rc);
+ ACR_THROW_EIO_ERROR(rc);
}
return wr;
}
@@ -226,7 +305,7 @@ ACR_IO_EXPORT(jint, MemoryStream, write4
}
if (rc != 0) {
wr = -1;
- ACR_THROW_NET_ERROR(rc);
+ ACR_THROW_EIO_ERROR(rc);
}
return wr;
}
@@ -261,7 +340,7 @@ ACR_IO_EXPORT(jint, MemoryStream, write5
}
if (rc != 0) {
wr = -1;
- ACR_THROW_NET_ERROR(rc);
+ ACR_THROW_EIO_ERROR(rc);
}
return wr;
}
Modified:
commons/sandbox/runtime/trunk/src/main/test/org/apache/commons/runtime/TestMemoryStream.java
URL:
http://svn.apache.org/viewvc/commons/sandbox/runtime/trunk/src/main/test/org/apache/commons/runtime/TestMemoryStream.java?rev=1159327&r1=1159326&r2=1159327&view=diff
==============================================================================
---
commons/sandbox/runtime/trunk/src/main/test/org/apache/commons/runtime/TestMemoryStream.java
(original)
+++
commons/sandbox/runtime/trunk/src/main/test/org/apache/commons/runtime/TestMemoryStream.java
Thu Aug 18 17:49:44 2011
@@ -28,13 +28,27 @@ public class TestMemoryStream extends As
throws Throwable
{
MemoryStream s = new MemoryStream();
- s.write(32);
- assertEquals(s.read(), 32);
+ s.write(230);
+ assertEquals(s.read(), 230);
assertEquals(s.read(), -1);
assertTrue(s.eof());
- s.write(33);
+ s.write(3);
assertFalse(s.eof());
s.close();
}
+ @Test(groups = { "core" })
+ public void simpleReadArray()
+ throws Throwable
+ {
+ MemoryStream s = new MemoryStream();
+ s.write(230);
+ s.write(3);
+ byte[] b = new byte[8];
+ assertEquals(s.read(b), 2);
+ assertEquals(s.read(b), -1);
+ assertTrue(s.eof());
+ s.close();
+ }
+
}