You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@mina.apache.org by jv...@apache.org on 2013/05/07 21:19:57 UTC
[09/25] git commit: refactoring / cleaning
refactoring / cleaning
Project: http://git-wip-us.apache.org/repos/asf/mina/repo
Commit: http://git-wip-us.apache.org/repos/asf/mina/commit/31f4baf1
Tree: http://git-wip-us.apache.org/repos/asf/mina/tree/31f4baf1
Diff: http://git-wip-us.apache.org/repos/asf/mina/diff/31f4baf1
Branch: refs/heads/trunk
Commit: 31f4baf1bdfb67375cefb969e942fd957cf7b891
Parents: d19dba8
Author: Raphaël P. Barazzutti <ra...@gmail.com>
Authored: Sun Apr 28 14:13:04 2013 +0200
Committer: jvermillard <jv...@apache.org>
Committed: Tue May 7 21:09:00 2013 +0200
----------------------------------------------------------------------
.../mina/codec/delimited/ByteBufferDecoder.java | 105 ++++++++
.../mina/codec/delimited/ByteBufferEncoder.java | 100 ++++++++
.../mina/codec/delimited/SizePrefixedDecoder.java | 19 +-
.../mina/codec/delimited/SizePrefixedEncoder.java | 26 +-
.../apache/mina/codec/delimited/Transcoder.java | 155 ------------
.../apache/mina/codec/delimited/ints/RawInt32.java | 184 +++++++++++++++
.../codec/delimited/ints/RawInt32Transcoder.java | 172 --------------
.../apache/mina/codec/delimited/ints/VarInt.java | 154 ++++++++++++
.../codec/delimited/ints/VarIntTranscoder.java | 131 ----------
.../serialization/ByteBuffersTranscoder.java | 48 ----
.../delimited/serialization/JavaNativeDecoder.java | 53 ++++
.../delimited/serialization/JavaNativeEncoder.java | 73 ++++++
.../serialization/JavaNativeTranscoder.java | 97 --------
.../delimited/serialization/ProtobufDecoder.java | 67 ++++++
.../serialization/ProtobufDynamicDecoder.java | 69 ++++++
.../delimited/serialization/ProtobufEncoder.java | 40 +++
.../serialization/ProtobufTranscoder.java | 57 -----
.../codec/delimited/TestSizePrefixedEncoder.java | 2 +-
.../mina/codec/delimited/ints/IntEncodingTest.java | 11 +-
.../ints/RawInt32BigEndianEncodingTest.java | 11 +-
.../ints/RawInt32LittleEndianEncodingTest.java | 11 +-
.../codec/delimited/ints/VarIntEncodingTest.java | 12 +-
.../serialization/GenericSerializerTest.java | 41 ++++
.../delimited/serialization/JavaNativeTest.java | 101 ++++++++
.../serialization/JavaNativeTranscoderTest.java | 94 --------
.../delimited/serialization/ProtobufTest.java | 47 ++++
.../serialization/ProtobufTranscoderTest.java | 25 --
.../delimited/serialization/TranscoderTest.java | 35 ---
28 files changed, 1081 insertions(+), 859 deletions(-)
----------------------------------------------------------------------
http://git-wip-us.apache.org/repos/asf/mina/blob/31f4baf1/codec/src/main/java/org/apache/mina/codec/delimited/ByteBufferDecoder.java
----------------------------------------------------------------------
diff --git a/codec/src/main/java/org/apache/mina/codec/delimited/ByteBufferDecoder.java b/codec/src/main/java/org/apache/mina/codec/delimited/ByteBufferDecoder.java
new file mode 100644
index 0000000..8bde1c6
--- /dev/null
+++ b/codec/src/main/java/org/apache/mina/codec/delimited/ByteBufferDecoder.java
@@ -0,0 +1,105 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF 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 org.apache.mina.codec.delimited;
+
+import java.nio.ByteBuffer;
+
+import org.apache.mina.codec.ProtocolDecoder;
+import org.apache.mina.codec.ProtocolDecoderException;
+import org.apache.mina.codec.StatelessProtocolDecoder;
+import org.apache.mina.codec.delimited.ints.RawInt32;
+import org.apache.mina.codec.delimited.ints.VarInt;
+
+/**
+ * Abstract class providing both encoding and decoding methods between a given
+ * type and ByteBuffers.
+ *
+ * <p>
+ * Transcoder is stateless class providing encoding and decoding facilities.
+ * Additionally this abstract requires two methods which allows to determine the
+ * size of a given message and to write it directly to a previously allocated
+ * ByteBuffer.
+ * </p>
+ *
+ * @param <INPUT>
+ * the type of the messages which will be encoded in ByteBuffers and
+ * decoded from ByteBuffers.
+ *
+ * @author <a href="http://mina.apache.org">Apache MINA Project</a>
+ *
+ */
+public abstract class ByteBufferDecoder<INPUT> implements StatelessProtocolDecoder<ByteBuffer, INPUT> {
+ /**
+ * Being stateless, this method is left empty
+ *
+ * @see ProtocolDecoder#createDecoderState()
+ */
+ @Override
+ final public Void createDecoderState() {
+ // stateless !
+ return null;
+ }
+
+ /**
+ * Decodes a message from a {@link ByteBuffer}
+ *
+ * <p>
+ * When a truncated input is given to this method it <b>may</b> return null.
+ * Not all decoder will be able to detect this issue and report it that way.
+ * Thanks to prefixing of messages, decoder will only receive appropriately
+ * sized ByteBuffers.
+ * </p>
+ *
+ * <p>
+ * n.b. The decoders used for the prefixing (i.e. {@link RawInt32} and
+ * {@link VarInt}) <b>have</b> to detect truncated ByteBuffers.
+ * </p>
+ *
+ * @param input
+ * data to be decoded as a TYPE message
+ * @return the decoded message on success, null otherwise
+ *
+ * @throws ProtocolDecoderException
+ */
+ abstract public INPUT decode(ByteBuffer input) throws ProtocolDecoderException;
+
+ /**
+ * Decodes a message from a {@link ByteBuffer}
+ * <p>
+ * The actual decoding needs to be implemented in the abstract method
+ * {@link ByteBufferDecoder#decode(ByteBuffer)}
+ * </p>
+ */
+ @Override
+ final public INPUT decode(ByteBuffer input, Void context) throws ProtocolDecoderException {
+ return decode(input);
+ }
+
+ /**
+ * Being stateless, this method is left empty
+ *
+ * @see ProtocolDecoder#finishDecode(Object)
+ */
+ @Override
+ final public void finishDecode(Void context) {
+ // stateless !
+ }
+
+}
http://git-wip-us.apache.org/repos/asf/mina/blob/31f4baf1/codec/src/main/java/org/apache/mina/codec/delimited/ByteBufferEncoder.java
----------------------------------------------------------------------
diff --git a/codec/src/main/java/org/apache/mina/codec/delimited/ByteBufferEncoder.java b/codec/src/main/java/org/apache/mina/codec/delimited/ByteBufferEncoder.java
new file mode 100644
index 0000000..bba4093
--- /dev/null
+++ b/codec/src/main/java/org/apache/mina/codec/delimited/ByteBufferEncoder.java
@@ -0,0 +1,100 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF 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 org.apache.mina.codec.delimited;
+
+import java.nio.ByteBuffer;
+
+import org.apache.mina.codec.ProtocolDecoder;
+import org.apache.mina.codec.StatelessProtocolEncoder;
+
+/**
+ * Abstract class providing both encoding and decoding methods between a given type and ByteBuffers.
+ *
+ * <p>
+ * Transcoder is stateless class providing encoding and decoding facilities.
+ * Additionally this abstract requires two methods which allows to determine the size of a given message and
+ * to write it directly to a previously allocated ByteBuffer.
+ * </p>
+ *
+ * @param <OUTPUT> the type of the messages which will be encoded in ByteBuffers and decoded from ByteBuffers.
+ *
+ * @author <a href="http://mina.apache.org">Apache MINA Project</a>
+ *
+ */
+public abstract class ByteBufferEncoder<OUTPUT> implements StatelessProtocolEncoder<OUTPUT, ByteBuffer> {
+
+ /**
+ * Being stateless, this method is left empty
+ * @see ProtocolDecoder#createDecoderState()
+ */
+ @Override
+ final public Void createEncoderState() {
+ // stateless !
+ return null;
+ }
+
+ /**
+ * Encodes a message to a {@link ByteBuffer}
+ *
+ * @param message a message to be encoded
+ * @return the buffer containing {@link ByteBuffer} representation of the message
+ */
+ public ByteBuffer encode(OUTPUT message) {
+ ByteBuffer buffer = ByteBuffer.allocate(getEncodedSize(message));
+ writeTo(message, buffer);
+ buffer.position(0);
+ return buffer;
+ }
+
+ /**
+ * Encodes a message to a {@link ByteBuffer}
+ * <p>
+ * The actual encoding needs to be implemented in the abstract method {@link ByteBufferEncoder#encode(Object)}
+ * </p>
+ */
+
+ @Override
+ final public ByteBuffer encode(OUTPUT message, Void context) {
+ return encode(message);
+ }
+
+ /**
+ *
+ * Computes the size of the serialized form of a message in bytes.
+ *
+ * @param message a message to be encoded
+ * @return the size of the serialized form of the message
+ */
+ abstract public int getEncodedSize(OUTPUT message);
+
+ /**
+ * Writes a message on a {@link ByteBuffer}.
+ *
+ * <p>
+ * n.b. The buffer is expected to have at least a sufficient capacity to handle the serialized form
+ * of the message.
+ * </p>
+ *
+ * @param message a message to be encoded
+ * @param buffer a target {@link ByteBuffer}
+ */
+ abstract public void writeTo(OUTPUT message, ByteBuffer buffer);
+
+}
http://git-wip-us.apache.org/repos/asf/mina/blob/31f4baf1/codec/src/main/java/org/apache/mina/codec/delimited/SizePrefixedDecoder.java
----------------------------------------------------------------------
diff --git a/codec/src/main/java/org/apache/mina/codec/delimited/SizePrefixedDecoder.java b/codec/src/main/java/org/apache/mina/codec/delimited/SizePrefixedDecoder.java
index 2912cbd..2b5199d 100644
--- a/codec/src/main/java/org/apache/mina/codec/delimited/SizePrefixedDecoder.java
+++ b/codec/src/main/java/org/apache/mina/codec/delimited/SizePrefixedDecoder.java
@@ -59,7 +59,7 @@ public class SizePrefixedDecoder<OUT> implements ProtocolDecoder<ByteBuffer, OUT
}
/**
- * Returns the existence (or not) of an integer in this mutable.
+ * Returns the existence (or not) of an integer in this mutable.
*
* @return true if it contains a value, false otherwise.
*/
@@ -77,21 +77,22 @@ public class SizePrefixedDecoder<OUT> implements ProtocolDecoder<ByteBuffer, OUT
/**
* Set the value.
*
- * @param value the value to set
+ * @param value
+ * the value to set
*/
public void setValue(Integer value) {
this.value = value;
}
}
- final private Transcoder<Integer, Integer> transcoder;
+ final private ByteBufferDecoder<Integer> sizeDecoder;
- final private Transcoder<OUT, ?> packetTranscoder;
+ final private ByteBufferDecoder<OUT> payloadDecoder;
- public SizePrefixedDecoder(Transcoder<Integer, Integer> transcoder, Transcoder<OUT, ?> packetTranscoder) {
+ public SizePrefixedDecoder(ByteBufferDecoder<Integer> sizeDecoder, ByteBufferDecoder<OUT> payloadDecoder) {
super();
- this.transcoder = transcoder;
- this.packetTranscoder = packetTranscoder;
+ this.sizeDecoder = sizeDecoder;
+ this.payloadDecoder = payloadDecoder;
}
@Override
@@ -104,7 +105,7 @@ public class SizePrefixedDecoder<OUT> implements ProtocolDecoder<ByteBuffer, OUT
public OUT decode(ByteBuffer input, MutableInt nextBlockSize) throws ProtocolDecoderException {
OUT output = null;
if (nextBlockSize.getValue() == null) {
- nextBlockSize.setValue(transcoder.decode(input));
+ nextBlockSize.setValue(sizeDecoder.decode(input));
}
if (nextBlockSize.isDefined()) {
@@ -112,7 +113,7 @@ public class SizePrefixedDecoder<OUT> implements ProtocolDecoder<ByteBuffer, OUT
ByteBuffer buffer = input.slice();
buffer.limit(buffer.position() + nextBlockSize.getValue());
- output = packetTranscoder.decode(buffer);
+ output = payloadDecoder.decode(buffer);
nextBlockSize.reset();
}
}
http://git-wip-us.apache.org/repos/asf/mina/blob/31f4baf1/codec/src/main/java/org/apache/mina/codec/delimited/SizePrefixedEncoder.java
----------------------------------------------------------------------
diff --git a/codec/src/main/java/org/apache/mina/codec/delimited/SizePrefixedEncoder.java b/codec/src/main/java/org/apache/mina/codec/delimited/SizePrefixedEncoder.java
index d83017b..0d4b9d9 100644
--- a/codec/src/main/java/org/apache/mina/codec/delimited/SizePrefixedEncoder.java
+++ b/codec/src/main/java/org/apache/mina/codec/delimited/SizePrefixedEncoder.java
@@ -30,11 +30,14 @@ import org.apache.mina.codec.StatelessProtocolEncoder;
*/
public class SizePrefixedEncoder<IN> implements StatelessProtocolEncoder<IN, ByteBuffer> {
- final private Transcoder<Integer, Integer> transcoder;
+ final private ByteBufferEncoder<Integer> sizeEncoder;
- public SizePrefixedEncoder(Transcoder<Integer, Integer> transcoder) {
+ final private ByteBufferEncoder<IN> payloadEncoder;
+
+ public SizePrefixedEncoder(ByteBufferEncoder<Integer> sizeEncoder, ByteBufferEncoder<IN> payloadEncoder) {
super();
- this.transcoder = transcoder;
+ this.sizeEncoder = sizeEncoder;
+ this.payloadEncoder = payloadEncoder;
}
@Override
@@ -45,17 +48,12 @@ public class SizePrefixedEncoder<IN> implements StatelessProtocolEncoder<IN, Byt
@Override
public ByteBuffer encode(IN message, Void context) {
- ByteBuffer buffer;
- if (message instanceof ByteBuffer) {
- ByteBuffer messageBuffer = (ByteBuffer) message;
-
- buffer = ByteBuffer.allocate(transcoder.getEncodedSize(messageBuffer.remaining())
- + messageBuffer.remaining());
- transcoder.writeTo(messageBuffer.remaining(), buffer);
- buffer.put(messageBuffer);
- } else {
- throw new RuntimeException("Message of type " + message.getClass() + " not handled");
- }
+ int messageSize = payloadEncoder.getEncodedSize(message);
+ ByteBuffer buffer = ByteBuffer.allocate(sizeEncoder.getEncodedSize(messageSize) + messageSize);
+
+ sizeEncoder.writeTo(messageSize, buffer);
+ payloadEncoder.writeTo(message, buffer);
+
buffer.flip();
return buffer;
}
http://git-wip-us.apache.org/repos/asf/mina/blob/31f4baf1/codec/src/main/java/org/apache/mina/codec/delimited/Transcoder.java
----------------------------------------------------------------------
diff --git a/codec/src/main/java/org/apache/mina/codec/delimited/Transcoder.java b/codec/src/main/java/org/apache/mina/codec/delimited/Transcoder.java
deleted file mode 100644
index de4e1d5..0000000
--- a/codec/src/main/java/org/apache/mina/codec/delimited/Transcoder.java
+++ /dev/null
@@ -1,155 +0,0 @@
-/*
- * Licensed to the Apache Software Foundation (ASF) under one
- * or more contributor license agreements. See the NOTICE file
- * distributed with this work for additional information
- * regarding copyright ownership. The ASF 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 org.apache.mina.codec.delimited;
-
-import java.nio.ByteBuffer;
-
-import org.apache.mina.codec.ProtocolDecoder;
-import org.apache.mina.codec.ProtocolDecoderException;
-import org.apache.mina.codec.StatelessProtocolDecoder;
-import org.apache.mina.codec.StatelessProtocolEncoder;
-import org.apache.mina.codec.delimited.ints.RawInt32Transcoder;
-import org.apache.mina.codec.delimited.ints.VarIntTranscoder;
-
-/**
- * Abstract class providing both encoding and decoding methods between a given type and ByteBuffers.
- *
- * <p>
- * Transcoder is stateless class providing encoding and decoding facilities.
- * Additionally this abstract requires two methods which allows to determine the size of a given message and
- * to write it directly to a previously allocated ByteBuffer.
- * </p>
- *
- * @param <TYPE> the type of the messages which will be encoded in ByteBuffers and decoded from ByteBuffers.
- *
- * @author <a href="http://mina.apache.org">Apache MINA Project</a>
- *
- */
-public abstract class Transcoder<INPUT, OUTPUT> implements StatelessProtocolDecoder<ByteBuffer, INPUT>,
- StatelessProtocolEncoder<OUTPUT, ByteBuffer> {
- /**
- * Being stateless, this method is left empty
- * @see ProtocolDecoder#createDecoderState()
- */
- @Override
- final public Void createDecoderState() {
- // stateless !
- return null;
- }
-
- /**
- * Being stateless, this method is left empty
- * @see ProtocolDecoder#createDecoderState()
- */
- @Override
- final public Void createEncoderState() {
- // stateless !
- return null;
- }
-
- /**
- * Decodes a message from a {@link ByteBuffer}
- *
- * <p>
- * When a truncated input is given to this method it <b>may</b> return null. Not all transcoder will be
- * able to detect this issue and report it that way. Thanks to prefixing of messages, transcoder will
- * only receive appropriately sized ByteBuffers.
- * </p>
- *
- * <p>
- * n.b. The transcoders used for the prefixing (i.e. {@link RawInt32Transcoder} and {@link VarIntTranscoder}) <b>have</b> to detect truncated ByteBuffers.
- * </p>
- *
- * @param input data to be decoded as a TYPE message
- * @return the decoded message on success, null otherwise
- *
- * @throws ProtocolDecoderException
- */
- abstract public INPUT decode(ByteBuffer input) throws ProtocolDecoderException;
-
- /**
- * Decodes a message from a {@link ByteBuffer}
- * <p>
- * The actual decoding needs to be implemented in the abstract method {@link Transcoder#decode(ByteBuffer)}
- * </p>
- */
- @Override
- final public INPUT decode(ByteBuffer input, Void context) throws ProtocolDecoderException {
- return decode(input);
- }
-
- /**
- * Encodes a message to a {@link ByteBuffer}
- *
- * @param message a message to be encoded
- * @return the buffer containing {@link ByteBuffer} representation of the message
- */
- public ByteBuffer encode(OUTPUT message) {
- ByteBuffer buffer = ByteBuffer.allocate(getEncodedSize(message));
- writeTo(message, buffer);
- buffer.position(0);
- return buffer;
- }
-
- /**
- * Encodes a message to a {@link ByteBuffer}
- * <p>
- * The actual encoding needs to be implemented in the abstract method {@link Transcoder#encode(Object)}
- * </p>
- */
-
- @Override
- final public ByteBuffer encode(OUTPUT message, Void context) {
- return encode(message);
- }
-
- /**
- * Being stateless, this method is left empty
- *
- * @see ProtocolDecoder#finishDecode(Object)
- */
- @Override
- final public void finishDecode(Void context) {
- // stateless !
- }
-
- /**
- *
- * Computes the size of the serialized form of a message in bytes.
- *
- * @param message a message to be encoded
- * @return the size of the serialized form of the message
- */
- abstract public int getEncodedSize(OUTPUT message);
-
- /**
- * Writes a message on a {@link ByteBuffer}.
- *
- * <p>
- * Nb. The buffer is expected to have at least a sufficient capacity to handle the serialized form
- * of the message.
- * </p>
- *
- * @param message a message to be encoded
- * @param buffer a target {@link ByteBuffer}
- */
- abstract public void writeTo(OUTPUT message, ByteBuffer buffer);
-
-}
http://git-wip-us.apache.org/repos/asf/mina/blob/31f4baf1/codec/src/main/java/org/apache/mina/codec/delimited/ints/RawInt32.java
----------------------------------------------------------------------
diff --git a/codec/src/main/java/org/apache/mina/codec/delimited/ints/RawInt32.java b/codec/src/main/java/org/apache/mina/codec/delimited/ints/RawInt32.java
new file mode 100644
index 0000000..42350cb
--- /dev/null
+++ b/codec/src/main/java/org/apache/mina/codec/delimited/ints/RawInt32.java
@@ -0,0 +1,184 @@
+package org.apache.mina.codec.delimited.ints;
+
+import java.nio.ByteBuffer;
+
+import org.apache.mina.codec.ProtocolDecoderException;
+import org.apache.mina.codec.delimited.ByteBufferDecoder;
+import org.apache.mina.codec.delimited.ByteBufferEncoder;
+
+/**
+ *
+ * Class providing raw/canonical representation of integers.
+ *
+ *
+ * <style type="text/css"> pre-fw { color: rgb(0, 0, 0); display: block;
+ * font-family:courier, "courier new", monospace; font-size: 13px; white-space:
+ * pre; } </style>
+ *
+ * <h2>RawInt32 encoder and decoder</h2>
+ * <p>
+ * This pair provides a mechanism called canonical form serialization.
+ *
+ * In this representations all 32-bits integer are encoded over 4 bytes.
+ *
+ *
+ * This library provides two variants <i>big-endian</i> and <i>small-endian</i>.
+ *
+ *
+ * In both cases, the inner bits of each byte are ordered from the most to the
+ * least significant bit.
+ *
+ * The difference between the two variants is the ordering of the four bytes.
+ *
+ * <ul>
+ * <li>Big-endian: <i>The bytes are ordered from the most to the least
+ * significant one</i></li>
+ * <li>Little-endian: <i>The bytes are ordered from the least to the most
+ * significant one</i></li>
+ * </ul>
+ *
+ * <p>
+ * This representation is often used since it is used internally in CPUs,
+ * therefore programmers using a low level languages (assembly, C, ...)
+ * appreciate using it (for ease of use or performance reasons). When integers
+ * are directly copied from memory, it is required to ensure this serializer
+ * uses the appropriate endianness on both ends.
+ * <ul>
+ * <li>Big-endian: 68k, MIPS, Alpha, SPARC</li>
+ * <li>Little-endian: x86, x86-64, ARM</li>
+ * <li><i>Bi-</i>endian (depends of the operating system): PowerPC, Itanium</li>
+ * </ul>
+ * </p>
+ *
+ * <p>
+ * More details availabile on the Wikipedia
+ * "<a href="http://en.wikipedia.org/wiki/Endianness">Endianness page</a>".
+ * </p>
+ *
+ * <h2>On-wire representation</h2>
+ * <p>
+ * Encoding of the value 67305985
+ * </p>
+ * <i>Big-Endian variant:</i>
+ *
+ * <pre-fw>
+ * 0000 0100 0000 0011 0000 0010 0000 0001
+ * ‾‾‾‾↓‾‾‾‾ ‾‾‾‾↓‾‾‾‾ ‾‾‾‾↓‾‾‾‾ ‾‾‾‾↓‾‾‾‾
+ * 4 3 2 1 // 4·2<sup>24</sup> + 3·2<sup>16</sup> + 2·2<sup>8</sup> + 1·2<sup>0</sup> = 67305985
+ *
+ * </pre-fw>
+ *
+ * <i>Little-Endian variant:</i>
+ *
+ * <pre-fw>
+ * 0000 0001 0000 0010 0000 0011 0000 0100
+ * ‾‾‾‾↓‾‾‾‾ ‾‾‾‾↓‾‾‾‾ ‾‾‾‾↓‾‾‾‾ ‾‾‾‾↓‾‾‾‾
+ * 1 2 3 4 // 1·2<sup>0</sup> + 2·2<sup>8</sup> + 3·2<sup>16</sup> + 4·2<sup>24</sup> = 67305985
+ * </pre-fw>
+ *
+ * </p>
+ *
+ * <p>
+ * n.b. This class doesn't have any dependency against Apache Thrift or
+ * any other library in order to provide this convenient integer serialization
+ * module to any software using FramedMINA.
+ * </p>
+ *
+ * @author <a href="http://mina.apache.org">Apache MINA Project</a>
+ *
+ */
+public class RawInt32 {
+ // this class should not be instanciated
+ private RawInt32() {
+ }
+
+ /**
+ * Documentation available in the {@link RawInt32} enclosing class.
+ *
+ * @author <a href="http://mina.apache.org">Apache MINA Project</a>
+ *
+ */
+ static public class Decoder extends ByteBufferDecoder<Integer> {
+
+ final private Endianness endianness;
+
+ public Decoder(Endianness endianness) {
+ super();
+ this.endianness = endianness;
+ }
+
+ @Override
+ public Integer decode(ByteBuffer input) throws ProtocolDecoderException {
+ if (input.remaining() < 4)
+ return null;
+
+ if (endianness == Endianness.BIG) {
+ if ((input.get(0) & 0x80) != 0)
+ throw new ProtocolDecoderException("Not the big endian representation of a signed int32");
+ return ((input.get() & 0xff) << 24) | ((input.get() & 0xff) << 16) | ((input.get() & 0xff) << 8)
+ | ((input.get() & 0xff));
+ } else {
+ if ((input.get(3) & 0x80) != 0)
+ throw new ProtocolDecoderException("Not the small endian representation of a signed int32");
+ return ((input.get() & 0xff)) | ((input.get() & 0xff) << 8) | ((input.get() & 0xff) << 16)
+ | ((input.get() & 0xff) << 24);
+ }
+ }
+ }
+
+ /**
+ * Documentation available in the {@link RawInt32} enclosing class.
+ *
+ * @author <a href="http://mina.apache.org">Apache MINA Project</a>
+ *
+ */
+ static public class Encoder extends ByteBufferEncoder<Integer> {
+
+ final private Endianness endianness;
+
+ public Encoder(Endianness endianness) {
+ super();
+ this.endianness = endianness;
+ }
+
+ @Override
+ public void writeTo(Integer message, ByteBuffer buffer) {
+ // VarInts don't support negative values
+ if (message < 0)
+ message = 0;
+ if (endianness == Endianness.BIG) {
+ buffer.put((byte) (0xff & (message >> 24)));
+ buffer.put((byte) (0xff & (message >> 16)));
+ buffer.put((byte) (0xff & (message >> 8)));
+ buffer.put((byte) (0xff & (message)));
+ } else {
+ buffer.put((byte) (0xff & (message)));
+ buffer.put((byte) (0xff & (message >> 8)));
+ buffer.put((byte) (0xff & (message >> 16)));
+ buffer.put((byte) (0xff & (message >> 24)));
+ }
+ buffer.flip();
+
+ }
+
+ @Override
+ public int getEncodedSize(Integer message) {
+ return 4;
+ }
+
+ }
+
+ /**
+ *
+ * This enumeration is used to select the endianness of the dncoder and
+ * the decoder class.
+ *
+ * Documentation available in the {@link RawInt32} enclosing class.
+ *
+ * @author <a href="http://mina.apache.org">Apache MINA Project</a>
+ */
+ public enum Endianness {
+ BIG, LITTLE
+ }
+
+}
http://git-wip-us.apache.org/repos/asf/mina/blob/31f4baf1/codec/src/main/java/org/apache/mina/codec/delimited/ints/RawInt32Transcoder.java
----------------------------------------------------------------------
diff --git a/codec/src/main/java/org/apache/mina/codec/delimited/ints/RawInt32Transcoder.java b/codec/src/main/java/org/apache/mina/codec/delimited/ints/RawInt32Transcoder.java
deleted file mode 100644
index 39e7731..0000000
--- a/codec/src/main/java/org/apache/mina/codec/delimited/ints/RawInt32Transcoder.java
+++ /dev/null
@@ -1,172 +0,0 @@
-/*
- * Licensed to the Apache Software Foundation (ASF) under one
- * or more contributor license agreements. See the NOTICE file
- * distributed with this work for additional information
- * regarding copyright ownership. The ASF 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 org.apache.mina.codec.delimited.ints;
-
-import java.nio.ByteBuffer;
-
-import org.apache.mina.codec.ProtocolDecoderException;
-import org.apache.mina.codec.delimited.Transcoder;
-
-/**
- *
- * A {@link Transcoder} providing raw/canonical representation of integers.
- *
- *
- * <style type="text/css"> pre-fw { color: rgb(0, 0, 0); display: block;
- * font-family:courier, "courier new", monospace; font-size: 13px; white-space:
- * pre; } </style>
- *
- * <h2>RawInt32 transcoder</h2>
- * <p>
- * This serializer provides a mechanism called canonical form serialization.
- *
- * In this representations all 32-bits integer are encoded over 4 bytes.
- *
- *
- * This library provides two variants <i>big-endian</i> and <i>small-endian</i>.
- *
- *
- * In both cases, the inner bits of each byte are ordered from the most to the
- * least significant bit.
- *
- * The difference between the two variants is the ordering of the four bytes.
- *
- * <ul>
- * <li>Big-endian: <i>The bytes are ordered from the most to the least
- * significant one</i></li>
- * <li>Little-endian: <i>The bytes are ordered from the least to the most
- * significant one</i></li>
- * </ul>
- *
- * <p>
- * This representation is often used since it is used internally in CPUs,
- * therefore programmers using a low level languages (assembly, C, ...)
- * appreciate using it (for ease of use or performance reasons). When integers
- * are directly copied from memory, it is required to ensure this serializer
- * uses the appropriate endianness on both ends.
- * <ul>
- * <li>Big-endian: 68k, MIPS, Alpha, SPARC</li>
- * <li>Little-endian: x86, x86-64, ARM</li>
- * <li><i>Bi-</i>endian (depends of the operating system): PowerPC, Itanium</li>
- * </ul>
- * </p>
- *
- * <p>
- * More details availabile on the Wikipedia
- * "<a href="http://en.wikipedia.org/wiki/Endianness">Endianness page</a>".
- * </p>
- *
- * <h2>On-wire representation</h2>
- * <p>
- * Encoding of the value 67305985
- * </p>
- * <i>Big-Endian variant:</i>
- *
- * <pre-fw>
- * 0000 0100 0000 0011 0000 0010 0000 0001
- * ‾‾‾‾↓‾‾‾‾ ‾‾‾‾↓‾‾‾‾ ‾‾‾‾↓‾‾‾‾ ‾‾‾‾↓‾‾‾‾
- * 4 3 2 1 // 4·2<sup>24</sup> + 3·2<sup>16</sup> + 2·2<sup>8</sup> + 1·2<sup>0</sup> = 67305985
- *
- * </pre-fw>
- *
- * <i>Little-Endian variant:</i>
- *
- * <pre-fw>
- * 0000 0001 0000 0010 0000 0011 0000 0100
- * ‾‾‾‾↓‾‾‾‾ ‾‾‾‾↓‾‾‾‾ ‾‾‾‾↓‾‾‾‾ ‾‾‾‾↓‾‾‾‾
- * 1 2 3 4 // 1·2<sup>0</sup> + 2·2<sup>8</sup> + 3·2<sup>16</sup> + 4·2<sup>24</sup> = 67305985
- * </pre-fw>
- *
- * </p>
- *
- * <p>
- * n.b. This class doesn't have any dependency against Apache Thrift or
- * any other library in order to provide this convenient integer serialization
- * module to any software using FramedMINA.
- * </p>
- *
- * @author <a href="http://mina.apache.org">Apache MINA Project</a>
- *
- */
-public class RawInt32Transcoder extends Transcoder<Integer, Integer> {
- /**
- *
- * This enumeration is used to select the endianness of the outer
- * RawInt32 transcoder class {@link RawInt32Transcoder (see
- * documentation)}.
- *
- * @author <a href="http://mina.apache.org">Apache MINA Project</a>
- */
-
- public enum Endianness {
- BIG, LITTLE
- }
-
- final private Endianness endianness;
-
- public RawInt32Transcoder(Endianness endianness) {
- super();
- this.endianness = endianness;
- }
-
- @Override
- public Integer decode(ByteBuffer input) throws ProtocolDecoderException {
- if (input.remaining() < 4)
- return null;
-
- if (endianness == Endianness.BIG) {
- if ((input.get(0) & 0x80) != 0)
- throw new ProtocolDecoderException("Not the big endian representation of a signed int32");
- return ((input.get() & 0xff) << 24) | ((input.get() & 0xff) << 16) | ((input.get() & 0xff) << 8)
- | ((input.get() & 0xff));
- } else {
- if ((input.get(3) & 0x80) != 0)
- throw new ProtocolDecoderException("Not the small endian representation of a signed int32");
- return ((input.get() & 0xff)) | ((input.get() & 0xff) << 8) | ((input.get() & 0xff) << 16)
- | ((input.get() & 0xff) << 24);
- }
- }
-
- @Override
- public void writeTo(Integer message, ByteBuffer buffer) {
- // VarInts don't support negative values
- if (message < 0)
- message = 0;
- if (endianness == Endianness.BIG) {
- buffer.put((byte) (0xff & (message >> 24)));
- buffer.put((byte) (0xff & (message >> 16)));
- buffer.put((byte) (0xff & (message >> 8)));
- buffer.put((byte) (0xff & (message)));
- } else {
- buffer.put((byte) (0xff & (message)));
- buffer.put((byte) (0xff & (message >> 8)));
- buffer.put((byte) (0xff & (message >> 16)));
- buffer.put((byte) (0xff & (message >> 24)));
- }
- buffer.flip();
-
- }
-
- @Override
- public int getEncodedSize(Integer message) {
- return 4;
- }
-
-}
http://git-wip-us.apache.org/repos/asf/mina/blob/31f4baf1/codec/src/main/java/org/apache/mina/codec/delimited/ints/VarInt.java
----------------------------------------------------------------------
diff --git a/codec/src/main/java/org/apache/mina/codec/delimited/ints/VarInt.java b/codec/src/main/java/org/apache/mina/codec/delimited/ints/VarInt.java
new file mode 100644
index 0000000..469b8da
--- /dev/null
+++ b/codec/src/main/java/org/apache/mina/codec/delimited/ints/VarInt.java
@@ -0,0 +1,154 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF 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 org.apache.mina.codec.delimited.ints;
+
+import java.nio.BufferUnderflowException;
+import java.nio.ByteBuffer;
+
+import org.apache.mina.codec.ProtocolDecoderException;
+import org.apache.mina.codec.delimited.ByteBufferDecoder;
+import org.apache.mina.codec.delimited.ByteBufferEncoder;
+
+/**
+ * Class providing a variable length representation of integers.
+ *
+ * <style type="text/css"> pre-fw { color: rgb(0, 0, 0); display: block;
+ * font-family:courier, "courier new", monospace; font-size: 13px; white-space:
+ * pre; } </style>
+ *
+ * <h2>Base 128 Varints serializer</h2>
+ * <p>
+ * This serializer is efficient in terms of computing costs as well as
+ * bandwith/memory usage.
+ * </p>
+ * <p>
+ * The average memory usage overall the range 0 to
+ * {@link java.lang.Integer#MAX_VALUE} is 4.87 bytes per number which is not far
+ * from the canonical form ({@link RawInt32}), however varints are an
+ * interesting solution since the small values (which are supposed to be more
+ * frequent) are using less bytes.
+ * </p>
+ * <p>
+ * All bytes forming a varint except the last one have the most significant bit
+ * (MSB) set. The lower 7 bits of each byte contains the actual representation
+ * of the two's complement representation of the number (least significant group
+ * first).
+ * </p>
+ * <p>
+ * n.b. This serializer is fully compatible with the 128 Varint mechanism
+ * shipped with the <a
+ * href="https://developers.google.com/protocol-buffers/docs/encoding#varints" >
+ * Google Protocol Buffer stack</a> as default representation of messages sizes.
+ * </p>
+ * <h2>On-wire representation</h2>
+ * <p>
+ * Encoding of the value 812
+ *
+ * <pre-fw>
+ *
+ * 1001 1100 0000 0110
+ * ↑ ↑
+ * 1 0 // the most significant bit being unset designs the last byte
+ * ___↑____ ___↑____
+ * 001 1100 000 0110 // the remaining bits defines the value itself
+ * → 44 6 // 44 + 128 * 6 = 812
+ * </pre-fw>
+ *
+ * </p>
+ * <p>
+ * n.b. This class doesn't have any dependency against Google Protocol Buffer or
+ * any other library in order to provide this convenient integer serialization
+ * module to any software using FramedMINA.
+ * </p>
+ *
+ * @author <a href="http://mina.apache.org">Apache MINA Project</a>
+ *
+ */
+public class VarInt {
+ // this class should not be instanciated
+ private VarInt() {
+ }
+
+ /**
+ * Documentation available in the {@link VarInt} enclosing class.
+ *
+ * @author <a href="http://mina.apache.org">Apache MINA Project</a>
+ *
+ */
+ static public class Decoder extends ByteBufferDecoder<Integer> {
+
+ @Override
+ public Integer decode(ByteBuffer input) throws ProtocolDecoderException {
+ int origpos = input.position();
+ int size = 0;
+
+ try {
+ for (int i = 0;; i += 7) {
+ byte tmp = input.get();
+
+ if ((tmp & 0x80) == 0 && (i != 4 * 7 || tmp < 1 << 3))
+ return size | (tmp << i);
+ else if (i < 4 * 7)
+ size |= (tmp & 0x7f) << i;
+ else
+ throw new ProtocolDecoderException("Not the varint representation of a signed int32");
+ }
+ } catch (BufferUnderflowException bue) {
+ input.position(origpos);
+ }
+ return null;
+ }
+ }
+
+ /**
+ * Documentation available in the {@link VarInt} enclosing class.
+ *
+ * @author <a href="http://mina.apache.org">Apache MINA Project</a>
+ *
+ */
+ static public class Encoder extends ByteBufferEncoder<Integer> {
+
+ @Override
+ public void writeTo(Integer message, ByteBuffer buffer) {
+ // VarInts don't support negative values
+ if (message < 0)
+ message = 0;
+ int value = message;
+
+ while (value > 0x7f) {
+ buffer.put((byte) ((value & 0x7f) | 0x80));
+ value >>= 7;
+ }
+
+ buffer.put((byte) value);
+ buffer.flip();
+ }
+
+ @Override
+ public int getEncodedSize(Integer message) {
+ if (message == 0)
+ return 1;
+ else {
+ int log2 = 32 - Integer.numberOfLeadingZeros(message);
+ return (log2 + 6) / 7;
+ }
+ }
+ }
+}
http://git-wip-us.apache.org/repos/asf/mina/blob/31f4baf1/codec/src/main/java/org/apache/mina/codec/delimited/ints/VarIntTranscoder.java
----------------------------------------------------------------------
diff --git a/codec/src/main/java/org/apache/mina/codec/delimited/ints/VarIntTranscoder.java b/codec/src/main/java/org/apache/mina/codec/delimited/ints/VarIntTranscoder.java
deleted file mode 100644
index 609521f..0000000
--- a/codec/src/main/java/org/apache/mina/codec/delimited/ints/VarIntTranscoder.java
+++ /dev/null
@@ -1,131 +0,0 @@
-/*
- * Licensed to the Apache Software Foundation (ASF) under one
- * or more contributor license agreements. See the NOTICE file
- * distributed with this work for additional information
- * regarding copyright ownership. The ASF 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 org.apache.mina.codec.delimited.ints;
-
-import java.nio.BufferUnderflowException;
-import java.nio.ByteBuffer;
-
-import org.apache.mina.codec.ProtocolDecoderException;
-import org.apache.mina.codec.delimited.Transcoder;
-
-/**
- * A {@link Transcoder} providing a variable length representation of integers.
- *
- * <style type="text/css"> pre-fw { color: rgb(0, 0, 0); display: block;
- * font-family:courier, "courier new", monospace; font-size: 13px; white-space:
- * pre; } </style>
- *
- * <h2>Base 128 Varints serializer</h2>
- * <p>
- * This serializer is efficient in terms of computing costs as well as
- * bandwith/memory usage.
- * </p>
- * <p>
- * The average memory usage overall the range 0 to
- * {@link java.lang.Integer#MAX_VALUE} is 4.87 bytes per number which is not far
- * from the canonical form ({@link RawInt32Transcoder}), however varints are an
- * interesting solution since the small values (which are supposed to be more
- * frequent) are using less bytes.
- * </p>
- * <p>
- * All bytes forming a varint except the last one have the most significant bit
- * (MSB) set. The lower 7 bits of each byte contains the actual representation
- * of the two's complement representation of the number (least significant group
- * first).
- * </p>
- * <p>
- * n.b. This serializer is fully compatible with the 128 Varint mechanism
- * shipped with the <a
- * href="https://developers.google.com/protocol-buffers/docs/encoding#varints" >
- * Google Protocol Buffer stack</a> as default representation of messages sizes.
- * </p>
- * <h2>On-wire representation</h2>
- * <p>
- * Encoding of the value 812
- *
- * <pre-fw>
- *
- * 1001 1100 0000 0110
- * ↑ ↑
- * 1 0 // the most significant bit being unset designs the last byte
- * ___↑____ ___↑____
- * 001 1100 000 0110 // the remaining bits defines the value itself
- * → 44 6 // 44 + 128 * 6 = 812
- * </pre-fw>
- *
- * </p>
- * <p>
- * n.b. This class doesn't have any dependency against Google Protocol Buffer or
- * any other library in order to provide this convenient integer serialization
- * module to any software using FramedMINA.
- * </p>
- *
- * @author <a href="http://mina.apache.org">Apache MINA Project</a>
- *
- */
-public class VarIntTranscoder extends Transcoder<Integer, Integer> {
- @Override
- public Integer decode(ByteBuffer input) throws ProtocolDecoderException {
- int origpos = input.position();
- int size = 0;
-
- try {
- for (int i = 0;; i += 7) {
- byte tmp = input.get();
-
- if ((tmp & 0x80) == 0 && (i != 4 * 7 || tmp < 1 << 3))
- return size | (tmp << i);
- else if (i < 4 * 7)
- size |= (tmp & 0x7f) << i;
- else
- throw new ProtocolDecoderException("Not the varint representation of a signed int32");
- }
- } catch (BufferUnderflowException bue) {
- input.position(origpos);
- }
- return null;
- }
-
- @Override
- public void writeTo(Integer message, ByteBuffer buffer) {
- // VarInts don't support negative values
- if (message < 0)
- message = 0;
- int value = message;
-
- while (value > 0x7f) {
- buffer.put((byte) ((value & 0x7f) | 0x80));
- value >>= 7;
- }
-
- buffer.put((byte) value);
- buffer.flip();
- }
-
- @Override
- public int getEncodedSize(Integer message) {
- if (message == 0)
- return 1;
- else {
- int log2 = 32 - Integer.numberOfLeadingZeros(message);
- return (log2 + 6) / 7;
- }
- }
-}
http://git-wip-us.apache.org/repos/asf/mina/blob/31f4baf1/codec/src/main/java/org/apache/mina/codec/delimited/serialization/ByteBuffersTranscoder.java
----------------------------------------------------------------------
diff --git a/codec/src/main/java/org/apache/mina/codec/delimited/serialization/ByteBuffersTranscoder.java b/codec/src/main/java/org/apache/mina/codec/delimited/serialization/ByteBuffersTranscoder.java
deleted file mode 100644
index e04edab..0000000
--- a/codec/src/main/java/org/apache/mina/codec/delimited/serialization/ByteBuffersTranscoder.java
+++ /dev/null
@@ -1,48 +0,0 @@
-/*
- * Licensed to the Apache Software Foundation (ASF) under one
- * or more contributor license agreements. See the NOTICE file
- * distributed with this work for additional information
- * regarding copyright ownership. The ASF 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 org.apache.mina.codec.delimited.serialization;
-
-import java.nio.ByteBuffer;
-
-import org.apache.mina.codec.ProtocolDecoderException;
-import org.apache.mina.codec.delimited.Transcoder;
-
-/**
- * @author <a href="http://mina.apache.org">Apache MINA Project</a>
- *
- */
-public class ByteBuffersTranscoder extends Transcoder<ByteBuffer,ByteBuffer> {
-
- @Override
- public ByteBuffer decode(ByteBuffer input) throws ProtocolDecoderException {
- return input;
- }
-
- @Override
- public int getEncodedSize(ByteBuffer message) {
- return message.remaining();
- }
-
- @Override
- public void writeTo(ByteBuffer message, ByteBuffer buffer) {
- buffer.put(message);
- }
-
-}
http://git-wip-us.apache.org/repos/asf/mina/blob/31f4baf1/codec/src/main/java/org/apache/mina/codec/delimited/serialization/JavaNativeDecoder.java
----------------------------------------------------------------------
diff --git a/codec/src/main/java/org/apache/mina/codec/delimited/serialization/JavaNativeDecoder.java b/codec/src/main/java/org/apache/mina/codec/delimited/serialization/JavaNativeDecoder.java
new file mode 100644
index 0000000..673bf42
--- /dev/null
+++ b/codec/src/main/java/org/apache/mina/codec/delimited/serialization/JavaNativeDecoder.java
@@ -0,0 +1,53 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF 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 org.apache.mina.codec.delimited.serialization;
+
+import java.io.IOException;
+import java.io.ObjectInputStream;
+import java.io.Serializable;
+import java.nio.ByteBuffer;
+
+import org.apache.mina.codec.ProtocolDecoderException;
+import org.apache.mina.codec.delimited.ByteBufferDecoder;
+import org.apache.mina.util.ByteBufferInputStream;
+
+/**
+ * Decoder providing the built-in Java-deserialization.
+ *
+ * @author <a href="http://mina.apache.org">Apache MINA Project</a>
+ *
+ */
+public class JavaNativeDecoder<IN extends Serializable> extends ByteBufferDecoder<IN> {
+
+ @SuppressWarnings("unchecked")
+ @Override
+ public IN decode(final ByteBuffer input) throws ProtocolDecoderException {
+ try {
+ ObjectInputStream ois = new ObjectInputStream(new ByteBufferInputStream(input));
+ IN s = (IN) ois.readObject();
+ ois.close();
+ return s;
+ } catch (ClassNotFoundException cnfe) {
+ throw new ProtocolDecoderException(cnfe);
+ } catch (IOException ioe) {
+ throw new ProtocolDecoderException(ioe);
+ }
+ }
+}
\ No newline at end of file
http://git-wip-us.apache.org/repos/asf/mina/blob/31f4baf1/codec/src/main/java/org/apache/mina/codec/delimited/serialization/JavaNativeEncoder.java
----------------------------------------------------------------------
diff --git a/codec/src/main/java/org/apache/mina/codec/delimited/serialization/JavaNativeEncoder.java b/codec/src/main/java/org/apache/mina/codec/delimited/serialization/JavaNativeEncoder.java
new file mode 100644
index 0000000..765c486
--- /dev/null
+++ b/codec/src/main/java/org/apache/mina/codec/delimited/serialization/JavaNativeEncoder.java
@@ -0,0 +1,73 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF 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 org.apache.mina.codec.delimited.serialization;
+
+import java.io.IOException;
+import java.io.ObjectOutputStream;
+import java.io.Serializable;
+import java.nio.ByteBuffer;
+
+import org.apache.mina.codec.delimited.ByteBufferEncoder;
+import org.apache.mina.util.ByteBufferOutputStream;
+
+/**
+ * Encoder providing the built-in Java-serialization.
+ *
+ * @author <a href="http://mina.apache.org">Apache MINA Project</a>
+ *
+ */
+public class JavaNativeEncoder<OUT extends Serializable> extends ByteBufferEncoder<OUT> {
+ @Override
+ public ByteBuffer encode(OUT message) {
+ // avoid the copy done in Transcoder
+ return serialize(message);
+ }
+
+ private OUT lastObject;
+
+ private ByteBuffer lastSerialized;
+
+ @Override
+ public int getEncodedSize(OUT message) {
+ return serialize(message).remaining();
+ }
+
+ private ByteBuffer serialize(OUT message) {
+ if (message != lastObject) {
+ ByteBufferOutputStream ebbosa = new ByteBufferOutputStream();
+ ebbosa.setElastic(true);
+ try {
+ ObjectOutputStream oos = new ObjectOutputStream(ebbosa);
+ oos.writeObject(message);
+ oos.close();
+ lastObject = message;
+ lastSerialized = ebbosa.getByteBuffer();
+ } catch (IOException e) {
+ throw new RuntimeException("");
+ }
+ }
+ return lastSerialized;
+ }
+
+ @Override
+ public void writeTo(OUT message, ByteBuffer buffer) {
+ buffer.put(serialize(message));
+ }
+}
\ No newline at end of file
http://git-wip-us.apache.org/repos/asf/mina/blob/31f4baf1/codec/src/main/java/org/apache/mina/codec/delimited/serialization/JavaNativeTranscoder.java
----------------------------------------------------------------------
diff --git a/codec/src/main/java/org/apache/mina/codec/delimited/serialization/JavaNativeTranscoder.java b/codec/src/main/java/org/apache/mina/codec/delimited/serialization/JavaNativeTranscoder.java
deleted file mode 100644
index 4647b9c..0000000
--- a/codec/src/main/java/org/apache/mina/codec/delimited/serialization/JavaNativeTranscoder.java
+++ /dev/null
@@ -1,97 +0,0 @@
-/*
- * Licensed to the Apache Software Foundation (ASF) under one
- * or more contributor license agreements. See the NOTICE file
- * distributed with this work for additional information
- * regarding copyright ownership. The ASF 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 org.apache.mina.codec.delimited.serialization;
-
-import java.io.IOException;
-import java.io.ObjectInputStream;
-import java.io.ObjectOutputStream;
-import java.io.Serializable;
-import java.nio.ByteBuffer;
-
-import org.apache.mina.codec.ProtocolDecoderException;
-import org.apache.mina.codec.delimited.Transcoder;
-import org.apache.mina.util.ByteBufferInputStream;
-import org.apache.mina.util.ByteBufferOutputStream;
-
-/**
- * Transcoder providing the built-in Java-serialization.
- *
- * @author <a href="http://mina.apache.org">Apache MINA Project</a>
- *
- */
-public class JavaNativeTranscoder<S extends Serializable> extends Transcoder<S,S> {
-
- public static <TYPE extends Serializable> JavaNativeTranscoder<TYPE> newInstance(Class<TYPE> cl) {
- return new JavaNativeTranscoder<TYPE>();
- }
-
- @Override
- public ByteBuffer encode(S message) {
- // avoid the copy done in Transcoder
- return serialize(message);
- }
-
- private S lastObject;
-
- private ByteBuffer lastSerialized;
-
- @SuppressWarnings("unchecked")
- @Override
- public S decode(final ByteBuffer input) throws ProtocolDecoderException {
- try {
- ObjectInputStream ois = new ObjectInputStream(new ByteBufferInputStream(input));
- S s = (S) ois.readObject();
- ois.close();
- return s;
- } catch (ClassNotFoundException cnfe) {
- throw new ProtocolDecoderException(cnfe);
- } catch (IOException ioe) {
- throw new ProtocolDecoderException(ioe);
- }
- }
-
- @Override
- public int getEncodedSize(S message) {
- return serialize(message).remaining();
- }
-
- private ByteBuffer serialize(S message) {
- if (message != lastObject) {
- ByteBufferOutputStream ebbosa = new ByteBufferOutputStream();
- ebbosa.setElastic(true);
- try {
- ObjectOutputStream oos = new ObjectOutputStream(ebbosa);
- oos.writeObject(message);
- oos.close();
- lastObject = message;
- lastSerialized = ebbosa.getByteBuffer();
- } catch (IOException e) {
- throw new RuntimeException("");
- }
- }
- return lastSerialized;
- }
-
- @Override
- public void writeTo(S message, ByteBuffer buffer) {
- buffer.put(serialize(message));
- }
-
-}
http://git-wip-us.apache.org/repos/asf/mina/blob/31f4baf1/codec/src/main/java/org/apache/mina/codec/delimited/serialization/ProtobufDecoder.java
----------------------------------------------------------------------
diff --git a/codec/src/main/java/org/apache/mina/codec/delimited/serialization/ProtobufDecoder.java b/codec/src/main/java/org/apache/mina/codec/delimited/serialization/ProtobufDecoder.java
new file mode 100644
index 0000000..76deb68
--- /dev/null
+++ b/codec/src/main/java/org/apache/mina/codec/delimited/serialization/ProtobufDecoder.java
@@ -0,0 +1,67 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF 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 org.apache.mina.codec.delimited.serialization;
+
+import java.io.InputStream;
+import java.lang.reflect.Method;
+import java.nio.ByteBuffer;
+
+import org.apache.mina.codec.ProtocolDecoderException;
+import org.apache.mina.codec.delimited.ByteBufferDecoder;
+import org.apache.mina.util.ByteBufferInputStream;
+
+import com.google.protobuf.ExtensionRegistryLite;
+import com.google.protobuf.GeneratedMessage;
+
+/**
+ * @author <a href="http://mina.apache.org">Apache MINA Project</a>
+ *
+ */
+public class ProtobufDecoder<IN extends GeneratedMessage> extends ByteBufferDecoder<IN> {
+ final private Method parseMethod;
+
+ final private ExtensionRegistryLite registry;
+
+ public static <TYPE extends GeneratedMessage> ProtobufDecoder<TYPE> newInstance(Class<TYPE> c) throws SecurityException,
+ NoSuchMethodException {
+ return newInstance(c, ExtensionRegistryLite.getEmptyRegistry());
+ }
+
+ public static <TYPE extends GeneratedMessage> ProtobufDecoder<TYPE> newInstance(Class<TYPE> c, ExtensionRegistryLite registry)
+ throws SecurityException, NoSuchMethodException {
+ return new ProtobufDecoder<TYPE>(c, registry);
+ }
+
+ private ProtobufDecoder(Class<IN> clazz, ExtensionRegistryLite registry) throws SecurityException, NoSuchMethodException {
+ super();
+ parseMethod = clazz.getDeclaredMethod("parseFrom", InputStream.class, ExtensionRegistryLite.class);
+ this.registry = registry;
+ }
+
+ @SuppressWarnings("unchecked")
+ @Override
+ public IN decode(ByteBuffer input) throws ProtocolDecoderException {
+ try {
+ return (IN) parseMethod.invoke(null, new ByteBufferInputStream(input), registry);
+ } catch (Exception e) {
+ throw new ProtocolDecoderException(e);
+ }
+ }
+}
http://git-wip-us.apache.org/repos/asf/mina/blob/31f4baf1/codec/src/main/java/org/apache/mina/codec/delimited/serialization/ProtobufDynamicDecoder.java
----------------------------------------------------------------------
diff --git a/codec/src/main/java/org/apache/mina/codec/delimited/serialization/ProtobufDynamicDecoder.java b/codec/src/main/java/org/apache/mina/codec/delimited/serialization/ProtobufDynamicDecoder.java
new file mode 100644
index 0000000..cf0fa36
--- /dev/null
+++ b/codec/src/main/java/org/apache/mina/codec/delimited/serialization/ProtobufDynamicDecoder.java
@@ -0,0 +1,69 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF 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 org.apache.mina.codec.delimited.serialization;
+
+import java.io.InputStream;
+import java.lang.reflect.InvocationTargetException;
+import java.lang.reflect.Method;
+import java.nio.ByteBuffer;
+
+import org.apache.mina.codec.ProtocolDecoderException;
+import org.apache.mina.codec.delimited.ByteBufferDecoder;
+import org.apache.mina.util.ByteBufferInputStream;
+
+import com.google.protobuf.ExtensionRegistryLite;
+
+/**
+ * An alternative decoder for protobuf which allows the use various target
+ * classes with the same decoder.
+ *
+ * This decoder converts incoming {@link ByteBuffer} into {@link SerializedMessage}.
+ *
+ * @author <a href="http://mina.apache.org">Apache MINA Project</a>
+ *
+ */
+public class ProtobufDynamicDecoder extends ByteBufferDecoder<ProtobufDynamicDecoder.SerializedMessage> {
+
+ @Override
+ public SerializedMessage decode(ByteBuffer input) throws ProtocolDecoderException {
+ return new SerializedMessage(input);
+ }
+
+ final static public class SerializedMessage {
+ final private ByteBuffer input;
+
+ public SerializedMessage(ByteBuffer input) {
+ this.input = input;
+ }
+
+ @SuppressWarnings("unchecked")
+ public <L> L get(Class<L> clazz, ExtensionRegistryLite registry) throws SecurityException,
+ NoSuchMethodException, IllegalArgumentException, IllegalAccessException, InvocationTargetException {
+ Method parseMethod = clazz.getDeclaredMethod("parseFrom", InputStream.class, ExtensionRegistryLite.class);
+ return (L) parseMethod.invoke(null, new ByteBufferInputStream(input.duplicate()), registry);
+ }
+
+ public <L> L get(Class<L> clazz) throws SecurityException, NoSuchMethodException, IllegalArgumentException,
+ IllegalAccessException, InvocationTargetException {
+ return get(clazz, ExtensionRegistryLite.getEmptyRegistry());
+ }
+ }
+
+}
http://git-wip-us.apache.org/repos/asf/mina/blob/31f4baf1/codec/src/main/java/org/apache/mina/codec/delimited/serialization/ProtobufEncoder.java
----------------------------------------------------------------------
diff --git a/codec/src/main/java/org/apache/mina/codec/delimited/serialization/ProtobufEncoder.java b/codec/src/main/java/org/apache/mina/codec/delimited/serialization/ProtobufEncoder.java
new file mode 100644
index 0000000..ccbc041
--- /dev/null
+++ b/codec/src/main/java/org/apache/mina/codec/delimited/serialization/ProtobufEncoder.java
@@ -0,0 +1,40 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF 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 org.apache.mina.codec.delimited.serialization;
+
+import java.nio.ByteBuffer;
+
+import org.apache.mina.codec.delimited.ByteBufferEncoder;
+
+import com.google.protobuf.GeneratedMessage;
+
+public class ProtobufEncoder<OUT extends GeneratedMessage> extends ByteBufferEncoder<OUT> {
+
+ @Override
+ public int getEncodedSize(OUT message) {
+ return message.getSerializedSize();
+ }
+
+ @Override
+ public void writeTo(OUT message, ByteBuffer buffer) {
+ buffer.put(message.toByteArray());
+ }
+
+}
\ No newline at end of file
http://git-wip-us.apache.org/repos/asf/mina/blob/31f4baf1/codec/src/main/java/org/apache/mina/codec/delimited/serialization/ProtobufTranscoder.java
----------------------------------------------------------------------
diff --git a/codec/src/main/java/org/apache/mina/codec/delimited/serialization/ProtobufTranscoder.java b/codec/src/main/java/org/apache/mina/codec/delimited/serialization/ProtobufTranscoder.java
deleted file mode 100644
index 2624285..0000000
--- a/codec/src/main/java/org/apache/mina/codec/delimited/serialization/ProtobufTranscoder.java
+++ /dev/null
@@ -1,57 +0,0 @@
-package org.apache.mina.codec.delimited.serialization;
-
-import java.io.InputStream;
-import java.lang.reflect.Method;
-import java.nio.ByteBuffer;
-
-import org.apache.mina.codec.ProtocolDecoderException;
-import org.apache.mina.codec.delimited.Transcoder;
-import org.apache.mina.util.ByteBufferInputStream;
-
-import com.google.protobuf.ExtensionRegistry;
-import com.google.protobuf.ExtensionRegistryLite;
-import com.google.protobuf.GeneratedMessage;
-
-public class ProtobufTranscoder<TYPE extends GeneratedMessage> extends Transcoder<TYPE,TYPE> {
-
- final private Method parseMethod;
-
- final private ExtensionRegistry registry;
-
- public static <TYPE extends GeneratedMessage> ProtobufTranscoder<TYPE> newInstance(Class<TYPE> c)
- throws SecurityException, NoSuchMethodException {
- return newInstance(c, ExtensionRegistry.getEmptyRegistry());
- }
-
- public static <TYPE extends GeneratedMessage> ProtobufTranscoder<TYPE> newInstance(Class<TYPE> c,
- ExtensionRegistry registry) throws SecurityException, NoSuchMethodException {
- return new ProtobufTranscoder<TYPE>(c, registry);
- }
-
- private ProtobufTranscoder(Class<TYPE> clazz, ExtensionRegistry registry) throws SecurityException,
- NoSuchMethodException {
- super();
- parseMethod = clazz.getDeclaredMethod("parseFrom", InputStream.class, ExtensionRegistryLite.class);
- this.registry = registry;
- }
-
- @SuppressWarnings("unchecked")
- @Override
- public TYPE decode(ByteBuffer input) throws ProtocolDecoderException {
- try {
- return (TYPE) parseMethod.invoke(null, new ByteBufferInputStream(input), registry);
- } catch (Exception e) {
- throw new ProtocolDecoderException(e);
- }
- }
-
- @Override
- public int getEncodedSize(TYPE message) {
- return message.getSerializedSize();
- }
-
- @Override
- public void writeTo(TYPE message, ByteBuffer buffer) {
- buffer.put(message.toByteArray());
- }
-}
http://git-wip-us.apache.org/repos/asf/mina/blob/31f4baf1/codec/src/test/java/org/apache/mina/codec/delimited/TestSizePrefixedEncoder.java
----------------------------------------------------------------------
diff --git a/codec/src/test/java/org/apache/mina/codec/delimited/TestSizePrefixedEncoder.java b/codec/src/test/java/org/apache/mina/codec/delimited/TestSizePrefixedEncoder.java
index 94ff64e..44276c5 100644
--- a/codec/src/test/java/org/apache/mina/codec/delimited/TestSizePrefixedEncoder.java
+++ b/codec/src/test/java/org/apache/mina/codec/delimited/TestSizePrefixedEncoder.java
@@ -2,7 +2,7 @@ package org.apache.mina.codec.delimited;
import java.nio.ByteBuffer;
-import org.apache.mina.codec.delimited.ints.VarIntTranscoder;
+import org.apache.mina.codec.delimited.ints.VarInt;
import org.junit.Test;
public class TestSizePrefixedEncoder {
http://git-wip-us.apache.org/repos/asf/mina/blob/31f4baf1/codec/src/test/java/org/apache/mina/codec/delimited/ints/IntEncodingTest.java
----------------------------------------------------------------------
diff --git a/codec/src/test/java/org/apache/mina/codec/delimited/ints/IntEncodingTest.java b/codec/src/test/java/org/apache/mina/codec/delimited/ints/IntEncodingTest.java
index a3ef049..d01cf89 100644
--- a/codec/src/test/java/org/apache/mina/codec/delimited/ints/IntEncodingTest.java
+++ b/codec/src/test/java/org/apache/mina/codec/delimited/ints/IntEncodingTest.java
@@ -27,7 +27,8 @@ import java.nio.ByteBuffer;
import java.util.Map;
import org.apache.mina.codec.ProtocolDecoderException;
-import org.apache.mina.codec.delimited.Transcoder;
+import org.apache.mina.codec.delimited.ByteBufferDecoder;
+import org.apache.mina.codec.delimited.ByteBufferEncoder;
import org.junit.Before;
import org.junit.Test;
@@ -37,13 +38,13 @@ import org.junit.Test;
*/
abstract public class IntEncodingTest {
- protected Transcoder<Integer,Integer> encoder;
+ protected ByteBufferEncoder<Integer> encoder;
- protected Transcoder<Integer,Integer> decoder;
+ protected ByteBufferDecoder<Integer> decoder;
- public abstract Transcoder<Integer,Integer> newDecoderInstance();
+ public abstract ByteBufferDecoder<Integer> newDecoderInstance();
- public abstract Transcoder<Integer,Integer> newEncoderInstance();
+ public abstract ByteBufferEncoder<Integer> newEncoderInstance();
public abstract Map<Integer, ByteBuffer> getEncodingSamples();
http://git-wip-us.apache.org/repos/asf/mina/blob/31f4baf1/codec/src/test/java/org/apache/mina/codec/delimited/ints/RawInt32BigEndianEncodingTest.java
----------------------------------------------------------------------
diff --git a/codec/src/test/java/org/apache/mina/codec/delimited/ints/RawInt32BigEndianEncodingTest.java b/codec/src/test/java/org/apache/mina/codec/delimited/ints/RawInt32BigEndianEncodingTest.java
index ff3889f..1f64ee1 100644
--- a/codec/src/test/java/org/apache/mina/codec/delimited/ints/RawInt32BigEndianEncodingTest.java
+++ b/codec/src/test/java/org/apache/mina/codec/delimited/ints/RawInt32BigEndianEncodingTest.java
@@ -25,7 +25,8 @@ import java.util.LinkedList;
import java.util.List;
import java.util.Map;
-import org.apache.mina.codec.delimited.Transcoder;
+import org.apache.mina.codec.delimited.ByteBufferDecoder;
+import org.apache.mina.codec.delimited.ByteBufferEncoder;
/**
* A {@link Int32Decoder} and {@link Int32Encoder} test in a big endian setup.
@@ -35,13 +36,13 @@ import org.apache.mina.codec.delimited.Transcoder;
public class RawInt32BigEndianEncodingTest extends IntEncodingTest {
@Override
- public Transcoder<Integer,Integer> newDecoderInstance() {
- return new RawInt32Transcoder(RawInt32Transcoder.Endianness.BIG);
+ public ByteBufferDecoder<Integer> newDecoderInstance() {
+ return new RawInt32.Decoder(RawInt32.Endianness.BIG);
}
@Override
- public Transcoder<Integer,Integer> newEncoderInstance() {
- return new RawInt32Transcoder(RawInt32Transcoder.Endianness.BIG);
+ public ByteBufferEncoder<Integer> newEncoderInstance() {
+ return new RawInt32.Encoder(RawInt32.Endianness.BIG);
}
@Override
http://git-wip-us.apache.org/repos/asf/mina/blob/31f4baf1/codec/src/test/java/org/apache/mina/codec/delimited/ints/RawInt32LittleEndianEncodingTest.java
----------------------------------------------------------------------
diff --git a/codec/src/test/java/org/apache/mina/codec/delimited/ints/RawInt32LittleEndianEncodingTest.java b/codec/src/test/java/org/apache/mina/codec/delimited/ints/RawInt32LittleEndianEncodingTest.java
index 4b8552a..63d5b8b 100644
--- a/codec/src/test/java/org/apache/mina/codec/delimited/ints/RawInt32LittleEndianEncodingTest.java
+++ b/codec/src/test/java/org/apache/mina/codec/delimited/ints/RawInt32LittleEndianEncodingTest.java
@@ -25,7 +25,8 @@ import java.util.LinkedList;
import java.util.List;
import java.util.Map;
-import org.apache.mina.codec.delimited.Transcoder;
+import org.apache.mina.codec.delimited.ByteBufferDecoder;
+import org.apache.mina.codec.delimited.ByteBufferEncoder;
/**
@@ -36,13 +37,13 @@ import org.apache.mina.codec.delimited.Transcoder;
public class RawInt32LittleEndianEncodingTest extends IntEncodingTest {
@Override
- public Transcoder<Integer,Integer> newDecoderInstance() {
- return new RawInt32Transcoder(RawInt32Transcoder.Endianness.LITTLE);
+ public ByteBufferDecoder<Integer> newDecoderInstance() {
+ return new RawInt32.Decoder(RawInt32.Endianness.LITTLE);
}
@Override
- public Transcoder<Integer,Integer> newEncoderInstance() {
- return new RawInt32Transcoder(RawInt32Transcoder.Endianness.LITTLE);
+ public ByteBufferEncoder<Integer> newEncoderInstance() {
+ return new RawInt32.Encoder(RawInt32.Endianness.LITTLE);
}
@Override
http://git-wip-us.apache.org/repos/asf/mina/blob/31f4baf1/codec/src/test/java/org/apache/mina/codec/delimited/ints/VarIntEncodingTest.java
----------------------------------------------------------------------
diff --git a/codec/src/test/java/org/apache/mina/codec/delimited/ints/VarIntEncodingTest.java b/codec/src/test/java/org/apache/mina/codec/delimited/ints/VarIntEncodingTest.java
index 3985240..fcd1f17 100644
--- a/codec/src/test/java/org/apache/mina/codec/delimited/ints/VarIntEncodingTest.java
+++ b/codec/src/test/java/org/apache/mina/codec/delimited/ints/VarIntEncodingTest.java
@@ -25,8 +25,8 @@ import java.util.LinkedList;
import java.util.List;
import java.util.Map;
-import org.apache.mina.codec.delimited.Transcoder;
-
+import org.apache.mina.codec.delimited.ByteBufferDecoder;
+import org.apache.mina.codec.delimited.ByteBufferEncoder;
/**
* A {@link VarIntDecoder} and {@link VarIntEncoder} test.
@@ -36,13 +36,13 @@ import org.apache.mina.codec.delimited.Transcoder;
public class VarIntEncodingTest extends IntEncodingTest {
@Override
- public Transcoder<Integer,Integer> newDecoderInstance() {
- return new VarIntTranscoder();
+ public ByteBufferDecoder<Integer> newDecoderInstance() {
+ return new VarInt.Decoder();
}
@Override
- public Transcoder<Integer,Integer> newEncoderInstance() {
- return new VarIntTranscoder();
+ public ByteBufferEncoder<Integer> newEncoderInstance() {
+ return new VarInt.Encoder();
}
@Override
http://git-wip-us.apache.org/repos/asf/mina/blob/31f4baf1/codec/src/test/java/org/apache/mina/codec/delimited/serialization/GenericSerializerTest.java
----------------------------------------------------------------------
diff --git a/codec/src/test/java/org/apache/mina/codec/delimited/serialization/GenericSerializerTest.java b/codec/src/test/java/org/apache/mina/codec/delimited/serialization/GenericSerializerTest.java
new file mode 100644
index 0000000..350a2bc
--- /dev/null
+++ b/codec/src/test/java/org/apache/mina/codec/delimited/serialization/GenericSerializerTest.java
@@ -0,0 +1,41 @@
+package org.apache.mina.codec.delimited.serialization;
+
+import static org.junit.Assert.assertEquals;
+
+import java.nio.ByteBuffer;
+import java.util.List;
+
+import org.apache.mina.codec.delimited.ByteBufferDecoder;
+import org.apache.mina.codec.delimited.ByteBufferEncoder;
+import org.junit.Test;
+
+abstract public class GenericSerializerTest<T, U> {
+
+ abstract public ByteBufferDecoder<T> getDecoder() throws Exception;
+
+ abstract public ByteBufferEncoder<T> getEncoder() throws Exception;
+
+ abstract public List<T> getObjects();
+
+ @Test
+ public void testSerialization() throws Exception {
+ ByteBufferDecoder<T> decoder = getDecoder();
+ ByteBufferEncoder<T> encoder = getEncoder();
+ for (T object : getObjects())
+ assertEquals(object, decoder.decode(encoder.encode(object)));
+ }
+
+ @Test
+ public void testEncodedSize() throws Exception {
+ ByteBufferDecoder<T> decoder = getDecoder();
+ ByteBufferEncoder<T> encoder = getEncoder();
+ for (T object : getObjects()) {
+ int size = encoder.getEncodedSize(object);
+ ByteBuffer out = ByteBuffer.allocate(size);
+ encoder.writeTo(object, out);
+ assertEquals(size, out.position());
+ out.position(0);
+ assertEquals(object, decoder.decode(out));
+ }
+ }
+}
http://git-wip-us.apache.org/repos/asf/mina/blob/31f4baf1/codec/src/test/java/org/apache/mina/codec/delimited/serialization/JavaNativeTest.java
----------------------------------------------------------------------
diff --git a/codec/src/test/java/org/apache/mina/codec/delimited/serialization/JavaNativeTest.java b/codec/src/test/java/org/apache/mina/codec/delimited/serialization/JavaNativeTest.java
new file mode 100644
index 0000000..8db77da
--- /dev/null
+++ b/codec/src/test/java/org/apache/mina/codec/delimited/serialization/JavaNativeTest.java
@@ -0,0 +1,101 @@
+package org.apache.mina.codec.delimited.serialization;
+
+import java.io.Serializable;
+import java.util.LinkedList;
+import java.util.List;
+
+import org.apache.mina.codec.delimited.ByteBufferDecoder;
+import org.apache.mina.codec.delimited.ByteBufferEncoder;
+
+public class JavaNativeTest extends
+ GenericSerializerTest<JavaNativeTest.TestBean, JavaNativeTest.TestBean> {
+ static public class TestBean implements Serializable {
+ private static final long serialVersionUID = 1L;
+
+ @Override
+ public int hashCode() {
+ final int prime = 31;
+ int result = 1;
+ result = prime * result + ((a == null) ? 0 : a.hashCode());
+ result = prime * result + b;
+ long temp;
+ temp = Double.doubleToLongBits(c);
+ result = prime * result + (int) (temp ^ (temp >>> 32));
+ return result;
+ }
+
+ @Override
+ public boolean equals(Object obj) {
+ if (this == obj)
+ return true;
+ if (obj == null)
+ return false;
+ if (getClass() != obj.getClass())
+ return false;
+ TestBean other = (TestBean) obj;
+ if (a == null) {
+ if (other.a != null)
+ return false;
+ } else if (!a.equals(other.a))
+ return false;
+ if (b != other.b)
+ return false;
+ if (Double.doubleToLongBits(c) != Double.doubleToLongBits(other.c))
+ return false;
+ return true;
+ }
+
+ public TestBean(String a, int b, double c) {
+ super();
+ this.a = a;
+ this.b = b;
+ this.c = c;
+ }
+
+ private String a;
+
+ private int b;
+
+ private double c;
+ }
+
+ // @Test
+ // public void testSerialization() {
+ // Transcoder<TestBean> transcoder = new JavaNativeTranscoder<TestBean>();
+ // TestBean bean1 = new TestBean("Hello", 86, 12.34);
+ // TestBean bean2 = new TestBean("MINA", 94, 67.89);
+ // assertEquals(bean1, transcoder.decode(transcoder.encode(bean1)));
+ // assertEquals(bean2, transcoder.decode(transcoder.encode(bean2)));
+ // }
+ //
+ // @Test
+ // public void testEncodedSize() {
+ // Transcoder<TestBean> transcoder = new JavaNativeTranscoder<TestBean>();
+ // TestBean bean = new TestBean("Hello", 86, 12.34);
+ // int size = transcoder.getEncodedSize(bean);
+ // ByteBuffer out = ByteBuffer.allocate(size);
+ // transcoder.writeTo(bean, out);
+ // assertEquals(size, out.position());
+ // out.position(0);
+ // assertEquals(bean, transcoder.decode(out));
+ // }
+
+ @Override
+ public List<TestBean> getObjects() {
+ List<TestBean> list = new LinkedList<TestBean>();
+ list.add(new TestBean("Hello", 86, 12.34));
+ list.add(new TestBean("MINA", 94, 67.89));
+ return list;
+ }
+
+ @Override
+ public ByteBufferDecoder<TestBean> getDecoder() throws Exception {
+ return new JavaNativeDecoder<TestBean>();
+ }
+
+ @Override
+ public ByteBufferEncoder<TestBean> getEncoder() throws Exception {
+ return new JavaNativeEncoder<TestBean>();
+ }
+
+}
http://git-wip-us.apache.org/repos/asf/mina/blob/31f4baf1/codec/src/test/java/org/apache/mina/codec/delimited/serialization/JavaNativeTranscoderTest.java
----------------------------------------------------------------------
diff --git a/codec/src/test/java/org/apache/mina/codec/delimited/serialization/JavaNativeTranscoderTest.java b/codec/src/test/java/org/apache/mina/codec/delimited/serialization/JavaNativeTranscoderTest.java
deleted file mode 100644
index 8c26fd0..0000000
--- a/codec/src/test/java/org/apache/mina/codec/delimited/serialization/JavaNativeTranscoderTest.java
+++ /dev/null
@@ -1,94 +0,0 @@
-package org.apache.mina.codec.delimited.serialization;
-
-import java.io.Serializable;
-import java.util.LinkedList;
-import java.util.List;
-
-import org.apache.mina.codec.delimited.Transcoder;
-
-public class JavaNativeTranscoderTest extends TranscoderTest<JavaNativeTranscoderTest.TestBean,JavaNativeTranscoderTest.TestBean> {
- static public class TestBean implements Serializable {
- private static final long serialVersionUID = 1L;
-
- @Override
- public int hashCode() {
- final int prime = 31;
- int result = 1;
- result = prime * result + ((a == null) ? 0 : a.hashCode());
- result = prime * result + b;
- long temp;
- temp = Double.doubleToLongBits(c);
- result = prime * result + (int) (temp ^ (temp >>> 32));
- return result;
- }
-
- @Override
- public boolean equals(Object obj) {
- if (this == obj)
- return true;
- if (obj == null)
- return false;
- if (getClass() != obj.getClass())
- return false;
- TestBean other = (TestBean) obj;
- if (a == null) {
- if (other.a != null)
- return false;
- } else if (!a.equals(other.a))
- return false;
- if (b != other.b)
- return false;
- if (Double.doubleToLongBits(c) != Double.doubleToLongBits(other.c))
- return false;
- return true;
- }
-
- public TestBean(String a, int b, double c) {
- super();
- this.a = a;
- this.b = b;
- this.c = c;
- }
-
- private String a;
-
- private int b;
-
- private double c;
- }
-
-// @Test
-// public void testSerialization() {
-// Transcoder<TestBean> transcoder = new JavaNativeTranscoder<TestBean>();
-// TestBean bean1 = new TestBean("Hello", 86, 12.34);
-// TestBean bean2 = new TestBean("MINA", 94, 67.89);
-// assertEquals(bean1, transcoder.decode(transcoder.encode(bean1)));
-// assertEquals(bean2, transcoder.decode(transcoder.encode(bean2)));
-// }
-//
-// @Test
-// public void testEncodedSize() {
-// Transcoder<TestBean> transcoder = new JavaNativeTranscoder<TestBean>();
-// TestBean bean = new TestBean("Hello", 86, 12.34);
-// int size = transcoder.getEncodedSize(bean);
-// ByteBuffer out = ByteBuffer.allocate(size);
-// transcoder.writeTo(bean, out);
-// assertEquals(size, out.position());
-// out.position(0);
-// assertEquals(bean, transcoder.decode(out));
-// }
-
- @Override
- public Transcoder<TestBean,TestBean> getTranscoderInstance() {
- return JavaNativeTranscoder.newInstance(TestBean.class);
- }
-
- @Override
- public List<TestBean> getObjects() {
- List<TestBean> list = new LinkedList<TestBean>();
- list.add(new TestBean("Hello", 86, 12.34));
- list.add(new TestBean("MINA", 94, 67.89));
- return list;
- }
-
-}
http://git-wip-us.apache.org/repos/asf/mina/blob/31f4baf1/codec/src/test/java/org/apache/mina/codec/delimited/serialization/ProtobufTest.java
----------------------------------------------------------------------
diff --git a/codec/src/test/java/org/apache/mina/codec/delimited/serialization/ProtobufTest.java b/codec/src/test/java/org/apache/mina/codec/delimited/serialization/ProtobufTest.java
new file mode 100644
index 0000000..f712abb
--- /dev/null
+++ b/codec/src/test/java/org/apache/mina/codec/delimited/serialization/ProtobufTest.java
@@ -0,0 +1,47 @@
+package org.apache.mina.codec.delimited.serialization;
+
+
+import static org.junit.Assert.assertEquals;
+
+import java.util.LinkedList;
+import java.util.List;
+
+import org.apache.mina.codec.delimited.ByteBufferDecoder;
+import org.apache.mina.codec.delimited.ByteBufferEncoder;
+import org.apache.mina.codec.delimited.serialization.ProtobufDynamicDecoder.SerializedMessage;
+import org.junit.Test;
+
+import com.google.protobuf.DescriptorProtos.DescriptorProto;
+
+
+public class ProtobufTest extends GenericSerializerTest<DescriptorProto, DescriptorProto> {
+
+ @Override
+ public List<DescriptorProto> getObjects() {
+ List<DescriptorProto> list = new LinkedList<DescriptorProto>();
+ list.add(DescriptorProto.newBuilder().setName("HELLO").build());
+ list.add(DescriptorProto.newBuilder().setName("WORLD").build());
+ return list;
+ }
+
+ @Override
+ public ByteBufferDecoder<DescriptorProto> getDecoder() throws Exception {
+ return ProtobufDecoder.newInstance(DescriptorProto.class);
+ }
+
+ @Override
+ public ByteBufferEncoder<DescriptorProto> getEncoder() throws Exception {
+ return new ProtobufEncoder<DescriptorProto>();
+ }
+
+ @Test
+ public void testDynamic() throws Exception {
+ ByteBufferEncoder<DescriptorProto> encoder = getEncoder();
+ ProtobufDynamicDecoder decoder = new ProtobufDynamicDecoder();
+
+ for (DescriptorProto object : getObjects()) {
+ SerializedMessage message=decoder.decode(encoder.encode(object));
+ assertEquals(object,message.get(DescriptorProto.class));
+ }
+ }
+}