下面列出了com.google.protobuf.ByteString#isValidUtf8 ( ) 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
public void testThreeBytes() {
// Travis' OOM killer doesn't like this test
if (System.getenv("TRAVIS") == null) {
int count = 0;
int valid = 0;
for (int i = Byte.MIN_VALUE; i <= Byte.MAX_VALUE; i++) {
for (int j = Byte.MIN_VALUE; j <= Byte.MAX_VALUE; j++) {
for (int k = Byte.MIN_VALUE; k <= Byte.MAX_VALUE; k++) {
byte[] bytes = new byte[]{(byte) i, (byte) j, (byte) k};
ByteString bs = ByteString.copyFrom(bytes);
if (!bs.isValidUtf8()) {
assertInvalid(bytes);
} else {
valid++;
}
count++;
if ((count % 1000000L) == 0) {
println("Processed " + (count / 1000000L) + " million characters");
}
}
}
}
assertEquals(IsValidUtf8TestUtil.EXPECTED_THREE_BYTE_ROUNDTRIPPABLE_COUNT, valid);
}
}
@Test
public void testOneByte() {
int valid = 0;
for (int i = Byte.MIN_VALUE; i <= Byte.MAX_VALUE; i++) {
ByteString bs = ByteString.copyFrom(new byte[] {(byte) i });
if (!bs.isValidUtf8()) { //from -128 to -1
assertInvalid(bs.toByteArray());
} else {
valid++; //from 0 to 127
}
}
assertEquals(IsValidUtf8TestUtil.EXPECTED_ONE_BYTE_ROUNDTRIPPABLE_COUNT, valid);
}
@Test
public void testTwoBytes() {
int valid = 0;
for (int i = Byte.MIN_VALUE; i <= Byte.MAX_VALUE; i++) {
for (int j = Byte.MIN_VALUE; j <= Byte.MAX_VALUE; j++) {
ByteString bs = ByteString.copyFrom(new byte[]{(byte) i, (byte) j});
if (!bs.isValidUtf8()) {
assertInvalid(bs.toByteArray());
} else {
valid++;
}
}
}
assertEquals(IsValidUtf8TestUtil.EXPECTED_TWO_BYTE_ROUNDTRIPPABLE_COUNT, valid);
}
/**
* Handles a control message from a client (publish, subscribe, etc.)
*/
private void handleControlMessage(ConnectedClient client, byte[] bytes) {
ControlMessage message;
try {
message = ControlMessage.parseFrom(bytes);
log.debug("Control message from client {}: {}", client, message);
} catch (InvalidProtocolBufferException e) {
// Bad message, ignore
log.warn("Bad control message from client {}: {}", client, e);
return;
}
// Do unsubscribes
for (ByteString unsubscribe : message.getTopicUnsubscribeList()) {
if (log.isInfoEnabled()) {
if (unsubscribe.isValidUtf8()) {
log.info("{} unsubscribing from {}", client, unsubscribe.toStringUtf8());
} else {
log.info("{} unsubscribing from {}", client, unsubscribe);
}
}
subscriptions.remove(client, unsubscribe);
}
// Do subscribes
for (ByteString subscribe : message.getTopicSubscribeList()) {
if (log.isInfoEnabled()) {
if (subscribe.isValidUtf8()) {
log.info("{} subscribing to {}", client, subscribe.toStringUtf8());
} else {
log.info("{} subscribing to {}", client, subscribe);
}
}
subscriptions.put(client, subscribe, true);
}
}