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.streamsets.pipeline.lib.parser.udp.syslog.TestSyslogParser.java

License:Apache License

@Test
public void testMessageParsingIPv6() throws Exception {
    SyslogParser parser = new SyslogParser(getContext(), StandardCharsets.UTF_8);
    List<String> messages = getTestMessageStrings();

    UnpooledByteBufAllocator allocator = new UnpooledByteBufAllocator(false);
    // test with default keepFields = false
    for (String msg : messages) {
        byte[] bytes = msg.getBytes(StandardCharsets.UTF_8);
        ByteBuf buffer = allocator.buffer(bytes.length);
        buffer.writeBytes(bytes);
        List<Record> records = parser.parse(buffer, InetSocketAddress.createUnresolved("::1", 5000),
                InetSocketAddress.createUnresolved("2001:db8::ff00:42:8329", 50000));
        Assert.assertEquals(1, records.size());
        Assert.assertEquals("Failure to parse known-good syslog message", msg,
                records.get(0).get("/raw").getValueAsString());
        Assert.assertEquals("Failure to parse known-good syslog message", "[::1]:5000",
                records.get(0).get("/receiverAddr").getValueAsString());
        Assert.assertEquals("Failure to parse known-good syslog message", "[2001:db8::ff00:42:8329]:50000",
                records.get(0).get("/senderAddr").getValueAsString());
        Assert.assertNotNull("Failure to parse known-good syslog message",
                records.get(0).get("/host").getValueAsString());
    }//from   ww w . j  ava  2  s .c o  m
}

From source file:com.teambr.modularsystems.core.network.SyncBlockValues.java

License:Creative Commons License

@Override
public void toBytes(ByteBuf buf) {
    byte[] compressedBytes = null;

    if (jsonBlockValues != null)
        compressedBytes = CompressionFunctions.compressStringToByteArray(jsonBlockValues);

    if (compressedBytes != null) {
        buf.writeInt(compressedBytes.length);
        buf.writeBytes(compressedBytes);
    } else/*from w w w.j a  v a  2 s .  c  o m*/
        buf.writeInt(0);
}

From source file:com.tesora.dve.db.mysql.common.MysqlAPIUtils.java

License:Open Source License

public static void putLengthCodedString(ByteBuf cb, byte[] data, boolean codeNullasZero) {
    if (data != null) {
        // need to handle the case of empty string (NULL and empty string are different)
        // mysql puts (byte)0 in the buffer for empty string
        if (data.length > 0) {
            putLengthCodedLong(cb, data.length);
            cb.writeBytes(data);
        } else//from   ww  w  .  j av a  2 s .co m
            cb.writeZero(1);
    } else {
        if (!codeNullasZero)
            cb.writeByte(LEN_CODED_NULL); // this indicates the string is NULL
        else
            cb.writeZero(1);
    }
}

From source file:com.tesora.dve.db.mysql.common.MysqlAPIUtils.java

License:Open Source License

public static void putLengthCodedBinary(ByteBuf cb, byte[] data) {
    final int length = data.length;
    if (length > 0) {
        putLengthCodedLong(cb, length);//from  w  ww .  j  av a 2  s .c om
        cb.writeBytes(data);
    } else {
        cb.writeZero(1);
    }
}

From source file:com.tesora.dve.db.mysql.libmy.BufferedExecute.java

License:Open Source License

