Permalink
Browse files

Add JBoss Marshalling Encoder/Decoder. See #324

  • Loading branch information...
1 parent 52a7d28 commit ef384a7b2181a79fcc1e44401aab55f13cd69099 @normanmaurer normanmaurer committed May 16, 2012
Showing with 961 additions and 0 deletions.
  1. +17 −0 codec/pom.xml
  2. +75 −0 codec/src/main/java/io/netty/handler/codec/marshalling/ChannelBufferByteInput.java
  3. +77 −0 codec/src/main/java/io/netty/handler/codec/marshalling/ChannelBufferByteOutput.java
  4. +108 −0 codec/src/main/java/io/netty/handler/codec/marshalling/LimitingByteInput.java
  5. +122 −0 codec/src/main/java/io/netty/handler/codec/marshalling/MarshallingDecoder.java
  6. +59 −0 codec/src/main/java/io/netty/handler/codec/marshalling/MarshallingEncoder.java
  7. +52 −0 codec/src/main/java/io/netty/handler/codec/marshalling/ThreadLocalMarshallingDecoder.java
  8. +21 −0 codec/src/main/java/io/netty/handler/codec/marshalling/package-info.java
  9. +135 −0 codec/src/test/java/io/netty/handler/codec/marshalling/AbstractMarshallingDecoderTest.java
  10. +63 −0 codec/src/test/java/io/netty/handler/codec/marshalling/AbstractMarshallingEncoderTest.java
  11. +37 −0 codec/src/test/java/io/netty/handler/codec/marshalling/RiverMarshallingDecoderTest.java
  12. +38 −0 codec/src/test/java/io/netty/handler/codec/marshalling/RiverMarshallingEncoderTest.java
  13. +27 −0 codec/src/test/java/io/netty/handler/codec/marshalling/RiverThreadLocalMarshallingDecoderTest.java
  14. +37 −0 codec/src/test/java/io/netty/handler/codec/marshalling/SerialMarshallingDecoderTest.java
  15. +38 −0 codec/src/test/java/io/netty/handler/codec/marshalling/SerialMarshallingEncoderTest.java
  16. +28 −0 codec/src/test/java/io/netty/handler/codec/marshalling/SerialThreadLocalMarshallingDecoderTest.java
  17. +27 −0 pom.xml
