Skip to content

Commit f9c1f5f

Browse files
authored
Remove usage of JDK7 features not available until Android SDK 19+ (#3462)
1 parent 1bd1f0e commit f9c1f5f

5 files changed

Lines changed: 25 additions & 19 deletions

File tree

‎shell/platform/android/io/flutter/plugin/common/FlutterMessageChannel.java‎

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,6 @@
1010
import io.flutter.view.FlutterView.BinaryMessageResponse;
1111
import io.flutter.view.FlutterView.OnBinaryMessageListenerAsync;
1212
import java.nio.ByteBuffer;
13-
import java.util.Objects;
14-
import java.util.concurrent.atomic.AtomicBoolean;
1513

1614
/**
1715
* A named channel for communicating with the Flutter application using semi-structured messages.
@@ -43,9 +41,12 @@ public final class FlutterMessageChannel<T> {
4341
* @param codec a {@link MessageCodec}.
4442
*/
4543
public FlutterMessageChannel(FlutterView view, String name, MessageCodec<T> codec) {
46-
this.view = Objects.requireNonNull(view);
47-
this.name = Objects.requireNonNull(name);
48-
this.codec = Objects.requireNonNull(codec);
44+
assert view != null;
45+
assert name != null;
46+
assert codec != null;
47+
this.view = view;
48+
this.name = name;
49+
this.codec = codec;
4950
}
5051

5152
/**

‎shell/platform/android/io/flutter/plugin/common/FlutterMethodChannel.java‎

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@
99
import io.flutter.view.FlutterView.BinaryMessageResponse;
1010
import io.flutter.view.FlutterView.OnBinaryMessageListenerAsync;
1111
import java.nio.ByteBuffer;
12-
import java.util.Objects;
1312
import java.util.concurrent.atomic.AtomicBoolean;
1413

1514
/**
@@ -53,9 +52,12 @@ public FlutterMethodChannel(FlutterView view, String name) {
5352
* @param codec a {@link MessageCodec}.
5453
*/
5554
public FlutterMethodChannel(FlutterView view, String name, MethodCodec codec) {
56-
this.view = Objects.requireNonNull(view);
57-
this.name = Objects.requireNonNull(name);
58-
this.codec = Objects.requireNonNull(codec);
55+
assert view != null;
56+
assert name != null;
57+
assert codec != null;
58+
this.view = view;
59+
this.name = name;
60+
this.codec = codec;
5961
}
6062

6163
/**

‎shell/platform/android/io/flutter/plugin/common/MethodCall.java‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,8 @@ public final class MethodCall {
2727
* @param arguments the arguments, a value supported by the channel's message codec.
2828
*/
2929
public MethodCall(String method, Object arguments) {
30-
this.method = Objects.requireNonNull(method);
30+
assert method != null;
31+
this.method = method;
3132
this.arguments = arguments;
3233
}
3334
}

‎shell/platform/android/io/flutter/plugin/common/StandardMessageCodec.java‎

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
import java.io.ByteArrayOutputStream;
88
import java.math.BigInteger;
99
import java.nio.ByteBuffer;
10-
import java.nio.charset.StandardCharsets;
10+
import java.nio.charset.Charset;
1111
import java.util.ArrayList;
1212
import java.util.HashMap;
1313
import java.util.List;
@@ -65,6 +65,7 @@ public Object decodeMessage(ByteBuffer message) {
6565
return value;
6666
}
6767

68+
private static final Charset UTF8 = Charset.forName("UTF8");
6869
private static final byte NULL = 0;
6970
private static final byte TRUE = 1;
7071
private static final byte FALSE = 2;
@@ -84,7 +85,7 @@ private static void writeSize(ByteArrayOutputStream stream, int value) {
8485
assert 0 <= value;
8586
if (value < 254) {
8687
stream.write(value);
87-
} else if (value < 0xffff) {
88+
} else if (value <= 0xffff) {
8889
stream.write(254);
8990
stream.write(value >>> 8);
9091
stream.write(value);
@@ -150,13 +151,13 @@ static void writeValue(ByteArrayOutputStream stream, Object value) {
150151
} else if (value instanceof BigInteger) {
151152
stream.write(BIGINT);
152153
writeBytes(stream,
153-
((BigInteger) value).toString(16).getBytes(StandardCharsets.UTF_8));
154+
((BigInteger) value).toString(16).getBytes(UTF8));
154155
} else {
155156
throw new IllegalArgumentException("Unsupported Number type: " + value.getClass());
156157
}
157158
} else if (value instanceof String) {
158159
stream.write(STRING);
159-
writeBytes(stream, ((String) value).getBytes(StandardCharsets.UTF_8));
160+
writeBytes(stream, ((String) value).getBytes(UTF8));
160161
} else if (value instanceof byte[]) {
161162
stream.write(BYTE_ARRAY);
162163
writeBytes(stream, (byte[]) value);
@@ -259,15 +260,15 @@ static Object readValue(ByteBuffer buffer) {
259260
break;
260261
case BIGINT: {
261262
final byte[] hex = readBytes(buffer);
262-
result = new BigInteger(new String(hex, StandardCharsets.UTF_8), 16);
263+
result = new BigInteger(new String(hex, UTF8), 16);
263264
break;
264265
}
265266
case DOUBLE:
266267
result = buffer.getDouble();
267268
break;
268269
case STRING: {
269270
final byte[] bytes = readBytes(buffer);
270-
result = new String(bytes, StandardCharsets.UTF_8);
271+
result = new String(bytes, UTF8);
271272
break;
272273
}
273274
case BYTE_ARRAY: {

‎shell/platform/android/io/flutter/plugin/common/StringMessageCodec.java‎

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,13 +5,14 @@
55
package io.flutter.plugin.common;
66

77
import java.nio.ByteBuffer;
8-
import java.nio.charset.StandardCharsets;
8+
import java.nio.charset.Charset;
99

1010
/**
1111
* A {@link MessageCodec} using UTF-8 encoded String messages.
1212
*/
1313
public final class StringMessageCodec implements MessageCodec<String> {
1414
// This codec must match the Dart codec of the same name in package flutter/services.
15+
private static final Charset UTF8 = Charset.forName("UTF8");
1516
public static final StringMessageCodec INSTANCE = new StringMessageCodec();
1617

1718
private StringMessageCodec() {
@@ -23,7 +24,7 @@ public ByteBuffer encodeMessage(String message) {
2324
return null;
2425
}
2526
// TODO(mravn): Avoid the extra copy below.
26-
final byte[] bytes = message.getBytes(StandardCharsets.UTF_8);
27+
final byte[] bytes = message.getBytes(UTF8);
2728
final ByteBuffer buffer = ByteBuffer.allocateDirect(bytes.length);
2829
buffer.put(bytes);
2930
return buffer;
@@ -46,6 +47,6 @@ public String decodeMessage(ByteBuffer message) {
4647
message.get(bytes);
4748
offset = 0;
4849
}
49-
return new String(bytes, offset, length, StandardCharsets.UTF_8);
50+
return new String(bytes, offset, length, UTF8);
5051
}
5152
}

0 commit comments

Comments
 (0)