com.couchbase.client.deps.io.netty.buffer.ByteBuf类的使用及代码示例

x33g5p2x  于2022-01-17 转载在 其他  
字(18.3k)|赞(0)|评价(0)|浏览(153)

本文整理了Java中com.couchbase.client.deps.io.netty.buffer.ByteBuf类的一些代码示例,展示了ByteBuf类的具体用法。这些代码示例主要来源于Github/Stackoverflow/Maven等平台,是从一些精选项目中提取出来的代码,具有较强的参考意义,能在一定程度帮忙到你。ByteBuf类的具体详情如下:
包路径:com.couchbase.client.deps.io.netty.buffer.ByteBuf
类名称:ByteBuf

ByteBuf介绍

[英]A random and sequential accessible sequence of zero or more bytes (octets). This interface provides an abstract view for one or more primitive byte arrays ( byte[]) and ByteBuffer.

Creation of a buffer

It is recommended to create a new buffer using the helper methods in Unpooled rather than calling an individual implementation's constructor.

Random Access Indexing

Just like an ordinary primitive byte array, ByteBuf uses zero-based indexing. It means the index of the first byte is always 0 and the index of the last byte is always #capacity(). For example, to iterate all bytes of a buffer, you can do the following, regardless of its internal implementation:

ByteBuf buffer = ...; 
for (int i = 0; i < buffer.capacity(); i ++) { 
byte b = buffer.getByte(i); 
System.out.println((char) b); 
}

Sequential Access Indexing

ByteBuf provides two pointer variables to support sequential read and write operations - #readerIndex() for a read operation and #writerIndex() for a write operation respectively. The following diagram shows how a buffer is segmented into three areas by the two pointers:

+-------------------+------------------+------------------+ 
| discardable bytes |  readable bytes  |  writable bytes  | 
|                   |     (CONTENT)    |                  | 
+-------------------+------------------+------------------+ 
|                   |                  |                  | 
0       
 Readable bytes (the actual content) 
This segment is where the actual data is stored.  Any operation whose name 
starts with  
 read or  
 skip will get or skip the data at the 
current  
#readerIndex() and increase it by the number of 
read bytes.  If the argument of the read operation is also a 
ByteBuf and no destination index is specified, the specified 
buffer's  
#writerIndex() is increased together. 
  
If there's not enough content left,  
IndexOutOfBoundsException is 
raised.  The default value of newly allocated, wrapped or copied buffer's 
#readerIndex() is  
 0. 
  
// Iterates the readable bytes of a buffer. 
ByteBuf buffer = ...; 
while (buffer.isReadable()) { 
System.out.println(buffer.readByte()); 
} 
 
 Writable bytes 
This segment is a undefined space which needs to be filled.  Any operation 
whose name starts with  
 write will write the data at the current 
#writerIndex() and increase it by the number of written 
bytes.  If the argument of the write operation is also a  
ByteBuf, 
and no source index is specified, the specified buffer's 
#readerIndex() is increased together. 
  
If there's not enough writable bytes left,  
IndexOutOfBoundsExceptionis raised.  The default value of newly allocated buffer's 
#writerIndex() is  
 0.  The default value of 
wrapped or copied buffer's  
#writerIndex() is the 
#capacity() of the buffer. 
  
// Fills the writable bytes of a buffer with random integers. 
ByteBuf buffer = ...; 
while (buffer.maxWritableBytes() >= 4) { 
buffer.writeInt(random.nextInt()); 
} 
 
 Discardable bytes 
This segment contains the bytes which were read already by a read operation. 
Initially, the size of this segment is  
 0, but its size increases up 
to the  
#writerIndex() as read operations are executed. 
The read bytes can be discarded by calling  
#discardReadBytes() to 
reclaim unused area as depicted by the following diagram: 
  
