本文整理了Java中com.google.protobuf.ByteString.wrap()
方法的一些代码示例,展示了ByteString.wrap()
的具体用法。这些代码示例主要来源于Github
/Stackoverflow
/Maven
等平台,是从一些精选项目中提取出来的代码,具有较强的参考意义,能在一定程度帮忙到你。ByteString.wrap()
方法的具体详情如下:
包路径:com.google.protobuf.ByteString
类名称:ByteString
方法名:wrap
[英]Wraps the given bytes into a ByteString. Intended for internal only usage.
[中]将给定字节包装到ByteString中。仅供内部使用。
代码示例来源:origin: com.google.protobuf/protobuf-java
/**
* An unsafe operation that returns a {@link ByteString} that is backed by the provided buffer.
*
* @param buffer the Java NIO buffer to be wrapped
* @return a {@link ByteString} backed by the provided buffer
*/
public static ByteString unsafeWrap(ByteBuffer buffer) {
return ByteString.wrap(buffer);
}
代码示例来源:origin: com.google.protobuf/protobuf-java
/**
* An unsafe operation that returns a {@link ByteString} that is backed by the provided buffer.
*
* @param buffer the buffer to be wrapped
* @return a {@link ByteString} backed by the provided buffer
*/
public static ByteString unsafeWrap(byte[] buffer) {
return ByteString.wrap(buffer);
}
代码示例来源:origin: com.google.protobuf/protobuf-java
/**
* An unsafe operation that returns a {@link ByteString} that is backed by a subregion of the
* provided buffer.
*
* @param buffer the buffer to be wrapped
* @param offset the offset of the wrapped region
* @param length the number of bytes of the wrapped region
* @return a {@link ByteString} backed by the provided buffer
*/
public static ByteString unsafeWrap(byte[] buffer, int offset, int length) {
return ByteString.wrap(buffer, offset, length);
}
代码示例来源:origin: com.google.protobuf/protobuf-java
/**
* Wraps the given bytes into a {@code ByteString}. Intended for internal only usage.
*/
static ByteString wrap(ByteBuffer buffer) {
if (buffer.hasArray()) {
final int offset = buffer.arrayOffset();
return ByteString.wrap(buffer.array(), offset + buffer.position(), buffer.remaining());
} else {
return new NioByteString(buffer);
}
}
代码示例来源:origin: com.google.protobuf/protobuf-java
Object writeReplace() {
return ByteString.wrap(toByteArray());
}
代码示例来源:origin: com.google.protobuf/protobuf-java
Object writeReplace() {
return ByteString.wrap(toByteArray());
}
代码示例来源:origin: com.google.protobuf/protobuf-java
@Override
public ByteString readBytes() throws IOException {
final int size = readRawVarint32();
if (size > 0 && size <= (limit - pos)) {
// Fast path: We already have the bytes in a contiguous buffer, so
// just copy directly from it.
final ByteString result =
immutable && enableAliasing
? ByteString.wrap(buffer, pos, size)
: ByteString.copyFrom(buffer, pos, size);
pos += size;
return result;
}
if (size == 0) {
return ByteString.EMPTY;
}
// Slow path: Build a byte array first then copy it.
return ByteString.wrap(readRawBytes(size));
}
代码示例来源:origin: com.google.protobuf/protobuf-java
/**
* Like readBytes, but caller must have already checked the fast path: (size <= (bufferSize -
* pos) && size > 0 || size == 0)
*/
private ByteString readBytesSlowPath(final int size) throws IOException {
final byte[] result = readRawBytesSlowPathOneChunk(size);
if (result != null) {
return ByteString.wrap(result);
}
final int originalBufferPos = pos;
final int bufferedBytes = bufferSize - pos;
// Mark the current buffer consumed.
totalBytesRetired += bufferSize;
pos = 0;
bufferSize = 0;
// Determine the number of bytes we need to read from the input stream.
int sizeLeft = size - bufferedBytes;
// The size is very large. For security reasons we read them in small
// chunks.
List<byte[]> chunks = readRawBytesSlowPathRemainingChunks(sizeLeft);
// Wrap the byte arrays into a single ByteString.
List<ByteString> byteStrings = new ArrayList<ByteString>(1 + chunks.size());
byteStrings.add(ByteString.copyFrom(buffer, originalBufferPos, bufferedBytes));
for (byte[] chunk : chunks) {
byteStrings.add(ByteString.wrap(chunk));
}
return ByteString.copyFrom(byteStrings);
}
代码示例来源:origin: com.google.protobuf/protobuf-java
@Override
public ByteString readBytes() throws IOException {
final int size = readRawVarint32();
if (size > 0 && size <= currentByteBufferLimit - currentByteBufferPos) {
if (immutable && enableAliasing) {
final int idx = (int) (currentByteBufferPos - currentAddress);
final ByteString result = ByteString.wrap(slice(idx, idx + size));
currentByteBufferPos += size;
return result;
} else {
byte[] bytes;
bytes = new byte[size];
UnsafeUtil.copyMemory(currentByteBufferPos, bytes, 0, size);
currentByteBufferPos += size;
return ByteString.wrap(bytes);
}
} else if (size > 0 && size <= remaining()) {
byte[] temp = new byte[size];
readRawBytesTo(temp, 0, size);
return ByteString.wrap(temp);
}
if (size == 0) {
return ByteString.EMPTY;
}
if (size < 0) {
throw InvalidProtocolBufferException.negativeSize();
}
throw InvalidProtocolBufferException.truncatedMessage();
}
代码示例来源:origin: com.google.protobuf/protobuf-java
/**
* Concatenates two strings by copying data values. This is called in a few
* cases in order to reduce the growth of the number of tree nodes.
*
* @param left string on the left
* @param right string on the right
* @return string formed by copying data bytes
*/
private static ByteString concatenateBytes(ByteString left,
ByteString right) {
int leftSize = left.size();
int rightSize = right.size();
byte[] bytes = new byte[leftSize + rightSize];
left.copyTo(bytes, 0, 0, leftSize);
right.copyTo(bytes, 0, leftSize, rightSize);
return ByteString.wrap(bytes); // Constructor wraps bytes
}
代码示例来源:origin: com.google.protobuf/protobuf-java
@Override
public ByteString readBytes() throws IOException {
final int size = readRawVarint32();
if (size > 0 && size <= remaining()) {
if (immutable && enableAliasing) {
final ByteBuffer result = slice(pos, pos + size);
pos += size;
return ByteString.wrap(result);
} else {
// Use UnsafeUtil to copy the memory to bytes instead of using ByteBuffer ways.
byte[] bytes = new byte[size];
UnsafeUtil.copyMemory(pos, bytes, 0, size);
pos += size;
return ByteString.wrap(bytes);
}
}
if (size == 0) {
return ByteString.EMPTY;
}
if (size < 0) {
throw InvalidProtocolBufferException.negativeSize();
}
throw InvalidProtocolBufferException.truncatedMessage();
}
代码示例来源:origin: com.google.protobuf/protobuf-java
? ByteString.wrap(result) // This reference has not been out of our control.
: ByteString.copyFrom(result, 0, pos);
代码示例来源:origin: com.google.protobuf/protobuf-lite
/** Read a {@code bytes} field value from the stream. */
public ByteString readBytes() throws IOException {
final int size = readRawVarint32();
if (size <= (bufferSize - bufferPos) && size > 0) {
// Fast path: We already have the bytes in a contiguous buffer, so
// just copy directly from it.
final ByteString result = bufferIsImmutable && enableAliasing
? ByteString.wrap(buffer, bufferPos, size)
: ByteString.copyFrom(buffer, bufferPos, size);
bufferPos += size;
return result;
} else if (size == 0) {
return ByteString.EMPTY;
} else {
// Slow path: Build a byte array first then copy it.
return ByteString.wrap(readRawBytesSlowPath(size));
}
}
代码示例来源:origin: com.google.protobuf/protobuf-lite
/**
* An unsafe operation that returns a {@link ByteString} that is backed by the provided buffer.
*
* @param buffer the Java NIO buffer to be wrapped
* @return a {@link ByteString} backed by the provided buffer
*/
public static ByteString unsafeWrap(ByteBuffer buffer) {
if (buffer.hasArray()) {
final int offset = buffer.arrayOffset();
return ByteString.wrap(buffer.array(), offset + buffer.position(), buffer.remaining());
} else {
return new NioByteString(buffer);
}
}
代码示例来源:origin: com.google.protobuf/protobuf-lite
Object writeReplace() {
return ByteString.wrap(toByteArray());
}
代码示例来源:origin: yeriomin/play-store-api
Object writeReplace() {
return ByteString.wrap(toByteArray());
}
代码示例来源:origin: yeriomin/play-store-api
/**
* An unsafe operation that returns a {@link ByteString} that is backed by the provided buffer.
*
* @param buffer the Java NIO buffer to be wrapped
* @return a {@link ByteString} backed by the provided buffer
*/
public static ByteString unsafeWrap(ByteBuffer buffer) {
if (buffer.hasArray()) {
final int offset = buffer.arrayOffset();
return ByteString.wrap(buffer.array(), offset + buffer.position(), buffer.remaining());
} else {
return new NioByteString(buffer);
}
}
代码示例来源:origin: com.google.protobuf/protobuf-lite
Object writeReplace() {
return ByteString.wrap(toByteArray());
}
代码示例来源:origin: yeriomin/play-store-api
Object writeReplace() {
return ByteString.wrap(toByteArray());
}
代码示例来源:origin: com.google.protobuf/protobuf-lite
/**
* Concatenates two strings by copying data values. This is called in a few
* cases in order to reduce the growth of the number of tree nodes.
*
* @param left string on the left
* @param right string on the right
* @return string formed by copying data bytes
*/
private static ByteString concatenateBytes(ByteString left,
ByteString right) {
int leftSize = left.size();
int rightSize = right.size();
byte[] bytes = new byte[leftSize + rightSize];
left.copyTo(bytes, 0, 0, leftSize);
right.copyTo(bytes, 0, leftSize, rightSize);
return ByteString.wrap(bytes); // Constructor wraps bytes
}
内容来源于网络,如有侵权,请联系作者删除!