indent(out) << " return lastComparison;" << endl;
indent(out) << "}" << endl;
- indent(out) << "if (" << generate_isset_check(field) << ") {";
+ indent(out) << "if (" << generate_isset_check(field) << ") {" << endl;
indent(out) << " lastComparison = TBaseHelper.compareTo(this." << field->get_name() << ", typedOther." << field->get_name() << ");" << endl;
indent(out) << " if (lastComparison != 0) {" << endl;
indent(out) << " return lastComparison;" << endl;
void t_java_generator::generate_deep_copy_non_container(ofstream& out, std::string source_name, std::string dest_name, t_type* type) {
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 << "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
+ if (((t_base_type*)type)->is_binary()) {
+ out << "TBaseHelper.copyBinary(" << source_name << ");" << endl;
+ } else {
+ // everything else can be copied directly
out << source_name;
+ }
} else {
out << "new " << type_name(type, true, true) << "(" << source_name << ")";
}
import java.util.TreeMap;
import java.util.TreeSet;
-public class TBaseHelper {
+public final class TBaseHelper {
+
+ private TBaseHelper(){}
private static final Comparator comparator = new NestedStructureComparator();
}
return ByteBuffer.wrap(byteBufferToByteArray(in));
}
+
+ public static ByteBuffer copyBinary(final ByteBuffer orig) {
+ ByteBuffer copy = ByteBuffer.wrap(new byte[orig.remaining()]);
+ if (orig.hasArray()) {
+ System.arraycopy(orig.array(), orig.arrayOffset() + orig.position(), copy.array(), 0, orig.remaining());
+ } else {
+ orig.slice().get(copy.array());
+ }
+
+ return copy;
+ }
+
+ public static byte[] copyBinary(final byte[] orig) {
+ byte[] copy = new byte[orig.length];
+ System.arraycopy(orig, 0, copy, 0, orig.length);
+ return copy;
+ }
}
assertEquals(3, b3.length);
assertEquals(ByteBuffer.wrap(b1, 1, 3), ByteBuffer.wrap(b3));
}
+
+ public void testCopyBinaryWithByteBuffer() throws Exception {
+ byte[] bytes = new byte[]{0, 1, 2, 3, 4, 5};
+ ByteBuffer b = ByteBuffer.wrap(bytes);
+ ByteBuffer bCopy = TBaseHelper.copyBinary(b);
+ assertEquals(b, bCopy);
+ assertEquals(0, b.position());
+
+ b = ByteBuffer.allocateDirect(6);
+ b.put(bytes);
+ b.position(0);
+ bCopy = TBaseHelper.copyBinary(b);
+ assertEquals(6, b.remaining());
+ assertEquals(0, b.position());
+ assertEquals(b, bCopy);
+
+ b.mark();
+ b.get();
+ bCopy = TBaseHelper.copyBinary(b);
+ assertEquals(ByteBuffer.wrap(bytes, 1, 5), bCopy);
+ assertEquals(1, b.position());
+ b.reset();
+ assertEquals(0, b.position());
+ }
+
+ public void testCopyBinaryWithByteArray() throws Exception {
+ byte[] bytes = new byte[]{0, 1, 2, 3, 4, 5};
+ byte[] copy = TBaseHelper.copyBinary(bytes);
+ assertEquals(ByteBuffer.wrap(bytes), ByteBuffer.wrap(copy));
+ assertNotSame(bytes, copy);
+ }
}