BEFORE discardReadBytes() 
+-------------------+------------------+------------------+ 
| discardable bytes |  readable bytes  |  writable bytes  | 
+-------------------+------------------+------------------+ 
|                   |                  |                  | 
0       
Please note that there is no guarantee about the content of writable bytes 
after calling  
#discardReadBytes().  The writable bytes will not be 
moved in most cases and could even be filled with completely different data 
depending on the underlying buffer implementation. 
 Clearing the buffer indexes 
You can set both  
#readerIndex() and 
#writerIndex() to  
 0 by calling  
#clear(). 
It does not clear the buffer content (e.g. filling with  
 0) but just 
clears the two pointers.  Please also note that the semantic of this 
operation is different from  
ByteBuffer#clear(). 
  
BEFORE clear() 
+-------------------+------------------+------------------+ 
| discardable bytes |  readable bytes  |  writable bytes  | 
+-------------------+------------------+------------------+ 
|                   |                  |                  | 
0       
 Search operations 
For simple single-byte searches, use  
#indexOf(int,int,byte) and  
#bytesBefore(int,int,byte). 
#bytesBefore(byte) is especially useful when you deal with a  
 NUL-terminated string. 
For complicated searches, use  
#forEachByte(int,int,ByteBufProcessor) with a  
ByteBufProcessorimplementation. 
 Mark and reset 
There are two marker indexes in every buffer. One is for storing 
#readerIndex() and the other is for storing 
#writerIndex().  You can always reposition one of the 
two indexes by calling a reset method.  It works in a similar fashion to 
the mark and reset methods in  
InputStream except that there's no 
 readlimit. 
 Derived buffers 
You can create a view of an existing buffer by calling either 
#duplicate(),  
#slice() or  
#slice(int,int). 
A derived buffer will have an independent  
#readerIndex(), 
#writerIndex() and marker indexes, while it shares 
other internal data representation, just like a NIO buffer does. 
  
In case a completely fresh copy of an existing buffer is required, please 
call  
#copy() method instead. 
  
Also be aware that obtaining derived buffers will NOT call  
#retain() and so the 
reference count will NOT be increased. 
 Conversion to existing JDK types 
 Byte array 
If a  
ByteBuf is backed by a byte array (i.e.  
 byte[]), 
you can access it directly via the  
#array() method.  To determine 
if a buffer is backed by a byte array,  
#hasArray() should be used. 
 NIO Buffers 
If a  
ByteBuf can be converted into an NIO  
ByteBuffer which shares its 
content (i.e. view buffer), you can get it via the  
#nioBuffer() method.  To determine 
if a buffer can be converted into an NIO buffer, use  
#nioBufferCount(). 
 Strings 
Various  
#toString(Charset) methods convert a  
ByteBufinto a  
String.  Please note that  
#toString() is not a 
conversion method. 
 I/O Streams 
Please refer to  
ByteBufInputStream and 
ByteBufOutputStream.

[中]零个或多个字节(八位字节)的随机顺序可访问序列。此接口为一个或多个基本字节数组(byte[])和ByteBuffer提供抽象视图。
####缓冲区的创建
建议使用Unpooled中的helper方法创建新缓冲区,而不是调用单个实现的构造函数。
####随机存取索引
与普通的基本字节数组一样,ByteBuf使用zero-based indexing。这意味着第一个字节的索引始终为0,最后一个字节的索引始终为#capacity()。例如,要迭代缓冲区的所有字节,无论其内部实现如何,都可以执行以下操作:

ByteBuf buffer = ...; 
for (int i = 0; i < buffer.capacity(); i ++) { 
byte b = buffer.getByte(i); 
System.out.println((char) b); 
}

####顺序存取索引
ByteBuf提供了两个指针变量来支持顺序读写操作——分别为读操作提供#readerIndex(),为写操作提供#writerIndex()。下图显示了如何通过两个指针将缓冲区分割为三个区域:

+-------------------+------------------+------------------+ 
| discardable bytes |  readable bytes  |  writable bytes  | 
|                   |     (CONTENT)    |                  | 
+-------------------+------------------+------------------+ 
|                   |                  |                  | 
0       
 Readable bytes (the actual content) 
