Example usage for io.netty.buffer ByteBufAllocator buffer

List of usage examples for io.netty.buffer ByteBufAllocator buffer

Introduction

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

Prototype

ByteBuf buffer(int initialCapacity);

Source Link

Document

Allocate a ByteBuf with the given initial capacity.

Usage

From source file:io.gatling.http.client.body.multipart.impl.MessageEndPartImpl.java

License:Apache License

private ByteBuf lazyLoadContentBuffer(ByteBufAllocator alloc) {
    if (contentBuffer == null) {
        contentBuffer = alloc.buffer((int) getContentLength());
        contentBuffer.writeBytes(EXTRA_BYTES).writeBytes(boundary).writeBytes(EXTRA_BYTES)
                .writeBytes(CRLF_BYTES);
    }/*  www .ja  va  2  s. c o  m*/
    return contentBuffer;
}

From source file:io.gatling.http.client.body.multipart.impl.MultipartChunkedInput.java

License:Apache License

@Override
public ByteBuf readChunk(ByteBufAllocator alloc) throws Exception {

    if (endOfInput) {
        return null;
    }/*from  ww w.  jav  a 2s  .  c  om*/

    ByteBuf buffer = alloc.buffer(chunkSize);
    ChunkedInputState state = copyInto(buffer);
    progress += buffer.writerIndex();
    switch (state) {
    case STOP:
        endOfInput = true;
        return buffer;
    case SUSPEND:
        // this will suspend the stream in ChunkedWriteHandler
        buffer.release();
        return null;
    case CONTINUE:
        return buffer;
    default:
        throw new IllegalStateException("Unknown state: " + state);
    }
}

From source file:io.reactivex.netty.channel.ByteTransformer.java

License:Apache License

@Override
public ByteBuf call(byte[] toTransform, ByteBufAllocator allocator) {
    return allocator.buffer(toTransform.length).writeBytes(toTransform);
}

From source file:io.reactivex.netty.channel.StringTransformer.java

License:Apache License

@Override
public ByteBuf call(String toTransform, ByteBufAllocator allocator) {
    byte[] contentAsBytes = toTransform.getBytes(charset);
    return allocator.buffer(contentAsBytes.length).writeBytes(contentAsBytes);
}

From source file:io.reactivex.netty.serialization.ByteTransformer.java

License:Apache License

@Override
public ByteBuf transform(byte[] toTransform, ByteBufAllocator byteBufAllocator) {
    return byteBufAllocator.buffer(toTransform.length).writeBytes(toTransform);
}

From source file:io.reactivex.netty.serialization.StringTransformer.java

License:Apache License

@Override
public ByteBuf transform(String toTransform, ByteBufAllocator byteBufAllocator) {
    byte[] contentAsBytes = toTransform.getBytes(charset);
    return byteBufAllocator.buffer(contentAsBytes.length).writeBytes(contentAsBytes);
}

From source file:me.nithanim.cultures.format.cif.CifFileWriter.java

