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 ByteBuf writeBytes(ByteBuffer src);

Source Link

Document

Transfers the specified source buffer's data to this buffer starting at the current writerIndex until the source buffer's position reaches its limit, and increases the writerIndex by the number of the transferred bytes.

Usage

From source file:com.spotify.folsom.client.Request.java

License:Apache License

protected static ByteBuf toBuffer(final ByteBufAllocator alloc, ByteBuffer dst, int extra) {
    // TODO (dano): write directly to target buffer
    dst.flip();//from  w  w  w  . j a v  a 2  s  .  co m
    final ByteBuf buffer = alloc.buffer(dst.remaining() + extra);
    buffer.writeBytes(dst);
    return buffer;
}

From source file:com.spotify.netty.handler.codec.zmtp.ZMTPUtils.java

License:Apache License

/**
 * Writes a ZMTP frame to a buffer.//from   w  w  w.  j  a va  2 s .  c om
 *
 * @param frame  The frame to write.
 * @param buffer The target buffer.
 * @param more   True to write a more flag, false to write a final flag.
 */
public static void writeFrame(final ZMTPFrame frame, final ByteBuf buffer, final boolean more,
        final int version) {
    if (version == 1) {
        encodeLength(frame.size() + 1, buffer);
        buffer.writeByte(more ? MORE_FLAG : FINAL_FLAG);
    } else { // version == 2
        encodeZMTP2FrameHeader(frame.size(), more ? MORE_FLAG : FINAL_FLAG, buffer);
    }
    if (frame.hasData()) {
        //      final ByteBuf source = frame.getDataBuffer();
        byte[] data = frame.getData();
        buffer.ensureWritable(data.length);

        buffer.writeBytes(data);
        //      source.getBytes(source.readerIndex(), buffer, source.readableBytes());
    }
}

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

License:Apache License

public void write(final ByteBuf buf) {
    buf.writeBytes(chars);
}

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

License:Apache License

/**
 * Write a ZMTP/1.0 greeting./*from   w ww  . j a  v  a  2s.  c  o m*/
 *
 * @param out      Target buffer.
 * @param identity Socket identity.
 */
static void writeGreeting(final ByteBuf out, final ByteBuffer identity) {
    writeLength(identity.remaining() + 1, out);
    out.writeByte(0x00);
    out.writeBytes(identity.duplicate());
}

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

License:Apache License

/**
 * Write a ZMTP/2.0 greeting body./*from  w  w  w . j  ava  2 s. co  m*/
 *
 * @param out        The buffer to write the greeting body to.
 * @param socketType The socket type.
 * @param identity   The socket identity.
 */
static void writeGreetingBody(final ByteBuf out, final ZMTPSocketType socketType, final ByteBuffer identity) {
    out.writeByte(0x01);
    // socket-type
    writeSocketType(out, socketType);
    // identity
    // the final-short flag octet
    out.writeByte(0x00);
    out.writeByte(identity.remaining());
    out.writeBytes(identity.duplicate());
}

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

License:Apache License

@Test
public void testOneFrame() throws Exception {
    final ZMTPWriter writer = ZMTPWriter.create(ZMTP10);
    final ByteBuf buf = Unpooled.buffer();
    writer.reset(buf);/*w ww  . j a  v a 2 s  . c  om*/

    ByteBuf frame = writer.frame(11, false);
    assertThat(frame, is(sameInstance(buf)));

    final ByteBuf content = copiedBuffer("hello world", UTF_8);

    frame.writeBytes(content.duplicate());

    final ZMTPFramingDecoder decoder = new ZMTPFramingDecoder(wireFormat(ZMTP10), new RawDecoder());
    decoder.decode(null, buf, out);

    assertThat(out, hasSize(1));
    assertThat(out, contains((Object) singletonList(content)));
}

From source file:com.srotya.sidewinder.core.ingress.binary.SeriesDataPointEncoder.java

License:Apache License

public static void encodeDPointToBuf(ByteBuf buf, DataPoint dataPoint) {
    byte[] dbNameBytes = dataPoint.getDbName().getBytes();
    buf.writeInt(dbNameBytes.length);//from w  ww  .j a  va2 s. c  om
    buf.writeBytes(dbNameBytes);
    byte[] measurementNameBytes = dataPoint.getMeasurementName().getBytes();
    buf.writeInt(measurementNameBytes.length);
    buf.writeBytes(measurementNameBytes);
    byte[] valueNameBytes = dataPoint.getValueFieldName().getBytes();
    buf.writeInt(valueNameBytes.length);
    buf.writeBytes(valueNameBytes);

    buf.writeLong(dataPoint.getTimestamp());
    if (dataPoint.isFp()) {
        buf.writeByte('0');
        buf.writeDouble(dataPoint.getValue());
    } else {
        buf.writeByte('1');
        buf.writeLong(dataPoint.getLongValue());
    }
}

From source file:com.streamsets.pipeline.lib.parser.collectd.TestCollectdParser.java

License:Apache License

@Test
public void testParser() throws Exception {
    UnpooledByteBufAllocator allocator = new UnpooledByteBufAllocator(false);
    CollectdParser parser = new CollectdParser(getContext(), false, null, false, null, CHARSET);
    byte[] bytes = Files.readAllBytes(SINGLE_PACKET.toPath());
    ByteBuf buf = allocator.buffer(bytes.length);
    buf.writeBytes(bytes);
    List<Record> records = parser.parse(buf, null, null);

    Assert.assertEquals(23, records.size()); // 23 Value parts

    Record record0 = records.get(0);//from www . java2  s  . c  o  m
    verifyRecord(expectedRecord0, record0);

    Record record2 = records.get(2);
    verifyRecord(expectedRecord2, record2);

}

From source file:com.streamsets.pipeline.lib.parser.collectd.TestCollectdParser.java

License:Apache License

@Test
public void testParserExcludeInterval() throws Exception {
    UnpooledByteBufAllocator allocator = new UnpooledByteBufAllocator(false);
    CollectdParser parser = new CollectdParser(getContext(), false, null, true, null, CHARSET);
    byte[] bytes = Files.readAllBytes(SINGLE_PACKET.toPath());
    ByteBuf buf = allocator.buffer(bytes.length);
    buf.writeBytes(bytes);
    List<Record> records = parser.parse(buf, null, null);

    Assert.assertEquals(23, records.size()); // 23 Value parts

    Record record0 = records.get(0);/*  w  w w .ja va  2  s . c  o m*/
    verifyRecord(expectedRecordNoInterval0, record0);

    Record record2 = records.get(2);
    verifyRecord(expectedRecordNoInterval2, record2);

}

From source file:com.streamsets.pipeline.lib.parser.collectd.TestCollectdParser.java

License:Apache License

@Test
public void testEncryptedRecord() throws Exception {
    // If unlimited strength encryption is not available, we cant run this test.
    Assume.assumeFalse(Cipher.getMaxAllowedKeyLength("AES") < 256);

    UnpooledByteBufAllocator allocator = new UnpooledByteBufAllocator(false);
    CollectdParser parser = new CollectdParser(getContext(), false, null, false, AUTH_FILE_PATH, CHARSET);
    byte[] bytes = Files.readAllBytes(SINGLE_ENCRYPTED_PACKET.toPath());
    ByteBuf buf = allocator.buffer(bytes.length);
    buf.writeBytes(bytes);
    List<Record> records = parser.parse(buf, null, null);

    Assert.assertEquals(24, records.size()); // 24 value parts
    Record record14 = records.get(14);/*from w ww .ja v  a  2  s .com*/
    verifyRecord(encryptedRecord14, record14);
    LOG.info("Num records: {}", records.size());
}