Java Code Examples for com.google.protobuf.ByteString#isValidUtf8()

The following examples show how to use com.google.protobuf.ByteString#isValidUtf8() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: Utf8Test.java    From incubator-datasketches-memory with Apache License 2.0 6 votes vote down vote up
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);
  }
}
 
Example 2
Source File: Utf8Test.java    From incubator-datasketches-memory with Apache License 2.0 5 votes vote down vote up
@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);
}
 
Example 3
Source File: Utf8Test.java    From incubator-datasketches-memory with Apache License 2.0 5 votes vote down vote up
@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);
}
 
Example 4
Source File: PubSubServer.java    From tracing-framework with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
/**
 * 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);
    }
}