下面列出了io.netty.buffer.ByteBufUtil#getBytes ( ) 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
public static Object toAvroObject(Object value) {
if (value != null) {
if (value instanceof ByteBuffer) {
ByteBuffer bb = (ByteBuffer) value;
byte[] bytes = new byte[bb.remaining()];
bb.duplicate().get(bytes);
return bytes;
} else if (value instanceof ByteBuf) {
return ByteBufUtil.getBytes((ByteBuf) value);
} else {
return value;
}
} else {
return null;
}
}
@Override
protected void channelRead0(final ChannelHandlerContext ctx, FullHttpRequest msg) throws Exception {
// request parse
final byte[] requestBytes = ByteBufUtil.getBytes(msg.content()); // byteBuf.toString(io.netty.util.CharsetUtil.UTF_8);
final String uri = msg.uri();
final boolean keepAlive = HttpUtil.isKeepAlive(msg);
// do invoke
serverHandlerPool.execute(new Runnable() {
@Override
public void run() {
process(ctx, uri, requestBytes, keepAlive);
}
});
}
static HandshakeV9Request decodeV9(ByteBuf buf, HandshakeHeader header) {
int bytes = buf.readableBytes();
if (bytes <= 0) {
return new HandshakeV9Request(header, EMPTY_BYTES);
}
byte[] salt;
if (buf.getByte(buf.writerIndex() - 1) == 0) {
salt = ByteBufUtil.getBytes(buf, buf.readerIndex(), bytes - 1);
} else {
salt = ByteBufUtil.getBytes(buf);
}
return new HandshakeV9Request(header, salt);
}
/**
* Like above, but only retrieves the bytes and template ids
*/
public static List<Map.Entry<Integer, byte[]>> parseTemplatesShallow(ByteBuf bb) {
final ImmutableList.Builder<Map.Entry<Integer, byte[]>> templates = ImmutableList.builder();
int len = bb.readUnsignedShort();
int p = 4; // flow set id and length field itself
while (p < len) {
final int start = bb.readerIndex();
final int templateId = bb.readUnsignedShort();
final int fieldCount = bb.readUnsignedShort();
final ImmutableList.Builder<NetFlowV9FieldDef> fieldDefs = ImmutableList.builder();
for (int i = 0; i < fieldCount; i++) {
int fieldType = bb.readUnsignedShort();
int fieldLength = bb.readUnsignedShort();
}
final byte[] bytes = ByteBufUtil.getBytes(bb, start, bb.readerIndex() - start);
final Map.Entry<Integer, byte[]> template = Maps.immutableEntry(templateId, bytes);
templates.add(template);
p += 4 + fieldCount * 4;
}
return templates.build();
}
private void logging(Channel conn, PulsarApi.BaseCommand.Type cmdtype, String info, List<RawMessage> messages) throws Exception{
if (messages != null) {
// lag
for (int i=0; i<messages.size(); i++) {
info = info + "["+ (System.currentTimeMillis() - messages.get(i).getPublishTime()) + "] " + new String(ByteBufUtil.getBytes((messages.get(i)).getData()), "UTF8");
}
}
// log conn format is like from source to target
switch (this.connType) {
case ParserProxyHandler.FRONTEND_CONN:
log.info(ParserProxyHandler.FRONTEND_CONN + ":{} cmd:{} msg:{}", "[" + conn.remoteAddress() + conn.localAddress() + "]", cmdtype, info);
break;
case ParserProxyHandler.BACKEND_CONN:
log.info(ParserProxyHandler.BACKEND_CONN + ":{} cmd:{} msg:{}", "[" + conn.localAddress() + conn.remoteAddress() + "]", cmdtype, info);
break;
}
}
@Override
protected final CompletableFuture<ByteBuf> handleMessage(ByteBuf message) {
final byte[] bytes;
try {
bytes = ByteBufUtil.getBytes(message);
} finally {
message.release();
}
return handleMessage(bytes).thenApply(Unpooled::wrappedBuffer);
}
static ChangeAuthMessage decode(ByteBuf buf) {
buf.skipBytes(1); // skip generic header 0xFE of change authentication messages
String authType = HandshakeHeader.readCStringAscii(buf);
int bytes = buf.readableBytes();
byte[] salt = bytes > 0 && buf.getByte(buf.writerIndex() - 1) == TERMINAL ?
ByteBufUtil.getBytes(buf, buf.readerIndex(), bytes - 1) :
ByteBufUtil.getBytes(buf);
// The terminal character has been removed from salt.
return new ChangeAuthMessage(authType, salt);
}
@Override
public byte[] readSizeFixedBytes(int length) {
require(length > 0, "length must be a positive integer");
ByteBuf buf = nonEmptyBuffer();
if (buf.readableBytes() >= length) {
return ByteBufUtil.getBytes(buf.readSlice(length));
}
return readBytes(buf, length);
}
@Override
public byte[] decode(ByteBuf value, FieldInformation info, Class<?> target, boolean binary, CodecContext context) {
if (!value.isReadable()) {
return EMPTY_BYTES;
}
return ByteBufUtil.getBytes(value);
}
private void cancelGoAwayStreams(int lastStreamId, long errorCode, ByteBuf debugData) {
Iterator<PendingStream> iter = pendingStreams.values().iterator();
Exception e = new Http2GoAwayException(lastStreamId, errorCode, ByteBufUtil.getBytes(debugData));
while (iter.hasNext()) {
PendingStream stream = iter.next();
if (stream.streamId > lastStreamId) {
iter.remove();
stream.close(e);
}
}
}
static ChangeAuthMessage decode(ByteBuf buf) {
buf.skipBytes(1); // skip generic header 0xFE of change authentication messages
String authType = HandshakeHeader.readCStringAscii(buf);
int bytes = buf.readableBytes();
byte[] salt = bytes > 0 && buf.getByte(buf.writerIndex() - 1) == TERMINAL ?
ByteBufUtil.getBytes(buf, buf.readerIndex(), bytes - 1) :
ByteBufUtil.getBytes(buf);
// The terminal character has been removed from salt.
return new ChangeAuthMessage(authType, salt);
}
public static byte[] uncompressedFrame(ByteBuf proto) {
final ByteBuf buf = Unpooled.buffer();
buf.writeByte(0);
buf.writeInt(proto.readableBytes());
buf.writeBytes(proto);
proto.release();
final byte[] result = ByteBufUtil.getBytes(buf);
buf.release();
return result;
}
@Override
public byte[] readSizeFixedBytes(int length) {
require(length > 0, "length must be a positive integer");
ByteBuf buf = nonEmptyBuffer();
if (buf.readableBytes() >= length) {
return ByteBufUtil.getBytes(buf.readSlice(length));
}
return readBytes(buf, length);
}
@Override
public byte[] decode(ByteBuf value, FieldInformation info, Class<?> target, boolean binary, CodecContext context) {
if (!value.isReadable()) {
return EMPTY_BYTES;
}
return ByteBufUtil.getBytes(value);
}
/**
* Read file to byte[]
* @param sourcePath sourcePath
* @param sourceFileName sourceFileName
* @return byte[]
* @throws FileNotFoundException FileNotFoundException
* @throws IOException IOException
*/
public static byte[] readFileToBytes(String sourcePath, String sourceFileName) throws FileNotFoundException,IOException {
ByteBuf byteBuf = readFileToByteBuf(sourcePath,sourceFileName);
writerModeToReadMode(byteBuf);
try {
return ByteBufUtil.getBytes(byteBuf,byteBuf.readerIndex(), byteBuf.readableBytes(),false);
}finally {
RecyclableUtil.release(byteBuf);
}
}
protected byte[] toByteArray(Encoder encoder, Object value) throws IOException {
if (value == null) {
return null;
}
ByteBuf buf = encoder.encode(value);
try {
return ByteBufUtil.getBytes(buf);
} finally {
buf.release();
}
}
protected byte[] toByteArray(Encoder encoder, Object value) throws IOException {
if (value == null) {
return null;
}
ByteBuf buf = encoder.encode(value);
try {
return ByteBufUtil.getBytes(buf);
} finally {
buf.release();
}
}
@Override
public byte[] readSizeFixedBytes(int length) {
require(length > 0, "length must be a positive integer");
return ByteBufUtil.getBytes(buf.readSlice(length));
}
@Override
public byte[] readSizeFixedBytes(int length) {
require(length > 0, "length must be a positive integer");
return ByteBufUtil.getBytes(buf.readSlice(length));
}
/**
* Returns a memory-based {@link DataBuffer} which will be garbage-collected.
*/
private DataBuffer withDataBufferFactory(PooledHttpData data) {
final byte[] dataArray = ByteBufUtil.getBytes(data.content());
data.release();
return delegate.wrap(dataArray);
}