public void marshallMessage(ByteBuf stmtExecuteBuf) {
    //header and type are marshalled by parent class.

    int rowsToFlush = this.size();

    MyNullBitmap nullBitmap = new MyNullBitmap(rowsToFlush * columnsPerTuple,
            MyNullBitmap.BitmapType.EXECUTE_REQUEST);

    //            stmtExecuteBuf.writeByte(MSPComStmtExecuteRequestMessage.TYPE_IDENTIFIER);
    stmtExecuteBuf.writeInt(this.stmtID);
    stmtExecuteBuf.writeZero(1); // flags
    stmtExecuteBuf.writeInt(1); // iteration count
    int nullBitmapIndex = stmtExecuteBuf.writerIndex();
    stmtExecuteBuf.writeZero(nullBitmap.length());

    // write the parameter types, as appropriate
    if (this.needsNewParams) {
        stmtExecuteBuf.writeByte(1);// ww  w  .j a  v  a2 s. com

        for (int i = 0; i < rowsToFlush; ++i) {
            stmtExecuteBuf.writeBytes(metadataFragment.slice());
        }

    } else
        stmtExecuteBuf.writeZero(1);

    // Copy the parameter values, updating the null bitmap
    // null bitmap is 1-based
    int rowsWritten = 0;
    int execStmtColIndex = 1;
    for (Iterator<MyBinaryResultRow> i = this.queuedPackets.iterator(); i.hasNext();) {
        MyBinaryResultRow rowPacketData = i.next();

        //                ByteBuf rowSet = Unpooled.buffer(rowPacketData.binRow.sizeInBytes()).order(ByteOrder.LITTLE_ENDIAN);
        //                rowPacketData.binRow.marshallFullMessage(rowSet);

        //                while (rowSet.isReadable() && rowsToWrite-- > 0) {
        //            System.out.println(siteCtx + "/" + myi + ": adding row");
        //                    int payloadLen = rowSet.readMedium();
        //                    rowSet.skipBytes(1);
        //                    byte packetHeader = rowSet.readByte();
        //                    if (packetHeader != 0)
        //                        throw new PEException("Out-of-sync reading redist rowSet");
        int bitmapSize = MyNullBitmap.computeSize(columnsPerTuple, MyNullBitmap.BitmapType.RESULT_ROW);
        int rowFields = rowPacketData.size();
        for (int colIndex = 1; colIndex <= columnsPerTuple; colIndex++, execStmtColIndex++) {
            //we are looping through target columns, which may exceed the source column count.
            if ((colIndex <= rowFields) && rowPacketData.isNull(colIndex - 1 /* zero based indexing */))
                nullBitmap.setBit(execStmtColIndex);
        }
        //                    rowSet.skipBytes(bitmapSize);

        //                    stmtExecuteBuf.writeBytes(rowSet, payloadLen-bitmapSize-1);
        rowPacketData.marshallRawValues(stmtExecuteBuf);
        ++rowsWritten;

    }

    if (rowsWritten != rowsToFlush) {
        //         System.out.println("At failure " + stmtExecuteBuf + "/" + siteCtx);
        throw new PECodingException("Asked to write " + rowsToFlush + " rows, but only " + rowsWritten
                + " were (" + rowsToFlush + " were made available to flushBuffers)");
    }

    // Go back and set the null bitmap and the payload size
    stmtExecuteBuf.setBytes(nullBitmapIndex, nullBitmap.getBitmapArray());
}

From source file:com.tesora.dve.db.mysql.libmy.MyBinaryResultRow.java

License:Open Source License

@Override
public void marshallMessage(ByteBuf cb) {
    cb.writeZero(1);//binary row marker
    byte[] bitmapArray = constructNullMap();
    cb.writeBytes(bitmapArray);
    marshallRawValues(cb);/*from  ww  w .j  a  v  a  2  s  .co m*/
}

From source file:com.tesora.dve.db.mysql.libmy.MyBinaryResultRow.java

License:Open Source License

public void marshallRawValues(ByteBuf cb) {
    for (int i = 0; i < fieldSlices.size(); i++) {
        ByteBuf fieldSlice = fieldSlices.get(i);
        if (fieldSlice != null)
            cb.writeBytes(fieldSlice.slice());
    }//from  w ww . j a  va2s. c  o  m
}

From source file:com.tesora.dve.db.mysql.libmy.MyBinaryResultRow.java

License:Open Source License

public MyBinaryResultRow projection(int[] desiredFields) {
    int expectedFieldCount = desiredFields.length;

    ArrayList<ByteBuf> newSlices = new ArrayList<>(expectedFieldCount);
    ArrayList<DecodedMeta> newConverters = new ArrayList<>(expectedFieldCount);

    for (int targetIndex = 0; targetIndex < expectedFieldCount; targetIndex++) {
        int sourceIndex = desiredFields[targetIndex];
        newConverters.add(fieldConverters.get(sourceIndex));//use the source index, not the target index..
        if (fieldSlices.get(sourceIndex) == null) {
            newSlices.add(null);/*from w w  w.j a v  a 2s.c  o  m*/
        } else {
            ByteBuf fieldSlice = fieldSlices.get(sourceIndex);
            ByteBuf copySlice = Unpooled.buffer(fieldSlice.readableBytes()).order(ByteOrder.LITTLE_ENDIAN);
            copySlice.writeBytes(fieldSlice.slice());
            newSlices.add(copySlice);
        }
    }

    return new MyBinaryResultRow(newConverters, newSlices);
}

From source file:com.tesora.dve.db.mysql.libmy.MyErrorResponse.java

License:Open Source License

@Override
public void marshallMessage(ByteBuf cb) {
    cb.writeByte(ERRORPKT_FIELD_COUNT);/*from  w w w  .j a  va2  s . c  o  m*/
    cb.writeShort((short) errorNumber);
    cb.writeBytes("#".getBytes());
    cb.writeBytes(sqlState.substring(0, 5).getBytes());
    cb.writeBytes(errorMsg.getBytes());
}

From source file:com.tesora.dve.db.mysql.libmy.MyFieldPktResponse.java

License:Open Source License

@Override
public void marshallMessage(ByteBuf cb) {
    if (state == CacheState.NOT_CACHED) {
        ByteBuf newCache = Unpooled.buffer().order(ByteOrder.LITTLE_ENDIAN);
        fullPack(newCache);//from  w  w w  .jav  a  2s  .c  o m
        updateCache(newCache);
    }
    cb.writeBytes(cachedBuffer.slice());
}