This segment is where the actual data is stored.  Any operation whose name 
starts with  
 read or  
 skip will get or skip the data at the 
current  
#readerIndex() and increase it by the number of 
read bytes.  If the argument of the read operation is also a 
ByteBuf and no destination index is specified, the specified 
buffer's  
#writerIndex() is increased together. 
  
If there's not enough content left,  
IndexOutOfBoundsException is 
raised.  The default value of newly allocated, wrapped or copied buffer's 
#readerIndex() is  
 0. 
  
// Iterates the readable bytes of a buffer. 
ByteBuf buffer = ...; 
while (buffer.isReadable()) { 
System.out.println(buffer.readByte()); 
} 
 
 Writable bytes 
This segment is a undefined space which needs to be filled.  Any operation 
whose name starts with  
 write will write the data at the current 
#writerIndex() and increase it by the number of written 
bytes.  If the argument of the write operation is also a  
ByteBuf, 
and no source index is specified, the specified buffer's 
#readerIndex() is increased together. 
  
If there's not enough writable bytes left,  
IndexOutOfBoundsExceptionis raised.  The default value of newly allocated buffer's 
#writerIndex() is  
 0.  The default value of 
wrapped or copied buffer's  
#writerIndex() is the 
#capacity() of the buffer. 
  
// Fills the writable bytes of a buffer with random integers. 
ByteBuf buffer = ...; 
while (buffer.maxWritableBytes() >= 4) { 
buffer.writeInt(random.nextInt()); 
} 
 
 Discardable bytes 
This segment contains the bytes which were read already by a read operation. 
Initially, the size of this segment is  
 0, but its size increases up 
to the  
#writerIndex() as read operations are executed. 
The read bytes can be discarded by calling  
#discardReadBytes() to 
reclaim unused area as depicted by the following diagram: 
  
BEFORE discardReadBytes() 
+-------------------+------------------+------------------+ 
| discardable bytes |  readable bytes  |  writable bytes  | 
+-------------------+------------------+------------------+ 
|                   |                  |                  | 
0       
Please note that there is no guarantee about the content of writable bytes 
after calling  
#discardReadBytes().  The writable bytes will not be 
moved in most cases and could even be filled with completely different data 
depending on the underlying buffer implementation. 
 Clearing the buffer indexes 
You can set both  
#readerIndex() and 
#writerIndex() to  
 0 by calling  
#clear(). 
It does not clear the buffer content (e.g. filling with  
 0) but just 
clears the two pointers.  Please also note that the semantic of this 
operation is different from  
ByteBuffer#clear(). 
  
BEFORE clear() 
+-------------------+------------------+------------------+ 
| discardable bytes |  readable bytes  |  writable bytes  | 
+-------------------+------------------+------------------+ 
|                   |                  |                  | 
0       
 Search operations 
For simple single-byte searches, use  
#indexOf(int,int,byte) and  
#bytesBefore(int,int,byte). 
#bytesBefore(byte) is especially useful when you deal with a  
 NUL-terminated string. 
For complicated searches, use  
#forEachByte(int,int,ByteBufProcessor) with a  
ByteBufProcessorimplementation. 
 Mark and reset 
There are two marker indexes in every buffer. One is for storing 
#readerIndex() and the other is for storing 
#writerIndex().  You can always reposition one of the 
two indexes by calling a reset method.  It works in a similar fashion to 
the mark and reset methods in  
InputStream except that there's no 
 readlimit. 
 Derived buffers 
You can create a view of an existing buffer by calling either 
#duplicate(),  
#slice() or  
#slice(int,int). 
A derived buffer will have an independent  
#readerIndex(), 
#writerIndex() and marker indexes, while it shares 
other internal data representation, just like a NIO buffer does. 
  
In case a completely fresh copy of an existing buffer is required, please 
call  
#copy() method instead. 
  
Also be aware that obtaining derived buffers will NOT call  
#retain() and so the 
reference count will NOT be increased. 
 Conversion to existing JDK types 
 Byte array 
