當前位置: 首頁>>代碼示例>>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;未經允許,請勿轉載。