本文整理了Java中io.netty.buffer.ByteBuf.writeBytes()
方法的一些代码示例,展示了ByteBuf.writeBytes()
的具体用法。这些代码示例主要来源于Github
/Stackoverflow
/Maven
等平台,是从一些精选项目中提取出来的代码,具有较强的参考意义,能在一定程度帮忙到你。ByteBuf.writeBytes()
方法的具体详情如下:
包路径:io.netty.buffer.ByteBuf
类名称:ByteBuf
方法名:writeBytes
[英]Transfers the specified source buffer's data to this buffer starting at the current writerIndex until the source buffer becomes unreadable, and increases the writerIndex by the number of the transferred bytes. This method is basically same with #writeBytes(ByteBuf,int,int), except that this method increases the readerIndex of the source buffer by the number of the transferred bytes while #writeBytes(ByteBuf,int,int)does not.
[中]从当前writerIndex开始将指定源缓冲区的数据传输到此缓冲区,直到源缓冲区变得不可读,并将writerIndex增加传输的字节数。此方法与#writeBytes(ByteBuf,int,int)基本相同,只是此方法将源缓冲区的readerIndex增加传输的字节数,而#writeBytes(ByteBuf,int,int)不增加。
代码示例来源:origin: netty/netty
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: AsyncHttpClient/async-http-client
private ByteBuf lazyLoadContentBuffer() {
if (contentBuffer == null) {
contentBuffer = ByteBufAllocator.DEFAULT.buffer((int) getContentLength());
contentBuffer.writeBytes(EXTRA_BYTES).writeBytes(boundary).writeBytes(EXTRA_BYTES).writeBytes(CRLF_BYTES);
}
return contentBuffer;
}
代码示例来源:origin: redisson/redisson
private void writeArgument(ByteBuf out, byte[] arg) {
out.writeByte(BYTES_PREFIX);
out.writeCharSequence(Long.toString(arg.length), CharsetUtil.US_ASCII);
out.writeBytes(CRLF);
out.writeBytes(arg);
out.writeBytes(CRLF);
}
代码示例来源:origin: traccar/traccar
public static ByteBuf formatMessage(int type, ByteBuf id, ByteBuf data) {
ByteBuf buf = Unpooled.buffer();
buf.writeByte(0x7e);
buf.writeShort(type);
buf.writeShort(data.readableBytes());
buf.writeBytes(id);
buf.writeShort(1); // index
buf.writeBytes(data);
data.release();
buf.writeByte(Checksum.xor(buf.nioBuffer(1, buf.readableBytes() - 1)));
buf.writeByte(0x7e);
return buf;
}
代码示例来源:origin: redisson/redisson
private void writeArgument(ByteBuf out, ByteBuf arg) {
out.writeByte(BYTES_PREFIX);
out.writeCharSequence(Long.toString(arg.readableBytes()), CharsetUtil.US_ASCII);
out.writeBytes(CRLF);
out.writeBytes(arg, arg.readerIndex(), arg.readableBytes());
out.writeBytes(CRLF);
}
代码示例来源:origin: netty/netty
/**
* Creates a new buffer whose content is a copy of the specified
* {@code buffer}'s readable bytes. The new buffer's {@code readerIndex}
* and {@code writerIndex} are {@code 0} and {@code buffer.readableBytes}
* respectively.
*/
public static ByteBuf copiedBuffer(ByteBuf buffer) {
int readable = buffer.readableBytes();
if (readable > 0) {
ByteBuf copy = buffer(readable);
copy.writeBytes(buffer, buffer.readerIndex(), readable);
return copy;
} else {
return EMPTY_BUFFER;
}
}
代码示例来源:origin: GlowstoneMC/Glowstone
private void sendResponse(ChannelHandlerContext ctx, int requestId, int type, String payload) {
ByteBuf buf = ctx.alloc().buffer().order(ByteOrder.LITTLE_ENDIAN);
buf.writeInt(requestId);
buf.writeInt(type);
buf.writeBytes(payload.getBytes(StandardCharsets.UTF_8));
buf.writeByte(0);
buf.writeByte(0);
ctx.write(buf);
}
代码示例来源:origin: eclipse-vertx/vert.x
private void end(ChannelHandlerContext ctx, ByteBuf buf, boolean h2c) {
if (current > 0) {
ByteBuf msg = Unpooled.buffer(current + buf.readableBytes());
msg.writeBytes(HTTP_2_PREFACE_ARRAY, 0, current);
msg.writeBytes(buf);
buf.release();
buf = msg;
}
configure(ctx, h2c);
ctx.pipeline().remove(this);
ctx.fireChannelRead(buf);
}
代码示例来源:origin: alibaba/Sentinel
private void encodeString(String param, ByteBuf target) {
target.writeByte(ClusterConstants.PARAM_TYPE_STRING);
byte[] tmpChars = param.getBytes();
target.writeInt(tmpChars.length);
target.writeBytes(tmpChars);
}
代码示例来源:origin: netty/netty
checkNotNull(buf, "buf");
checkNotNull(charset, "charset");
final int maxIndex = buf.readerIndex() + buf.readableBytes();
if (index < 0 || length < 0 || index > maxIndex - length) {
throw new IndexOutOfBoundsException("index: " + index + " length: " + length);
ByteBuf heapBuffer = buf.alloc().heapBuffer(length);
try {
heapBuffer.writeBytes(buf, index, length);
decoder.decode(heapBuffer.internalNioBuffer(heapBuffer.readerIndex(), length));
} finally {
heapBuffer.release();
代码示例来源:origin: redisson/redisson
private Object[] copy(Object[] params) {
List<Object> result = new ArrayList<Object>();
for (Object object : params) {
if (object instanceof ByteBuf) {
ByteBuf b = ((ByteBuf) object);
ByteBuf nb = ByteBufAllocator.DEFAULT.buffer(b.readableBytes());
int ri = b.readerIndex();
nb.writeBytes(b);
b.readerIndex(ri);
result.add(nb);
} else {
result.add(object);
}
}
return result.toArray();
}
代码示例来源:origin: redisson/redisson
private void encodeRawRecord(DnsRawRecord record, ByteBuf out) throws Exception {
encodeRecord0(record, out);
ByteBuf content = record.content();
int contentLen = content.readableBytes();
out.writeShort(contentLen);
out.writeBytes(content, content.readerIndex(), contentLen);
}
代码示例来源:origin: netty/netty
@Override
public ByteBuf copy(int index, int length) {
checkIndex(index, length);
boolean release = true;
ByteBuf buf = alloc().buffer(length);
try {
buf.writeBytes(this, index, length);
release = false;
return buf;
} finally {
if (release) {
buf.release();
}
}
}
代码示例来源:origin: traccar/traccar
private ByteBuf encodeContent(String content) {
ByteBuf buf = Unpooled.buffer();
buf.writeShort(content.length() + 6);
buf.writeShort(0); // index
buf.writeByte(0x04); // command type
buf.writeByte(0); // optional header
buf.writeBytes(content.getBytes(StandardCharsets.US_ASCII));
return buf;
}
代码示例来源:origin: AsyncHttpClient/async-http-client
protected long transfer(ByteBuf source, ByteBuf target, MultipartState sourceFullyWrittenState) {
int sourceRemaining = source.readableBytes();
int targetRemaining = target.writableBytes();
if (sourceRemaining <= targetRemaining) {
target.writeBytes(source);
state = sourceFullyWrittenState;
return sourceRemaining;
} else {
target.writeBytes(source, targetRemaining);
return targetRemaining;
}
}
代码示例来源:origin: netty/netty
out.writeBytes(STREAM_START);
int dataLength = in.readableBytes();
if (dataLength > MIN_COMPRESSIBLE_LENGTH) {
for (;;) {
out.writeInt(0);
if (dataLength > Short.MAX_VALUE) {
ByteBuf slice = in.readSlice(Short.MAX_VALUE);
代码示例来源:origin: GlowstoneMC/Glowstone
private ByteBuf responseToByteBuf(ChannelHandlerContext ctx, String string) {
ByteBuf bytebuf = ctx.alloc().buffer(3 + string.length());
bytebuf.writeByte(0xFF);
bytebuf.writeShort(string.length());
try {
bytebuf.writeBytes(string.getBytes("UTF-16BE"));
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
}
return bytebuf;
}
}
代码示例来源:origin: atomix/atomix
@Override
public void broadcast(String subject, byte[] payload) {
if (enabled) {
Message message = new Message(subject, payload);
byte[] bytes = SERIALIZER.encode(message);
ByteBuf buf = serverChannel.alloc().buffer(4 + bytes.length);
buf.writeInt(bytes.length).writeBytes(bytes);
serverChannel.writeAndFlush(new DatagramPacket(buf, groupAddress));
}
}
代码示例来源:origin: traccar/traccar
private ByteBuf encodeContent(String content) {
ByteBuf buf = Unpooled.buffer(12 + 56);
buf.writeCharSequence("\r\n*KW", StandardCharsets.US_ASCII);
buf.writeByte(0);
buf.writeShortLE(buf.capacity());
buf.writeShortLE(NoranProtocolDecoder.MSG_CONTROL);
buf.writeInt(0); // gis ip
buf.writeShortLE(0); // gis port
buf.writeBytes(content.getBytes(StandardCharsets.US_ASCII));
buf.writerIndex(buf.writerIndex() + 50 - content.length());
buf.writeCharSequence("\r\n", StandardCharsets.US_ASCII);
return buf;
}
代码示例来源:origin: mpusher/mpush
public void encodeBytes(ByteBuf body, byte[] field) {
if (field == null || field.length == 0) {
body.writeShort(0);
} else if (field.length < Short.MAX_VALUE) {
body.writeShort(field.length).writeBytes(field);
} else {
body.writeShort(Short.MAX_VALUE).writeInt(field.length - Short.MAX_VALUE).writeBytes(field);
}
}
内容来源于网络,如有侵权,请联系作者删除!