If a  
ByteBuf is backed by a byte array (i.e.  
 byte[]), 
you can access it directly via the  
#array() method.  To determine 
if a buffer is backed by a byte array,  
#hasArray() should be used. 
 NIO Buffers 
If a  
ByteBuf can be converted into an NIO  
ByteBuffer which shares its 
content (i.e. view buffer), you can get it via the  
#nioBuffer() method.  To determine 
if a buffer can be converted into an NIO buffer, use  
#nioBufferCount(). 
 Strings 
Various  
#toString(Charset) methods convert a  
ByteBufinto a  
String.  Please note that  
#toString() is not a 
conversion method. 
 I/O Streams 
Please refer to  
ByteBufInputStream and 
ByteBufOutputStream.

代码示例

代码示例来源:origin: apache/incubator-gobblin

@Test
public void testBasicConvert() throws Exception {
 Schema dataRecordSchema = SchemaBuilder.record("Data")
   .fields()
   .name("data").type().bytesType().noDefault()
   .name("flags").type().intType().noDefault()
   .endRecord();
 Schema schema = SchemaBuilder.record("TestRecord")
   .fields()
   .name("key").type().stringType().noDefault()
   .name("data").type(dataRecordSchema).noDefault()
   .endRecord();
 GenericData.Record testRecord = new GenericData.Record(schema);
 String testContent = "hello world";
 GenericData.Record dataRecord = new GenericData.Record(dataRecordSchema);
 dataRecord.put("data", ByteBuffer.wrap(testContent.getBytes(Charset.forName("UTF-8"))));
 dataRecord.put("flags", 0);
 testRecord.put("key", "hello");
 testRecord.put("data", dataRecord);
 Converter<Schema, String, GenericRecord, TupleDocument> recordConverter = new AvroToCouchbaseTupleConverter();
 TupleDocument returnDoc = recordConverter.convertRecord("", testRecord, null).iterator().next();
 byte[] returnedBytes = new byte[returnDoc.content().value1().readableBytes()];
 returnDoc.content().value1().readBytes(returnedBytes);
 Assert.assertEquals(returnedBytes, testContent.getBytes(Charset.forName("UTF-8")));
 int returnedFlags = returnDoc.content().value2();
 Assert.assertEquals(returnedFlags, 0);
}

代码示例来源:origin: apache/nifi

byteBuf.getBytes(byteBuf.readerIndex(), out, byteBuf.readableBytes());
  byteBuf.release();
};
doc = document;

代码示例来源:origin: apache/nifi

public static String getStringContent(Object content) {
  if (content instanceof String) {
    return (String) content;
  } else if (content instanceof byte[]) {
    return new String((byte[]) content, StandardCharsets.UTF_8);
  } else if (content instanceof ByteBuf) {
    final ByteBuf byteBuf = (ByteBuf) content;
    byte[] bytes = new byte[byteBuf.readableBytes()];
    byteBuf.readBytes(bytes);
    byteBuf.release();
    return new String(bytes, StandardCharsets.UTF_8);
  }
  return content.toString();
}

代码示例来源:origin: apache/incubator-gobblin

void onWrite(AbstractDocument doc)
  throws UnsupportedEncodingException {
 recordClass = doc.getClass();
 if (doc instanceof TupleDocument) {
  ByteBuf outgoingBuf = (((TupleDocument) doc).content()).value1();
  byte[] outgoingBytes = new byte[outgoingBuf.readableBytes()];
  outgoingBuf.getBytes(0, outgoingBytes);
  verificationCache.put(doc.id(), outgoingBytes);
 } else if (doc instanceof RawJsonDocument) {
  verificationCache.put(doc.id(), ((RawJsonDocument) doc).content().getBytes("UTF-8"));
 } else {
  throw new UnsupportedOperationException("Can only support TupleDocument or RawJsonDocument at this time");
 }
}

代码示例来源:origin: com.couchbase.client/core-io

