Java Code Examples for io.netty.buffer.ByteBuf#setBytes()
The following examples show how to use
io.netty.buffer.ByteBuf#setBytes() .
You can vote up the ones you like or vote down the ones you don't like,
and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: ZstdEncoder.java From x-pipe with Apache License 2.0 | 6 votes |
private void writeUnCompressedData(ByteBuf out) { int flushableBytes = buffer.readableBytes(); if (flushableBytes == 0) { return; } checksum.reset(); checksum.update(buffer.internalNioBuffer(buffer.readerIndex(), flushableBytes)); final int check = (int) checksum.getValue(); out.ensureWritable(flushableBytes + HEADER_LENGTH); final int idx = out.writerIndex(); out.setBytes(idx + HEADER_LENGTH, buffer, 0, flushableBytes); out.setInt(idx, MAGIC_NUMBER); out.setByte(idx + TOKEN_OFFSET, (byte) BLOCK_TYPE_NON_COMPRESSED); out.setIntLE(idx + COMPRESSED_LENGTH_OFFSET, flushableBytes); out.setIntLE(idx + DECOMPRESSED_LENGTH_OFFSET, flushableBytes); out.setIntLE(idx + CHECKSUM_OFFSET, check); out.writerIndex(idx + HEADER_LENGTH + flushableBytes); buffer.clear(); }
Example 2
Source File: ExRequestAuth.java From vethrfolnir-mu with GNU General Public License v3.0 | 5 votes |
@Override public void read(MuClient client, ByteBuf buff, Object... params) { if(MuNetworkServer.onlineClients() >= capacity) { invalidate(buff); client.sendPacket(MuPackets.ExAuthAnswer, AuthResult.ServerIsFull); return; } byte[] data1 = new byte[10]; byte[] data2 = new byte[10]; buff.getBytes(4, data1); buff.getBytes(14, data2); MuCryptUtils.Dec3bit(data1, 0, 10); MuCryptUtils.Dec3bit(data2, 0, 10); buff.setBytes(4, data1); buff.setBytes(14, data2); buff.readerIndex(4); String user = readS(buff, 10); String password = readS(buff, 10); buff.readerIndex(38); String version = readS(buff, 5); String mainSerial = readS(buff, 16); System.out.println("user: [" + user + "] pass[" + password + "] - "+" Version:["+version+"] "+" Serial: ["+mainSerial+"]"); enqueue(client, user, password, version, mainSerial); }
Example 3
Source File: HandshakePacket.java From Mycat-Balance with Apache License 2.0 | 5 votes |
/** * @param args * @throws DecodeException */ public static void main(String[] args) { byte[] bs = new byte[] { 82, 0, 0, 0, 10, 49, 48, 46, 48, 46, 49, 45, 77, 97, 114, 105, 97, 68, 66, 0, -98, 1, 0, 0, 110, 104, 61, 56, 64, 122, 101, 107, 0, -1, -9, 8, 2, 0, 15, -96, 21, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 105, 78, 41, 35, 111, 43, 39, 124, 98, 82, 87, 60, 0, 109, 121, 115, 113, 108, 95, 110, 97, 116, 105, 118, 101, 95, 112, 97, 115, 115, 119, 111, 114, 100, 0 }; ByteBuf byteBuf = Unpooled.buffer(bs.length); byteBuf = byteBuf.order(ByteOrder.LITTLE_ENDIAN); byteBuf.setBytes(0, bs); HandshakePacket handshakePacket = new HandshakePacket(); try { handshakePacket.decode(byteBuf); byteBuf.readerIndex(0); handshakePacket.decode(byteBuf); byteBuf.readerIndex(0); handshakePacket.decode(byteBuf); byteBuf.readerIndex(0); handshakePacket.decode(byteBuf); byteBuf.readerIndex(0); } catch (DecodeException e) { e.printStackTrace(); } }
Example 4
Source File: AuthPacket.java From Mycat-Balance with Apache License 2.0 | 5 votes |
@Override public void encodeBody(ByteBuf byteBuf) { int index = byteBuf.readerIndex(); String xx = Long.toBinaryString(clientFlags); byteBuf.setLong(index, clientFlags); index += 4; byteBuf.setLong(index, maxPacketSize); index += 4; byteBuf.setByte(index, charsetIndex); index++; byteBuf.setBytes(index, extra); index += extra.length; byteBuf.setBytes(index, user); index += user.length; byteBuf.setByte(index, 0); index++; byteBuf.setByte(index, passwordLen); index++; byteBuf.setBytes(index, password); index += password.length; byteBuf.setBytes(index, database); index += database.length; byteBuf.setByte(index, 0); index++; }
Example 5
Source File: CollectdParser.java From datacollector with Apache License 2.0 | 5 votes |
private void decrypt(int offset, int length, ByteBuf buf, String user, byte[] iv) throws OnRecordErrorException { int contentLength = length - offset; if (contentLength < 26) { throw new OnRecordErrorException(Errors.COLLECTD_03, "Content Length was: " + contentLength); } if (!authKeys.containsKey(user)) { throw new OnRecordErrorException(Errors.COLLECTD_03, "Auth File doesn't contain requested user: " + user); } String key = authKeys.get(user); try { MessageDigest sha256 = MessageDigest.getInstance("SHA-256"); sha256.update(key.getBytes(charset)); Cipher cipher = Cipher.getInstance("AES/OFB/NoPadding"); SecretKeySpec keySpec = new SecretKeySpec(sha256.digest(), "AES"); IvParameterSpec ivSpec = new IvParameterSpec(iv); cipher.init( Cipher.DECRYPT_MODE, keySpec, ivSpec ); byte[] encrypted = new byte[contentLength]; buf.getBytes(offset, encrypted, 0, contentLength); byte[] decrypted = cipher.doFinal(encrypted); if (!verifySha1Sum(decrypted)) { throw new OnRecordErrorException(Errors.COLLECTD_03, "SHA-1 Checksum Failed"); } buf.setBytes(offset, decrypted); } catch (GeneralSecurityException e) { throw new OnRecordErrorException(Errors.COLLECTD_03, e.toString()); } }
Example 6
Source File: EchoServerHandler.java From examples-javafx-repos1 with Apache License 2.0 | 5 votes |
@Override protected void channelRead0(ChannelHandlerContext ctx, ByteBuf in) throws Exception { String in_s = in.toString(CharsetUtil.UTF_8); String uc = in_s.toUpperCase(); if( logger.isInfoEnabled() ) { logger.info("[READ] read " + in_s + ", writing " + uc); } in.setBytes(0, uc.getBytes(CharsetUtil.UTF_8)); ctx.write(in); }
Example 7
Source File: SessionClosedTest.java From x-pipe with Apache License 2.0 | 5 votes |
@Test(expected = UnsupportedOperationException.class) public void testTryWrite() { ByteBuf byteBuf = new UnpooledByteBufAllocator(false).buffer(); byteBuf.setBytes(0, "+OK\r\n".getBytes()); try { sessionClosed.tryWrite(byteBuf); } catch (Exception e) { throw e; } finally { ReferenceCountUtil.release(byteBuf); } }
Example 8
Source File: SessionClosingTest.java From x-pipe with Apache License 2.0 | 5 votes |
@Test(expected = UnsupportedOperationException.class) public void testTryWrite() { ByteBuf byteBuf = new UnpooledByteBufAllocator(false).buffer(); byteBuf.setBytes(0, "+OK\r\n".getBytes()); seesionClosing.tryWrite(byteBuf); ReferenceCountUtil.release(byteBuf); }
Example 9
Source File: ZstdEncoder.java From x-pipe with Apache License 2.0 | 5 votes |
private void flushBufferedData(ByteBuf out) { final int flushableBytes = buffer.readableBytes(); if (flushableBytes == 0) { return; } checksum.reset(); checksum.update(buffer.internalNioBuffer(buffer.readerIndex(), flushableBytes)); final int check = (int) checksum.getValue(); final int bufSize = (int) Zstd.compressBound(flushableBytes) + HEADER_LENGTH; out.ensureWritable(bufSize); final int idx = out.writerIndex(); int compressedLength; try { ByteBuffer outNioBuffer = out.internalNioBuffer(idx + HEADER_LENGTH, out.writableBytes() - HEADER_LENGTH); compressedLength = Zstd.compress( outNioBuffer, buffer.internalNioBuffer(buffer.readerIndex(), flushableBytes), DEFAULT_COMPRESS_LEVEL); } catch (Exception e) { throw new CompressionException(e); } final int blockType; if (compressedLength >= flushableBytes) { blockType = BLOCK_TYPE_NON_COMPRESSED; compressedLength = flushableBytes; out.setBytes(idx + HEADER_LENGTH, buffer, 0, flushableBytes); } else { blockType = BLOCK_TYPE_COMPRESSED; } out.setInt(idx, MAGIC_NUMBER); out.setByte(idx + TOKEN_OFFSET, (byte) (blockType | compressionLevel)); out.setIntLE(idx + COMPRESSED_LENGTH_OFFSET, compressedLength); out.setIntLE(idx + DECOMPRESSED_LENGTH_OFFSET, flushableBytes); out.setIntLE(idx + CHECKSUM_OFFSET, check); out.writerIndex(idx + HEADER_LENGTH + compressedLength); buffer.clear(); }
Example 10
Source File: RntbdTransportClientTest.java From azure-cosmosdb-java with MIT License | 5 votes |
@Override protected void handleOutboundMessage(final Object message) { assertTrue(message instanceof ByteBuf); final ByteBuf out = Unpooled.buffer(); final ByteBuf in = (ByteBuf) message; // This is the end of the outbound pipeline and so we can do what we wish with the outbound message if (in.getUnsignedIntLE(4) == 0) { final RntbdContextRequest request = RntbdContextRequest.decode(in.copy()); final RntbdContext rntbdContext = RntbdContext.from(request, serverProperties, HttpResponseStatus.OK); rntbdContext.encode(out); } else { final RntbdRequest rntbdRequest = RntbdRequest.decode(in.copy()); final RntbdResponse rntbdResponse; try { rntbdResponse = this.responses.take(); } catch (final Exception error) { throw new AssertionError(String.format("%s: %s", error.getClass(), error.getMessage())); } assertEquals(rntbdRequest.getTransportRequestId(), rntbdResponse.getTransportRequestId()); rntbdResponse.encode(out); out.setBytes(8, in.slice(8, 16)); // Overwrite activityId } this.writeInbound(out); }
Example 11
Source File: JsonSerializerTest.java From dapeng-soa with Apache License 2.0 | 5 votes |
private static void doTest3(OptimizedMetadata.OptimizedService service, Method method, OptimizedMetadata.OptimizedStruct struct, String json, String desc) throws TException { InvocationContextImpl invocationContext = (InvocationContextImpl) InvocationContextImpl.Factory.createNewInstance(); invocationContext.codecProtocol(CodecProtocol.CompressedBinary); invocationContext.serviceName(service.getService().name); invocationContext.versionName(service.getService().meta.version); invocationContext.methodName(method.name); invocationContext.callerMid("JsonCaller"); final ByteBuf requestBuf = PooledByteBufAllocator.DEFAULT.buffer(8192); // String hex = "000000b50201000000000a0b00010000002c636f6d2e746f6461792e6170692e676f6f64732e736572766963652e4f70656e476f6f6473536572766963650b0002000000156c697374536b7544657461696c4279536b754e6f730b000300000005312e302e300b00040000000a4a736f6e43616c6c6572080005c0a8c7ab0a0009b81cc7ab00007e940d00170b0b00000000000c00010f00010b000000010000000832303534333833390f0003080000000100000002000003"; // String hex = "00000975020101000000550b000100000034636f6d2e746f6461792e6170692e6d656d62657241646d696e2e736572766963652e4d656d62657241646d696e536572766963650b00020000001c6d656d626572436f75706f6e51756572794c697374536572766963650b000300000005312e302e300b000400000009436d6443616c6c6572080005c0a814c80a0007b56f14c8e62df7640a0009b56f14c8e62df6d90b000b00000004303030300b000c000000026f6b0a000dac190002173c8c0b08000ec0a80a7e080010000023800b001200000057636f6d2e746f6461792e6170692e6d656d62657241646d696e2e736572766963652e4d656d62657241646d696e536572766963653a6d656d626572436f75706f6e51756572794c697374536572766963653a312e302e300800150000009a0800160000009a0d00170b0b00000000000c001c1500151415320019ac16a0cd830318044576657216a8ab9d0b16a8ab9d0b1816e4b889e6988ee6b2bb35e58583e4bba3e98791e588b8168090c28f955916b0b085e5a75918026f6b16d0b7c5b595591500160e150216f0b5f3d3955918143832313038353037353131303938363835343436180016d0b7c5b59559150216f0b5f3d3955915021800180b3133373531373737313031182433304236314139372d394639452d333433322d463935332d3434373642313232313732381680a60e1809e8b68ae7a780e5ba9716bce3f80e17000000000000144015080016a0cd830318044576657216aaab9d0b16aaab9d0b1823e585a8e59cbae6bba1313030e58583e7ab8be5878f3230e58583e4bba3e98791e588b8168090c28f955916b0b085e5a75918026f6b16d0b7c5b595591500160e1500160018143832313038353130373831333233313830363934180016d0b7c5b59559150216d0b7c5b5955915021800180b3133373531373737313031182446413934443944362d433134382d323342422d363337392d36363841333830453242323416001800160017000000000000344015080016a0cd830318044576657216acab9d0b16acab9d0b1821e9b29ce9a39fe6bba13230e58583e7ab8be5878f35e58583e4bba3e98791e588b8168090c28f955916b0b085e5a75918026f6b16d0b7c5b595591500160e15021680d4a7b09d5918143832313038353037353431303934393531313636180016d0b7c5b5955915021680d4a7b09d5915021800180b3133373531373737313031182443364433464432312d433639412d323033462d453338302d4134313033414342424335361680a60e1809e8b68ae7a780e5ba9716f09da31117000000000000144015080016a0cd830318044576657216aeab9d0b16aeab9d0b1821e9b29ce9a39fe6bba13230e58583e7ab8be5878f35e58583e4bba3e98791e588b8168090c28f955916b0b085e5a75918026f6b16d0b7c5b595591500160e1500160018143832313038353130383131303336363530393639180016d0b7c5b59559150216d0b7c5b5955915021800180b3133373531373737313031182443364433464432312d433639412d323033462d453338302d41343130334143424243353616001800160017000000000000144015080016a0cd830318044576657216b0ab9d0b16b0ab9d0b1821e9b29ce9a39fe6bba13230e58583e7ab8be5878f35e58583e4bba3e98791e588b8168090c28f955916b0b085e5a75918026f6b16d0b7c5b595591500160e1500160018143832313038353037353531343739303434333432180016d0b7c5b59559150216d0b7c5b5955915021800180b3133373531373737313031182443364433464432312d433639412d323033462d453338302d41343130334143424243353616001800160017000000000000144015080016a0cd830318044576657216b2ab9d0b16b2ab9d0b1821e9b29ce9a39fe6bba13230e58583e7ab8be5878f35e58583e4bba3e98791e588b8168090c28f955916b0b085e5a75918026f6b16d0b7c5b595591500160e1500160018143832313038353037353631383738383130373533180016d0b7c5b59559150216d0b7c5b5955915021800180b3133373531373737313031182443364433464432312d433639412d323033462d453338302d41343130334143424243353616001800160017000000000000144015080016a0cd830318044576657216eab19d0b16eab19d0b181b546f646179e99c9ce6b787e6b78b38e58583e4bba3e98791e588b8168090c28f955916b0b085e5a75918026f6b16d0b7c5b595591500160e1500160018143832313038353037353031373332373930353833180016d0b7c5b59559150216d0b7c5b5955915021800180b3133373531373737313031182439353639454436322d413642322d413734342d423631422d32453635453241364132363016001800160017000000000000204015080016a0cd830318044576657216ecb19d0b16ecb19d0b1816e4b889e6988ee6b2bb35e58583e4bba3e98791e588b8168090c28f955916b0b085e5a75918026f6b16d0b7c5b595591500160e150216f0879dd6965918143832313038353130373631313132363939353536180016d0b7c5b59559150216f0879dd6965915021800180b3133373531373737313031182433304236314139372d394639452d333433322d463935332d3434373642313232313732381680a60e1809e8b68ae7a780e5ba9716b0ada10f17000000000000144015080016a0cd830318044576657216eeb19d0b16eeb19d0b1813e4bebfe5bd9336e58583e4bba3e98791e588b8168090c28f955916b0b085e5a75918026f6b16d0b7c5b595591500160e150216b09bc3d6985918143832313038353037353231333732383635333232180016d0b7c5b59559150216b09bc3d6985915021800180b3133373531373737313031182445444530453133312d373234322d374646382d334143322d34323844333443413443333516c6a70e180ce5beaae7a4bce997a8e5ba9716f693ec0f17000000000000184015080016a0cd830318044576657216f0b19d0b16f0b19d0b1813e4bebfe5bd9336e58583e4bba3e98791e588b8168090c28f955916b0b085e5a75918026f6b16d0b7c5b595591500160e150216f0a69be9985918143832313038353037353331353538323437363531180016d0b7c5b59559150216f0a69be9985915021800180b3133373531373737313031182445444530453133312d373234322d374646382d334143322d34323844333443413443333516c6a70e180ce5beaae7a4bce997a8e5ba9716e8b6f30f17000000000000184015080016a0cd8303180b3133373531373737313031000003"; //String hex = "000000d4020101000498670b00010000002c636f6d2e746f6461792e6170692e676f6f64732e736572766963652e4f70656e476f6f6473536572766963650b0002000000156c697374536b7544657461696c4279536b754e6f730b000300000005312e302e300b0004000000252f6170692f6531626664373632333231653430396365653461633062366538343139363363080005ac1200020a0007ac1e00020000c9e608000879292c580a0009ac1e00020000c9e50d00170b0b00000000001c19f881800008323035343534343429f581800004000003"; String hex = "000002690201010000168e0b000100000033636f6d2e746f6461792e6170692e6461696c796f726465722e736572766963652e4461696c794f7264657253657276696365320b0002000000126f726465724461696c7953756d51756572790b000300000005312e302e300b0004000000252f6170692f6531626664373632333231653430396365653461633062366538343139363363080005ac14000d0a0007ac18000dd796b3390800080a0a0a630a0009ac18000dd796b3380b000b00000004303030300b000c000000026f6b0a000dac370013feace60108000ec0a80a06080010000023ee0b00120000004c636f6d2e746f6461792e6170692e6461696c796f726465722e736572766963652e4461696c794f7264657253657276696365323a6f726465724461696c7953756d51756572793a312e302e3008001500000184080016000001850d00170b0b000000000a0018000000000001d4c0000c00191c1606180737382e363730300019ec16021806302e303130300016041806302e303130300016061806332e303030300016081806332e3030303000160a180731302e3030303000160c18083130382e363330300016121806332e303030300016141806332e303030300016161806352e303030300016181806302e303130300016221806302e303130300016261806332e303030300016281806352e3030303000162c1806352e3030303000190c1806302e30303030180732302e30303030180735302e303030301806302e303030301806302e303030301806302e3030303018083130382e323030301806302e303030301806302e343330301806302e303030301806302e30303030000003"; byte[] bytes = hexStr2bytes(hex); requestBuf.setBytes(0, bytes); requestBuf.writerIndex(bytes.length); // System.out.println("origJson:\n" + json); System.out.println(dumpToStr(requestBuf)); JsonSerializer jsonDecoder = new JsonSerializer(service, method, "1.0.0", struct); SoaMessageParser<String> parser = new SoaMessageParser<>(requestBuf, jsonDecoder); parser.parseHeader(); System.out.println(parser.getHeader()); System.out.println("after enCode and decode:\n" + parser.parseBody().getBody()); System.out.println(desc + " ends====================="); requestBuf.release(); InvocationContextImpl.Factory.removeCurrentInstance(); }
Example 12
Source File: DumpUtilTest.java From dapeng-soa with Apache License 2.0 | 5 votes |
public static void main(String[] args) { String hexStr = "000000d4020101000496730b00010000002c636f6d2e746f6461792e6170692e676f6f64732e736572766963652e4f70656e476f6f6473536572766963650b0002000000156c697374536b7544657461696c4279536b754e6f730b000300000005312e302e300b0004000000252f6170692f6531626664373632333231653430396365653461633062366538343139363363080005ac1200020a0007ac1e00020000c30e08000879292c580a0009ac1e00020000c30d0d00170b0b00000000001c19f882800008323035343338333929f581800004000003"; byte[] bytes = hexStr2bytes(hexStr); final ByteBuf requestBuf = PooledByteBufAllocator.DEFAULT.buffer(8192); requestBuf.setBytes(0, bytes); }
Example 13
Source File: HttpObjectEncoder.java From servicetalk with Apache License 2.0 | 5 votes |
private static void writeBufferToByteBuf(Buffer src, ByteBuf dstByteBuf, Buffer dstBuffer, int dstOffset) { ByteBuf byteBuf = toByteBufNoThrow(src); if (byteBuf != null) { // We don't want to modify either src or dst's reader/writer indexes so use the setBytes method which // doesn't modify indexes. dstByteBuf.setBytes(dstOffset, byteBuf, byteBuf.readerIndex(), byteBuf.readableBytes()); } else { // Use src.getBytes instead of dstByteBuf.setBytes to utilize internal optimizations of ReadOnlyByteBuffer. // We don't want to modify either src or dst's reader/writer indexes so use the getBytes method which // doesn't modify indexes. src.getBytes(src.readerIndex(), dstBuffer, dstOffset, src.readableBytes()); } }
Example 14
Source File: Lz4FrameEncoder.java From netty-4.1.22 with Apache License 2.0 | 5 votes |
private void flushBufferedData(ByteBuf out) { int flushableBytes = buffer.readableBytes(); if (flushableBytes == 0) { return; } checksum.reset(); checksum.update(buffer, buffer.readerIndex(), flushableBytes); final int check = (int) checksum.getValue(); final int bufSize = compressor.maxCompressedLength(flushableBytes) + HEADER_LENGTH; out.ensureWritable(bufSize); final int idx = out.writerIndex(); int compressedLength; try { ByteBuffer outNioBuffer = out.internalNioBuffer(idx + HEADER_LENGTH, out.writableBytes() - HEADER_LENGTH); int pos = outNioBuffer.position(); // We always want to start at position 0 as we take care of reusing the buffer in the encode(...) loop. compressor.compress(buffer.internalNioBuffer(buffer.readerIndex(), flushableBytes), outNioBuffer); compressedLength = outNioBuffer.position() - pos; } catch (LZ4Exception e) { throw new CompressionException(e); } final int blockType; if (compressedLength >= flushableBytes) { blockType = BLOCK_TYPE_NON_COMPRESSED; compressedLength = flushableBytes; out.setBytes(idx + HEADER_LENGTH, buffer, 0, flushableBytes); } else { blockType = BLOCK_TYPE_COMPRESSED; } out.setLong(idx, MAGIC_NUMBER); out.setByte(idx + TOKEN_OFFSET, (byte) (blockType | compressionLevel)); out.setIntLE(idx + COMPRESSED_LENGTH_OFFSET, compressedLength); out.setIntLE(idx + DECOMPRESSED_LENGTH_OFFSET, flushableBytes); out.setIntLE(idx + CHECKSUM_OFFSET, check); out.writerIndex(idx + HEADER_LENGTH + compressedLength); buffer.clear(); }
Example 15
Source File: ReferenceCountedOpenSslEngine.java From netty-4.1.22 with Apache License 2.0 | 5 votes |
/** * Write plaintext data to the OpenSSL internal BIO * * Calling this function with src.remaining == 0 is undefined. */ private int writePlaintextData(final ByteBuffer src, int len) { final int pos = src.position(); final int limit = src.limit(); final int sslWrote; if (src.isDirect()) { sslWrote = SSL.writeToSSL(ssl, bufferAddress(src) + pos, len); if (sslWrote > 0) { src.position(pos + sslWrote); } } else { ByteBuf buf = alloc.directBuffer(len); try { src.limit(pos + len); buf.setBytes(0, src); src.limit(limit); sslWrote = SSL.writeToSSL(ssl, memoryAddress(buf), len); if (sslWrote > 0) { src.position(pos + sslWrote); } else { src.position(pos); } } finally { buf.release(); } } return sslWrote; }
Example 16
Source File: ExtendedBatchQueryCommandCodec.java From vertx-sql-client with Apache License 2.0 | 4 votes |
private void sendBatchStatementExecuteCommand(MySQLPreparedStatement statement, Tuple params) { ByteBuf packet = allocateBuffer(); // encode packet header int packetStartIdx = packet.writerIndex(); packet.writeMediumLE(0); // will set payload length later by calculation packet.writeByte(sequenceId); // encode packet payload packet.writeByte(CommandType.COM_STMT_EXECUTE); packet.writeIntLE((int) statement.statementId); packet.writeByte(CURSOR_TYPE_NO_CURSOR); // iteration count, always 1 packet.writeIntLE(1); /* * Null-bit map and type should always be reconstructed for every batch of parameters here */ int numOfParams = statement.bindingTypes().length; int bitmapLength = (numOfParams + 7) / 8; byte[] nullBitmap = new byte[bitmapLength]; int pos = packet.writerIndex(); if (numOfParams > 0) { // write a dummy bitmap first packet.writeBytes(nullBitmap); packet.writeByte(1); for (int i = 0; i < params.size(); i++) { Object param = params.getValue(i); DataType dataType = DataTypeCodec.inferDataTypeByEncodingValue(param); packet.writeByte(dataType.id); packet.writeByte(0); // parameter flag: signed } for (int i = 0; i < numOfParams; i++) { Object value = params.getValue(i); if (value != null) { DataTypeCodec.encodeBinary(DataTypeCodec.inferDataTypeByEncodingValue(value), value, encoder.encodingCharset, packet); } else { nullBitmap[i / 8] |= (1 << (i & 7)); } } // padding null-bitmap content packet.setBytes(pos, nullBitmap); } // set payload length int payloadLength = packet.writerIndex() - packetStartIdx - 4; packet.setMediumLE(packetStartIdx, payloadLength); sendPacket(packet, payloadLength); }
Example 17
Source File: ExtendedQueryCommandCodec.java From vertx-sql-client with Apache License 2.0 | 4 votes |
private void sendStatementExecuteCommand(MySQLPreparedStatement statement, boolean sendTypesToServer, Tuple params, byte cursorType) { ByteBuf packet = allocateBuffer(); // encode packet header int packetStartIdx = packet.writerIndex(); packet.writeMediumLE(0); // will set payload length later by calculation packet.writeByte(sequenceId); // encode packet payload packet.writeByte(CommandType.COM_STMT_EXECUTE); packet.writeIntLE((int) statement.statementId); packet.writeByte(cursorType); // iteration count, always 1 packet.writeIntLE(1); int numOfParams = statement.bindingTypes().length; int bitmapLength = (numOfParams + 7) / 8; byte[] nullBitmap = new byte[bitmapLength]; int pos = packet.writerIndex(); if (numOfParams > 0) { // write a dummy bitmap first packet.writeBytes(nullBitmap); packet.writeBoolean(sendTypesToServer); if (sendTypesToServer) { for (DataType bindingType : statement.bindingTypes()) { packet.writeByte(bindingType.id); packet.writeByte(0); // parameter flag: signed } } for (int i = 0; i < numOfParams; i++) { Object value = params.getValue(i); if (value != null) { DataTypeCodec.encodeBinary(statement.bindingTypes()[i], value, encoder.encodingCharset, packet); } else { nullBitmap[i / 8] |= (1 << (i & 7)); } } // padding null-bitmap content packet.setBytes(pos, nullBitmap); } // set payload length int payloadLength = packet.writerIndex() - packetStartIdx - 4; packet.setMediumLE(packetStartIdx, payloadLength); sendPacket(packet, payloadLength); }
Example 18
Source File: Packet.java From riiablo with Apache License 2.0 | 4 votes |
static void setContent(ByteBuf bb, ByteBuffer src) { setContentSize(bb, src.remaining()); src.mark(); bb.setBytes(CONTENT_OFFSET, src); src.reset(); }
Example 19
Source File: AuthPacket.java From Mycat-Balance with Apache License 2.0 | 4 votes |
public void decodeBody(ByteBuf _byteBuf) { byte[] bs = new byte[] { -117, -93, 2, 0, 0, 0, 0, 64, 33, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 114, 111, 111, 116, 0, 20, -19, -111, -3, 39, -46, -116, -128, -44, -112, -26, -48, 42, 70, -85, 8, 83, 83, 100, 103, 68, 116, 97, 108, 101, 110, 116, 95, 98, 97, 115, 101, 119, 101, 98, 50, 48, 49, 0 }; ByteBuf byteBuf = Unpooled.buffer(bs.length); byteBuf = byteBuf.order(ByteOrder.LITTLE_ENDIAN); byteBuf.setBytes(0, bs, 0, bs.length); int _index = byteBuf.readerIndex(); int index = _index; clientFlags = byteBuf.getInt(index); //172939 index += 4; maxPacketSize = byteBuf.getInt(index); //1073741824 index += 4; charsetIndex = byteBuf.getByte(index); //33 index += 1; index += extra.length; int len = 0; while (byteBuf.getByte(index+len) != 0) { len++; } user = new byte[len]; byteBuf.getBytes(index, user, 0, len); index += len; index++; passwordLen = byteBuf.getByte(index); index += 1; password = new byte[passwordLen]; byteBuf.getBytes(index, password, 0, passwordLen); len = 0; while (byteBuf.getByte(index+len) != 0) { len++; } database = new byte[len]; byteBuf.getBytes(index, database, 0, len); index += len; index++; }
Example 20
Source File: DecimalUtility.java From Bats with Apache License 2.0 | 4 votes |
/** * Returns a BigDecimal object from the dense decimal representation. * First step is to convert the dense representation into an intermediate representation * and then invoke getBigDecimalFromDrillBuf() to get the BigDecimal object */ public static BigDecimal getBigDecimalFromDense(DrillBuf data, int startIndex, int nDecimalDigits, int scale, int maxPrecision, int width) { /* This method converts the dense representation to * an intermediate representation. The intermediate * representation has one more integer than the dense * representation. */ byte[] intermediateBytes = new byte[(nDecimalDigits + 1) * INTEGER_SIZE]; // Start storing from the least significant byte of the first integer int intermediateIndex = 3; int[] mask = {0x03, 0x0F, 0x3F, 0xFF}; int[] reverseMask = {0xFC, 0xF0, 0xC0, 0x00}; int maskIndex; int shiftOrder; byte shiftBits; if (maxPrecision == 38) { maskIndex = 0; shiftOrder = 6; shiftBits = 0x00; intermediateBytes[intermediateIndex++] = (byte) (data.getByte(startIndex) & 0x7F); } else if (maxPrecision == 28) { maskIndex = 1; shiftOrder = 4; shiftBits = (byte) ((data.getByte(startIndex) & 0x03) << shiftOrder); intermediateBytes[intermediateIndex++] = (byte) (((data.getByte(startIndex) & 0x3C) & 0xFF) >>> 2); } else { throw new UnsupportedOperationException("Dense types with max precision 38 and 28 are only supported"); } int inputIndex = 1; boolean sign = false; if ((data.getByte(startIndex) & 0x80) != 0) { sign = true; } while (inputIndex < width) { intermediateBytes[intermediateIndex] = (byte) ((shiftBits) | (((data.getByte(startIndex + inputIndex) & reverseMask[maskIndex]) & 0xFF) >>> (8 - shiftOrder))); shiftBits = (byte) ((data.getByte(startIndex + inputIndex) & mask[maskIndex]) << shiftOrder); inputIndex++; intermediateIndex++; if (((inputIndex - 1) % INTEGER_SIZE) == 0) { shiftBits = (byte) ((shiftBits & 0xFF) >>> 2); maskIndex++; shiftOrder -= 2; } } /* copy the last byte */ intermediateBytes[intermediateIndex] = shiftBits; if (sign) { intermediateBytes[0] = (byte) (intermediateBytes[0] | 0x80); } final ByteBuf intermediate = UnpooledByteBufAllocator.DEFAULT.buffer(intermediateBytes.length); try { intermediate.setBytes(0, intermediateBytes); // In the intermediate representation we don't pad the scale with zeroes, so set truncate = false return getBigDecimalFromDrillBuf(intermediate, 0, nDecimalDigits + 1, scale, false); } finally { intermediate.release(); } }