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.ebay.jetstream.messaging.transport.netty.compression.MessageDecompressionHandler.java

License:MIT License

@Override
protected Object decode(ChannelHandlerContext ctx, ByteBuf in) throws Exception {

    ByteBuf frame = (ByteBuf) super.decode(ctx, in);

    if (frame == null) {
        return null;
    }//from w  w  w. ja  v  a  2 s.  com

    byte[] uncompressedbuf;

    if (m_allocBuf)
        uncompressedbuf = new byte[m_tmpBufSz];
    else
        uncompressedbuf = m_tmpBuf;

    int framelen = frame.readableBytes();

    int len = 0;

    try {

        len = Snappy.rawUncompress(frame.readBytes(framelen).array(), 0, framelen, uncompressedbuf, 0);

    } catch (Throwable t) {

        LOGGER.debug("Failed to uncompress - " + t.getLocalizedMessage());

        frame.release();
        return null;
    }

    frame.release();

    ByteBuf buf = ctx.alloc().directBuffer(len);

    return buf.writeBytes(uncompressedbuf, 0, len);

}

From source file:com.flowpowered.engine.network.codec.ChunkDataCodec.java

License:MIT License

@Override
public ByteBuf encode(ByteBuf buffer, ChunkDataMessage message) throws IOException {
    if (message.isUnload()) {
        buffer.writeByte(ISUNLOAD); // we're unloading
        buffer.writeInt(message.getX());
        buffer.writeInt(message.getY());
        buffer.writeInt(message.getZ());
    } else {/*from  www. j  av  a2s.  c  o  m*/
        int dataSize = INTIAL_DATA_SIZE;

        byte[] uncompressedData = new byte[dataSize];
        byte[] compressedData = new byte[dataSize];

        int index = 0;
        for (int i : message.getBlocks()) {
            uncompressedData[index++] = (byte) i;
            uncompressedData[index++] = (byte) (i >> 8);
            uncompressedData[index++] = (byte) (i >> 16);
            uncompressedData[index++] = (byte) (i >> 24);
        }

        Deflater deflater = new Deflater();
        deflater.setInput(uncompressedData);
        deflater.finish();
        int compressedSize = deflater.deflate(compressedData);
        deflater.end();

        if (compressedSize == 0) {
            throw new IOException("Not all data compressed!");
        }

        buffer.writeByte(0); // Not unload
        buffer.writeInt(message.getX());
        buffer.writeInt(message.getY());
        buffer.writeInt(message.getZ());
        buffer.writeInt(compressedSize);
        buffer.writeBytes(compressedData, 0, compressedSize);
    }
    return buffer;
}

From source file:com.flowpowered.network.pipeline.MessageProcessorDecoderTest.java

License:MIT License

@Test
public void test() throws Exception {
    // Preprocessor basically is split into two parts
    // Part 1 is just a direct copy
    // Part 2 negates all bytes before copying
    final AtomicReference<MessageProcessor> processor = new AtomicReference<>();
    MessageProcessorDecoder processorDecoder = new MessageProcessorDecoder(null) {
        @Override//from   w  w w . j  a  v a 2  s.  com
        protected MessageProcessor getProcessor() {
            return processor.get();
        }
    };

    // Set up a fake ChannelHandlerContext
    FakeChannelHandlerContext fake = ChannelHandlerContextFaker.setup();
    AtomicReference<ByteBuf> ref = new AtomicReference<>();
    fake.setReference(ref);
    LinkedList<byte[]> outputList = new LinkedList<>();

    Random r = new Random();

    // Get some random bytes for data
    byte[] input = new byte[LENGTH];
    r.nextBytes(input);

    boolean breakOccured = false;
    int position = 0;

    for (int i = 0; i < input.length;) {
        // Simulate real data read
        int burstSize = r.nextInt(512);
        // With a 1/10 chance of having an extra-large burst
        if (r.nextInt(10) == 0) {
            burstSize *= 10;
        }

        // Final burst needs to be clamped
        if (i + burstSize > input.length) {
            burstSize = input.length - i;
        }

        // And we can't negate in the middle of a burst
        if (i + burstSize > BREAK && !breakOccured) {
            burstSize = BREAK - i;
        }

        // Write info to a new ByteBuf
        final ByteBuf buf = Unpooled.buffer(burstSize);
        buf.writeBytes(input, i, burstSize);
        i += burstSize;

        // Fake a read
        processorDecoder.channelRead(fake, buf);

        final ByteBuf returned = ref.get();

        while (returned != null) {
            int packetSize = r.nextInt(128) + 1;
            if (r.nextInt(10) == 0) {
                packetSize *= 20;
            }

            if (packetSize > returned.readableBytes()) {
                packetSize = returned.readableBytes();
            }
            if (position + packetSize > BREAK && !breakOccured) {
                packetSize = BREAK - position;
            }
            if (position + packetSize > LENGTH) {
                packetSize = LENGTH - position;
            }

            if (packetSize == 0) {
                break;
            }

            byte[] array = new byte[packetSize];

            returned.readBytes(array);
            position += packetSize;

            if (position == BREAK) {
                processor.set(new NegatingProcessor(512));
                breakOccured = true;
            }
            outputList.add(array);
        }
    }

    // Get the output data and combine into one array
    byte[] output = new byte[LENGTH];
    int i = 0;
    for (byte[] array : outputList) {
        for (int j = 0; j < array.length; j++) {
            output[i++] = array[j];
        }
    }

    for (i = 0; i < input.length; i++) {
        byte expected = i < BREAK ? input[i] : (byte) ~input[i];
        if (output[i] != expected) {
            for (int j = Math.max(0, i - 10); j <= i + 10; j++) {
                System.out.println(j + ") " + Integer.toBinaryString(j < BREAK ? input[j] : (byte) ~input[j])
                        + " " + Integer.toBinaryString(output[j]));
            }
        }

        if (i < BREAK) {
            assertTrue("Input/Output mismatch at position " + i + ". Expected " + input[i] + " but got "
                    + output[i] + ". Break is: " + BREAK, output[i] == input[i]);
        } else {
            assertTrue(
                    "Input/Output mismatch at position " + i + ", after the processor change. Expected "
                            + (byte) ~input[i] + " but got " + output[i] + ". Break is: " + BREAK,
                    output[i] == (byte) ~input[i]);
        }
    }
}