View
@@ -39,6 +39,23 @@
<artifactId>protobuf-java</artifactId>
<optional>true</optional>
</dependency>
+ <dependency>
+ <groupId>org.jboss.marshalling</groupId>
+ <artifactId>jboss-marshalling</artifactId>
+ <optional>true</optional>
+ </dependency>
+
+ <!-- Test dependencies for jboss marshalling encoder/decoder -->
+ <dependency>
+ <groupId>org.jboss.marshalling</groupId>
+ <artifactId>jboss-marshalling-serial</artifactId>
+ <scope>test</scope>
+ </dependency>
+ <dependency>
+ <groupId>org.jboss.marshalling</groupId>
+ <artifactId>jboss-marshalling-river</artifactId>
+ <scope>test</scope>
+ </dependency>
</dependencies>
</project>
@@ -0,0 +1,75 @@
+/*
+ * Copyright 2012 The Netty Project
+ *
+ * The Netty Project 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.
+ */
+package io.netty.handler.codec.marshalling;
+
+import java.io.IOException;
+
+import org.jboss.marshalling.ByteInput;
+import io.netty.buffer.ChannelBuffer;
+
+/**
+ * {@link ByteInput} implementation which reads its data from a {@link ChannelBuffer}
+ *
+ *
+ */
+class ChannelBufferByteInput implements ByteInput {
+
+ private final ChannelBuffer buffer;
+
+ public ChannelBufferByteInput(ChannelBuffer buffer) {
+ this.buffer = buffer;
+ }
+
+ public void close() throws IOException {
+ // nothing to do
+ }
+
+ public int available() throws IOException {
+ return buffer.readableBytes();
+ }
+
+ public int read() throws IOException {
+ if (buffer.readable()) {
+ return buffer.readByte() & 0xff;
+ }
+ return -1;
+ }
+
+ public int read(byte[] array) throws IOException {
+ return read(array, 0, array.length);
+ }
+
+ public int read(byte[] dst, int dstIndex, int length) throws IOException {
+ int available = available();
+ if (available == 0) {
+ return -1;
+ }
+
+ length = Math.min(available, length);
+ buffer.readBytes(dst, dstIndex, length);
+ return length;
+ }
+
+ public long skip(long bytes) throws IOException {
+ int readable = buffer.readableBytes();
+ if (readable < bytes) {
+ bytes = readable;
+ }
+ buffer.readerIndex((int) (buffer.readerIndex() + bytes));
+ return bytes;
+ }
+
+}
@@ -0,0 +1,77 @@
+/*
+ * Copyright 2012 The Netty Project
+ *
+ * The Netty Project 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.
+ */
+package io.netty.handler.codec.marshalling;
+
+import java.io.IOException;
+
+import org.jboss.marshalling.ByteOutput;
+import io.netty.buffer.ChannelBuffer;
+import io.netty.buffer.ChannelBufferFactory;
+import io.netty.buffer.ChannelBuffers;
+
+/**
+ * {@link ByteOutput} implementation which writes the data to a {@link ChannelBuffer}
+ *
+ *
+ */
+class ChannelBufferByteOutput implements ByteOutput {
+
+ private final ChannelBuffer buffer;
+
+
+ /**
+ * Create a new instance which use the given {@link ChannelBuffer}
+ */
+ public ChannelBufferByteOutput(ChannelBuffer buffer) {
+ this.buffer = buffer;
+ }
+
+ /**
+ * Calls {@link #ChannelBufferByteOutput(ChannelBuffer)} with a dynamic {@link ChannelBuffer}
+ */
+ public ChannelBufferByteOutput(ChannelBufferFactory factory, int estimatedLength) {
+ this(ChannelBuffers.dynamicBuffer(estimatedLength, factory));
+ }
+
+ public void close() throws IOException {
+ // Nothing todo
+ }
+
+ public void flush() throws IOException {
+ // nothing to do
+ }
+
+ public void write(int b) throws IOException {
+ buffer.writeByte(b);
+ }
+
+ public void write(byte[] bytes) throws IOException {
+ buffer.writeBytes(bytes);
+ }
+
+ public void write(byte[] bytes, int srcIndex, int length) throws IOException {
+ buffer.writeBytes(bytes, srcIndex, length);
+ }
+
+ /**
+ * Return the {@link ChannelBuffer} which contains the written content
+ *
+ */
+ public ChannelBuffer getBuffer() {
+ return buffer;
+ }
+
+}
@@ -0,0 +1,108 @@
+/*
+ * Copyright 2012 The Netty Project
+ *
+ * The Netty Project 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.
+ */
+package io.netty.handler.codec.marshalling;
+
+import java.io.IOException;
+
+import org.jboss.marshalling.ByteInput;
+
+/**
+ * {@link ByteInput} implementation which wraps another {@link ByteInput} and throws a {@link TooBigObjectException}
+ * if the read limit was reached.
+ *
+ *
+ */
+class LimitingByteInput implements ByteInput {
+
+ // Use a static instance here to remove the overhead of fillStacktrace
+ private static final TooBigObjectException EXCEPTION = new TooBigObjectException();
+
+ private final ByteInput input;
+ private final long limit;
+ private long read;
+
+ public LimitingByteInput(ByteInput input, long limit) {
+ if (limit <= 0) {
+ throw new IllegalArgumentException("The limit MUST be > 0");
+ }
+ this.input = input;
+ this.limit = limit;
+ }
+
+ public void close() throws IOException {
+ // Nothing todo
+ }
+
+ public int available() throws IOException {
+ int available = input.available();
+ int readable = readable(available);
+ return readable;
+ }
+
+ public int read() throws IOException {
+ int readable = readable(1);
+ if (readable > 0) {
+ int b = input.read();
+ read++;
+ return b;
+ } else {
+ throw EXCEPTION;
+ }
+ }
+
+ public int read(byte[] array) throws IOException {
+ return read(array, 0, array.length);
+ }
+
+ public int read(byte[] array, int offset, int length) throws IOException {
+ int readable = readable(length);
+ if (readable > 0) {
+ int i = input.read(array, offset, readable);
+ read += i;
+ return i;
+ } else {
+ throw EXCEPTION;
+ }
+ }
+
+ public long skip(long bytes) throws IOException {
+ int readable = readable((int) bytes);
+ if (readable > 0) {
+ long i = input.skip(readable);
+ read += i;
+ return i;
+ } else {
+ throw EXCEPTION;
+ }
+ }
+
+ private int readable(int length) {
+ return (int) Math.min(length, limit - read);
+ }
+
+ /**
+ * Exception that will get thrown if the {@link Object} is to big to unmarshall
+ *
+ */
+ static final class TooBigObjectException extends IOException {
+
+ /**
+ *
+ */
+ private static final long serialVersionUID = 1L;
+
+ }
+}
Oops, something went wrong.

0 comments on commit ef384a7

Please sign in to comment.