@Override
public void pack(CifFile o, ByteBuf buf) throws IOException {
    List<String> lines = o.getLines();

    ByteBufAllocator alloc = ByteBufAllocator.DEFAULT;
    ByteBuf indexTable = alloc.buffer(o.getLines().size()).order(ByteOrder.LITTLE_ENDIAN);
    ByteBuf contentTable = alloc.buffer(o.getLines().size() * 10).order(ByteOrder.LITTLE_ENDIAN);
    if (o.getFileFormat() == CifFile.FileFormat.CIF) {
        for (String l : lines) {
            int idx = contentTable.writerIndex();
            ByteBufUtil.hexDump(contentTable);
            indexTable.writeInt(idx);/*from  w  w w . j  a  v  a 2  s  .c o m*/

            l = l.trim();
            if (l.startsWith("[")) {
                l = l.substring(1, l.length() - 1);
                contentTable.writeByte(1);
            } else {
                contentTable.writeByte(2);
            }

            contentTable.writeBytes(l.getBytes(CharsetUtil.ISO_8859_1));
            contentTable.writeByte('\0');
        }
    } else {
        for (String l : lines) {
            int idx = contentTable.writerIndex();
            indexTable.writeInt(idx);

            l = l.trim();

            contentTable.writeBytes(l.getBytes(CharsetUtil.ISO_8859_1));
            contentTable.writeByte('\0');
        }
    }
    EncryptedInformation ei = new EncryptedInformation(o.getLines().size(), indexTable.writerIndex(),
            indexTable, contentTable.writerIndex(), contentTable);

    Writer<EncryptedInformation> eiw;
    if (o.getInternalFormat() == CifFile.InternalFormat.TYPE1) {
        buf.writeInt(65601);
        eiw = type1Writer;
    } else if (o.getInternalFormat() == CifFile.InternalFormat.TYPE2) {
        buf.writeInt(1021);
        eiw = type2Writer;
    } else {
        throw new UnsupportedDataTypeException("The given data is not a cif file!");
    }
    eiw.pack(ei, buf);
}

From source file:nearenough.protocol.RtWire.java

License:Open Source License

/**
 * Encode the given message for network transmission using the provided ByteBuf allocator.
 *
 * @return A {@link ByteBuf} containing this message encoded for transmission.
 *//*from   w ww.j av a2s .  co  m*/
public static ByteBuf toWire(RtMessage msg, ByteBufAllocator allocator) {
    checkNotNull(msg, "msg");
    checkNotNull(allocator, "allocator");

    int encodedSize = computeEncodedSize(msg.mapping());
    ByteBuf buf = allocator.buffer(encodedSize);
    checkState(buf.writableBytes() >= 4, "nonsensical output buf size %s", buf.writableBytes());

    writeNumTags(msg, buf);
    writeOffsets(msg, buf);
    writeTags(msg, buf);
    writeValues(msg, buf);

    // Output buffer should have been completely used
    checkState(buf.writableBytes() == 0, "message was not completely written");

    return buf;
}

From source file:org.apache.tinkerpop.gremlin.driver.ser.AbstractGraphSONMessageSerializerV1d0.java

License:Apache License

@Override
public ByteBuf serializeResponseAsBinary(final ResponseMessage responseMessage,
        final ByteBufAllocator allocator) throws SerializationException {
    ByteBuf encodedMessage = null;/*from w w w . ja  va  2  s  .com*/
    try {
        final byte[] payload = mapper.writeValueAsBytes(responseMessage);
        encodedMessage = allocator.buffer(payload.length);
        encodedMessage.writeBytes(payload);

        return encodedMessage;
    } catch (Exception ex) {
        if (encodedMessage != null)
            ReferenceCountUtil.release(encodedMessage);

        logger.warn("Response [{}] could not be serialized by {}.", responseMessage.toString(),
                AbstractGraphSONMessageSerializerV1d0.class.getName());
        throw new SerializationException(ex);
    }
}

From source file:org.apache.tinkerpop.gremlin.driver.ser.AbstractGraphSONMessageSerializerV1d0.java

License:Apache License

@Override
public ByteBuf serializeRequestAsBinary(final RequestMessage requestMessage, final ByteBufAllocator allocator)
        throws SerializationException {
    ByteBuf encodedMessage = null;/*from www  . ja  v a2 s  .c om*/
    try {
        final byte[] header = obtainHeader();
        final byte[] payload = mapper.writeValueAsBytes(requestMessage);

        encodedMessage = allocator.buffer(header.length + payload.length);
        encodedMessage.writeBytes(header);
        encodedMessage.writeBytes(payload);

        return encodedMessage;
    } catch (Exception ex) {
        if (encodedMessage != null)
            ReferenceCountUtil.release(encodedMessage);

        logger.warn("Request [{}] could not be serialized by {}.", requestMessage.toString(),
                AbstractGraphSONMessageSerializerV1d0.class.getName());
        throw new SerializationException(ex);
    }
}