Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improves MessageBuffer support for Java 11 #514

Merged
merged 9 commits into from
Nov 18, 2019
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -70,19 +70,7 @@ public class MessageBuffer

try {
// Check java version
String javaVersion = System.getProperty("java.specification.version", "");
int dotPos = javaVersion.indexOf('.');
boolean isJavaAtLeast7 = false;
if (dotPos != -1) {
try {
int major = Integer.parseInt(javaVersion.substring(0, dotPos));
int minor = Integer.parseInt(javaVersion.substring(dotPos + 1));
isJavaAtLeast7 = major > 1 || (major == 1 && minor >= 7);
}
catch (NumberFormatException e) {
e.printStackTrace(System.err);
}
}
int javaVersion = getJavaVersion();

boolean hasUnsafe = false;
try {
Expand All @@ -97,12 +85,14 @@ public class MessageBuffer
// Is Google App Engine?
boolean isGAE = System.getProperty("com.google.appengine.runtime.version") != null;

// For Java6, android and JVM that has no Unsafe class, use Universal MessageBuffer
// For Java6, android and JVM that has no Unsafe class, use Universal MessageBuffer (based on ByteBuffer).
// Java9 onward doesn't allow to access Cleaner by reflection, so we use Universal MessageBuffer again.
ppkarwasz marked this conversation as resolved.
Show resolved Hide resolved
useUniversalBuffer =
Boolean.parseBoolean(System.getProperty("msgpack.universal-buffer", "false"))
|| isAndroid
|| isGAE
|| !isJavaAtLeast7
|| javaVersion < 7
|| javaVersion > 9
|| !hasUnsafe;

if (!useUniversalBuffer) {
Expand Down Expand Up @@ -175,6 +165,31 @@ public class MessageBuffer
}
}

static int getJavaVersion()
{
String javaVersion = System.getProperty("java.specification.version", "");
int dotPos = javaVersion.indexOf('.');
if (dotPos != -1) {
try {
int major = Integer.parseInt(javaVersion.substring(0, dotPos));
int minor = Integer.parseInt(javaVersion.substring(dotPos + 1));
return major > 1 ? major : minor;
}
catch (NumberFormatException e) {
e.printStackTrace(System.err);
}
}
else {
try {
return Integer.parseInt(javaVersion);
}
catch (NumberFormatException e) {
e.printStackTrace(System.err);
}
}
return 6;
}

/**
* Base object for resolving the relative address of the raw byte array.
* If base == null, the address value is a raw memory address
Expand Down Expand Up @@ -366,7 +381,12 @@ else if (DirectBufferAccess.isDirectByteBufferInstance(buffer.reference)) {
{
if (bb.isDirect()) {
if (isUniversalBuffer) {
throw new UnsupportedOperationException("Cannot create MessageBuffer from a DirectBuffer on this platform");
// MessageBufferU overrides almost all methods, only field 'size' is used.
this.base = null;
this.address = 0;
this.size = bb.remaining();
this.reference = null;
return;
}
// Direct buffer or off-heap memory
this.base = null;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -258,4 +258,16 @@ public byte[] toByteArray()
getBytes(0, b, 0, b.length);
return b;
}

@Override
public boolean hasArray()
{
return !wrap.isDirect();
}

@Override
public byte[] array()
{
return hasArray() ? wrap.array() : null;
}
}