Skip to content

Commit a33b7b3

Browse files
committed
Added a workaround for openjdk6. No problem exists for Sun's jdk6
1 parent e5b185f commit a33b7b3

File tree

1 file changed

+18
-2
lines changed

1 file changed

+18
-2
lines changed

msgpack-core/src/main/java/org/msgpack/core/buffer/MessageBuffer.java

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ public class MessageBuffer {
2323

2424
static final Unsafe unsafe;
2525
static final Constructor byteBufferConstructor;
26+
static final boolean isByteBufferConstructorTakesBufferReference;
2627
static final int ARRAY_BYTE_BASE_OFFSET;
2728
static final int ARRAY_BYTE_INDEX_SCALE;
2829

@@ -56,7 +57,19 @@ public class MessageBuffer {
5657

5758
// Find the hidden constructor for DirectByteBuffer
5859
Class<?> directByteBufferClass = ClassLoader.getSystemClassLoader().loadClass("java.nio.DirectByteBuffer");
59-
byteBufferConstructor = directByteBufferClass.getDeclaredConstructor(long.class, int.class, Object.class);
60+
Constructor directByteBufferConstructor = null;
61+
boolean isAcceptReference = true;
62+
try {
63+
directByteBufferConstructor = directByteBufferClass.getDeclaredConstructor(long.class, int.class, Object.class);
64+
}
65+
catch(NoSuchMethodError e) {
66+
directByteBufferConstructor = directByteBufferClass.getDeclaredConstructor(long.class, int.class);
67+
isAcceptReference = true;
68+
}
69+
byteBufferConstructor = directByteBufferConstructor;
70+
isByteBufferConstructorTakesBufferReference = isAcceptReference;
71+
if(byteBufferConstructor == null)
72+
throw new RuntimeException("Constructor of DirectByteBuffer is not found");
6073
byteBufferConstructor.setAccessible(true);
6174

6275
// Check the endian of this CPU
@@ -389,7 +402,10 @@ public ByteBuffer toByteBuffer(int index, int length) {
389402
return ByteBuffer.wrap((byte[]) base, (int) ((address-ARRAY_BYTE_BASE_OFFSET) + index), length);
390403
}
391404
try {
392-
return (ByteBuffer) byteBufferConstructor.newInstance(address + index, length, reference);
405+
if(isByteBufferConstructorTakesBufferReference)
406+
return (ByteBuffer) byteBufferConstructor.newInstance(address + index, length, reference);
407+
else
408+
return (ByteBuffer) byteBufferConstructor.newInstance(address + index, length);
393409
} catch(Throwable e) {
394410
// Convert checked exception to unchecked exception
395411
throw new RuntimeException(e);

0 commit comments

Comments
 (0)