Example usage for io.netty.buffer ByteBuf slice

List of usage examples for io.netty.buffer ByteBuf slice

Introduction

In this page you can find the example usage for io.netty.buffer ByteBuf slice.

Prototype

public abstract ByteBuf slice(int index, int length);

Source Link

Document

Returns a slice of this buffer's sub-region.

Usage

From source file:org.virtue.network.session.impl.OnDemandSession.java

License:Open Source License

/**
 * Processes a request and then send the data to the encoder
 *///from   www  .j a v  a 2  s .  co  m
public void processFileQueue() {
    OnDemandRequestMessage request;

    synchronized (fileQueue) {
        request = fileQueue.pop();
        if (fileQueue.isEmpty()) {
            idle = true;
        } else {
            service.addPendingSession(this);
            idle = false;
        }
    }

    if (request != null) {
        int type = request.getType();
        int file = request.getFile();

        Cache cache = Virtue.getInstance().getCache();
        ByteBuf buf;

        try {
            if (type == 255 && file == 255) {
                buf = Unpooled.wrappedBuffer(Virtue.getInstance().getChecksumTable());
            } else {
                buf = Unpooled.wrappedBuffer(cache.getStore().read(type, file));
                if (type != 255) {
                    buf = buf.slice(0, buf.readableBytes() - 2);
                }
            }
            channel.writeAndFlush(new OnDemandResponseMessage(request.isPriority(), type, file, buf));
        } catch (IOException ex) {
            logger.warn("Failed to service file request " + type + ", " + file + ".", ex);
        }
    }
}

From source file:org.waarp.openr66.protocol.networkhandler.packet.NetworkPacketCodec.java

License:Open Source License

@Override
protected void decode(ChannelHandlerContext ctx, ByteBuf buf, List<Object> out) throws Exception {
    // Make sure if the length field was received.
    if (buf.readableBytes() < 4) {
        // The length field was not received yet - return null.
        // This method will be invoked again when more packets are
        // received and appended to the buffer.
        return;//  w  ww . j  a  v  a  2s .  c  om
    }
    // Mark the current buffer position
    buf.markReaderIndex();
    // Read the length field
    final int length = buf.readInt();
    if (length < 9) {
        throw new OpenR66ProtocolPacketException(
                "Incorrect decode first field in Network Packet: " + length + " < 9");
    }
    if (buf.readableBytes() < length) {
        buf.resetReaderIndex();
        return;
    }
    // Now we can read the two Ids
    final int localId = buf.readInt();
    final int remoteId = buf.readInt();
    final byte code = buf.readByte();
    int readerInder = buf.readerIndex();
    ByteBuf buffer = buf.slice(readerInder, length - 9);
    buffer.retain();
    buf.skipBytes(length - 9);
    NetworkPacket networkPacket = new NetworkPacket(localId, remoteId, code, buffer);
    if (code == LocalPacketFactory.KEEPALIVEPACKET) {
        KeepAlivePacket keepAlivePacket = (KeepAlivePacket) LocalPacketCodec
                .decodeNetworkPacket(networkPacket.getBuffer());
        if (keepAlivePacket.isToValidate()) {
            keepAlivePacket.validate();
            NetworkPacket response = new NetworkPacket(ChannelUtils.NOCHANNEL, ChannelUtils.NOCHANNEL,
                    keepAlivePacket, null);
            NetworkChannelReference nc = NetworkTransaction.getImmediateNetworkChannel(ctx.channel());
            if (nc != null) {
                nc.useIfUsed();
            }
            ctx.writeAndFlush(response.getNetworkPacket());
            buffer.release();
        }
        // Replaced by a NoOp packet
        networkPacket = new NetworkPacket(localId, remoteId, new NoOpPacket(), null);
        NetworkServerHandler nsh = (NetworkServerHandler) ctx.pipeline().last();
        nsh.setKeepAlivedSent();
    }
    out.add(networkPacket);
}

From source file:org.waarp.openr66.proxy.network.NetworkPacketCodec.java

License:Open Source License

@Override
protected void decode(ChannelHandlerContext ctx, ByteBuf buf, List<Object> out) throws Exception {
    // Make sure if the length field was received.
    if (buf.readableBytes() < 4) {
        // The length field was not received yet - return null.
        // This method will be invoked again when more packets are
        // received and appended to the buffer.
        return;//  www . j a  va  2 s  .  co m
    }
    // Mark the current buffer position
    buf.markReaderIndex();
    // Read the length field
    final int length = buf.readInt();
    if (buf.readableBytes() < length) {
        buf.resetReaderIndex();
        return;
    }
    // Now we can read the two Ids
    // Slight change in Proxy = first is remote and second is local!
    final int remoteId = buf.readInt();
    final int localId = buf.readInt();
    final byte code = buf.readByte();
    int readerInder = buf.readerIndex();
    ByteBuf buffer = buf.slice(readerInder, length - 9);
    buffer.retain();
    buf.skipBytes(length - 9);
    NetworkPacket networkPacket = new NetworkPacket(localId, remoteId, code, buffer);
    if (code == LocalPacketFactory.KEEPALIVEPACKET) {
        KeepAlivePacket keepAlivePacket = (KeepAlivePacket) LocalPacketCodec
                .decodeNetworkPacket(networkPacket.getBuffer());
        if (keepAlivePacket.isToValidate()) {
            keepAlivePacket.validate();
            NetworkPacket response = new NetworkPacket(ChannelUtils.NOCHANNEL, ChannelUtils.NOCHANNEL,
                    keepAlivePacket, null);
            ctx.writeAndFlush(response.getNetworkPacket());
        }
        // Replaced by a NoOp packet
        networkPacket = new NetworkPacket(localId, remoteId, new NoOpPacket(), null);
        NetworkServerHandler nsh = (NetworkServerHandler) ctx.pipeline().last();
        nsh.setKeepAlivedSent();
    }
    out.add(networkPacket);
}

From source file:util.MarshallingDecoder.java

protected Object decode(ByteBuf frame) throws IOException, ClassNotFoundException {
    int objectSize = frame.readInt();
    ByteBuf buf = frame.slice(frame.readerIndex(), objectSize);
    ByteInput input = new ChannelBufferByteInput(buf);

    try {//from ww  w  .jav a2s. c  o  m
        unmarshaller.start(input);
        Object obj = unmarshaller.readObject();
        unmarshaller.finish();
        frame.readerIndex(frame.readerIndex() + objectSize);

        return obj;
    } finally {
        unmarshaller.close();
    }
}

From source file:whitespell.net.websockets.socketio.transport.FlashPolicyHandler.java

License:Apache License

@Override
public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception {
    if (msg instanceof ByteBuf) {
        ByteBuf message = (ByteBuf) msg;
        ByteBuf data = message.slice(0, requestBuffer.readableBytes());
        if (data.equals(requestBuffer)) {
            message.release();//w  ww. j a  v a2  s.  co  m
            ChannelFuture f = ctx.writeAndFlush(responseBuffer);
            f.addListener(ChannelFutureListener.CLOSE);
            return;
        }
        ctx.pipeline().remove(this);
    }
    ctx.fireChannelRead(msg);
}