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.github.milenkovicm.kafka.handler.CopyProducerHandler.java

License:Apache License

ByteBuf creteProduceRequest(ByteBufAllocator allocator, ByteBuf messageSet, String topic) {
    final int messageSetSize = messageSet.readableBytes();
    // a bit hardcoded logic follows
    // total length is length of the all fields
    // i've pre-calculated sizes of arrays and strings in case of
    // topic and client id
    final int totalLength = 22 + messageSetSize + Convert.sizeOfString(topicNameEncoded)
            + Convert.sizeOfString(clientIdEncoded);

    ByteBuf buffer = allocator.buffer(totalLength + 4); // msg length + size of length field
    createMessageHeader(totalLength, buffer);
    updateCrc(messageSet);/*from  ww w .j a va  2s.com*/
    buffer.writeBytes(messageSet);

    return buffer;
}

From source file:com.github.mrstampy.kitchensync.stream.header.SequenceHeaderPrepender.java

License:Open Source License

@Override
protected ByteBuf processImpl(Streamer<?> streamer, byte[] message) {
    int headerLength = sizeInBytes(streamer);

    ByteBuf buf = createByteBuf(headerLength + message.length);

    buf.writeBytes(SequenceHeader.SEQUENCE_HEADER_BYTES);
    buf.writeLong(streamer.getSequence());
    appendToHeader(streamer, buf, headerLength);
    buf.writeBytes(message);/*  w w w .  j  a  v a 2  s .  com*/

    return buf;
}

From source file:com.github.mrstampy.pprspray.core.receiver.AbstractChunkReceiver.java

License:Open Source License

/**
 * Rehydrate and transform.//from  www. j ava2 s  .c o m
 *
 * @param array
 *          the array
 * @return the byte[]
 * @see #setTransformer(MediaTransformer)
 */
protected byte[] rehydrateAndTransform(Set<AMC> set) {
    int size = calcSize(set);
    ByteBuf buf = Unpooled.buffer(size);

    for (AMC chunk : set) {
        buf.writeBytes(chunk.getData());
    }

    return transform(buf.array());
}

From source file:com.github.mrstampy.pprspray.core.streamer.chunk.AbstractMediaChunkProcessor.java

License:Open Source License

@Override
protected ByteBuf processImpl(Streamer<?> streamer, byte[] message) {
    int headerLength = sizeInBytes(streamer);

    ByteBuf buf = createByteBuf(headerLength + message.length);

    writeHeader(streamer, buf, headerLength);
    appendToHeader(streamer, buf, headerLength);
    buf.writeBytes(message);

    return buf;/* www  . j a  v a2 s  . c  o  m*/
}

From source file:com.github.mrstampy.pprspray.core.streamer.footer.MediaFooter.java

License:Open Source License

private byte[] buildFooter() {
    ByteBuf buf = Unpooled.buffer(MediaStreamerUtils.FOOTER_LENGTH);

    buf.writeBytes(getType().eomBytes());
    buf.writeInt(getMessageHash());/*from  www  .ja v  a2 s  .c o m*/
    buf.writeInt(getMediaHash());

    return buf.array();
}

From source file:com.github.mrstampy.pprspray.core.streamer.negotiation.NegotiationMessageUtils.java

License:Open Source License

/**
 * Gets the negotiation message.//from  w w w  .jav a2s  .  c  o  m
 *
 * @param mediaHash
 *          the media hash
 * @param requestedType
 *          the requested type
 * @return the negotiation message
 */
public static ByteBuf getNegotiationMessage(int mediaHash, MediaStreamType requestedType) {
    int headerLength = MediaStreamerUtils.DEFAULT_HEADER_LENGTH;

    ByteBuf buf = Unpooled.buffer(headerLength + 4);

    MediaStreamerUtils.writeHeader(buf, MediaStreamType.NEGOTIATION, headerLength, 0, mediaHash, 0, false);

    buf.writeBytes(requestedType.ordinalBytes());

    return buf;
}

From source file:com.github.mrstampy.pprspray.core.streamer.text.AbstractMetaTextChunkProcessor.java

License:Open Source License

protected void appendToHeader(Streamer<?> streamer, ByteBuf buf, int headerLength) {
    buf.writeBytes(headerKey);
    buf.writeInt(getMarshallingClassHash());
}

From source file:com.github.mrstampy.pprspray.core.streamer.util.MediaStreamerUtils.java

License:Open Source License

/**
 * Checks if is media footer./*from ww  w .  j  a v a2  s.c o m*/
 *
 * @param message
 *          the message
 * @param type
 *          the type
 * @param mediaHash
 *          the media hash
 * @return true, if checks if is media footer
 */
public static boolean isMediaFooter(byte[] message, MediaStreamType type, int mediaHash) {
    if (message == null || message.length != FOOTER_LENGTH)
        return false;

    ByteBuf buf = Unpooled.buffer(8);
    buf.writeBytes(message);

    return Integer.MAX_VALUE - type.ordinal() == buf.getInt(0) && mediaHash == buf.getInt(4);
}

From source file:com.github.mrstampy.pprspray.core.streamer.util.MediaStreamerUtils.java

License:Open Source License

/**
 * Write header./* www.jav a  2 s .c  om*/
 *
 * @param buf
 *          the buf
 * @param type
 *          the type
 * @param headerLength
 *          the header length
 * @param messageHash
 *          the message hash
 * @param mediaHash
 *          the media hash
 * @param sequence
 *          the sequence
 * @param ackRequired
 *          the ack required
 */
public static void writeHeader(ByteBuf buf, MediaStreamType type, int headerLength, int messageHash,
        int mediaHash, long sequence, boolean ackRequired) {
    buf.writeBytes(type.ordinalBytes());
    buf.writeShort(headerLength);
    buf.writeInt(messageHash);
    buf.writeInt(mediaHash);
    buf.writeLong(sequence);
    buf.writeBoolean(ackRequired);
}

From source file:com.github.nettybook.ch3.EchoClientHandler.java

License:Apache License

@Override
public void channelActive(ChannelHandlerContext ctx) {
    String sendMessage = "Hello netty";

    ByteBuf messageBuffer = Unpooled.buffer();
    messageBuffer.writeBytes(sendMessage.getBytes());

    StringBuilder builder = new StringBuilder();
    builder.append(" ? [");
    builder.append(sendMessage);//  w w w . j a  v a 2s  . co  m
    builder.append("]");

    System.out.println(builder.toString());
    ctx.writeAndFlush(messageBuffer);
}