private int setInput(ByteBuf compressed) {
  int len = compressed.readableBytes();
  if (compressed.hasArray()) {
    decompressor.setInput(compressed.array(), compressed.arrayOffset() + compressed.readerIndex(), len);
  } else {
    byte[] in = new byte[len];
    compressed.getBytes(compressed.readerIndex(), in);
    decompressor.setInput(in, 0, in.length);
  }
  return len;
}

代码示例来源:origin: com.couchbase.client/core-io

public ByteBuf encodeDataFrame(ByteBufAllocator allocator, int streamId, boolean last, ByteBuf data) {
  byte flags = last ? SPDY_DATA_FLAG_FIN : 0;
  int length = data.readableBytes();
  ByteBuf frame = allocator.ioBuffer(SPDY_HEADER_SIZE + length).order(ByteOrder.BIG_ENDIAN);
  frame.writeInt(streamId & 0x7FFFFFFF);
  frame.writeByte(flags);
  frame.writeMedium(length);
  frame.writeBytes(data, data.readerIndex(), length);
  return frame;
}

代码示例来源:origin: com.couchbase.client/core-io

@Override
public byte[] get() {
  if (byteBuf == null) {
    return EMPTY_BUFFER.array();
  }
  byte[] array = new byte[byteBuf.readableBytes()];
  byteBuf.getBytes(byteBuf.readerIndex(), array);
  return array;
}

代码示例来源:origin: com.couchbase.client/core-io

static ByteBuf expandCumulation(ByteBufAllocator alloc, ByteBuf cumulation, int readable) {
  ByteBuf oldCumulation = cumulation;
  cumulation = alloc.buffer(oldCumulation.readableBytes() + readable);
  cumulation.writeBytes(oldCumulation);
  oldCumulation.release();
  return cumulation;
}

代码示例来源:origin: com.couchbase.client/core-io

@Override
public long skip(long bytes) throws IOException {
  int readable = buffer.readableBytes();
  if (readable < bytes) {
    bytes = readable;
  }
  buffer.readerIndex((int) (buffer.readerIndex() + bytes));
  return bytes;
}

代码示例来源:origin: com.couchbase.client/core-io

@Override
public ByteBuf setBytes(int index, ByteBuf src, int srcIndex, int length) {
  checkSrcIndex(index, length, srcIndex, src.capacity());
  if (src.hasMemoryAddress()) {
    PlatformDependent.copyMemory(src.memoryAddress() + srcIndex, array, index, length);
  } else  if (src.hasArray()) {
    setBytes(index, src.array(), src.arrayOffset() + srcIndex, length);
  } else {
    src.getBytes(srcIndex, array, index, length);
  }
  return this;
}

代码示例来源:origin: couchbase/java-dcp-client

public static void setExtras(ByteBuf extras, ByteBuf buffer) {
  byte oldExtrasLength = buffer.getByte(EXTRAS_LENGTH_OFFSET);
  byte newExtrasLength = (byte) extras.readableBytes();
  int oldBodyLength = buffer.getInt(BODY_LENGTH_OFFSET);
  int newBodyLength = oldBodyLength - oldExtrasLength + newExtrasLength;
  buffer.setByte(EXTRAS_LENGTH_OFFSET, newExtrasLength);
  buffer.setInt(BODY_LENGTH_OFFSET, newBodyLength);
  buffer.setBytes(HEADER_SIZE, extras);
  buffer.writerIndex(HEADER_SIZE + newBodyLength);
}

代码示例来源:origin: apache/incubator-gobblin

@Override
 public void onNext(D doc) {
  try {
   callbackFired.set(true);
   WriteResponse writeResponse = new GenericWriteResponse<D>(doc);
   writeResponseQueue.add(new Pair<WriteResponse, Throwable>(writeResponse, null));
   callback.onSuccess(writeResponse);
  } finally {
   if (doc instanceof TupleDocument) {
    ((TupleDocument) doc).content().value1().release();
   }
  }
 }
});

代码示例来源:origin: com.couchbase.client/core-io