From source file:com.flowpowered.network.processor.simple.SimpleMessageProcessor.java

License:MIT License

@Override
public final synchronized ByteBuf processOutbound(ChannelHandlerContext ctx, final ByteBuf input,
        ByteBuf buffer) {
    int remaining;
    while ((remaining = input.readableBytes()) > 0) {
        int clamped = Math.min(remaining, capacity);
        input.readBytes(encodingByteBuffer, 0, clamped);
        writeEncode(encodingByteBuffer, clamped);
        int read;
        while ((read = readEncode(encodingByteBuffer)) > 0) {
            buffer.writeBytes(encodingByteBuffer, 0, read);
        }//from  w ww  .j  ava 2  s.co m
    }
    return buffer;
}

From source file:com.flowpowered.network.processor.simple.SimpleMessageProcessor.java

License:MIT License

@Override
public final synchronized ByteBuf processInbound(ChannelHandlerContext ctx, final ByteBuf input,
        ByteBuf buffer) {
    int remaining;
    while ((remaining = input.readableBytes()) > 0) {
        int clamped = Math.min(remaining, capacity);
        input.readBytes(decodingByteBuffer, 0, clamped);
        writeDecode(decodingByteBuffer, clamped);
        int read;
        while ((read = readDecode(decodingByteBuffer)) > 0) {
            buffer.writeBytes(decodingByteBuffer, 0, read);
        }//from w w  w. ja  va2s.c  o  m
    }
    return buffer;
}

From source file:com.flowpowered.networking.pipeline.MessageProcessorDecoderTest.java

License:MIT License

