Example usage for io.netty.buffer ByteBufAllocator DEFAULT

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

Introduction

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

Prototype

ByteBufAllocator DEFAULT

To view the source code for io.netty.buffer ByteBufAllocator DEFAULT.

Click Source Link

Usage

From source file:me.melchor9000.net.TCPSocket.java

License:Open Source License

TCPSocket(TCPAcceptor acceptor, SocketChannel socket) {
    super(acceptor.service);
    channel = this.socket = socket;
    readBuffer = ByteBufAllocator.DEFAULT.directBuffer(1460, 1460 * 100).retain();
    readOperations = new ConcurrentLinkedQueue<>();
    readManager = new ReadManager();
    socket.pipeline().addLast("readManager", readManager);
}

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   ww w  .  ja v a2  s .  com

            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.RtMessage.java

License:Open Source License

/**
 * @return A new {@code RtMessage} by parsing the contents of the provided {@code byte[]}. The
 * contents of {@code srcBytes} must be a well-formed Roughtime message.
 *///from ww  w  .  j  av a  2  s  .c  o  m
public static RtMessage fromBytes(byte[] srcBytes) {
    checkNotNull(srcBytes, "srcBytes");

    ByteBuf buf = ByteBufAllocator.DEFAULT.buffer(srcBytes.length);
    buf.writeBytes(srcBytes);
    return new RtMessage(buf);
}

From source file:nearenough.protocol.RtMessage.java

License:Open Source License

/**
 * @return A new {@code RtMessage} by parsing the contents of the provided {@code ByteBuffer},
 * which can be direct or heap based. The contents of {@code srcBuf} must be a well-formed
 * Roughtime message./*from w ww .  j av a 2s . c om*/
 */
public static RtMessage fromByteBuffer(ByteBuffer srcBuf) {
    checkNotNull(srcBuf, "srcBuf");

    ByteBuf buf = ByteBufAllocator.DEFAULT.buffer(srcBuf.remaining());
    buf.writeBytes(srcBuf);
    return new RtMessage(buf);
}

From source file:nearenough.protocol.RtMessageBuilderTest.java

License:Open Source License

@Test
public void addValueOverloads() {
    byte[] value1 = new byte[64];
    Arrays.fill(value1, (byte) 'b');

    ByteBuf value2Buf = ByteBufAllocator.DEFAULT.buffer(14);
    byte[] value2 = "This is a test".getBytes();
    value2Buf.writeBytes(value2);/*w  w w  .j  a  va  2 s.c o m*/

    RtMessage value3Msg = RtMessage.builder().add(RtTag.PAD, new byte[12]).build();
    ByteBuf value3Buf = RtWire.toWire(value3Msg);
    byte[] value3 = new byte[value3Buf.readableBytes()];
    value3Buf.readBytes(value3);

    RtMessage msg = RtMessage.builder().add(RtTag.INDX, value1).add(RtTag.MAXT, value2Buf)
            .add(RtTag.NONC, value3Msg).build();

    assertArrayEquals(msg.get(RtTag.INDX), value1);
    assertArrayEquals(msg.get(RtTag.MAXT), value2);
    assertArrayEquals(msg.get(RtTag.NONC), value3);
}

From source file:nearenough.protocol.RtWire.java

License:Open Source License

/**
 * Encode the given message for network transmission using the system default ByteBuf allocator.
 *
 * @return A {@link ByteBuf} containing this message encoded for transmission.
 *///from ww w .  ja  v a2 s. c  o  m
public static ByteBuf toWire(RtMessage msg) {
    return toWire(msg, ByteBufAllocator.DEFAULT);
}

From source file:net.hasor.rsf.remoting.transport.protocol.codec.RpcRequestProtocol.java

License:Apache License

private ByteBuf encodeRequest(RequestSocketBlock reqMsg) {
    ByteBuf bodyBuf = ByteBufAllocator.DEFAULT.heapBuffer();
    //* --------------------------------------------------------bytes =14
    //* byte[2]  servicesName-(attr-index)            ???
    bodyBuf.writeShort(reqMsg.getServiceName());
    //* byte[2]  servicesGroup-(attr-index)           ?
    bodyBuf.writeShort(reqMsg.getServiceGroup());
    //* byte[2]  servicesVersion-(attr-index)         ?
    bodyBuf.writeShort(reqMsg.getServiceVersion());
    //* byte[2]  servicesMethod-(attr-index)          ???
    bodyBuf.writeShort(reqMsg.getTargetMethod());
    //* byte[2]  serializeType-(attr-index)           ?
    bodyBuf.writeShort(reqMsg.getSerializeType());
    //* byte[4]  clientTimeout                        
    bodyBuf.writeInt(reqMsg.getClientTimeout());
    //* --------------------------------------------------------bytes =1 ~ 1021
    //* byte[1]  paramCount                           ?
    int[] paramMapping = reqMsg.getParameters();
    bodyBuf.writeByte(paramMapping.length);
    for (int i = 0; i < paramMapping.length; i++) {
        //* byte[4]  ptype-0-(attr-index,attr-index)  ?1
        //* byte[4]  ptype-1-(attr-index,attr-index)  ?2
        bodyBuf.writeInt(paramMapping[i]);
    }/*from  w  w w  .j av a  2s  .  c  om*/
    //* --------------------------------------------------------bytes =1 ~ 1021
    //* byte[1]  optionCount                          ?
    int[] optionMapping = reqMsg.getOptions();
    bodyBuf.writeByte(optionMapping.length);
    for (int i = 0; i < optionMapping.length; i++) {
        //* byte[4]  ptype-0-(attr-index,attr-index)  ?1
        //* byte[4]  ptype-1-(attr-index,attr-index)  ?2
        bodyBuf.writeInt(optionMapping[i]);
    }
    //* --------------------------------------------------------bytes =6 ~ 8192
    //* byte[2]  attrPool-size (Max = 2047)           ? 0x07FF
    int[] poolData = reqMsg.getPoolData();
    bodyBuf.writeShort(poolData.length);
    for (int i = 0; i < poolData.length; i++) {
        //* byte[4]  ptype-0-(attr-index,attr-index)  1?
        //* byte[4]  ptype-1-(attr-index,attr-index)  1?
        bodyBuf.writeInt(poolData[i]);
    }
    //* --------------------------------------------------------bytes =n
    //* dataBody                                      ?
    reqMsg.fillTo(bodyBuf);
    return bodyBuf;
}

