List of usage examples for io.netty.buffer ByteBuf array
public abstract byte[] array();
From source file:com.scurrilous.circe.checksum.Crc32cLongChecksum.java
License:Apache License
/** * Computes crc32c checksum: if it is able to load crc32c native library then it computes using that native library * which is faster as it computes using hardware machine instruction else it computes using crc32c algo. * * @param payload/*from w ww . ja v a2 s. c o m*/ * @return */ public static long computeChecksum(ByteBuf payload) { int crc; if (payload.hasMemoryAddress() && (CRC32C_HASH instanceof Sse42Crc32C)) { crc = CRC32C_HASH.calculate(payload.memoryAddress() + payload.readerIndex(), payload.readableBytes()); } else if (payload.hasArray()) { crc = CRC32C_HASH.calculate(payload.array(), payload.arrayOffset() + payload.readerIndex(), payload.readableBytes()); } else { crc = CRC32C_HASH.calculate(payload.nioBuffer()); } return crc & 0xffffffffL; }
From source file:com.scurrilous.circe.checksum.Crc32cLongChecksum.java
License:Apache License
/** * Computes incremental checksum with input previousChecksum and input payload * * @param previousChecksum : previously computed checksum * @param payload//from w w w.j a va 2 s. c o m * @return */ public static long resumeChecksum(long previousChecksum, ByteBuf payload) { int crc = (int) previousChecksum; if (payload.hasMemoryAddress() && (CRC32C_HASH instanceof Sse42Crc32C)) { crc = CRC32C_HASH.resume(crc, payload.memoryAddress() + payload.readerIndex(), payload.readableBytes()); } else if (payload.hasArray()) { crc = CRC32C_HASH.resume(crc, payload.array(), payload.arrayOffset() + payload.readerIndex(), payload.readableBytes()); } else { crc = CRC32C_HASH.resume(crc, payload.nioBuffer()); } return crc & 0xffffffffL; }
From source file:com.talent.balance.backend.handler.BackendPacketHandler.java
License:Open Source License
@Override public byte[] onSend(Packet packet, ChannelContext channelContext) throws Exception { BalancePacket balancePacket = (BalancePacket) packet; if (BackendExt.PROTOCOL_MYSQL.equals(channelContext.getProtocol())) { MysqlRequestPacket mysqlRequestPacket = (MysqlRequestPacket) balancePacket; ByteBuf byteBuf = mysqlRequestPacket.encode(); byte[] bs1 = byteBuf.array(); log.warn("sent to backend:{}", Arrays.toString(bs1)); return bs1; }// w ww . java2 s . com byte[] bs = balancePacket.getBuffer().array(); // log.warn("sent to back {}, {}, {}", balancePacket.getBuffer().capacity(), Arrays.toString(bs), new String(bs, // "utf-8")); // FileUtils.writeStringToFile(new File("h:/"+channelContext.hashCode()+"__"+BackendExt.getFrontend(channelContext).hashCode()+"/toBackend.txt"), Arrays.toString(bs), true); return bs; }
From source file:com.talent.mysql.packet.request.AuthPacket.java
License:Open Source License
/** * @param args//from w w w . ja v a 2 s.c o m */ 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.nio.communicate.receive.DecodeRunnable.java
License:Open Source License
/** * ??//w w w .j a va 2s.c om * * @param datas */ public void addMsg(ByteBuf datas) { if (DebugUtils.isNeedDebug(channelContext)) { log.error("com.talent.nio.communicate.receive.DecodeRunnable.addMsg(byte[]):" + ArrayUtils.toString(datas)); try { log.error("com.talent.nio.communicate.receive.DecodeRunnable.addMsg(byte[]):" + new String(datas.array(), "utf-8")); } catch (UnsupportedEncodingException e) { log.error(e.getMessage(), e); } } getMsgQueue().add(datas); }
From source file:com.tesora.dve.db.mysql.common.MysqlAPIUtils.java
License:Open Source License
/** * Returns a byte[] that matches the readable content of the provided ByteBuf. If possible, this method will * return a direct reference to the backing array, rather than copy into a new array. If the readable content * is smaller than the backing array, or there is no backing array because the buffer is direct, the contents * are copied into a new byte[]. The readIndex and writeIndex of the provided ByteBuf are not modified by this call. * @param cb source of bytes// w ww.java 2 s . c o m * @return direct reference to backing byte[] data, or a copy if needed, */ public static byte[] unwrapOrCopyReadableBytes(ByteBuf cb) { if (cb.hasArray() && cb.readableBytes() == cb.array().length) { //already a heap array, and readable length is entire array return cb.array(); } else { byte[] copy = new byte[cb.readableBytes()]; cb.slice().readBytes(copy); return copy; } }
From source file:com.tesora.dve.sql.LargeMaxPktTest.java
License:Open Source License
@Test public void testComQueryMessageContinuationOverlap() throws Exception { int defaultMaxPacket = 0xFFFFFF; int payloadSize = (defaultMaxPacket * 4) + (4 * 40); //four full packets and a little change, divisible by 4. ByteBuf source = Unpooled.buffer(payloadSize, payloadSize); Random rand = new Random(239873L); while (source.isWritable()) source.writeInt(rand.nextInt()); Assert.assertEquals(source.writableBytes(), 0, "Oops, I intended to fill up the source buffer"); ByteBuf dest = Unpooled.buffer(payloadSize); MSPComQueryRequestMessage outboundMessage = MSPComQueryRequestMessage.newMessage(source.array()); Packet.encodeFullMessage((byte) 0, outboundMessage, dest); int lengthOfNonUserdata = 5 + 4 + 4 + 4 + 4; Assert.assertEquals(dest.readableBytes(), payloadSize + lengthOfNonUserdata, "Number of bytes in destination buffer is wrong"); Assert.assertEquals(dest.getUnsignedMedium(0), defaultMaxPacket, "First length should be maximum"); Assert.assertEquals(dest.getByte(3), (byte) 0, "First sequenceID should be zero"); Assert.assertEquals(dest.getByte(4), (byte) MSPComQueryRequestMessage.TYPE_IDENTIFIER, "First byte of payload should be MSPComQueryRequestMessage.TYPE_IDENTIFIER"); ByteBuf sliceFirstPayload = dest.slice(5, (0xFFFFFF - 1)); Assert.assertEquals(sliceFirstPayload, source.slice(0, 0xFFFFFF - 1)); }
From source file:com.tesora.dve.sql.LargeMaxPktTest.java
License:Open Source License
@Test public void testComQueryMessageContinuationExact() throws Exception { int defaultMaxPacket = 0xFFFFFF; int payloadSize = (defaultMaxPacket * 4); //four full packets exactly, requires an empty trailing packet. ByteBuf source = Unpooled.buffer(payloadSize, payloadSize); Random rand = new Random(239873L); while (source.isWritable()) source.writeInt(rand.nextInt()); Assert.assertEquals(source.writableBytes(), 0, "Oops, I intended to fill up the source buffer"); ByteBuf dest = Unpooled.buffer(payloadSize); MSPComQueryRequestMessage outboundMessage = MSPComQueryRequestMessage.newMessage(source.array()); Packet.encodeFullMessage((byte) 0, outboundMessage, dest); int lengthOfNonUserdata = 5 + 4 + 4 + 4 + 4;//last packet has zero length payload Assert.assertEquals(dest.readableBytes(), payloadSize + lengthOfNonUserdata, "Number of bytes in destination buffer is wrong"); Assert.assertEquals(dest.getUnsignedMedium(0), defaultMaxPacket, "First length should be maximum"); Assert.assertEquals(dest.getByte(3), (byte) 0, "First sequenceID should be zero"); Assert.assertEquals(dest.getByte(4), (byte) MSPComQueryRequestMessage.TYPE_IDENTIFIER, "First byte of payload should be MSPComQueryRequestMessage.TYPE_IDENTIFIER"); ByteBuf sliceFirstPayload = dest.slice(5, (0xFFFFFF - 1)); Assert.assertEquals(sliceFirstPayload, source.slice(0, 0xFFFFFF - 1)); }
From source file:com.thomas.netty4.websocket.client.GriffinWebSocketClientHandler.java
License:Apache License
@Override public void channelRead0(ChannelHandlerContext ctx, Object msg) throws Exception { Channel ch = ctx.channel();/* w w w . j ava 2 s . c o m*/ if (!handshaker.isHandshakeComplete()) { handshaker.finishHandshake(ch, (FullHttpResponse) msg); System.out.println("WebSocket Client connected!"); handshakeFuture.setSuccess(); return; } if (msg instanceof FullHttpResponse) { FullHttpResponse response = (FullHttpResponse) msg; throw new IllegalStateException("Unexpected FullHttpResponse (getStatus=" + response.getStatus() + ", content=" + response.content().toString(CharsetUtil.UTF_8) + ')'); } WebSocketFrame frame = (WebSocketFrame) msg; if (frame instanceof TextWebSocketFrame) { TextWebSocketFrame textFrame = (TextWebSocketFrame) frame; MessagePack msgp = new MessagePack(); try { byte[] array = frame.content().array(); statistic(array); Value val = msgp.read(array); System.out.println(val); } catch (IOException ioe) { ioe.printStackTrace(); } System.out.println("WebSocket Client received message: " + textFrame.text()); } else if (frame instanceof BinaryWebSocketFrame) { BinaryWebSocketFrame binFrame = (BinaryWebSocketFrame) frame; MessagePack msgp = new MessagePack(); try { ByteBuf buf = Unpooled.copiedBuffer(binFrame.content()); // ByteBuf buf = binFrame.content(); byte[] array = buf.array(); statistic(array); Value val = msgp.read(array); System.out.println("WebSocket Client received message: " + val); // System.out.println(val); } catch (IOException ioe) { ioe.printStackTrace(); } } else if (frame instanceof PongWebSocketFrame) { System.out.println("WebSocket Client received pong"); } else if (frame instanceof CloseWebSocketFrame) { System.out.println("WebSocket Client received closing"); ch.close(); } }
From source file:com.vethrfolnir.game.network.mu.packets.MuReadPacket.java
License:Open Source License
protected String readS(ByteBuf buff, int max) { try {/*from ww w . j a v a 2 s. c o m*/ ByteBuf copy = buff.readBytes(max); String str = new String(copy.array(), "ISO-8859-1"); copy.release(); return str.trim(); } catch (UnsupportedEncodingException e) { log.warn("Failed reading string!", e); } return null; }