com.google.protobuf.UnsafeByteOperations#unsafeWriteTo ( )源码实例Demo

下面列出了com.google.protobuf.UnsafeByteOperations#unsafeWriteTo ( ) 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。

源代码1 项目: dremio-oss   文件: TestOptimisticByteOutput.java
@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());
}
 
源代码2 项目: DataflowTemplates   文件: BigtableToAvro.java
/**
 * 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();
  }
}
 
源代码3 项目: dremio-oss   文件: TestOptimisticByteOutput.java
@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());
}
 
源代码4 项目: dremio-oss   文件: TestOptimisticByteOutput.java
@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());
}
 
源代码5 项目: dremio-oss   文件: TestOptimisticByteOutput.java
@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());
}
 
@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());
}
 
源代码7 项目: dremio-oss   文件: CoreIndexedStoreImpl.java
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;
}