当前位置: 首页>>代码示例>>Java>>正文


Java ByteBuf.readableBytes方法代码示例

本文整理汇总了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;
}
 
开发者ID:warlock-china,项目名称:azeroth,代码行数:21,代码来源:ReplierSupport.java

示例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);
}
 
开发者ID:tinylcy,项目名称:buddha,代码行数:17,代码来源:RpcResponseCodec.java

示例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);
}
 
开发者ID:yanghuijava,项目名称:elephant,代码行数:26,代码来源:NettyDecoder.java

示例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);
    }
}
 
开发者ID:Notoh,项目名称:DecompiledMinecraft,代码行数:18,代码来源:MessageSerializer2.java

示例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()});
                }
            }
        }
    }
}
 
开发者ID:sudofox,项目名称:Backmemed,代码行数:33,代码来源:NettyPacketDecoder.java

示例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);
        }
    }
}
 
开发者ID:ZhangJiupeng,项目名称:AgentX,代码行数:29,代码来源:Tcp2UdpHandler.java

示例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" );
}
 
开发者ID:lukas81298,项目名称:FlexMC,代码行数:29,代码来源:MessageSplitter.java

示例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));
}
 
开发者ID:sudofox,项目名称:Backmemed,代码行数:14,代码来源:NettyEncryptionTranslator.java

示例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());
}
 
开发者ID:rubenswagner,项目名称:L2J-Global,代码行数:10,代码来源:LengthFieldBasedFrameEncoder.java

示例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);
}
 
开发者ID:int08h,项目名称:nearenough,代码行数:10,代码来源:RtMessage.java

示例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);
        	}
        }
    }
 
开发者ID:islittlechen,项目名称:lionrpc,代码行数:29,代码来源:MessageDecoder.java

示例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();
}
 
开发者ID:geeker-lait,项目名称:tasfe-framework,代码行数:16,代码来源:HttpContentInputStream.java

示例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();
    }
}
 
开发者ID:SkidJava,项目名称:BaseClient,代码行数:28,代码来源:NettyCompressionEncoder.java

示例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();
    }
}
 
开发者ID:F1r3w477,项目名称:CustomWorldGen,代码行数:28,代码来源:NettyCompressionEncoder.java

示例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());
    }
}
 
开发者ID:bitkylin,项目名称:ClusterDeviceControlPlatform,代码行数:12,代码来源:ParsedMessageInBoundHandler.java


注:本文中的io.netty.buffer.ByteBuf.readableBytes方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。