本文整理了Java中io.netty.buffer.ByteBuf.readableBytes()
方法的一些代码示例,展示了ByteBuf.readableBytes()
的具体用法。这些代码示例主要来源于Github
/Stackoverflow
/Maven
等平台,是从一些精选项目中提取出来的代码,具有较强的参考意义,能在一定程度帮忙到你。ByteBuf.readableBytes()
方法的具体详情如下:
包路径:io.netty.buffer.ByteBuf
类名称:ByteBuf
方法名:readableBytes
[英]Returns the number of readable bytes which is equal to (this.writerIndex - this.readerIndex).
[中]返回等于(this.writerIndex-this.readerIndex)的可读字节数。
代码示例来源:origin: netty/netty
@Override
public int readableBytes() {
if (terminated) {
return buffer.readableBytes();
} else {
return Integer.MAX_VALUE - buffer.readerIndex();
}
}
代码示例来源:origin: Graylog2/graylog2-server
@Override
protected void channelRead0(ChannelHandlerContext ctx, ByteBuf msg) throws Exception {
if (sourceInputLog.isTraceEnabled()) {
sourceInputLog.trace("Recv network data: {} bytes via input '{}' <{}> from remote address {}",
msg.readableBytes(), sourceInputName, sourceInputId, ctx.channel().remoteAddress());
}
}
}
代码示例来源:origin: alibaba/fescar
@Override
public boolean decode(ByteBuf in) {
int i = in.readableBytes();
if (i < 3) {
return false;
}
i -= 3;
this.identified = (in.readByte() == 1);
short len = in.readShort();
if (len > 0) {
if (i < len) {
return false;
}
byte[] bs = new byte[len];
in.readBytes(bs);
this.setVersion(new String(bs, UTF8));
}
return true;
}
}
代码示例来源:origin: apache/drill
if (component.readableBytes() > wrapSizeLimit) {
throw new RpcException(String.format("Component Chunk size: %d is greater than the wrapSizeLimit: %d",
component.readableBytes(), wrapSizeLimit));
component.getBytes(component.readerIndex(), origMsg, 0, component.readableBytes());
if(logger.isTraceEnabled()) {
logger.trace("Trying to encrypt chunk of size:{} with wrapSizeLimit:{} and chunkMode: {}",
component.readableBytes(), wrapSizeLimit);
final byte[] wrappedMsg = saslCodec.wrap(origMsg, 0, component.readableBytes());
if(logger.isTraceEnabled()) {
logger.trace("Successfully encrypted message, original size: {} Final Size: {}",
component.readableBytes(), wrappedMsg.length);
msg.skipBytes(component.readableBytes());
component.skipBytes(component.readableBytes());
代码示例来源:origin: Graylog2/graylog2-server
LOG.trace("Attempting to decode DNS record [{}]", dnsRecord);
try {
final ByteBuf byteBuf = dnsRawRecord.content();
ipAddressBytes = new byte[byteBuf.readableBytes()];
int readerIndex = byteBuf.readerIndex();
byteBuf.getBytes(readerIndex, ipAddressBytes);
} finally {
LOG.trace("The IP address has [{}] bytes", ipAddressBytes.length);
LOG.trace("The resulting IP address is [{}]", ipAddress.getHostAddress());
代码示例来源:origin: ethereum/ethereumj
protected void decode(ChannelHandlerContext ctx, ByteBuf in, List<Object> out) throws IOException {
if (in.readableBytes() == 0) {
loggerWire.trace("in.readableBytes() == 0");
return;
}
loggerWire.trace("Decoding frame (" + in.readableBytes() + " bytes)");
List<FrameCodec.Frame> frames = frameCodec.readFrames(in);
// Check if a full frame was available. If not, we'll try later when more bytes come in.
if (frames == null || frames.isEmpty()) return;
for (int i = 0; i < frames.size(); i++) {
FrameCodec.Frame frame = frames.get(i);
channel.getNodeStatistics().rlpxInMessages.add();
}
out.addAll(frames);
}
代码示例来源:origin: apache/hive
@Override
protected void decode(ChannelHandlerContext ctx, ByteBuf in, List<Object> out)
throws Exception {
if (in.readableBytes() < 4) {
return;
}
in.markReaderIndex();
int msgSize = in.readInt();
checkSize(msgSize);
if (in.readableBytes() < msgSize) {
// Incomplete message in buffer.
in.resetReaderIndex();
return;
}
try {
ByteBuffer nioBuffer = maybeDecrypt(in.nioBuffer(in.readerIndex(), msgSize));
Input kryoIn = new Input(new ByteBufferInputStream(nioBuffer));
Object msg = kryos.get().readClassAndObject(kryoIn);
LOG.trace("Decoded message of type {} ({} bytes)",
msg != null ? msg.getClass().getName() : msg, msgSize);
out.add(msg);
} finally {
in.skipBytes(msgSize);
}
}
代码示例来源:origin: runelite/runelite
@Override
public void decode(ChannelHandlerContext ctx, ByteBuf in, List<Object> out) throws Exception
if (in.readableBytes() < 8)
int compressedFileSize = copy.readInt();
if (size + 3 + breaks > in.readableBytes())
logger.trace("Index {} archive {}: Not enough data yet {} > {}", index, file, size + 3 + breaks, in.readableBytes());
return;
int bytesToRead = Math.min(bytesInBlock, size - compressedData.writerIndex());
logger.trace("{}/{}: reading block {}/{}, read so far this block: {}, file status: {}/{}",
index, file,
(totalRead % CHUNK_SIZE), CHUNK_SIZE,
logger.trace("{}/{}: done downloading file, remaining buffer {}",
index, file,
in.readableBytes());
代码示例来源:origin: org.apache.plc4x/plc4j-protocol-s7
protected void decode(ChannelHandlerContext ctx, IsoTPMessage in, List<Object> out) {
if (logger.isTraceEnabled()) {
logger.trace("Got Data: {}", ByteBufUtil.hexDump(in.getUserData()));
if (userData.readableBytes() == 0) {
return;
userData.readShort(); // Reserved (is always constant 0x0000)
short tpduReference = userData.readShort();
short headerParametersLength = userData.readShort();
short userDataLength = userData.readShort();
byte errorClass = 0;
代码示例来源:origin: redisson/redisson
@Override
public Object decode(ByteBuf buf, State state) throws IOException {
int decompressSize = buf.readInt();
ByteBuf out = ByteBufAllocator.DEFAULT.buffer(decompressSize);
try {
LZ4SafeDecompressor decompressor = factory.safeDecompressor();
ByteBuffer outBuffer = out.internalNioBuffer(out.writerIndex(), out.writableBytes());
int pos = outBuffer.position();
decompressor.decompress(buf.internalNioBuffer(buf.readerIndex(), buf.readableBytes()), outBuffer);
int compressedLength = outBuffer.position() - pos;
out.writerIndex(compressedLength);
return innerCodec.getValueDecoder().decode(out, state);
} finally {
out.release();
}
}
};
代码示例来源:origin: weibocom/motan
private void decodeV1(ChannelHandlerContext ctx, ByteBuf in, List<Object> out) throws Exception {
long startTime = System.currentTimeMillis();
in.resetReaderIndex();
in.skipBytes(2);// skip magic num
byte messageType = (byte) in.readShort();
long requestId = in.readLong();
int dataLength = in.readInt();
// FIXME 如果dataLength过大,可能导致问题
if (in.readableBytes() < dataLength) {
in.resetReaderIndex();
return;
}
checkMaxContext(dataLength, ctx, messageType == MotanConstants.FLAG_REQUEST, requestId);
byte[] data = new byte[dataLength];
in.readBytes(data);
decode(data, out, messageType == MotanConstants.FLAG_REQUEST, requestId).setStartTime(startTime);
}
代码示例来源:origin: qunarcorp/qmq
@Override
protected void decode(ChannelHandlerContext channelHandlerContext, ByteBuf in, List<Object> list) throws Exception {
if (in.readableBytes() < RemotingHeader.MIN_HEADER_SIZE + RemotingHeader.LENGTH_FIELD) return;
int magicCode = in.getInt(in.readerIndex() + RemotingHeader.LENGTH_FIELD);
if (DEFAULT_MAGIC_CODE != magicCode) {
throw new IOException("Illegal Data, MagicCode=" + Integer.toHexString(magicCode));
int total = in.readInt();
if (in.readableBytes() < total) {
in.resetReaderIndex();
return;
short headerSize = in.readShort();
RemotingHeader remotingHeader = decodeHeader(in);
代码示例来源:origin: alibaba/fescar
@Override
public boolean decode(ByteBuf in) {
int i = in.readableBytes();
try {
short len = in.readShort();
if (len > 0) {
byte[] bs = new byte[len];
this.setVersion(new String(bs, UTF8));
len = in.readShort();
if (len > 0) {
byte[] bs = new byte[len];
this.setApplicationId(new String(bs, UTF8));
len = in.readShort();
if (len > 0) {
byte[] bs = new byte[len];
LOGGER.debug(in.writerIndex() == in.readerIndex() ? "true" : "false" + this);
代码示例来源:origin: apache/zookeeper
if (LOG.isTraceEnabled()) {
LOG.trace("0x{} buf {}",
Long.toHexString(sessionId),
ByteBufUtil.hexDump(buf));
queuedBuffer = channel.alloc().buffer(buf.readableBytes());
if (LOG.isTraceEnabled()) {
LOG.trace("0x{} queuedBuffer {}",
Long.toHexString(sessionId),
ByteBufUtil.hexDump(queuedBuffer));
if (LOG.isTraceEnabled()) {
LOG.trace("Before copy {}", buf);
queuedBuffer = channel.alloc().buffer(buf.readableBytes());
代码示例来源:origin: apache/drill
public void decode(ChannelHandlerContext ctx, ByteBuf msg, List<Object> out) throws IOException {
logger.trace("Channel closed before decoding the message of {} bytes", msg.readableBytes());
msg.skipBytes(msg.readableBytes());
return;
if(logger.isTraceEnabled()) {
logger.trace("Trying to decrypt the encrypted message of size: {} with maxWrappedSize", msg.readableBytes());
msg.getBytes(msg.readerIndex(), lengthOctets.array(), 0, RpcConstants.LENGTH_FIELD_LENGTH);
final int wrappedMsgLength = lengthOctets.getInt(0);
msg.skipBytes(RpcConstants.LENGTH_FIELD_LENGTH);
assert(msg.readableBytes() == wrappedMsgLength);
msg.getBytes(msg.readerIndex(), wrappedMsg, 0, wrappedMsgLength);
if(logger.isTraceEnabled()) {
logger.trace("Successfully decrypted incoming message. Length after decryption: {}", decodedMsg.length);
代码示例来源:origin: whizzosoftware/WZWave
cbuf.addComponent(previousBuf.copy());
cbuf.addComponent(in);
cbuf.writerIndex(previousBuf.readableBytes() + in.readableBytes());
data = cbuf;
if (data.readableBytes() == 1 && isSingleByteFrame(data, data.readerIndex())) {
out.add(createSingleByteFrame(data));
} else {
boolean foundFrame = false;
for (int searchStartIx = data.readerIndex(); searchStartIx < data.readerIndex() + data.readableBytes(); searchStartIx++) {
if (data.getByte(searchStartIx) == DataFrame.START_OF_FRAME) {
int frameEndIx = scanForFrame(data, searchStartIx);
in.readBytes(in.readableBytes());
logger.trace("Done processing received data: {}", out);
代码示例来源:origin: kaaproject/kaa
@Override
public HttpResponse getResponse() {
LOG.trace("CommandName: " + COMMAND_NAME + ": getHttpResponse..");
ByteBuf data = Unpooled.copiedBuffer(responseBody);
LOG.warn("Response data: {}", Arrays.toString(data.array()));
FullHttpResponse httpResponse = new DefaultFullHttpResponse(HTTP_1_1, OK, data);
httpResponse.headers().set(CONTENT_TYPE, CommonEpConstans.RESPONSE_CONTENT_TYPE);
httpResponse.headers().set(CONTENT_LENGTH, data.readableBytes());
LOG.warn("Response size: {}", data.readableBytes());
httpResponse
.headers()
.set(CommonEpConstans.RESPONSE_TYPE, CommonEpConstans.RESPONSE_TYPE_OPERATION);
if (responseSignature != null) {
httpResponse
.headers()
.set(CommonEpConstans.SIGNATURE_HEADER_NAME, encodeBase64String(responseSignature));
}
if (isNeedConnectionClose()) {
httpResponse.headers().set(CONNECTION, HttpHeaders.Values.CLOSE);
} else {
if (HttpHeaders.isKeepAlive(getRequest())) {
httpResponse.headers().set(CONNECTION, HttpHeaders.Values.KEEP_ALIVE);
} else {
httpResponse.headers().set(CONNECTION, HttpHeaders.Values.CLOSE);
}
}
return httpResponse;
}
代码示例来源:origin: netty/netty
@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: redisson/redisson
@Override
public Object decode(ByteBuf buf, State state) throws IOException {
int decompressSize = buf.readInt();
ByteBuf out = ByteBufAllocator.DEFAULT.buffer(decompressSize);
try {
LZ4SafeDecompressor decompressor = factory.safeDecompressor();
ByteBuffer outBuffer = out.internalNioBuffer(out.writerIndex(), out.writableBytes());
int pos = outBuffer.position();
decompressor.decompress(buf.internalNioBuffer(buf.readerIndex(), buf.readableBytes()), outBuffer);
int compressedLength = outBuffer.position() - pos;
out.writerIndex(compressedLength);
return innerCodec.getValueDecoder().decode(out, state);
} finally {
out.release();
}
}
};
代码示例来源:origin: alibaba/fescar
@Override
public boolean decode(ByteBuf in) {
int leftLen = in.readableBytes();
int read = 0;
int xidLen = in.readShort();
if (xidLen > 0) {
if (leftLen < xidLen) {
leftLen --;
int resourceIdLen = in.readShort();
if (resourceIdLen > 0) {
if (leftLen < resourceIdLen) {
int applicationDataLen = in.readInt();
if (applicationDataLen > 0) {
if (leftLen < applicationDataLen) {
内容来源于网络,如有侵权,请联系作者删除!