FazBrowse GitHub Viewer | Trending |
URL:
| Home
Tools: [Download Repo ZIP]   [Original HTTPS Page]

Avoid using CharsetEncoder of Android 4.x by komamitsu · Pull Request #409 · msgpack/msgpack-java · GitHub

Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension .java  (1) All 1 file type selected
Viewed files
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Unified
Split
Hide whitespace
Diff view
Unified
Split
Hide whitespace
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,9 @@
import java.io.Closeable;
import java.io.Flushable;
import java.io.IOException;
import java.lang.reflect.Constructor;
import java.lang.reflect.Field;
import java.lang.reflect.InvocationTargetException;
import java.math.BigInteger;
import java.nio.ByteBuffer;
import java.nio.CharBuffer;
Expand Down Expand Up @@ -130,6 +133,46 @@
public class MessagePacker
implements Closeable, Flushable
{
private static final boolean CORRUPTED_CHARSET_ENCODER;

static {
boolean corruptedCharsetEncoder = false;
try {
Class<?> klass = Class.forName("android.os.Build$VERSION");
Constructor<?> constructor = klass.getConstructor();
Object version = constructor.newInstance();
Field sdkIntField = klass.getField("SDK_INT");
int sdkInt = sdkIntField.getInt(version);
// Android 4.x has a bug in CharsetEncoder where offset calculation is wrong.
// See
// - https://github.com/msgpack/msgpack-java/issues/405
// - https://github.com/msgpack/msgpack-java/issues/406
// Android 5 and later and 3.x don't have this bug.
if (sdkInt >= 14 && sdkInt < 21) {
corruptedCharsetEncoder = true;
}
}
catch (ClassNotFoundException e) {
// This platform isn't Android
}
catch (NoSuchMethodException e) {
e.printStackTrace();
}
catch (IllegalAccessException e) {
e.printStackTrace();
}
catch (InstantiationException e) {
e.printStackTrace();
}
catch (InvocationTargetException e) {
e.printStackTrace();
}
catch (NoSuchFieldException e) {
e.printStackTrace();
}
CORRUPTED_CHARSET_ENCODER = corruptedCharsetEncoder;
}

private final int smallStringOptimizationThreshold;

private final int bufferFlushThreshold;
Expand Down Expand Up @@ -675,8 +718,9 @@ public MessagePacker packString(String s)
packRawStringHeader(0);
return this;
}
else if (s.length() < smallStringOptimizationThreshold) {
// Using String.getBytes is generally faster for small strings
else if (CORRUPTED_CHARSET_ENCODER || s.length() < smallStringOptimizationThreshold) {
// Using String.getBytes is generally faster for small strings.
// Also, when running on a platform that has a corrupted CharsetEncoder (i.e. Android 4.x), avoid using it.
packStringWithGetBytes(s);
return this;
}
Expand Down

Back | FazBrowse Home | New Git URL