@Test
public void test() throws Exception {
    // Preprocessor basically is split into two parts
    // Part 1 is just a direct copy
    // Part 2 negates all bytes before copying
    final AtomicReference<MessageProcessor> processor = new AtomicReference<>();
    MessageProcessorDecoder processorDecoder = new MessageProcessorDecoder(null) {
        @Override/*from   w w  w .j a  v a2  s. co m*/
        protected MessageProcessor getProcessor() {
            return processor.get();
        }
    };

    // Set up a fake ChannelHandlerContext
    FakeChannelHandlerContext fake = ChannelHandlerContextFaker.setup();
    AtomicReference<ByteBuf> ref = new AtomicReference<>();
    fake.setReference(ref);
    LinkedList<byte[]> outputList = new LinkedList<byte[]>();

    Random r = new Random();

    // Get some random bytes for data
    byte[] input = new byte[LENGTH];
    r.nextBytes(input);

    boolean breakOccured = false;
    int position = 0;

    for (int i = 0; i < input.length;) {
        // Simulate real data read
        int burstSize = r.nextInt(512);
        // With a 1/10 chance of having an extra-large burst
        if (r.nextInt(10) == 0) {
            burstSize *= 10;
        }

        // Final burst needs to be clamped
        if (i + burstSize > input.length) {
            burstSize = input.length - i;
        }

        // And we can't negate in the middle of a burst
        if (i + burstSize > BREAK && !breakOccured) {
            burstSize = BREAK - i;
        }

        // Write info to a new ByteBuf
        final ByteBuf buf = Unpooled.buffer(burstSize);
        buf.retain();
        buf.writeBytes(input, i, burstSize);
        i += burstSize;

        // Fake a read
        processorDecoder.channelRead(fake, buf);

        final ByteBuf returned = ref.get();

        while (returned != null && true) {
            int packetSize = r.nextInt(128) + 1;
            if (r.nextInt(10) == 0) {
                packetSize *= 20;
            }

            if (packetSize > returned.readableBytes()) {
                packetSize = returned.readableBytes();
            }
            if (position + packetSize > BREAK && !breakOccured) {
                packetSize = BREAK - position;
            }
            if (position + packetSize > LENGTH) {
                packetSize = LENGTH - position;
            }

            if (packetSize == 0) {
                break;
            }

            byte[] array = new byte[packetSize];

            returned.readBytes(array);
            position += packetSize;

            if (position == BREAK) {
                processor.set(new NegatingProcessor(512));
                breakOccured = true;
            }
            outputList.add(array);
        }
    }

    // Get the output data and combine into one array
    byte[] output = new byte[LENGTH];
    int i = 0;
    for (byte[] array : outputList) {
        for (int j = 0; j < array.length; j++) {
            output[i++] = array[j];
        }
    }

    for (i = 0; i < input.length; i++) {
        byte expected = i < BREAK ? input[i] : (byte) ~input[i];
        if (output[i] != expected) {
            for (int j = Math.max(0, i - 10); j <= i + 10; j++) {
                System.out.println(j + ") " + Integer.toBinaryString(j < BREAK ? input[j] : (byte) ~input[j])
                        + " " + Integer.toBinaryString(output[j]));
            }
        }

        if (i < BREAK) {
            assertTrue("Input/Output mismatch at position " + i + ". Expected " + input[i] + " but got "
                    + output[i] + ". Break is: " + BREAK, output[i] == input[i]);
        } else {
            assertTrue(
                    "Input/Output mismatch at position " + i + ", after the processor change. Expected "
                            + (byte) ~input[i] + " but got " + output[i] + ". Break is: " + BREAK,
                    output[i] == (byte) ~input[i]);
        }
    }
}

From source file:com.github.milenkovicm.kafka.protocol.Convert.java

License:Apache License

public static void encodeBytes(ByteBuf bytes, ByteBuf buf) {
    if (bytes != null) {
        int readable = bytes.readableBytes();
        encodeInteger(readable, buf);// w  w  w  .j  a v a 2 s . c o  m
        buf.writeBytes(bytes, 0, readable); // content
    } else {
        encodeInteger(NULL_VALUE_LENGTH, buf); // indicates null
    }
}

From source file:com.github.milenkovicm.kafka.protocol.Convert.java

License:Apache License

public static void encodeBytes(byte[] bytes, ByteBuf buf) {
    if (bytes != null) {
        int readable = bytes.length;
        encodeInteger(readable, buf); // int32 (length field)
        buf.writeBytes(bytes, 0, readable); // content
    } else {// ww  w.  java 2  s .  com
        encodeInteger(NULL_VALUE_LENGTH, buf); // indicates null
    }
}

From source file:com.github.milenkovicm.kafka.protocol.Convert.java

License:Apache License

public static void encodeString(String str, ByteBuf buf) {
    if (str != null) {
        byte[] bytes = getBytesFromString(str);
        int readable = bytes.length;
        if (readable <= Short.MAX_VALUE) {
            encodeShort(readable, buf); // int16 (length field)
            buf.writeBytes(bytes, 0, readable); // content
        } else {/*from   w ww  .  jav a2 s  .co  m*/
            throw new RuntimeException("Size of string exceeds " + Short.MAX_VALUE + ".");
        }
    } else {
        encodeShort(NULL_VALUE_LENGTH, buf); // indicates null
    }
}

From source file:com.github.milenkovicm.kafka.protocol.Convert.java

License:Apache License

public static void encodeString(byte[] str, ByteBuf buf) {
    if (str != null) {
        int readable = str.length;
        if (readable <= Short.MAX_VALUE) {
            encodeShort(readable, buf); // int16 (length field)
            buf.writeBytes(str, 0, readable); // content
        } else {// ww  w . jav a2  s  .c  om
            throw new RuntimeException("Size of string exceeds " + Short.MAX_VALUE + ".");
        }
    } else {
        encodeShort(NULL_VALUE_LENGTH, buf); // indicates null
    }
}