本文整理汇总了Java中io.netty.buffer.ByteBuf.getBytes方法的典型用法代码示例。如果您正苦于以下问题:Java ByteBuf.getBytes方法的具体用法?Java ByteBuf.getBytes怎么用?Java ByteBuf.getBytes使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类io.netty.buffer.ByteBuf
的用法示例。
在下文中一共展示了ByteBuf.getBytes方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: readBytes
import io.netty.buffer.ByteBuf; //导入方法依赖的package包/类
@Override
public ByteBuffer readBytes(ByteBuf source) {
int len = readInt(source);
if (len < 0) {
return null;
}
ByteBuf slice = source.readSlice(len);
// if direct byte buffer, return underlying nioBuffer.
if (slice.isDirect()) {
return slice.nioBuffer();
}
// otherwise copy to a byte array and wrap it.
final byte[] out = new byte[slice.readableBytes()];
source.getBytes(source.readerIndex(), out, 0, len);
return ByteBuffer.wrap(out);
}
示例2: decode
import io.netty.buffer.ByteBuf; //导入方法依赖的package包/类
@Override
protected void decode(ChannelHandlerContext ctx, NettoFrame msg, List<Object> out) throws Exception {
ByteBuf headerContent = msg.getHeaderContent();
byte[] headerBytesBuf = new byte[msg.getHeaderContentSize()];
headerContent.getBytes(headerContent.readerIndex(), headerBytesBuf);
NettoMessage message = new NettoMessage();
ByteBuf body = msg.getBody();
byte[] bodyBytesBuf = new byte[msg.getBodySize()];
body.getBytes(body.readerIndex(), bodyBytesBuf);
Map<String,String> headers = this.decoderHeader(new String(headerBytesBuf));
message.setBody(bodyBytesBuf);
message.setHeaders(headers);
out.add(message);
}
示例3: encode
import io.netty.buffer.ByteBuf; //导入方法依赖的package包/类
public ByteBuf encode(ByteBufAllocator allocator, SocksCmdRequest msg) {
if (LOG.isTraceEnabled()) {
LOG.trace("encode target address");
}
ByteBuf buf = allocator.directBuffer();
msg.encodeAsByteBuf(buf);
buf.skipBytes(3);
if (LOG.isTraceEnabled()) {
byte[] bytes = new byte[buf.readableBytes()];
buf.getBytes(buf.readerIndex(), bytes);
}
return buf;
}
示例4: decode
import io.netty.buffer.ByteBuf; //导入方法依赖的package包/类
@Override
protected void decode(ChannelHandlerContext ctx, ByteBuf msg, List<Object> out) throws Exception {
final byte[] array;
final int offset;
final int length = msg.readableBytes();
if (msg.hasArray()) {
array = msg.array();
offset = msg.arrayOffset() + msg.readerIndex();
} else {
array = new byte[length];
msg.getBytes(msg.readerIndex(), array, 0, length);
offset = 0;
}
if (extensionRegistry == null) {
if (HAS_PARSER) {
out.add(prototype.getParserForType().parseFrom(array, offset, length));
} else {
out.add(prototype.newBuilderForType().mergeFrom(array, offset, length).build());
}
} else {
if (HAS_PARSER) {
out.add(prototype.getParserForType().parseFrom(array, offset, length, extensionRegistry));
} else {
out.add(prototype.newBuilderForType().mergeFrom(array, offset, length, extensionRegistry).build());
}
}
}
示例5: decode
import io.netty.buffer.ByteBuf; //导入方法依赖的package包/类
@Override
protected void decode(ChannelHandlerContext ctx, ByteBuf msg, List<Object> out) throws Exception {
final int length = msg.readableBytes();
final byte[] array = new byte[length];
msg.getBytes(msg.readerIndex(), array, 0, length);
out.add(new MessagePack().read(array, Packet.class));
}
示例6: dotStuffUsingByteBuf
import io.netty.buffer.ByteBuf; //导入方法依赖的package包/类
private String dotStuffUsingByteBuf(String testString, boolean atStartOfLine, boolean appendCRLF) {
ByteBuf sourceBuffer = ALLOCATOR.buffer();
sourceBuffer.writeBytes(testString.getBytes(StandardCharsets.UTF_8));
byte[] previousBytes = atStartOfLine ? null : new byte[] { 'x', 'y' };
ByteBuf destBuffer = DotStuffing.createDotStuffedBuffer(ALLOCATOR, sourceBuffer, previousBytes,
appendCRLF ? MessageTermination.ADD_CRLF : MessageTermination.DO_NOT_TERMINATE);
byte[] bytes = new byte[destBuffer.readableBytes()];
destBuffer.getBytes(0, bytes);
return new String(bytes, CharsetUtil.UTF_8);
}
示例7: channelRead
import io.netty.buffer.ByteBuf; //导入方法依赖的package包/类
@Override
public void channelRead(ChannelHandlerContext ctx, Object msg) {
Channel udpChannel = XChannelMapper.getUdpChannel(udpSource);
if (udpChannel == null) {
log.warn("Bad Connection! (udp channel closed)");
XChannelMapper.closeChannelGracefullyByTcpChannel(ctx.channel());
} else if (udpChannel.isActive()) {
ByteBuf byteBuf = (ByteBuf) msg;
try {
if (!byteBuf.hasArray()) {
byte[] bytes = new byte[byteBuf.readableBytes()];
byteBuf.getBytes(0, bytes);
bytes = wrapper.unwrap(bytes);
XRequest request = requestResolver.parse(bytes);
String host = request.getHost();
int port = request.getPort();
byte[] content = Arrays.copyOfRange(bytes, bytes.length - request.getSubsequentDataLength(), bytes.length);
log.info("\t Proxy << Target \tFrom {}:{}", host, port);
// redirect tcp -> udp
udpChannel.writeAndFlush(new DatagramPacket(Unpooled.wrappedBuffer(content), udpSource, new InetSocketAddress(host, port)));
log.info("\tClient << Proxy \tGet [{} bytes]", content.length);
}
} finally {
ReferenceCountUtil.release(msg);
}
}
}
示例8: directBuffer
import io.netty.buffer.ByteBuf; //导入方法依赖的package包/类
/**
* Listing 5.2
*/
public static void directBuffer(ByteBuf directBuf) {
if (!directBuf.hasArray()) {
int length = directBuf.readableBytes();
byte[] array = new byte[length];
directBuf.getBytes(directBuf.readerIndex(), array);
handleArray(array, 0, length);
}
}
示例9: extractMultiMapping
import io.netty.buffer.ByteBuf; //导入方法依赖的package包/类
private Map<RtTag, byte[]> extractMultiMapping(ByteBuf msg) {
// extractOffsets will leave the reader index positioned at the first tag
int[] offsets = extractOffsets(msg);
int startOfValues = msg.readerIndex() + (4 * numTags);
Map<RtTag, byte[]> mapping = new LinkedHashMap<>(numTags);
RtTag prevTag = null;
for (int i = 0; i < offsets.length; i++) {
long uintCurrTag = msg.readUnsignedInt();
RtTag currTag = RtTag.fromUnsignedInt((int) uintCurrTag);
if ((prevTag != null) && currTag.isLessThan(prevTag)) {
String exMsg = String.format(
"tags not strictly increasing: current '%s' (0x%08x), previous '%s' (0x%08x)",
currTag, currTag.valueLE(), prevTag, prevTag.valueLE()
);
throw new TagsNotIncreasingException(exMsg);
}
int valueIdx = startOfValues + offsets[i];
int valueLen = ((i + 1) == offsets.length) ? msg.readableBytes() - offsets[i]
: offsets[i + 1] - offsets[i];
byte[] valueBytes = new byte[valueLen];
msg.getBytes(valueIdx, valueBytes);
mapping.put(currTag, valueBytes);
prevTag = currTag;
}
return mapping;
}
示例10: readContent
import io.netty.buffer.ByteBuf; //导入方法依赖的package包/类
protected static byte[] readContent(FullHttpRequest request) {
ByteBuf buf = request.content();
if (buf == null) {
return null;
}
byte[] bytes = new byte[buf.readableBytes()];
buf.getBytes(buf.readerIndex(), bytes);
return bytes;
}
示例11: writeToStream
import io.netty.buffer.ByteBuf; //导入方法依赖的package包/类
public void writeToStream(FSDataOutputStream stream) throws IOException {
Stopwatch watch = Stopwatch.createStarted();
ByteBuf buf = null;
try {
check = ThreadLocalRandom.current().nextLong();
start = stream.getPos();
logger.debug("Writing check value {} at position {}", check, start);
stream.writeLong(check);
batch.getHeader().writeDelimitedTo(stream);
buf = batch.getBody();
if (buf != null) {
bodyLength = buf.capacity();
} else {
bodyLength = 0;
}
if (bodyLength > 0) {
buf.getBytes(0, stream, bodyLength);
}
stream.hsync();
FileStatus status = spillFile.getFileStatus();
long len = status.getLen();
logger.debug("After spooling batch, stream at position {}. File length {}", stream.getPos(), len);
long t = watch.elapsed(TimeUnit.MICROSECONDS);
logger.debug("Took {} us to spool {} to disk. Rate {} mb/s", t, bodyLength, bodyLength / t);
} finally {
// even if the try block throws an exception we still want to send an ACK and release the lock
// the caller will add the exception to deferred attribute and it will be thrown when the poll() method is called
try {
batch.sendOk(); // this can also throw an exception
} finally {
state = BatchState.SPILLED;
batch = null;
if (buf != null) {
buf.release();
}
}
}
}
示例12: encode
import io.netty.buffer.ByteBuf; //导入方法依赖的package包/类
@Override
protected void encode(ChannelHandlerContext channelHandlerContext, OutboundMsg msg, ByteBuf out) throws Exception {
byte type = msg.getType();
byte opt = msg.getOpt();
String msgStr = msg.getMsgStr();
byte[] data = msg.getData();
byte[] msgBytes = msgStr.getBytes(Charset.forName("utf-8"));
// 文本消息长度
int msgLength = msgBytes.length;
// 二进制数据长度
int dataLength = data == null ? 0 : data.length;
// 总消息长度
int allDataLength = msgLength + dataLength + OTHER_INFO_LENGTH;
out.writeBytes(OutboundMsg.getHEADER());//输出头部标识
out.writeInt(allDataLength);//其后所有的数据长度(字节)
out.writeByte(type);//数据类型
out.writeByte(opt);//操作类型
out.writeInt(msgLength);//信息数据长度(字节)
out.writeBytes(msgBytes);//信息字符串内容(已加密,字节)
if (data != null) {
out.writeBytes(data);//二进制数据
}
byte[] bytesToCheck = new byte[allDataLength - 1];
out.getBytes(7, bytesToCheck);
byte check = calculate(bytesToCheck);
out.writeByte(check);//冗余校验值
}
示例13: decode
import io.netty.buffer.ByteBuf; //导入方法依赖的package包/类
/**
* 解码
*
* @param in
* @return
* @throws Exception
*/
public static LibraMessage decode(ByteBuf in) throws Exception {
LibraMessage message = new LibraMessage();
message.setHead(LibraHead.decode(in));
short bodyLength = in.readShort();// 消息体的长度
if (bodyLength != in.readableBytes()) {
LibraLog.error("LibraMessage.decode is error! params:bodyLength:" + bodyLength + "\treadableLength:" + in.readableBytes());
return null;
}
ByteBuf bodyByteBuf = in.readBytes(in.readableBytes());
byte[] array;
// 反序列化数据的起始点
int offset;
// 可读的数据字节长度
int readableLen = bodyByteBuf.readableBytes();
// 分为包含数组数据和不包含数组数据两种形式
if (bodyByteBuf.hasArray()) {
array = bodyByteBuf.array();
offset = bodyByteBuf.arrayOffset() + bodyByteBuf.readerIndex();
} else {
array = new byte[readableLen];
bodyByteBuf.getBytes(bodyByteBuf.readerIndex(), array, 0, readableLen);
offset = 0;
}
// 反序列化
message.setBody(decodeBody(message.getHead().getProtocolID(), array, offset, readableLen));
return message;
}
示例14: writeToStream
import io.netty.buffer.ByteBuf; //导入方法依赖的package包/类
public void writeToStream(FSDataOutputStream stream) throws IOException {
Stopwatch watch = new Stopwatch();
watch.start();
available = false;
check = ThreadLocalRandom.current().nextLong();
start = stream.getPos();
logger.debug("Writing check value {} at position {}", check, start);
stream.writeLong(check);
batch.getHeader().writeDelimitedTo(stream);
ByteBuf buf = batch.getBody();
if (buf != null) {
bodyLength = buf.capacity();
} else {
bodyLength = 0;
}
if (bodyLength > 0) {
buf.getBytes(0, stream, bodyLength);
}
stream.hsync();
FileStatus status = fs.getFileStatus(path);
long len = status.getLen();
logger.debug("After spooling batch, stream at position {}. File length {}", stream.getPos(), len);
batch.sendOk();
latch.countDown();
long t = watch.elapsed(TimeUnit.MICROSECONDS);
logger.debug("Took {} us to spool {} to disk. Rate {} mb/s", t, bodyLength, bodyLength / t);
if (buf != null) {
buf.release();
}
}
示例15: extract
import io.netty.buffer.ByteBuf; //导入方法依赖的package包/类
private String extract(Object o) {
assertThat(o).isInstanceOf(ByteBuf.class);
ByteBuf buffer = (ByteBuf) o;
byte[] bytes = new byte[buffer.readableBytes()];
buffer.getBytes(0, bytes);
return new String(bytes, CharsetUtil.UTF_8);
}