List of usage examples for io.netty.buffer ByteBuf setBytes
public abstract ByteBuf setBytes(int index, ByteBuffer src);
From source file:com.fjn.helper.frameworkex.netty.v4.discardserver.DiscardServerHandler.java
License:Apache License
/** * ???//from w ww. j ava 2 s . com * @param ctx * @param msg * @throws Exception */ public void channelRead(final ChannelHandlerContext ctx, Object msg) throws Exception { ByteBuf in = (ByteBuf) msg; try { while (in.isReadable()) { char c = (char) in.readByte(); content += c; if (c == '\n' || c == '\r') { if (!line.isEmpty()) { System.out.println(line); } if ("quit".equals(line)) { ByteBuf bf = ctx.alloc().buffer(content.getBytes().length); bf.setBytes(0, content.getBytes()); final ChannelFuture f = ctx.writeAndFlush(bf); // (3) f.addListener(new ChannelFutureListener() { @Override public void operationComplete(ChannelFuture future) throws InterruptedException { assert f == future; ctx.close(); } }); // (4) } line = ""; continue; } else { line += c; } } } finally { ReferenceCountUtil.release(msg); } }
From source file:com.streamsets.pipeline.lib.parser.collectd.CollectdParser.java
License:Apache License
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); }/*from w w w . j a va2 s. c om*/ 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()); } }
From source file:com.talent.mysql.packet.request.AuthPacket.java
License:Open Source License
/** * @param args//from ww w .j a v a2 s . com */ public static void main(String[] args) { AuthPacket authPacket1 = new AuthPacket(); authPacket1.decodeBody(null); 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); } catch (DecodeException e) { e.printStackTrace(); } BackendConf backendConf = BackendConf.getInstance(); BackendServerConf backendServerConf = backendConf.getServers()[0]; AuthPacket authPacket = new AuthPacket(); authPacket.charsetIndex = (byte) (handshakePacket.charset & 0xff); authPacket.user = backendServerConf.getProps().get("user").getBytes(); try { authPacket.password = getPass(backendServerConf.getProps().get("pwd"), handshakePacket); } catch (NoSuchAlgorithmException e) { e.printStackTrace(); } authPacket.passwordLen = (byte) authPacket.password.length; authPacket.database = backendServerConf.getProps().get("db").getBytes(); ByteBuf byteBuf1 = authPacket.encode(); System.out.println(Arrays.toString(byteBuf1.array())); }
From source file:com.talent.mysql.packet.request.AuthPacket.java
License:Open Source License
@Override public void encodeBody(ByteBuf byteBuf) { int index = byteBuf.readerIndex(); String xx = Long.toBinaryString(clientFlags); byteBuf.setLong(index, clientFlags); index += 4;/*from w w w. j a v a2 s. c om*/ 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++; }
From source file:com.talent.mysql.packet.response.HandshakePacket.java
License:Open Source License
/** * @param args/* ww w. j a va 2s.c o m*/ * @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(); } }
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);/*from w w w . jav a 2 s . c om*/ 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.portal.protocol.MSPComStmtExecuteRequestMessage.java
License:Open Source License
@Override protected void marshall(ParsedData state, ByteBuf destination) { ByteBuf leBuf = destination.order(ByteOrder.LITTLE_ENDIAN); leBuf.writeByte(TYPE_IDENTIFIER);/*w w w.j a va 2 s.com*/ leBuf.writeInt((int) state.statementID); leBuf.writeByte(state.flags); leBuf.writeInt((int) state.iterationCount); if (state.metadata == null) throw new IllegalStateException("Cannot build execute request, no prepare metadata provided."); int numParams = state.metadata.getNumParams(); if (numParams > 0) { MyNullBitmap nullBitmap = new MyNullBitmap(numParams, MyNullBitmap.BitmapType.EXECUTE_REQUEST); int bitmapIndex = leBuf.writerIndex(); leBuf.writeZero(nullBitmap.length()); if (state.metadata.isNewParameterList()) { leBuf.writeByte(1); for (MyParameter param : state.metadata.getParameters()) { leBuf.writeByte(param.getType().getByteValue()); leBuf.writeZero(1); } } else { leBuf.writeZero(1); } List<Object> params = state.values; for (int i = 0; i < params.size(); ++i) { if (params.get(i) != null) DBTypeBasedUtils.getJavaTypeFunc(params.get(i).getClass()).writeObject(leBuf, params.get(i)); else nullBitmap.setBit(i + 1); } leBuf.setBytes(bitmapIndex, nullBitmap.getBitmapArray()); } }
From source file:com.vethrfolnir.game.network.mu.received.ExRequestAuth.java
License:Open Source License
@Override public void read(MuClient client, ByteBuf buff, Object... params) { if (MuNetworkServer.onlineClients() >= capacity) { invalidate(buff);//www . j a va 2s .c om 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); }
From source file:net.tomp2p.storage.AlternativeCompositeByteBuf.java
License:Apache License
@Override public AlternativeCompositeByteBuf setBytes(int index, ByteBuffer src) { int limit = src.limit(); int length = src.remaining(); checkIndex(index, length);//from w w w . java 2s .co m if (length == 0) { return this; } int i = findIndex(index); try { while (length > 0) { Component c = components.get(i); ByteBuf s = c.buf; int adjustment = c.offset; int localLength = Math.min(length, s.writableBytes()); src.limit(src.position() + localLength); s.setBytes(index - adjustment, src); index += localLength; length -= localLength; i++; } } finally { src.limit(limit); } return this; }
From source file:org.apache.drill.exec.util.DecimalUtility.java
License:Apache License
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.// ww w.j a va2 s . c om */ 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; // TODO: Some of the logic here is common with casting from Dense to Sparse types, factor out common code 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 == true) { intermediateBytes[0] = (byte) (intermediateBytes[0] | 0x80); } final ByteBuf intermediate = UnpooledByteBufAllocator.DEFAULT.buffer(intermediateBytes.length); try { intermediate.setBytes(0, intermediateBytes); BigDecimal ret = getBigDecimalFromIntermediate(intermediate, 0, nDecimalDigits + 1, scale); return ret; } finally { intermediate.release(); } }