[jboss-remoting-commits] JBoss Remoting SVN: r4016 - remoting3/trunk/util/src/main/java/org/jboss/cx/remoting/util.

jboss-remoting-commits at lists.jboss.org jboss-remoting-commits at lists.jboss.org
Fri Apr 18 17:37:17 EDT 2008


Author: david.lloyd at jboss.com
Date: 2008-04-18 17:37:17 -0400 (Fri, 18 Apr 2008)
New Revision: 4016

Added:
   remoting3/trunk/util/src/main/java/org/jboss/cx/remoting/util/AbstractOutputStreamByteMessageOutput.java
   remoting3/trunk/util/src/main/java/org/jboss/cx/remoting/util/InputStreamByteMessageInput.java
Log:
Add base classes for byte message I/O

Added: remoting3/trunk/util/src/main/java/org/jboss/cx/remoting/util/AbstractOutputStreamByteMessageOutput.java
===================================================================
--- remoting3/trunk/util/src/main/java/org/jboss/cx/remoting/util/AbstractOutputStreamByteMessageOutput.java	                        (rev 0)
+++ remoting3/trunk/util/src/main/java/org/jboss/cx/remoting/util/AbstractOutputStreamByteMessageOutput.java	2008-04-18 21:37:17 UTC (rev 4016)
@@ -0,0 +1,46 @@
+package org.jboss.cx.remoting.util;
+
+import java.io.OutputStream;
+import java.io.IOException;
+
+/**
+ *
+ */
+public abstract class AbstractOutputStreamByteMessageOutput implements ByteMessageOutput {
+    private final OutputStream outputStream;
+    private int count;
+
+    protected AbstractOutputStreamByteMessageOutput(final OutputStream outputStream) {
+        if (outputStream == null) {
+            throw new NullPointerException("outputStream is null");
+        }
+        this.outputStream = outputStream;
+    }
+
+    public void write(final int b) throws IOException {
+        outputStream.write(b);
+        count ++;
+    }
+
+    public void write(final byte[] b) throws IOException {
+        outputStream.write(b);
+        count += b.length;
+    }
+
+    public void write(final byte[] b, final int offs, final int len) throws IOException {
+        outputStream.write(b, offs, len);
+        count += len;
+    }
+
+    public int getBytesWritten() throws IOException {
+        return count;
+    }
+
+    public void close() throws IOException {
+        outputStream.close();
+    }
+
+    public void flush() throws IOException {
+        outputStream.flush();
+    }
+}

Added: remoting3/trunk/util/src/main/java/org/jboss/cx/remoting/util/InputStreamByteMessageInput.java
===================================================================
--- remoting3/trunk/util/src/main/java/org/jboss/cx/remoting/util/InputStreamByteMessageInput.java	                        (rev 0)
+++ remoting3/trunk/util/src/main/java/org/jboss/cx/remoting/util/InputStreamByteMessageInput.java	2008-04-18 21:37:17 UTC (rev 4016)
@@ -0,0 +1,51 @@
+package org.jboss.cx.remoting.util;
+
+import java.io.IOException;
+import java.io.InputStream;
+
+/**
+ *
+ */
+public class InputStreamByteMessageInput implements ByteMessageInput {
+    private final InputStream inputStream;
+    private int remaining;
+
+    public InputStreamByteMessageInput(final InputStream inputStream, final int size) {
+        this.inputStream = inputStream;
+        remaining = size;
+    }
+
+    public int read() throws IOException {
+        final int data = inputStream.read();
+        if (data != -1 && remaining >= 0) {
+            remaining--;
+        }
+        return data;
+    }
+
+    public int read(final byte[] data) throws IOException {
+        final int cnt = inputStream.read(data);
+        if (cnt != -1 && remaining >= 0) {
+            remaining -= cnt;
+        }
+        return cnt;
+    }
+
+    public int read(final byte[] data, final int offs, final int len) throws IOException {
+        final int cnt = inputStream.read(data, offs, len);
+        if (cnt != -1 && remaining >= 0) {
+            remaining -= cnt;
+        }
+        return cnt;
+    }
+
+    public int remaining() {
+        final int remaining = this.remaining;
+        return remaining < 0 ? -1 : remaining;
+    }
+
+    public void close() throws IOException {
+        remaining = -1;
+        inputStream.close();
+    }
+}




More information about the jboss-remoting-commits mailing list