com.google.protobuf.UnsafeByteOperations Java Examples

The following examples show how to use com.google.protobuf.UnsafeByteOperations. 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: TestOptimisticByteOutput.java    From dremio-oss with Apache License 2.0 6 votes vote down vote up
@Test
public void testRopeByteString() throws Exception {
  ByteString literal = ByteString.copyFrom(smallData);

  ByteString data = literal;
  for (int i = 0; i < 3; i++) {
    data = data.concat(literal);
  }

  final byte[] expected;
  try (ByteArrayOutputStream baos = new ByteArrayOutputStream()) {
    for (int i = 0; i < 4; i++) {
      baos.write(smallData);
    }
    expected = baos.toByteArray();
  }

  OptimisticByteOutput byteOutput = new OptimisticByteOutput(smallData.length * 4);
  UnsafeByteOperations.unsafeWriteTo(data, byteOutput);

  assertArrayEquals(expected, byteOutput.toByteArray());
}
 
Example #2
Source File: ProtobufTranslationImpl.java    From calcite-avatica with Apache License 2.0 6 votes vote down vote up
void serializeMessage(OutputStream out, Message msg) throws IOException {
  // Serialize the protobuf message
  UnsynchronizedBuffer buffer = threadLocalBuffer.get();
  ByteString serializedMsg;
  try {
    msg.writeTo(buffer);
    // Make a bytestring from it
    serializedMsg = UnsafeByteOperations.unsafeWrap(buffer.toArray());
  } finally {
    buffer.reset();
  }

  // Wrap the serialized message in a WireMessage
  WireMessage wireMsg = WireMessage.newBuilder().setNameBytes(getClassNameBytes(msg.getClass()))
      .setWrappedMessage(serializedMsg).build();

  // Write the WireMessage to the provided OutputStream
  wireMsg.writeTo(out);
}
 
Example #3
Source File: ProtobufTranslationImpl.java    From calcite-avatica with Apache License 2.0 6 votes vote down vote up
@Override public Request parseRequest(byte[] bytes) throws IOException {
  ByteString byteString = UnsafeByteOperations.unsafeWrap(bytes);
  CodedInputStream inputStream = byteString.newCodedInput();
  // Enable aliasing to avoid an extra copy to get at the serialized Request inside of the
  // WireMessage.
  inputStream.enableAliasing(true);
  WireMessage wireMsg = WireMessage.parseFrom(inputStream);

  String serializedMessageClassName = wireMsg.getName();

  try {
    RequestTranslator translator = getParserForRequest(serializedMessageClassName);

    // The ByteString should be logical offsets into the original byte array
    return translator.transform(wireMsg.getWrappedMessage());
  } catch (RuntimeException e) {
    if (LOG.isDebugEnabled()) {
      LOG.debug("Failed to parse request message '{}'", TextFormat.shortDebugString(wireMsg));
    }
    throw e;
  }
}
 
Example #4
Source File: ProtobufTranslationImpl.java    From calcite-avatica with Apache License 2.0 6 votes vote down vote up
@Override public Response parseResponse(byte[] bytes) throws IOException {
  ByteString byteString = UnsafeByteOperations.unsafeWrap(bytes);
  CodedInputStream inputStream = byteString.newCodedInput();
  // Enable aliasing to avoid an extra copy to get at the serialized Response inside of the
  // WireMessage.
  inputStream.enableAliasing(true);
  WireMessage wireMsg = WireMessage.parseFrom(inputStream);

  String serializedMessageClassName = wireMsg.getName();
  try {
    ResponseTranslator translator = getParserForResponse(serializedMessageClassName);

    return translator.transform(wireMsg.getWrappedMessage());
  } catch (RuntimeException e) {
    if (LOG.isDebugEnabled()) {
      LOG.debug("Failed to parse response message '{}'", TextFormat.shortDebugString(wireMsg));
    }
    throw e;
  }
}
 
Example #5
Source File: ProtobufSerializationTest.java    From calcite-avatica with Apache License 2.0 6 votes vote down vote up
@Test public void testExecuteSerialization() throws Exception {
  Service.ExecuteRequest executeRequest = new Service.ExecuteRequest(
      new StatementHandle("connection", 12345, getSignature()), getTypedValues(), 0);

  Requests.ExecuteRequest pbExecuteRequest = executeRequest.serialize();
  ByteArrayOutputStream baos = new ByteArrayOutputStream(1024);
  pbExecuteRequest.writeTo(baos);

  byte[] serialized = baos.toByteArray();
  baos.reset();
  WireMessage wireMsg = WireMessage.newBuilder().setName(Requests.ExecuteRequest.class.getName())
      .setWrappedMessage(UnsafeByteOperations.unsafeWrap(serialized)).build();
  wireMsg.writeTo(baos);
  serialized = baos.toByteArray();

  ProtobufTranslation translator = new ProtobufTranslationImpl();

  Request newRequest = translator.parseRequest(serialized);

  Assert.assertEquals(executeRequest, newRequest);
}
 
