summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorBryan Duxbury <bryanduxbury@apache.org>2010-08-06 00:18:25 +0000
committerBryan Duxbury <bryanduxbury@apache.org>2010-08-06 00:18:25 +0000
commitf5abd26858f6839c9b8853c8fe810b94b310d1ad (patch)
treea8e94995d36edfcb9318d8cb0c8f6c2ee1542506
parent7f3285eddb0c4e1f58e2facacd47cf992e50c34c (diff)
downloadthrift-f5abd26858f6839c9b8853c8fe810b94b310d1ad.tar.gz
THRIFT-830. java: Switch binary field implementation from byte[] to ByteBuffer
This patch switches the implementation of binary fields to ByteBuffer in the Java generated code, yielding up to 2.5x speed boost. git-svn-id: https://svn.apache.org/repos/asf/incubator/thrift/trunk@982839 13f79535-47bb-0310-9956-ffa450edef68
-rw-r--r--compiler/cpp/src/generate/t_java_generator.cc21
-rw-r--r--lib/java/build.xml2
-rw-r--r--lib/java/src/org/apache/thrift/TBaseHelper.java23
-rw-r--r--lib/java/src/org/apache/thrift/TDeserializer.java5
-rw-r--r--lib/java/src/org/apache/thrift/protocol/TBinaryProtocol.java19
-rwxr-xr-xlib/java/src/org/apache/thrift/protocol/TCompactProtocol.java21
-rw-r--r--lib/java/src/org/apache/thrift/protocol/TJSONProtocol.java22
-rw-r--r--lib/java/src/org/apache/thrift/protocol/TProtocol.java6
-rw-r--r--lib/java/src/org/apache/thrift/protocol/TSimpleJSONProtocol.java9
-rw-r--r--lib/java/test/org/apache/thrift/Fixtures.java5
-rw-r--r--lib/java/test/org/apache/thrift/TestStruct.java89
-rw-r--r--lib/java/test/org/apache/thrift/TestTDeserializer.java10
-rw-r--r--lib/java/test/org/apache/thrift/TestTUnion.java19
-rw-r--r--lib/java/test/org/apache/thrift/protocol/ProtocolTestBase.java9
-rw-r--r--lib/java/test/org/apache/thrift/test/EqualityTest.java68
-rw-r--r--lib/java/test/org/apache/thrift/test/JavaBeansTest.java4
-rw-r--r--lib/java/test/org/apache/thrift/test/ToStringTest.java92
17 files changed, 207 insertions, 217 deletions
diff --git a/compiler/cpp/src/generate/t_java_generator.cc b/compiler/cpp/src/generate/t_java_generator.cc
index 298809a4e..0248cf78a 100644
--- a/compiler/cpp/src/generate/t_java_generator.cc
+++ b/compiler/cpp/src/generate/t_java_generator.cc
@@ -322,6 +322,7 @@ string t_java_generator::java_type_imports() {
"import java.util.EnumSet;\n" +
"import java.util.Collections;\n" +
"import java.util.BitSet;\n" +
+ "import java.nio.ByteBuffer;\n"
"import java.util.Arrays;\n" +
"import org.slf4j.Logger;\n" +
"import org.slf4j.LoggerFactory;\n\n";
@@ -1002,8 +1003,7 @@ void t_java_generator::generate_union_comparisons(ofstream& out, t_struct* tstru
out << endl;
indent(out) << "public boolean equals(" << tstruct->get_name() << " other) {" << endl;
- indent(out) << " return other != null && getSetField() == other.getSetField() && ((value_ instanceof byte[]) ? " << endl;
- indent(out) << " Arrays.equals((byte[])getFieldValue(), (byte[])other.getFieldValue()) : getFieldValue().equals(other.getFieldValue()));" << endl;
+ indent(out) << " return other != null && getSetField() == other.getSetField() && getFieldValue().equals(other.getFieldValue());" << endl;
indent(out) << "}" << endl;
out << endl;
@@ -1312,7 +1312,7 @@ void t_java_generator::generate_java_struct_equality(ofstream& out,
indent() << " return false;" << endl;
if (t->is_base_type() && ((t_base_type*)t)->is_binary()) {
- unequal = "!java.util.Arrays.equals(this." + name + ", that." + name + ")";
+ unequal = "!this." + name + ".equals(that." + name + ")";
} else if (can_be_null) {
unequal = "!this." + name + ".equals(that." + name + ")";
} else {
@@ -1965,12 +1965,7 @@ void t_java_generator::generate_java_struct_tostring(ofstream& out,
}
if (field->get_type()->is_base_type() && ((t_base_type*)(field->get_type()))->is_binary()) {
- indent(out) << " int __" << field->get_name() << "_size = Math.min(this." << field->get_name() << ".length, 128);" << endl;
- indent(out) << " for (int i = 0; i < __" << field->get_name() << "_size; i++) {" << endl;
- indent(out) << " if (i != 0) sb.append(\" \");" << endl;
- indent(out) << " sb.append(Integer.toHexString(this." << field->get_name() << "[i]).length() > 1 ? Integer.toHexString(this." << field->get_name() << "[i]).substring(Integer.toHexString(this." << field->get_name() << "[i]).length() - 2).toUpperCase() : \"0\" + Integer.toHexString(this." << field->get_name() << "[i]).toUpperCase());" <<endl;
- indent(out) << " }" << endl;
- indent(out) << " if (this." << field->get_name() << ".length > 128) sb.append(\" ...\");" << endl;
+ indent(out) << "TBaseHelper.toString(this." << field->get_name() << ", sb);" << endl;
} else {
indent(out) << "sb.append(this." << (*f_iter)->get_name() << ");" << endl;
}
@@ -3313,7 +3308,7 @@ string t_java_generator::base_type_name(t_base_type* type,
return "void";
case t_base_type::TYPE_STRING:
if (type->is_binary()) {
- return "byte[]";
+ return "ByteBuffer";
} else {
return "String";
}
@@ -3691,7 +3686,7 @@ void t_java_generator::generate_deep_copy_container(ofstream &out, std::string s
} else {
// iterative copy
if(((t_base_type*)elem_type)->is_binary()){
- indent(out) << "byte[] temp_binary_element = ";
+ indent(out) << "ByteBuffer temp_binary_element = ";
generate_deep_copy_non_container(out, iterator_element_name, "temp_binary_element", elem_type);
out << ";" << endl;
indent(out) << result_name << ".add(temp_binary_element);" << endl;
@@ -3714,8 +3709,8 @@ void t_java_generator::generate_deep_copy_non_container(ofstream& out, std::stri
if (type->is_base_type() || type->is_enum() || type->is_typedef()) {
// binary fields need to be copied with System.arraycopy
if (((t_base_type*)type)->is_binary()){
- out << "new byte[" << source_name << ".length];" << endl;
- indent(out) << "System.arraycopy(" << source_name << ", 0, " << dest_name << ", 0, " << source_name << ".length)";
+ out << "ByteBuffer.wrap(new byte[" << source_name << ".limit() - " << source_name << ".arrayOffset()]);" << endl;
+ indent(out) << "System.arraycopy(" << source_name << ".array(), " << source_name << ".arrayOffset(), " << dest_name << ".array(), 0, " << source_name << ".limit() - " << source_name << ".arrayOffset())";
}
// everything else can be copied directly
else
diff --git a/lib/java/build.xml b/lib/java/build.xml
index b68c95a0a..aca4bea2c 100644
--- a/lib/java/build.xml
+++ b/lib/java/build.xml
@@ -204,8 +204,6 @@
<target name="deprecated-test" description="Run the non-JUnit test suite" depends="compile-test">
<java classname="org.apache.thrift.test.EqualityTest"
classpathref="test.classpath" failonerror="true" />
- <java classname="org.apache.thrift.test.ToStringTest"
- classpathref="test.classpath" failonerror="true" />
<java classname="org.apache.thrift.test.JavaBeansTest"
classpathref="test.classpath" failonerror="true" />
</target>
diff --git a/lib/java/src/org/apache/thrift/TBaseHelper.java b/lib/java/src/org/apache/thrift/TBaseHelper.java
index 55e950544..9a7ee73f4 100644
--- a/lib/java/src/org/apache/thrift/TBaseHelper.java
+++ b/lib/java/src/org/apache/thrift/TBaseHelper.java
@@ -17,6 +17,7 @@
*/
package org.apache.thrift;
+import java.nio.ByteBuffer;
import java.util.Comparator;
import java.util.Iterator;
import java.util.List;
@@ -217,4 +218,26 @@ public class TBaseHelper {
}
}
+ public static void toString(ByteBuffer bb, StringBuilder sb) {
+ byte[] buf = bb.array();
+
+ int arrayOffset = bb.arrayOffset();
+ int origLimit = bb.limit();
+ int limit = (origLimit - arrayOffset > 128) ? arrayOffset + 128 : origLimit;
+
+ for (int i = arrayOffset; i < limit; i++) {
+ if (i > arrayOffset) {
+ sb.append(" ");
+ }
+ sb.append(paddedByteString(buf[i]));
+ }
+ if (origLimit != limit) {
+ sb.append("...");
+ }
+ }
+
+ public static String paddedByteString(byte b) {
+ int extended = (b | 0x100) & 0x1ff;
+ return Integer.toHexString(extended).toUpperCase().substring(1);
+ }
}
diff --git a/lib/java/src/org/apache/thrift/TDeserializer.java b/lib/java/src/org/apache/thrift/TDeserializer.java
index 3e9c7c379..dbefcd84d 100644
--- a/lib/java/src/org/apache/thrift/TDeserializer.java
+++ b/lib/java/src/org/apache/thrift/TDeserializer.java
@@ -20,6 +20,7 @@
package org.apache.thrift;
import java.io.UnsupportedEncodingException;
+import java.nio.ByteBuffer;
import org.apache.thrift.protocol.TBinaryProtocol;
import org.apache.thrift.protocol.TField;
@@ -203,9 +204,9 @@ public class TDeserializer {
* @param fieldIdPathRest The rest FieldId's that define a path to a binary field
* @throws TException
*/
- public byte[] partialDeserializeByteArray(byte[] bytes, TFieldIdEnum fieldIdPathFirst, TFieldIdEnum ... fieldIdPathRest) throws TException {
+ public ByteBuffer partialDeserializeByteArray(byte[] bytes, TFieldIdEnum fieldIdPathFirst, TFieldIdEnum ... fieldIdPathRest) throws TException {
// TType does not have binary, so we use the arbitrary num 100
- return (byte[]) partialDeserializeField((byte)100, bytes, fieldIdPathFirst, fieldIdPathRest);
+ return (ByteBuffer) partialDeserializeField((byte)100, bytes, fieldIdPathFirst, fieldIdPathRest);
}
/**
diff --git a/lib/java/src/org/apache/thrift/protocol/TBinaryProtocol.java b/lib/java/src/org/apache/thrift/protocol/TBinaryProtocol.java
index 1cfa69dc2..913573baa 100644
--- a/lib/java/src/org/apache/thrift/protocol/TBinaryProtocol.java
+++ b/lib/java/src/org/apache/thrift/protocol/TBinaryProtocol.java
@@ -20,6 +20,7 @@
package org.apache.thrift.protocol;
import java.io.UnsupportedEncodingException;
+import java.nio.ByteBuffer;
import org.apache.thrift.TException;
import org.apache.thrift.transport.TTransport;
@@ -189,9 +190,10 @@ public class TBinaryProtocol extends TProtocol {
}
}
- public void writeBinary(byte[] bin) throws TException {
- writeI32(bin.length);
- trans_.write(bin, 0, bin.length);
+ public void writeBinary(ByteBuffer bin) throws TException {
+ int length = bin.limit() - bin.position() - bin.arrayOffset();
+ writeI32(length);
+ trans_.write(bin.array(), bin.position() + bin.arrayOffset(), length);
}
/**
@@ -356,12 +358,19 @@ public class TBinaryProtocol extends TProtocol {
}
}
- public byte[] readBinary() throws TException {
+ public ByteBuffer readBinary() throws TException {
int size = readI32();
checkReadLength(size);
+
+ if (trans_.getBytesRemainingInBuffer() >= size) {
+ ByteBuffer bb = ByteBuffer.wrap(trans_.getBuffer(), trans_.getBufferPosition(), size);
+ trans_.consumeBuffer(size);
+ return bb;
+ }
+
byte[] buf = new byte[size];
trans_.readAll(buf, 0, size);
- return buf;
+ return ByteBuffer.wrap(buf);
}
private int readAll(byte[] buf, int off, int len) throws TException {
diff --git a/lib/java/src/org/apache/thrift/protocol/TCompactProtocol.java b/lib/java/src/org/apache/thrift/protocol/TCompactProtocol.java
index f4979423e..4c2cfc0f0 100755
--- a/lib/java/src/org/apache/thrift/protocol/TCompactProtocol.java
+++ b/lib/java/src/org/apache/thrift/protocol/TCompactProtocol.java
@@ -21,6 +21,7 @@
package org.apache.thrift.protocol;
import java.io.UnsupportedEncodingException;
+import java.nio.ByteBuffer;
import org.apache.thrift.ShortStack;
import org.apache.thrift.TException;
@@ -294,7 +295,8 @@ public final class TCompactProtocol extends TProtocol {
*/
public void writeString(String str) throws TException {
try {
- writeBinary(str.getBytes("UTF-8"));
+ byte[] bytes = str.getBytes("UTF-8");
+ writeBinary(bytes, 0, bytes.length);
} catch (UnsupportedEncodingException e) {
throw new TException("UTF-8 not supported!");
}
@@ -303,9 +305,14 @@ public final class TCompactProtocol extends TProtocol {
/**
* Write a byte array, using a varint for the size.
*/
- public void writeBinary(byte[] bin) throws TException {
- writeVarint32(bin.length);
- trans_.write(bin);
+ public void writeBinary(ByteBuffer bin) throws TException {
+ int length = bin.limit() - bin.position() - bin.arrayOffset();
+ writeBinary(bin.array(), bin.position() + bin.arrayOffset(), length);
+ }
+
+ private void writeBinary(byte[] buf, int offset, int length) throws TException {
+ writeVarint32(length);
+ trans_.write(buf, offset, length);
}
//
@@ -626,13 +633,13 @@ public final class TCompactProtocol extends TProtocol {
/**
* Read a byte[] from the wire.
*/
- public byte[] readBinary() throws TException {
+ public ByteBuffer readBinary() throws TException {
int length = readVarint32();
- if (length == 0) return new byte[0];
+ if (length == 0) return ByteBuffer.wrap(new byte[0]);
byte[] buf = new byte[length];
trans_.readAll(buf, 0, length);
- return buf;
+ return ByteBuffer.wrap(buf);
}
/**
diff --git a/lib/java/src/org/apache/thrift/protocol/TJSONProtocol.java b/lib/java/src/org/apache/thrift/protocol/TJSONProtocol.java
index 89ba9b497..d00980e91 100644
--- a/lib/java/src/org/apache/thrift/protocol/TJSONProtocol.java
+++ b/lib/java/src/org/apache/thrift/protocol/TJSONProtocol.java
@@ -19,12 +19,14 @@
package org.apache.thrift.protocol;
-import org.apache.thrift.TException;
-import org.apache.thrift.TByteArrayOutputStream;
-import org.apache.thrift.transport.TTransport;
import java.io.UnsupportedEncodingException;
+import java.nio.ByteBuffer;
import java.util.Stack;
+import org.apache.thrift.TByteArrayOutputStream;
+import org.apache.thrift.TException;
+import org.apache.thrift.transport.TTransport;
+
/**
* JSON protocol implementation for thrift.
*
@@ -438,11 +440,11 @@ public class TJSONProtocol extends TProtocol {
// Write out contents of byte array b as a JSON string with base-64 encoded
// data
- private void writeJSONBase64(byte[] b) throws TException {
+ private void writeJSONBase64(byte[] b, int offset, int length) throws TException {
context_.write();
trans_.write(QUOTE);
- int len = b.length;
- int off = 0;
+ int len = length;
+ int off = offset;
while (len >= 3) {
// Encode 3 bytes at a time
TBase64Utils.encode(b, off, 3, tmpbuf_, 0);
@@ -604,8 +606,8 @@ public class TJSONProtocol extends TProtocol {
}
@Override
- public void writeBinary(byte[] bin) throws TException {
- writeJSONBase64(bin);
+ public void writeBinary(ByteBuffer bin) throws TException {
+ writeJSONBase64(bin.array(), bin.position() + bin.arrayOffset(), bin.limit() - bin.position() - bin.arrayOffset());
}
/**
@@ -927,8 +929,8 @@ public class TJSONProtocol extends TProtocol {
}
@Override
- public byte[] readBinary() throws TException {
- return readJSONBase64();
+ public ByteBuffer readBinary() throws TException {
+ return ByteBuffer.wrap(readJSONBase64());
}
}
diff --git a/lib/java/src/org/apache/thrift/protocol/TProtocol.java b/lib/java/src/org/apache/thrift/protocol/TProtocol.java
index 78b07f9b2..16fac64f5 100644
--- a/lib/java/src/org/apache/thrift/protocol/TProtocol.java
+++ b/lib/java/src/org/apache/thrift/protocol/TProtocol.java
@@ -19,6 +19,8 @@
package org.apache.thrift.protocol;
+import java.nio.ByteBuffer;
+
import org.apache.thrift.TException;
import org.apache.thrift.transport.TTransport;
@@ -97,7 +99,7 @@ public abstract class TProtocol {
public abstract void writeString(String str) throws TException;
- public abstract void writeBinary(byte[] bin) throws TException;
+ public abstract void writeBinary(ByteBuffer buf) throws TException;
/**
* Reading methods.
@@ -141,7 +143,7 @@ public abstract class TProtocol {
public abstract String readString() throws TException;
- public abstract byte[] readBinary() throws TException;
+ public abstract ByteBuffer readBinary() throws TException;
/**
* Reset any internal state back to a blank slate. This method only needs to
diff --git a/lib/java/src/org/apache/thrift/protocol/TSimpleJSONProtocol.java b/lib/java/src/org/apache/thrift/protocol/TSimpleJSONProtocol.java
index a60bdf407..83a253296 100644
--- a/lib/java/src/org/apache/thrift/protocol/TSimpleJSONProtocol.java
+++ b/lib/java/src/org/apache/thrift/protocol/TSimpleJSONProtocol.java
@@ -20,6 +20,7 @@
package org.apache.thrift.protocol;
import java.io.UnsupportedEncodingException;
+import java.nio.ByteBuffer;
import java.util.Stack;
import org.apache.thrift.TException;
@@ -282,10 +283,10 @@ public class TSimpleJSONProtocol extends TProtocol {
_writeStringData(escape.toString());
}
- public void writeBinary(byte[] bin) throws TException {
+ public void writeBinary(ByteBuffer bin) throws TException {
try {
// TODO(mcslee): Fix this
- writeString(new String(bin, "UTF-8"));
+ writeString(new String(bin.array(), bin.position() + bin.arrayOffset(), bin.limit() - bin.position() - bin.arrayOffset(), "UTF-8"));
} catch (UnsupportedEncodingException uex) {
throw new TException("JVM DOES NOT SUPPORT UTF-8");
}
@@ -376,9 +377,9 @@ public class TSimpleJSONProtocol extends TProtocol {
return "";
}
- public byte[] readBinary() throws TException {
+ public ByteBuffer readBinary() throws TException {
// TODO(mcslee): implement
- return new byte[0];
+ return ByteBuffer.wrap(new byte[0]);
}
}
diff --git a/lib/java/test/org/apache/thrift/Fixtures.java b/lib/java/test/org/apache/thrift/Fixtures.java
index 75070d09c..14bdc08d5 100644
--- a/lib/java/test/org/apache/thrift/Fixtures.java
+++ b/lib/java/test/org/apache/thrift/Fixtures.java
@@ -20,6 +20,7 @@
package org.apache.thrift;
+import java.nio.ByteBuffer;
import java.util.*;
import thrift.test.*;
@@ -52,7 +53,7 @@ public class Fixtures {
oneOfEach.double_precision = Math.PI;
oneOfEach.some_characters = "JSON THIS! \"\1";
oneOfEach.zomg_unicode = new String(kUnicodeBytes, "UTF-8");
- oneOfEach.base64 = "base64".getBytes();
+ oneOfEach.base64 = ByteBuffer.wrap("base64".getBytes());
// byte, i16, and i64 lists are populated by default constructor
Bonk bonk = new Bonk();
@@ -112,7 +113,7 @@ public class Fixtures {
// superhuge compact proto test struct
compactProtoTestStruct = new CompactProtoTestStruct(thrift.test.Constants.COMPACT_TEST);
- compactProtoTestStruct.a_binary = new byte[]{0,1,2,3,4,5,6,7,8};
+ compactProtoTestStruct.a_binary = ByteBuffer.wrap(new byte[]{0,1,2,3,4,5,6,7,8});
} catch (Exception e) {
throw new RuntimeException(e);
}
diff --git a/lib/java/test/org/apache/thrift/TestStruct.java b/lib/java/test/org/apache/thrift/TestStruct.java
index 755534d91..e9f165d93 100644
--- a/lib/java/test/org/apache/thrift/TestStruct.java
+++ b/lib/java/test/org/apache/thrift/TestStruct.java
@@ -18,10 +18,7 @@
*/
package org.apache.thrift;
-import java.io.ByteArrayInputStream;
-import java.io.ByteArrayOutputStream;
-import java.io.ObjectInputStream;
-import java.io.ObjectOutputStream;
+import java.nio.ByteBuffer;
import java.util.HashMap;
import java.util.Map;
@@ -39,32 +36,13 @@ import thrift.test.Bonk;
import thrift.test.CrazyNesting;
import thrift.test.HolyMoley;
import thrift.test.Insanity;
+import thrift.test.JavaTestHelper;
import thrift.test.Nesting;
import thrift.test.Numberz;
import thrift.test.OneOfEach;
import thrift.test.Xtruct;
public class TestStruct extends TestCase {
-
- public static Object deepCopyViaSerialization(Object oldObj) throws Exception {
- ObjectOutputStream oos = null;
- ObjectInputStream ois = null;
- try {
- ByteArrayOutputStream bos =
- new ByteArrayOutputStream();
- oos = new ObjectOutputStream(bos);
- oos.writeObject(oldObj);
- oos.flush();
- ByteArrayInputStream bis =
- new ByteArrayInputStream(bos.toByteArray());
- ois = new ObjectInputStream(bis);
- return ois.readObject();
- } finally {
- oos.close();
- ois.close();
- }
- }
-
public void testIdentity() throws Exception {
TSerializer binarySerializer = new TSerializer(new TBinaryProtocol.Factory());
TDeserializer binaryDeserializer = new TDeserializer(new TBinaryProtocol.Factory());
@@ -72,7 +50,7 @@ public class TestStruct extends TestCase {
OneOfEach ooe = Fixtures.oneOfEach;
Nesting n = new Nesting();
- n.my_ooe = (OneOfEach)deepCopyViaSerialization(ooe);
+ n.my_ooe = ooe;
n.my_ooe.integer16 = 16;
n.my_ooe.integer32 = 32;
n.my_ooe.integer64 = 64;
@@ -127,10 +105,10 @@ public class TestStruct extends TestCase {
assertEquals(hmCopy, hmCopy2);
// change binary value in original object
- hm.big.get(0).base64[0]++;
+ hm.big.get(0).base64.array()[0]++;
// make sure the change didn't propagate to the copied object
assertFalse(hm.equals(hmCopy2));
- hm.big.get(0).base64[0]--; // undo change
+ hm.big.get(0).base64.array()[0]--; // undo change
hmCopy2.bonks.get("two").get(1).message = "What else?";
@@ -261,4 +239,61 @@ public class TestStruct extends TestCase {
assertTrue(vmd.valueMetaData.isTypedef());
assertFalse(vmd.keyMetaData.isTypedef());
}
+
+ public void testToString() throws Exception {
+ JavaTestHelper object = new JavaTestHelper();
+ object.req_int = 0;
+ object.req_obj = "";
+
+ object.req_bin = ByteBuffer.wrap(new byte[] {
+ 0, -1, 2, -3, 4, -5, 6, -7, 8, -9, 10, -11, 12, -13, 14, -15,
+ 16, -17, 18, -19, 20, -21, 22, -23, 24, -25, 26, -27, 28, -29,
+ 30, -31, 32, -33, 34, -35, 36, -37, 38, -39, 40, -41, 42, -43, 44,
+ -45, 46, -47, 48, -49, 50, -51, 52, -53, 54, -55, 56, -57, 58, -59,
+ 60, -61, 62, -63, 64, -65, 66, -67, 68, -69, 70, -71, 72, -73, 74,
+ -75, 76, -77, 78, -79, 80, -81, 82, -83, 84, -85, 86, -87, 88, -89,
+ 90, -91, 92, -93, 94, -95, 96, -97, 98, -99, 100, -101, 102, -103,
+ 104, -105, 106, -107, 108, -109, 110, -111, 112, -113, 114, -115,
+ 116, -117, 118, -119, 120, -121, 122, -123, 124, -125, 126, -127,
+ });
+
+ assertEquals("JavaTestHelper(req_int:0, req_obj:, req_bin:"+
+ "00 FF 02 FD 04 FB 06 F9 08 F7 0A F5 0C F3 0E F1 10 EF 12 ED 14 "+
+ "EB 16 E9 18 E7 1A E5 1C E3 1E E1 20 DF 22 DD 24 DB 26 D9 28 D7 "+
+ "2A D5 2C D3 2E D1 30 CF 32 CD 34 CB 36 C9 38 C7 3A C5 3C C3 3E "+
+ "C1 40 BF 42 BD 44 BB 46 B9 48 B7 4A B5 4C B3 4E B1 50 AF 52 AD "+
+ "54 AB 56 A9 58 A7 5A A5 5C A3 5E A1 60 9F 62 9D 64 9B 66 99 68 "+
+ "97 6A 95 6C 93 6E 91 70 8F 72 8D 74 8B 76 89 78 87 7A 85 7C 83 "+
+ "7E 81)",
+ object.toString());
+
+ object.req_bin = ByteBuffer.wrap(new byte[] {
+ 0, -1, 2, -3, 4, -5, 6, -7, 8, -9, 10, -11, 12, -13, 14, -15,
+ 16, -17, 18, -19, 20, -21, 22, -23, 24, -25, 26, -27, 28, -29,
+ 30, -31, 32, -33, 34, -35, 36, -37, 38, -39, 40, -41, 42, -43, 44,
+ -45, 46, -47, 48, -49, 50, -51, 52, -53, 54, -55, 56, -57, 58, -59,
+ 60, -61, 62, -63, 64, -65, 66, -67, 68, -69, 70, -71, 72, -73, 74,
+ -75, 76, -77, 78, -79, 80, -81, 82, -83, 84, -85, 86, -87, 88, -89,
+ 90, -91, 92, -93, 94, -95, 96, -97, 98, -99, 100, -101, 102, -103,
+ 104, -105, 106, -107, 108, -109, 110, -111, 112, -113, 114, -115,
+ 116, -117, 118, -119, 120, -121, 122, -123, 124, -125, 126, -127,
+ 0,
+ });
+
+ assertEquals("JavaTestHelper(req_int:0, req_obj:, req_bin:"+
+ "00 FF 02 FD 04 FB 06 F9 08 F7 0A F5 0C F3 0E F1 10 EF 12 ED 14 "+
+ "EB 16 E9 18 E7 1A E5 1C E3 1E E1 20 DF 22 DD 24 DB 26 D9 28 D7 "+
+ "2A D5 2C D3 2E D1 30 CF 32 CD 34 CB 36 C9 38 C7 3A C5 3C C3 3E "+
+ "C1 40 BF 42 BD 44 BB 46 B9 48 B7 4A B5 4C B3 4E B1 50 AF 52 AD "+
+ "54 AB 56 A9 58 A7 5A A5 5C A3 5E A1 60 9F 62 9D 64 9B 66 99 68 "+
+ "97 6A 95 6C 93 6E 91 70 8F 72 8D 74 8B 76 89 78 87 7A 85 7C 83 "+
+ "7E 81...)",
+ object.toString());
+
+ object.req_bin = ByteBuffer.wrap(new byte[] {});
+ object.setOpt_binIsSet(true);
+
+ assertEquals("JavaTestHelper(req_int:0, req_obj:, req_bin:)",
+ object.toString());
+ }
}
diff --git a/lib/java/test/org/apache/thrift/TestTDeserializer.java b/lib/java/test/org/apache/thrift/TestTDeserializer.java
index aae2ee4d6..1f141bdcc 100644
--- a/lib/java/test/org/apache/thrift/TestTDeserializer.java
+++ b/lib/java/test/org/apache/thrift/TestTDeserializer.java
@@ -18,7 +18,7 @@
*/
package org.apache.thrift;
-import java.util.Arrays;
+import java.nio.ByteBuffer;
import junit.framework.TestCase;
@@ -97,10 +97,10 @@ public class TestTDeserializer extends TestCase {
String resultString = deserializer.partialDeserializeString(serialize(level1SWU, factory), StructWithAUnion._Fields.TEST_UNION, TestUnion._Fields.STRUCT_FIELD, OneOfEach._Fields.SOME_CHARACTERS);
assertEquals(expectedString, resultString);
- byte[] expectedBinary = level3OneOfEach.getBase64();
- byte[] resultBinary = deserializer.partialDeserializeByteArray(serialize(level1SWU, factory), StructWithAUnion._Fields.TEST_UNION, TestUnion._Fields.STRUCT_FIELD, OneOfEach._Fields.BASE64);
- assertEquals(expectedBinary.length, resultBinary.length);
- assertTrue(Arrays.equals(expectedBinary, resultBinary));
+ byte[] expectedBinary = level3OneOfEach.getBase64().array();
+ ByteBuffer resultBinary = deserializer.partialDeserializeByteArray(serialize(level1SWU, factory), StructWithAUnion._Fields.TEST_UNION, TestUnion._Fields.STRUCT_FIELD, OneOfEach._Fields.BASE64);
+ assertEquals(expectedBinary.length, resultBinary.limit() - resultBinary.position() - resultBinary.arrayOffset());
+ assertEquals(ByteBuffer.wrap(expectedBinary), resultBinary);
// Test field id in Union
short id = deserializer.partialDeserializeSetFieldIdInUnion(serialize(level1SWU, factory), StructWithAUnion._Fields.TEST_UNION);
diff --git a/lib/java/test/org/apache/thrift/TestTUnion.java b/lib/java/test/org/apache/thrift/TestTUnion.java
index a8b1287d1..b6032cc34 100644
--- a/lib/java/test/org/apache/thrift/TestTUnion.java
+++ b/lib/java/test/org/apache/thrift/TestTUnion.java
@@ -18,6 +18,15 @@
*/
package org.apache.thrift;
+import java.nio.ByteBuffer;
+import java.util.ArrayList;
+import java.util.HashMap;
+import java.util.HashSet;
+import java.util.Map;
+import java.util.Set;
+
+import junit.framework.TestCase;
+
import org.apache.thrift.protocol.TBinaryProtocol;
import org.apache.thrift.protocol.TProtocol;
import org.apache.thrift.transport.TMemoryBuffer;
@@ -29,12 +38,6 @@ import thrift.test.SomeEnum;
import thrift.test.StructWithAUnion;
import thrift.test.TestUnion;
import thrift.test.TestUnionMinusStringField;
-import java.util.ArrayList;
-import java.util.HashMap;
-import java.util.HashSet;
-import java.util.Map;
-import java.util.Set;
-import junit.framework.TestCase;
public class TestTUnion extends TestCase {
@@ -95,12 +98,12 @@ public class TestTUnion extends TestCase {
assertTrue(cu.compareTo(cu2) < 0);
assertTrue(cu2.compareTo(cu) > 0);
- cu2 = ComparableUnion.binary_field(new byte[]{2});
+ cu2 = ComparableUnion.binary_field(ByteBuffer.wrap(new byte[]{2}));
assertTrue(cu.compareTo(cu2) < 0);
assertTrue(cu2.compareTo(cu) > 0);
- cu = ComparableUnion.binary_field(new byte[]{1});
+ cu = ComparableUnion.binary_field(ByteBuffer.wrap(new byte[]{1}));
assertTrue(cu.compareTo(cu2) < 0);
assertTrue(cu2.compareTo(cu) > 0);
diff --git a/lib/java/test/org/apache/thrift/protocol/ProtocolTestBase.java b/lib/java/test/org/apache/thrift/protocol/ProtocolTestBase.java
index da0de057c..5f22cfc8a 100644
--- a/lib/java/test/org/apache/thrift/protocol/ProtocolTestBase.java
+++ b/lib/java/test/org/apache/thrift/protocol/ProtocolTestBase.java
@@ -18,6 +18,7 @@
*/
package org.apache.thrift.protocol;
+import java.nio.ByteBuffer;
import java.util.Arrays;
import java.util.List;
@@ -231,18 +232,18 @@ public abstract class ProtocolTestBase extends TestCase {
private void internalTestNakedBinary(byte[] data) throws Exception {
TMemoryBuffer buf = new TMemoryBuffer(0);
TProtocol proto = getFactory().getProtocol(buf);
- proto.writeBinary(data);
- assertTrue(Arrays.equals(data, proto.readBinary()));
+ proto.writeBinary(ByteBuffer.wrap(data));
+ assertEquals(ByteBuffer.wrap(data), proto.readBinary());
}
private void internalTestBinaryField(final byte[] data) throws Exception {
internalTestStructField(new StructFieldTestCase(TType.STRING, (short)15) {
public void writeMethod(TProtocol proto) throws TException {
- proto.writeBinary(data);
+ proto.writeBinary(ByteBuffer.wrap(data));
}
public void readMethod(TProtocol proto) throws TException {
- assertTrue(Arrays.equals(data, proto.readBinary()));
+ assertEquals(ByteBuffer.wrap(data), proto.readBinary());
}
});
}
diff --git a/lib/java/test/org/apache/thrift/test/EqualityTest.java b/lib/java/test/org/apache/thrift/test/EqualityTest.java
index f01378f74..94ba543a8 100644
--- a/lib/java/test/org/apache/thrift/test/EqualityTest.java
+++ b/lib/java/test/org/apache/thrift/test/EqualityTest.java
@@ -129,7 +129,9 @@ public class EqualityTest {
package org.apache.thrift.test;
// Generated code
-import thrift.test.*;
+import java.nio.ByteBuffer;
+
+import thrift.test.JavaTestHelper;
/**
*/
@@ -486,8 +488,8 @@ public class EqualityTest {
lhs = new JavaTestHelper();
rhs = new JavaTestHelper();
- lhs.req_bin = new byte[]{1,2};
- rhs.req_bin = new byte[]{1,2};
+ lhs.req_bin = ByteBuffer.wrap(new byte[]{1,2});
+ rhs.req_bin = ByteBuffer.wrap(new byte[]{1,2});
lhs.req_bin = null;
rhs.req_bin = null;
// this_present = False
@@ -499,8 +501,8 @@ public class EqualityTest {
lhs = new JavaTestHelper();
rhs = new JavaTestHelper();
- lhs.req_bin = new byte[]{1,2};
- rhs.req_bin = new byte[]{3,4};
+ lhs.req_bin = ByteBuffer.wrap(new byte[]{1,2});
+ rhs.req_bin = ByteBuffer.wrap(new byte[]{3,4});
lhs.req_bin = null;
rhs.req_bin = null;
// this_present = False
@@ -512,8 +514,8 @@ public class EqualityTest {
lhs = new JavaTestHelper();
rhs = new JavaTestHelper();
- lhs.req_bin = new byte[]{1,2};
- rhs.req_bin = new byte[]{1,2};
+ lhs.req_bin = ByteBuffer.wrap(new byte[]{1,2});
+ rhs.req_bin = ByteBuffer.wrap(new byte[]{1,2});
lhs.req_bin = null;
// this_present = False
// that_present = True
@@ -522,8 +524,8 @@ public class EqualityTest {
lhs = new JavaTestHelper();
rhs = new JavaTestHelper();
- lhs.req_bin = new byte[]{1,2};
- rhs.req_bin = new byte[]{3,4};
+ lhs.req_bin = ByteBuffer.wrap(new byte[]{1,2});
+ rhs.req_bin = ByteBuffer.wrap(new byte[]{3,4});
lhs.req_bin = null;
// this_present = False
// that_present = True
@@ -532,8 +534,8 @@ public class EqualityTest {
lhs = new JavaTestHelper();
rhs = new JavaTestHelper();
- lhs.req_bin = new byte[]{1,2};
- rhs.req_bin = new byte[]{1,2};
+ lhs.req_bin = ByteBuffer.wrap(new byte[]{1,2});
+ rhs.req_bin = ByteBuffer.wrap(new byte[]{1,2});
rhs.req_bin = null;
// this_present = True
// that_present = False
@@ -542,8 +544,8 @@ public class EqualityTest {
lhs = new JavaTestHelper();
rhs = new JavaTestHelper();
- lhs.req_bin = new byte[]{1,2};
- rhs.req_bin = new byte[]{3,4};
+ lhs.req_bin = ByteBuffer.wrap(new byte[]{1,2});
+ rhs.req_bin = ByteBuffer.wrap(new byte[]{3,4});
rhs.req_bin = null;
// this_present = True
// that_present = False
@@ -552,8 +554,8 @@ public class EqualityTest {
lhs = new JavaTestHelper();
rhs = new JavaTestHelper();
- lhs.req_bin = new byte[]{1,2};
- rhs.req_bin = new byte[]{1,2};
+ lhs.req_bin = ByteBuffer.wrap(new byte[]{1,2});
+ rhs.req_bin = ByteBuffer.wrap(new byte[]{1,2});
// this_present = True
// that_present = True
if (lhs.equals(rhs) != true)
@@ -563,8 +565,8 @@ public class EqualityTest {
lhs = new JavaTestHelper();
rhs = new JavaTestHelper();
- lhs.req_bin = new byte[]{1,2};
- rhs.req_bin = new byte[]{3,4};
+ lhs.req_bin = ByteBuffer.wrap(new byte[]{1,2});
+ rhs.req_bin = ByteBuffer.wrap(new byte[]{3,4});
// this_present = True
// that_present = True
if (lhs.equals(rhs) != false)
@@ -572,8 +574,8 @@ public class EqualityTest {
lhs = new JavaTestHelper();
rhs = new JavaTestHelper();
- lhs.opt_bin = new byte[]{1,2};
- rhs.opt_bin = new byte[]{1,2};
+ lhs.opt_bin = ByteBuffer.wrap(new byte[]{1,2});
+ rhs.opt_bin = ByteBuffer.wrap(new byte[]{1,2});
lhs.opt_bin = null;
rhs.opt_bin = null;
// this_present = False
@@ -585,8 +587,8 @@ public class EqualityTest {
lhs = new JavaTestHelper();
rhs = new JavaTestHelper();
- lhs.opt_bin = new byte[]{1,2};
- rhs.opt_bin = new byte[]{3,4};
+ lhs.opt_bin = ByteBuffer.wrap(new byte[]{1,2});
+ rhs.opt_bin = ByteBuffer.wrap(new byte[]{3,4});
lhs.opt_bin = null;
rhs.opt_bin = null;
// this_present = False
@@ -598,8 +600,8 @@ public class EqualityTest {
lhs = new JavaTestHelper();
rhs = new JavaTestHelper();
- lhs.opt_bin = new byte[]{1,2};
- rhs.opt_bin = new byte[]{1,2};
+ lhs.opt_bin = ByteBuffer.wrap(new byte[]{1,2});
+ rhs.opt_bin = ByteBuffer.wrap(new byte[]{1,2});
lhs.opt_bin = null;
// this_present = False
// that_present = True
@@ -608,8 +610,8 @@ public class EqualityTest {
lhs = new JavaTestHelper();
rhs = new JavaTestHelper();
- lhs.opt_bin = new byte[]{1,2};
- rhs.opt_bin = new byte[]{3,4};
+ lhs.opt_bin = ByteBuffer.wrap(new byte[]{1,2});
+ rhs.opt_bin = ByteBuffer.wrap(new byte[]{3,4});
lhs.opt_bin = null;
// this_present = False
// that_present = True
@@ -618,8 +620,8 @@ public class EqualityTest {
lhs = new JavaTestHelper();
rhs = new JavaTestHelper();
- lhs.opt_bin = new byte[]{1,2};
- rhs.opt_bin = new byte[]{1,2};
+ lhs.opt_bin = ByteBuffer.wrap(new byte[]{1,2});
+ rhs.opt_bin = ByteBuffer.wrap(new byte[]{1,2});
rhs.opt_bin = null;
// this_present = True
// that_present = False
@@ -628,8 +630,8 @@ public class EqualityTest {
lhs = new JavaTestHelper();
rhs = new JavaTestHelper();
- lhs.opt_bin = new byte[]{1,2};
- rhs.opt_bin = new byte[]{3,4};
+ lhs.opt_bin = ByteBuffer.wrap(new byte[]{1,2});
+ rhs.opt_bin = ByteBuffer.wrap(new byte[]{3,4});
rhs.opt_bin = null;
// this_present = True
// that_present = False
@@ -638,8 +640,8 @@ public class EqualityTest {
lhs = new JavaTestHelper();
rhs = new JavaTestHelper();
- lhs.opt_bin = new byte[]{1,2};
- rhs.opt_bin = new byte[]{1,2};
+ lhs.opt_bin = ByteBuffer.wrap(new byte[]{1,2});
+ rhs.opt_bin = ByteBuffer.wrap(new byte[]{1,2});
// this_present = True
// that_present = True
if (lhs.equals(rhs) != true)
@@ -649,8 +651,8 @@ public class EqualityTest {
lhs = new JavaTestHelper();
rhs = new JavaTestHelper();
- lhs.opt_bin = new byte[]{1,2};
- rhs.opt_bin = new byte[]{3,4};
+ lhs.opt_bin = ByteBuffer.wrap(new byte[]{1,2});
+ rhs.opt_bin = ByteBuffer.wrap(new byte[]{3,4});
// this_present = True
// that_present = True
if (lhs.equals(rhs) != false)
diff --git a/lib/java/test/org/apache/thrift/test/JavaBeansTest.java b/lib/java/test/org/apache/thrift/test/JavaBeansTest.java
index b72bd388c..3f8d22a0a 100644
--- a/lib/java/test/org/apache/thrift/test/JavaBeansTest.java
+++ b/lib/java/test/org/apache/thrift/test/JavaBeansTest.java
@@ -19,7 +19,9 @@
package org.apache.thrift.test;
+import java.nio.ByteBuffer;
import java.util.LinkedList;
+
import thrift.test.OneOfEachBeans;
public class JavaBeansTest {
@@ -58,7 +60,7 @@ public class JavaBeansTest {
// Everything is set
ooe.set_a_bite((byte) 1);
- ooe.set_base64("bytes".getBytes());
+ ooe.set_base64(ByteBuffer.wrap("bytes".getBytes()));
ooe.set_byte_list(new LinkedList<Byte>());
ooe.set_double_precision(1);
ooe.set_i16_list(new LinkedList<Short>());
diff --git a/lib/java/test/org/apache/thrift/test/ToStringTest.java b/lib/java/test/org/apache/thrift/test/ToStringTest.java
index 569a61c47..e69de29bb 100644
--- a/lib/java/test/org/apache/thrift/test/ToStringTest.java
+++ b/lib/java/test/org/apache/thrift/test/ToStringTest.java
@@ -1,92 +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.thrift.test;
-
-import thrift.test.*;
-
-/**
- */
-public class ToStringTest {
- public static void main(String[] args) throws Exception {
- JavaTestHelper object = new JavaTestHelper();
- object.req_int = 0;
- object.req_obj = "";
-
-
- object.req_bin = new byte[] {
- 0, -1, 2, -3, 4, -5, 6, -7, 8, -9, 10, -11, 12, -13, 14, -15,
- 16, -17, 18, -19, 20, -21, 22, -23, 24, -25, 26, -27, 28, -29,
- 30, -31, 32, -33, 34, -35, 36, -37, 38, -39, 40, -41, 42, -43, 44,
- -45, 46, -47, 48, -49, 50, -51, 52, -53, 54, -55, 56, -57, 58, -59,
- 60, -61, 62, -63, 64, -65, 66, -67, 68, -69, 70, -71, 72, -73, 74,
- -75, 76, -77, 78, -79, 80, -81, 82, -83, 84, -85, 86, -87, 88, -89,
- 90, -91, 92, -93, 94, -95, 96, -97, 98, -99, 100, -101, 102, -103,
- 104, -105, 106, -107, 108, -109, 110, -111, 112, -113, 114, -115,
- 116, -117, 118, -119, 120, -121, 122, -123, 124, -125, 126, -127,
- };
-
- if (!object.toString().equals(
- "JavaTestHelper(req_int:0, req_obj:, req_bin:"+
- "00 FF 02 FD 04 FB 06 F9 08 F7 0A F5 0C F3 0E F1 10 EF 12 ED 14 "+
- "EB 16 E9 18 E7 1A E5 1C E3 1E E1 20 DF 22 DD 24 DB 26 D9 28 D7 "+
- "2A D5 2C D3 2E D1 30 CF 32 CD 34 CB 36 C9 38 C7 3A C5 3C C3 3E "+
- "C1 40 BF 42 BD 44 BB 46 B9 48 B7 4A B5 4C B3 4E B1 50 AF 52 AD "+
- "54 AB 56 A9 58 A7 5A A5 5C A3 5E A1 60 9F 62 9D 64 9B 66 99 68 "+
- "97 6A 95 6C 93 6E 91 70 8F 72 8D 74 8B 76 89 78 87 7A 85 7C 83 "+
- "7E 81)")) {
- throw new RuntimeException();
- }
-
- object.req_bin = new byte[] {
- 0, -1, 2, -3, 4, -5, 6, -7, 8, -9, 10, -11, 12, -13, 14, -15,
- 16, -17, 18, -19, 20, -21, 22, -23, 24, -25, 26, -27, 28, -29,
- 30, -31, 32, -33, 34, -35, 36, -37, 38, -39, 40, -41, 42, -43, 44,
- -45, 46, -47, 48, -49, 50, -51, 52, -53, 54, -55, 56, -57, 58, -59,
- 60, -61, 62, -63, 64, -65, 66, -67, 68, -69, 70, -71, 72, -73, 74,
- -75, 76, -77, 78, -79, 80, -81, 82, -83, 84, -85, 86, -87, 88, -89,
- 90, -91, 92, -93, 94, -95, 96, -97, 98, -99, 100, -101, 102, -103,
- 104, -105, 106, -107, 108, -109, 110, -111, 112, -113, 114, -115,
- 116, -117, 118, -119, 120, -121, 122, -123, 124, -125, 126, -127,
- 0,
- };
-
- if (!object.toString().equals(
- "JavaTestHelper(req_int:0, req_obj:, req_bin:"+
- "00 FF 02 FD 04 FB 06 F9 08 F7 0A F5 0C F3 0E F1 10 EF 12 ED 14 "+
- "EB 16 E9 18 E7 1A E5 1C E3 1E E1 20 DF 22 DD 24 DB 26 D9 28 D7 "+
- "2A D5 2C D3 2E D1 30 CF 32 CD 34 CB 36 C9 38 C7 3A C5 3C C3 3E "+
- "C1 40 BF 42 BD 44 BB 46 B9 48 B7 4A B5 4C B3 4E B1 50 AF 52 AD "+
- "54 AB 56 A9 58 A7 5A A5 5C A3 5E A1 60 9F 62 9D 64 9B 66 99 68 "+
- "97 6A 95 6C 93 6E 91 70 8F 72 8D 74 8B 76 89 78 87 7A 85 7C 83 "+
- "7E 81 ...)")) {
- throw new RuntimeException();
- }
-
- object.req_bin = new byte[] {};
- object.setOpt_binIsSet(true);
-
-
- if (!object.toString().equals(
- "JavaTestHelper(req_int:0, req_obj:, req_bin:)")) {
- throw new RuntimeException();
- }
- }
-}
-