private RawQueryResponse handleRawQueryResponse(boolean lastChunk, ChannelHandlerContext ctx) {
  if (!lastChunk) {
    return null;
  }
  ResponseStatus status = ResponseStatusConverter.fromHttp(responseHeader.getStatus().code());
  ByteBuf responseCopy = ctx.alloc().buffer(responseContent.readableBytes(), responseContent.readableBytes());
  responseCopy.writeBytes(responseContent);
  return new RawQueryResponse(status, currentRequest(), responseCopy,
      responseHeader.getStatus().code(),
      responseHeader.getStatus().reasonPhrase());
}

代码示例来源:origin: com.couchbase.client/core-io

public ByteBuf encodeHeadersFrame(ByteBufAllocator allocator, int streamId, boolean last, ByteBuf headerBlock) {
  int headerBlockLength = headerBlock.readableBytes();
  byte flags = last ? SPDY_FLAG_FIN : 0;
  int length = 4 + headerBlockLength;
  ByteBuf frame = allocator.ioBuffer(SPDY_HEADER_SIZE + length).order(ByteOrder.BIG_ENDIAN);
  writeControlFrameHeader(frame, SPDY_HEADERS_FRAME, flags, length);
  frame.writeInt(streamId);
  frame.writeBytes(headerBlock, headerBlock.readerIndex(), headerBlockLength);
  return frame;
}

代码示例来源:origin: com.couchbase.client/core-io

@Override
public ByteBuf getBytes(int index, ByteBuf dst, int dstIndex, int length) {
  checkDstIndex(index, length, dstIndex, dst.capacity());
  if (dst.hasMemoryAddress()) {
    PlatformDependent.copyMemory(array, index, dst.memoryAddress() + dstIndex, length);
  } else if (dst.hasArray()) {
    getBytes(index, dst.array(), dst.arrayOffset() + dstIndex, length);
  } else {
    dst.setBytes(dstIndex, array, index, length);
  }
  return this;
}

代码示例来源:origin: com.couchbase.client/core-io

/**
 * Returns the proxy protocol specification version in the buffer if the version is found.
 * Returns -1 if no version was found in the buffer.
 */
private static int findVersion(final ByteBuf buffer) {
  final int n = buffer.readableBytes();
  // per spec, the version number is found in the 13th byte
  if (n < 13) {
    return -1;
  }
  int idx = buffer.readerIndex();
  return match(BINARY_PREFIX, buffer, idx) ? buffer.getByte(idx + BINARY_PREFIX_LENGTH) : 1;
}

代码示例来源:origin: com.couchbase.client/core-io

private boolean compressInto(ByteBuf compressed) {
  byte[] out = compressed.array();
  int off = compressed.arrayOffset() + compressed.writerIndex();
  int toWrite = compressed.writableBytes();
  int numBytes = compressor.deflate(out, off, toWrite, Deflater.SYNC_FLUSH);
  compressed.writerIndex(compressed.writerIndex() + numBytes);
  return numBytes == toWrite;
}

代码示例来源:origin: com.couchbase.client/core-io

public static ByteBuf decode(ByteBuf src, Base64Dialect dialect) {
  if (src == null) {
    throw new NullPointerException("src");
  }
  ByteBuf dest = decode(src, src.readerIndex(), src.readableBytes(), dialect);
  src.readerIndex(src.writerIndex());
  return dest;
}

代码示例来源:origin: com.couchbase.client/core-io

public void setValue(String value, int rank) {
  if (value == null) {
    throw new NullPointerException("value");
  }
  ByteBuf buf = Unpooled.copiedBuffer(value, charset);
  ByteBuf old = this.value.set(rank, buf);
  if (old != null) {
    size -= old.readableBytes();
    old.release();
  }
  size += buf.readableBytes();
}

代码示例来源:origin: com.couchbase.client/core-io

@Override
public ByteBuf readBytes(ByteBuf dst, int dstIndex, int length) {
  buf.readBytes(dst, dstIndex, length);
  return this;
}

相关文章