Example #6
Source File: StackdriverSender.java    From zipkin-gcp with Apache License 2.0 5 votes vote down vote up
static Span parseTraceIdPrefixedSpan(
    byte[] traceIdPrefixedSpan, int spanNameSize, ByteString traceIdPrefix) {
  // start parsing after the trace ID
  int off = 32, len = traceIdPrefixedSpan.length - off;
  Span.Builder span = Span.newBuilder();
  try {
    span.mergeFrom(traceIdPrefixedSpan, off, len);
  } catch (IOException e) {
    throw new AssertionError(e);
  }

  int offset = 0;

  // Span name in Stackdriver is the global unique identifier of the span, including project ID,
  // trace ID, and span ID. It is _not_ the same as the name in Zipkin which is the semantic name.
  byte[] spanName = new byte[spanNameSize];
  traceIdPrefix.copyTo(spanName, offset);
  offset += traceIdPrefix.size();
  System.arraycopy(traceIdPrefixedSpan, 0, spanName, offset, 32);
  offset += 32;
  SPAN_ID_PREFIX.copyTo(spanName, offset);
  offset += SPAN_ID_PREFIX.size();
  span.getSpanIdBytes().copyTo(spanName, offset);

  span.setNameBytes(UnsafeByteOperations.unsafeWrap(spanName));
  return span.build();
}
 
Example #7
Source File: TopicMessage.java    From hedera-mirror-node with Apache License 2.0 5 votes vote down vote up
public ConsensusTopicResponse toResponse() {
    if (response == null) {
        response = ConsensusTopicResponse.newBuilder()
                .setConsensusTimestamp(Timestamp.newBuilder()
                        .setSeconds(getConsensusTimestampInstant().getEpochSecond())
                        .setNanos(getConsensusTimestampInstant().getNano())
                        .build())
                .setMessage(UnsafeByteOperations.unsafeWrap(message))
                .setRunningHash(UnsafeByteOperations.unsafeWrap(runningHash))
                .setRunningHashVersion(runningHashVersion)
                .setSequenceNumber(sequenceNumber)
                .build();
    }
    return response;
}
 
Example #8
Source File: TestOptimisticByteOutputUnsafeByteOperations.java    From dremio-oss with Apache License 2.0 5 votes vote down vote up
@Test
public void testUnsafeByteOperations() throws IOException {
  // Arrange
  final ByteString bytes = ByteString.copyFrom(data);
  final OptimisticByteOutput byteOutput = new OptimisticByteOutput(bytes.size());

  // Act
  UnsafeByteOperations.unsafeWriteTo(bytes, byteOutput);

  // Assert
  assertNotSame(data, byteOutput.toByteArray());
  assertArrayEquals(data, byteOutput.toByteArray());
}
 
Example #9
Source File: TestOptimisticByteOutput.java    From dremio-oss with Apache License 2.0 5 votes vote down vote up
@Test
public void testRopeByteStringWithZeroOnRight() throws Exception {
  ByteString literal = ByteString.copyFrom(smallData);

  ByteString data = (ByteString) NEW_ROPE_BYTE_STRING_INSTANCE.invoke(null, literal, ByteString.EMPTY);

  OptimisticByteOutput byteOutput = new OptimisticByteOutput(smallData.length);
  UnsafeByteOperations.unsafeWriteTo(data, byteOutput);

  byte[] array = (byte[]) FieldUtils.readField(literal, "bytes", true);
  assertArrayEquals(smallData, byteOutput.toByteArray());
  assertSame(array, byteOutput.toByteArray());
}
 
Example #10
Source File: TestOptimisticByteOutput.java    From dremio-oss with Apache License 2.0 5 votes vote down vote up
@Test
public void testRopeByteStringWithZeroOnLeft() throws Exception {
  ByteString literal = ByteString.copyFrom(smallData);

  ByteString data = (ByteString) NEW_ROPE_BYTE_STRING_INSTANCE.invoke(null, ByteString.EMPTY, literal);

  OptimisticByteOutput byteOutput = new OptimisticByteOutput(smallData.length);
  UnsafeByteOperations.unsafeWriteTo(data, byteOutput);

  byte[] array = (byte[]) FieldUtils.readField(literal, "bytes", true);
  assertArrayEquals(smallData, byteOutput.toByteArray());
  assertSame(array, byteOutput.toByteArray());
}
 
Example #11
Source File: TestOptimisticByteOutput.java    From dremio-oss with Apache License 2.0 5 votes vote down vote up
@Test
public void testLiteralByteString() throws Exception {
  ByteString literal = ByteString.copyFrom(smallData);

  OptimisticByteOutput byteOutput = new OptimisticByteOutput(literal.size());
  UnsafeByteOperations.unsafeWriteTo(literal, byteOutput);

  byte[] array = (byte[]) FieldUtils.readField(literal, "bytes", true);
  assertArrayEquals(smallData, byteOutput.toByteArray());
  assertSame(array, byteOutput.toByteArray());
}
 