From source file:net.hasor.rsf.remoting.transport.protocol.codec.RpcResponseProtocol.java

License:Apache License

private ByteBuf encodeResponse(ResponseSocketBlock resMsg) {
    ByteBuf bodyBuf = ByteBufAllocator.DEFAULT.heapBuffer();
    ///* w w  w.j a v  a 2 s . c o m*/
    //* --------------------------------------------------------bytes =8
    //* byte[2]  status                               ??
    bodyBuf.writeShort(resMsg.getStatus());
    //* byte[2]  serializeType-(attr-index)           ?
    bodyBuf.writeShort(resMsg.getSerializeType());
    //* byte[2]  returnType-(attr-index)              
    bodyBuf.writeShort(resMsg.getReturnType());
    //* byte[2]  returnData-(attr-index)              ?
    bodyBuf.writeShort(resMsg.getReturnData());
    //* --------------------------------------------------------bytes =1 ~ 1021
    //* byte[1]  optionCount                          ?
    int[] optionMapping = resMsg.getOptions();
    bodyBuf.writeByte(optionMapping.length);
    for (int i = 0; i < optionMapping.length; i++) {
        //* byte[4]  ptype-0-(attr-index,attr-index)  ?1
        //* byte[4]  ptype-1-(attr-index,attr-index)  ?2
        bodyBuf.writeInt(optionMapping[i]);
    }
    //* --------------------------------------------------------bytes =6 ~ 8192
    //* byte[2]  attrPool-size (Max = 2047)           ? 0x07FF
    int[] poolData = resMsg.getPoolData();
    bodyBuf.writeShort(poolData.length);
    for (int i = 0; i < poolData.length; i++) {
        //* byte[4]  ptype-0-(attr-index,attr-index)  1?
        //* byte[4]  ptype-1-(attr-index,attr-index)  2?
        bodyBuf.writeInt(poolData[i]);
    }
    //* --------------------------------------------------------bytes =n
    //* dataBody                                      ?
    resMsg.fillTo(bodyBuf);
    return bodyBuf;
}

From source file:net.hasor.rsf.utils.ProtocolUtils.java

License:Apache License

/**ByteBuf*/
public static ByteBuf newByteBuf() {
    return ByteBufAllocator.DEFAULT.heapBuffer();
}

From source file:org.apache.activemq.artemis.cli.commands.tools.xml.XMLMessageImporter.java

License:Apache License

/**
 * Message bodies are written to XML as one or more Base64 encoded CDATA elements. Some parser implementations won't
 * read an entire CDATA element at once (e.g. Woodstox) so it's possible that multiple CDATA/CHARACTERS events need
 * to be combined to reconstruct the Base64 encoded string.  You can't decode bits and pieces of each CDATA.  Each
 * CDATA has to be decoded in its entirety.
 *
 * @param processor used to deal with the decoded CDATA elements
 * @param textMessage If this a text message we decode UTF8 and encode as a simple string
 *///from   ww  w.java  2 s  .c  o  m
private void getMessageBodyBytes(MessageBodyBytesProcessor processor, boolean decodeTextMessage)
        throws IOException, XMLStreamException {
    int currentEventType;
    StringBuilder cdata = new StringBuilder();
    while (reader.hasNext()) {
        currentEventType = reader.getEventType();
        if (currentEventType == XMLStreamConstants.END_ELEMENT) {
            break;
        } else if (currentEventType == XMLStreamConstants.CHARACTERS && reader.isWhiteSpace()
                && cdata.length() > 0) {
            /* when we hit a whitespace CHARACTERS event we know that the entire CDATA is complete so decode, pass back to
             * the processor, and reset the cdata for the next event(s)
             */
            if (decodeTextMessage) {
                SimpleString text = new SimpleString(cdata.toString());
                ByteBuf byteBuf = ByteBufAllocator.DEFAULT.buffer(SimpleString.sizeofNullableString(text));
                SimpleString.writeNullableSimpleString(byteBuf, text);
                byte[] bytes = new byte[SimpleString.sizeofNullableString(text)];
                byteBuf.readBytes(bytes);
                processor.processBodyBytes(bytes);
            } else {
                processor.processBodyBytes(decode(cdata.toString()));
                cdata.setLength(0);
            }
        } else {
            cdata.append(new String(reader.getTextCharacters(), reader.getTextStart(), reader.getTextLength())
                    .trim());
        }
        reader.next();
    }
}