Remove usage of JDK7 features not available until Android SDK 19+ (#3462)

This commit is contained in:
Mikkel Nygaard Ravn 2017-03-06 13:22:29 +01:00 committed by GitHub
parent 1bd1f0e25b
commit f9c1f5fa53
5 changed files with 25 additions and 19 deletions

View File

@ -10,8 +10,6 @@ import io.flutter.view.FlutterView.BinaryMessageReplyCallback;
import io.flutter.view.FlutterView.BinaryMessageResponse;
import io.flutter.view.FlutterView.OnBinaryMessageListenerAsync;
import java.nio.ByteBuffer;
import java.util.Objects;
import java.util.concurrent.atomic.AtomicBoolean;
/**
* A named channel for communicating with the Flutter application using semi-structured messages.
@ -43,9 +41,12 @@ public final class FlutterMessageChannel<T> {
* @param codec a {@link MessageCodec}.
*/
public FlutterMessageChannel(FlutterView view, String name, MessageCodec<T> codec) {
this.view = Objects.requireNonNull(view);
this.name = Objects.requireNonNull(name);
this.codec = Objects.requireNonNull(codec);
assert view != null;
assert name != null;
assert codec != null;
this.view = view;
this.name = name;
this.codec = codec;
}
/**

View File

@ -9,7 +9,6 @@ import io.flutter.view.FlutterView;
import io.flutter.view.FlutterView.BinaryMessageResponse;
import io.flutter.view.FlutterView.OnBinaryMessageListenerAsync;
import java.nio.ByteBuffer;
import java.util.Objects;
import java.util.concurrent.atomic.AtomicBoolean;
/**
@ -53,9 +52,12 @@ public final class FlutterMethodChannel {
* @param codec a {@link MessageCodec}.
*/
public FlutterMethodChannel(FlutterView view, String name, MethodCodec codec) {
this.view = Objects.requireNonNull(view);
this.name = Objects.requireNonNull(name);
this.codec = Objects.requireNonNull(codec);
assert view != null;
assert name != null;
assert codec != null;
this.view = view;
this.name = name;
this.codec = codec;
}
/**

View File

@ -27,7 +27,8 @@ public final class MethodCall {
* @param arguments the arguments, a value supported by the channel's message codec.
*/
public MethodCall(String method, Object arguments) {
this.method = Objects.requireNonNull(method);
assert method != null;
this.method = method;
this.arguments = arguments;
}
}

View File

@ -7,7 +7,7 @@ package io.flutter.plugin.common;
import java.io.ByteArrayOutputStream;
import java.math.BigInteger;
import java.nio.ByteBuffer;
import java.nio.charset.StandardCharsets;
import java.nio.charset.Charset;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
@ -65,6 +65,7 @@ public final class StandardMessageCodec implements MessageCodec<Object> {
return value;
}
private static final Charset UTF8 = Charset.forName("UTF8");
private static final byte NULL = 0;
private static final byte TRUE = 1;
private static final byte FALSE = 2;
@ -84,7 +85,7 @@ public final class StandardMessageCodec implements MessageCodec<Object> {
assert 0 <= value;
if (value < 254) {
stream.write(value);
} else if (value < 0xffff) {
} else if (value <= 0xffff) {
stream.write(254);
stream.write(value >>> 8);
stream.write(value);
@ -150,13 +151,13 @@ public final class StandardMessageCodec implements MessageCodec<Object> {
} else if (value instanceof BigInteger) {
stream.write(BIGINT);
writeBytes(stream,
((BigInteger) value).toString(16).getBytes(StandardCharsets.UTF_8));
((BigInteger) value).toString(16).getBytes(UTF8));
} else {
throw new IllegalArgumentException("Unsupported Number type: " + value.getClass());
}
} else if (value instanceof String) {
stream.write(STRING);
writeBytes(stream, ((String) value).getBytes(StandardCharsets.UTF_8));
writeBytes(stream, ((String) value).getBytes(UTF8));
} else if (value instanceof byte[]) {
stream.write(BYTE_ARRAY);
writeBytes(stream, (byte[]) value);
@ -259,7 +260,7 @@ public final class StandardMessageCodec implements MessageCodec<Object> {
break;
case BIGINT: {
final byte[] hex = readBytes(buffer);
result = new BigInteger(new String(hex, StandardCharsets.UTF_8), 16);
result = new BigInteger(new String(hex, UTF8), 16);
break;
}
case DOUBLE:
@ -267,7 +268,7 @@ public final class StandardMessageCodec implements MessageCodec<Object> {
break;
case STRING: {
final byte[] bytes = readBytes(buffer);
result = new String(bytes, StandardCharsets.UTF_8);
result = new String(bytes, UTF8);
break;
}
case BYTE_ARRAY: {

View File

@ -5,13 +5,14 @@
package io.flutter.plugin.common;
import java.nio.ByteBuffer;
import java.nio.charset.StandardCharsets;
import java.nio.charset.Charset;
/**
* A {@link MessageCodec} using UTF-8 encoded String messages.
*/
public final class StringMessageCodec implements MessageCodec<String> {
// This codec must match the Dart codec of the same name in package flutter/services.
private static final Charset UTF8 = Charset.forName("UTF8");
public static final StringMessageCodec INSTANCE = new StringMessageCodec();
private StringMessageCodec() {
@ -23,7 +24,7 @@ public final class StringMessageCodec implements MessageCodec<String> {
return null;
}
// TODO(mravn): Avoid the extra copy below.
final byte[] bytes = message.getBytes(StandardCharsets.UTF_8);
final byte[] bytes = message.getBytes(UTF8);
final ByteBuffer buffer = ByteBuffer.allocateDirect(bytes.length);
buffer.put(bytes);
return buffer;
@ -46,6 +47,6 @@ public final class StringMessageCodec implements MessageCodec<String> {
message.get(bytes);
offset = 0;
}
return new String(bytes, offset, length, StandardCharsets.UTF_8);
return new String(bytes, offset, length, UTF8);
}
}