本文整理汇总了Java中io.netty.buffer.ByteBuf.readableBytes方法的典型用法代码示例。如果您正苦于以下问题:Java ByteBuf.readableBytes方法的具体用法?Java ByteBuf.readableBytes怎么用?Java ByteBuf.readableBytes使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类io.netty.buffer.ByteBuf
的用法示例。
在下文中一共展示了ByteBuf.readableBytes方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: readHead
import io.netty.buffer.ByteBuf; //导入方法依赖的package包/类
private void readHead(ByteBuf in) {
if (in.readableBytes() < FastdfsConstants.FDFS_HEAD_LEN) {
return;
}
length = in.readLong();
byte cmd = in.readByte();
byte errno = in.readByte();
if (errno != 0) {
throw new FastdfsException("Fastdfs responsed with an error, errno is " + errno);
}
if (cmd != RESP) {
throw new FastdfsException("Expect response command code error : " + cmd);
}
long expectLength = expectLength();
if (expectLength >= 0 && length != expectLength) {
throw new FastdfsException(
"Expect response length : " + expectLength + " , but reply length : " + length);
}
atHead = false;
}
示例2: decode
import io.netty.buffer.ByteBuf; //导入方法依赖的package包/类
@Override
protected void decode(ChannelHandlerContext context, ByteBuf byteBuf, List<Object> list)
throws Exception {
if (byteBuf.readableBytes() < 4) {
return;
}
int len = byteBuf.readInt();
if (byteBuf.readableBytes() < len) {
throw new RuntimeException("Insufficient bytes to be read, expected: " + len);
}
byte[] bytes = new byte[len];
byteBuf.readBytes(bytes);
Serializer serializer = SerializerFactory.load();
Object object = serializer.deserialize(bytes, RpcResponse.class);
list.add(object);
}
示例3: decode
import io.netty.buffer.ByteBuf; //导入方法依赖的package包/类
@Override
protected void decode(ChannelHandlerContext ctx, ByteBuf in,
List<Object> out) throws Exception {
//获取消息头所标识的消息体字节数组长度
if (in.readableBytes() < 4) {
return;
}
in.markReaderIndex();
int dataLength = in.readInt();
if (dataLength < 0) {
ctx.close();
}
//若当前可以获取到的字节数小于实际长度,则直接返回,直到当前可以获取到的字节数等于实际长度
if (in.readableBytes() < dataLength) {
in.resetReaderIndex();
return;
}
//读取完整的消息体字节数组
byte[] data = new byte[dataLength];
in.readBytes(data);
//将字节数组反序列化为java对象(SerializerEngine参考序列化与反序列化章节)
Object obj = SerializerEngine.deserialize(data, genericClass, serializeType.getCode());
out.add(obj);
}
示例4: encode
import io.netty.buffer.ByteBuf; //导入方法依赖的package包/类
protected void encode(ChannelHandlerContext p_encode_1_, ByteBuf p_encode_2_, ByteBuf p_encode_3_) throws Exception
{
int i = p_encode_2_.readableBytes();
int j = PacketBuffer.getVarIntSize(i);
if (j > 3)
{
throw new IllegalArgumentException("unable to fit " + i + " into " + 3);
}
else
{
PacketBuffer packetbuffer = new PacketBuffer(p_encode_3_);
packetbuffer.ensureWritable(j + i);
packetbuffer.writeVarIntToBuffer(i);
packetbuffer.writeBytes(p_encode_2_, p_encode_2_.readerIndex(), i);
}
}
示例5: decode
import io.netty.buffer.ByteBuf; //导入方法依赖的package包/类
protected void decode(ChannelHandlerContext p_decode_1_, ByteBuf p_decode_2_, List<Object> p_decode_3_) throws IOException, InstantiationException, IllegalAccessException, Exception
{
if (p_decode_2_.readableBytes() != 0)
{
PacketBuffer packetbuffer = new PacketBuffer(p_decode_2_);
int i = packetbuffer.readVarIntFromBuffer();
Packet<?> packet = ((EnumConnectionState)p_decode_1_.channel().attr(NetworkManager.PROTOCOL_ATTRIBUTE_KEY).get()).getPacket(this.direction, i);
if (packet == null)
{
throw new IOException("Bad packet id " + i);
}
else
{
packet.readPacketData(packetbuffer);
if (packetbuffer.readableBytes() > 0)
{
throw new IOException("Packet " + ((EnumConnectionState)p_decode_1_.channel().attr(NetworkManager.PROTOCOL_ATTRIBUTE_KEY).get()).getId() + "/" + i + " (" + packet.getClass().getSimpleName() + ") was larger than I expected, found " + packetbuffer.readableBytes() + " bytes extra whilst reading packet " + i);
}
else
{
p_decode_3_.add(packet);
if (LOGGER.isDebugEnabled())
{
LOGGER.debug(RECEIVED_PACKET_MARKER, " IN: [{}:{}] {}", new Object[] {p_decode_1_.channel().attr(NetworkManager.PROTOCOL_ATTRIBUTE_KEY).get(), Integer.valueOf(i), packet.getClass().getName()});
}
}
}
}
}
示例6: 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);
}
}
}
示例7: decode
import io.netty.buffer.ByteBuf; //导入方法依赖的package包/类
@Override
protected void decode( ChannelHandlerContext channelHandlerContext, ByteBuf input, List<Object> output ) throws Exception {
input.markReaderIndex();
byte[] bytes = new byte[3];
for ( int i = 0; i < bytes.length; i++ ) {
if ( !input.isReadable() ) {
input.resetReaderIndex();
return;
}
bytes[i] = input.readByte();
if ( bytes[i] >= 0 ) {
ByteBuf buf = Unpooled.wrappedBuffer( bytes );
try {
int j = Message.readVarInt( buf );
if ( input.readableBytes() < j ) {
input.resetReaderIndex();
return;
}
output.add( input.readBytes( j ) );
return;
} finally {
buf.release();
}
}
}
throw new CorruptedFrameException( "length wider than 21-bit" );
}
示例8: cipher
import io.netty.buffer.ByteBuf; //导入方法依赖的package包/类
protected void cipher(ByteBuf in, ByteBuf out) throws ShortBufferException
{
int i = in.readableBytes();
byte[] abyte = this.bufToBytes(in);
int j = this.cipher.getOutputSize(i);
if (this.outputBuffer.length < j)
{
this.outputBuffer = new byte[j];
}
out.writeBytes((byte[])this.outputBuffer, 0, this.cipher.update(abyte, 0, i, this.outputBuffer));
}
示例9: encode
import io.netty.buffer.ByteBuf; //导入方法依赖的package包/类
@Override
protected void encode(ChannelHandlerContext ctx, ByteBuf msg, List<Object> out)
{
final ByteBuf buf = ctx.alloc().buffer(2);
final short length = (short) (msg.readableBytes() + 2);
buf.writeShort(buf.order() != ByteOrder.LITTLE_ENDIAN ? Short.reverseBytes(length) : length);
out.add(buf);
out.add(msg.retain());
}
示例10: extractSingleMapping
import io.netty.buffer.ByteBuf; //导入方法依赖的package包/类
private Map<RtTag, byte[]> extractSingleMapping(ByteBuf msg) {
long uintTag = msg.readUnsignedInt();
RtTag tag = RtTag.fromUnsignedInt((int) uintTag);
byte[] value = new byte[msg.readableBytes()];
msg.readBytes(value);
return Collections.singletonMap(tag, value);
}
示例11: decode
import io.netty.buffer.ByteBuf; //导入方法依赖的package包/类
protected void decode(ChannelHandlerContext channelHandlerContext, ByteBuf in, List<Object> out) throws Exception {
if(in.readableBytes() < 4){
return;
}
in.markReaderIndex();
int dataLength = in.readInt();
if(in.readableBytes() < dataLength){
in.resetReaderIndex();
return;
}
byte[] data = new byte[dataLength];
in.readBytes(data);
Kryo kryo = null;
try{
kryo = pool.borrow();
Input input = new Input(data);
Object obj = kryo.readObject(input,this.clazz);
out.add(obj);
}catch(Exception e){
LOG.warn("MessageDecoder happen exception.",e);
}finally{
if(kryo != null){
pool.release(kryo);
}
}
}
示例12: read
import io.netty.buffer.ByteBuf; //导入方法依赖的package包/类
@Override
public int read(byte[] b, int off, int len) throws IOException {
checkNotNull(b);
if (0 == len) {
return 0;
}
poll();
if (isFinished()) {
return -1;
}
ByteBuf byteBuf = readContent(len);
int readableBytes = byteBuf.readableBytes();
byteBuf.readBytes(b, off, readableBytes);
return readableBytes - byteBuf.readableBytes();
}
示例13: encode
import io.netty.buffer.ByteBuf; //导入方法依赖的package包/类
protected void encode(ChannelHandlerContext p_encode_1_, ByteBuf p_encode_2_, ByteBuf p_encode_3_) throws Exception
{
int i = p_encode_2_.readableBytes();
PacketBuffer packetbuffer = new PacketBuffer(p_encode_3_);
if (i < this.treshold)
{
packetbuffer.writeVarIntToBuffer(0);
packetbuffer.writeBytes(p_encode_2_);
}
else
{
byte[] abyte = new byte[i];
p_encode_2_.readBytes(abyte);
packetbuffer.writeVarIntToBuffer(abyte.length);
this.deflater.setInput(abyte, 0, i);
this.deflater.finish();
while (!this.deflater.finished())
{
int j = this.deflater.deflate(this.buffer);
packetbuffer.writeBytes((byte[])this.buffer, 0, j);
}
this.deflater.reset();
}
}
示例14: encode
import io.netty.buffer.ByteBuf; //导入方法依赖的package包/类
protected void encode(ChannelHandlerContext p_encode_1_, ByteBuf p_encode_2_, ByteBuf p_encode_3_) throws Exception
{
int i = p_encode_2_.readableBytes();
PacketBuffer packetbuffer = new PacketBuffer(p_encode_3_);
if (i < this.threshold)
{
packetbuffer.writeVarIntToBuffer(0);
packetbuffer.writeBytes(p_encode_2_);
}
else
{
byte[] abyte = new byte[i];
p_encode_2_.readBytes(abyte);
packetbuffer.writeVarIntToBuffer(abyte.length);
this.deflater.setInput(abyte, 0, i);
this.deflater.finish();
while (!this.deflater.finished())
{
int j = this.deflater.deflate(this.buffer);
packetbuffer.writeBytes((byte[])this.buffer, 0, j);
}
this.deflater.reset();
}
}
示例15: channelRead0
import io.netty.buffer.ByteBuf; //导入方法依赖的package包/类
@Override
protected void channelRead0(ChannelHandlerContext ctx, FrameMajor msg) throws Exception {
FrameMajorHeader head = msg.getHead();
ByteBuf byteBuf = msg.getData();
while (byteBuf.readableBytes() >= FrameSetting.SUB_FRAME_HEAD_LENGTH) {
int subMsgId = byteBuf.readByte() & 0xFF;
byte[] data = new byte[byteBuf.readShort()];
byteBuf.readBytes(data);
server.decodeAndHuntMessage(head, subMsgId, data, ctx.channel());
}
}