Example #12
Source File: BigtableToAvro.java    From DataflowTemplates with Apache License 2.0 5 votes vote down vote up
/**
 * Extracts the byte array from the given {@link ByteString} without copy.
 *
 * @param byteString A {@link ByteString} from which to extract the array.
 * @return an array of byte.
 */
protected static byte[] toByteArray(final ByteString byteString) {
  try {
    ZeroCopyByteOutput byteOutput = new ZeroCopyByteOutput();
    UnsafeByteOperations.unsafeWriteTo(byteString, byteOutput);
    return byteOutput.bytes;
  } catch (IOException e) {
    return byteString.toByteArray();
  }
}
 
Example #13
Source File: KeyUtils.java    From etcd-java with Apache License 2.0 5 votes vote down vote up
public static ByteString fromHexString(CharSequence seq) {
    int len = seq.length();
    if (len == 0) {
        return ByteString.EMPTY;
    }
    if (len % 2 != 0) {
        throw new IllegalArgumentException("must be even number of chars");
    }
    int blen = len >> 1;
    byte[] bytes = new byte[blen];
    for (int i = 0, j = 0; i < blen; i ++, j += 2) {
        bytes[i] = (byte) ((digitFor(seq.charAt(j)) << 4) | digitFor(seq.charAt(j + 1)));
    }
    return UnsafeByteOperations.unsafeWrap(bytes);
}
 
Example #14
Source File: CoreIndexedStoreImpl.java    From dremio-oss with Apache License 2.0 4 votes vote down vote up
private Document toDoc(KVStoreTuple<K> key, PutOption option) {
  final Document doc = new Document();
  final SimpleDocumentWriter documentWriter = new SimpleDocumentWriter(doc);

  RemoteDataStoreProtobuf.PutOptionInfo putOptionInfo = option.getPutOptionInfo();

  for (RemoteDataStoreProtobuf.PutRequestIndexField indexField : putOptionInfo.getIndexFieldsList()) {
    IndexKey indexKey = RemoteDataStoreUtils.toIndexKey(indexField.getKey());

    if (indexField.getValueDoubleCount() > 0) {
      indexField.getValueDoubleList().forEach(v -> documentWriter.write(indexKey, v));
    } else if (indexField.getValueInt32Count() > 0) {
      indexField.getValueInt32List().forEach(v -> documentWriter.write(indexKey, v));
    } else if (indexField.getValueInt64Count() > 0) {
      indexField.getValueInt64List().forEach(v -> documentWriter.write(indexKey, v));
    } else if (indexField.getValueBytesCount() > 0) {
      final byte[][] byteArray = new byte[indexField.getValueBytesList().size()][];
      for (int i = 0; i < indexField.getValueBytesList().size(); i++) {
        byteArray[i] = indexField.getValueBytes(i).toByteArray();
        final ByteString byteString = indexField.getValueBytes(i);

        final OptimisticByteOutput byteOutput = new OptimisticByteOutput(byteString.size());
        try {
          UnsafeByteOperations.unsafeWriteTo(byteString, byteOutput);
        } catch (IOException e) {
          throw new IllegalStateException(String.format("Problem reading binary data from field: %s", indexKey.getIndexFieldName()), e);
        }
        byteArray[i] = byteOutput.toByteArray();
      }

      documentWriter.write(indexKey, byteArray);
    } else if (indexField.getValueStringCount() > 0) {
      documentWriter.write(indexKey, indexField.getValueStringList().toArray(new String[indexField.getValueStringList().size()]));
    } else {
      throw new IllegalStateException(String.format("Unknown index field type for field name: %s", indexField.getKey().getIndexFieldName()));
    }
  }

  if (doc.getFields().isEmpty()) {
    return null;
  }

  documentWriter.write(ID_KEY, key.getSerializedBytes());

  return doc;
}
 
Example #15
Source File: ProtobufTranslationImpl.java    From calcite-avatica with Apache License 2.0 4 votes vote down vote up
private static ByteString wrapClassName(Class<?> clz) {
  return UnsafeByteOperations.unsafeWrap(clz.getName().getBytes(UTF_8));
}
 
Example #16
Source File: Service.java    From calcite-avatica with Apache License 2.0 4 votes vote down vote up
public RpcMetadataResponse(@JsonProperty("serverAddress") String serverAddress) {
  this.serverAddress = serverAddress;
  this.serverAddressAsBytes = UnsafeByteOperations.unsafeWrap(serverAddress.getBytes(UTF_8));
}
 
Example #17
Source File: KeyUtils.java    From etcd-java with Apache License 2.0 4 votes vote down vote up
public static ByteString singleByte(int b) {
    return UnsafeByteOperations.unsafeWrap(new byte[] { (byte) b });
}