Example usage for io.netty.buffer ByteBuf writeBytes

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

Introduction

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

Prototype

public abstract int writeBytes(FileChannel in, long position, int length) throws IOException;

Source Link

Document

Transfers the content of the specified channel starting at the given file position to this buffer starting at the current writerIndex and increases the writerIndex by the number of the transferred bytes.

Usage

From source file:com.necla.simba.server.gateway.server.frontend.FrontendFrameEncoder.java

License:Apache License

private void compress(ChannelHandlerContext ctx, ByteBuf msg, ByteBuf out) {
    LOG.debug("compress here");
    byte[] inAry = new byte[msg.readableBytes()];
    msg.readBytes(inAry);// www .ja  v  a 2  s  . c o m
    int sizeEstimate = (int) Math.ceil(inAry.length * 1.001) + 12 + 4;
    LOG.debug("compress here2");

    out.ensureWritable(sizeEstimate);

    int beginIndex = out.writerIndex();

    out.writerIndex(beginIndex + 4);

    try {

        deflater.setInput(inAry);

        while (!deflater.needsInput()) {
            LOG.debug("compress here3333");

            int numBytes = deflater.deflate(encodeBuf, 0, encodeBuf.length);
            LOG.debug("Compressed numBytes=" + numBytes);
            out.writeBytes(encodeBuf, 0, numBytes);
            LOG.debug("compress here4");

        }

        deflater.finish();
        while (!deflater.finished()) {
            int numBytes = deflater.deflate(encodeBuf, 0, encodeBuf.length);
            out.writeBytes(encodeBuf, 0, numBytes);
            LOG.debug("compress here5");

        }
        deflater.reset();
        int len = out.writerIndex() - beginIndex - 4;

        Stats.sent(out.writerIndex() + beginIndex + 4);

        LOG.debug("Compressed len=" + len);
        len |= (1 << 30);
        out.setInt(beginIndex, len);
    } catch (Exception e) {
        LOG.debug("Exception" + e);
    }
}

From source file:com.shelf.messagepack.MessagePackFrameDecoder.java

License:Apache License

protected ByteBuf extractFrame(ChannelHandlerContext ctx, ByteBuf buffer, int index, int length) {
    // make a sliced buffer for reading full contents, then if enough data doesn't reached yet, 
    // ReplayingDecoder will throw an error for replaying decode operation at this line.
    // /*w  w  w . j a v a 2s  .  c  om*/
    // Don't create a new buffer with ctx.alloc().buffer() before enough data has come. It will not be released (and leaked).
    // If sliced buffer is created successfully, enough data has come.
    ByteBuf slice = buffer.slice(index, length);
    ByteBuf frame = ctx.alloc().buffer(length);
    frame.writeBytes(slice, 0, length);
    return frame;
}

From source file:com.spotify.netty4.handler.codec.zmtp.benchmarks.CustomReqRepBenchmark.java

License:Apache License

private static void writePayload(final ZMTPWriter writer, final ByteBuffer payload) {
    final ByteBuf buf = writer.frame(payload.remaining(), false);
    if (payload.hasArray()) {
        buf.writeBytes(payload.array(), payload.arrayOffset() + payload.position(), payload.remaining());
    } else {//  w w  w  .java  2 s  .com
        final int pos = payload.position();
        for (int i = 0; i < payload.remaining(); i++) {
            buf.writeByte(payload.get(pos + i));
        }
        payload.position(pos);
    }
}

From source file:com.spotify.netty4.handler.codec.zmtp.ZMTPMessageEncoder.java

License:Apache License

@Override
public void encode(final Object msg, final ZMTPWriter writer) {
    final ZMTPMessage message = (ZMTPMessage) msg;
    for (int i = 0; i < message.size(); i++) {
        final ByteBuf frame = message.frame(i);
        final boolean more = i < message.size() - 1;
        final ByteBuf dst = writer.frame(frame.readableBytes(), more);
        dst.writeBytes(frame, frame.readerIndex(), frame.readableBytes());
    }//from  ww  w .j ava2  s .c  om
}