List of usage examples for io.netty.buffer ByteBuf setIndex
public abstract ByteBuf setIndex(int readerIndex, int writerIndex);
From source file:TestTCP.java
License:Open Source License
public static void main(String... args) throws Throwable { Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize(); int width = (int) screenSize.getWidth(); int height = (int) screenSize.getHeight(); JFrame ventanica = new JFrame("HTTP test"); ventanica.setBounds((width - 500) / 2, (height - 400) / 2, 500, 400); resultado = new JTextPane(); resultado.setEditable(true);/* ww w . j ava2 s .co m*/ resultado.setContentType("text/txt"); resultado.setEditable(false); final JTextField direccion = new JTextField(); JScrollPane scrollPane = new JScrollPane(resultado); final JLabel bytesSentLabel = new JLabel("Bytes Sent: 0B"); final JLabel bytesReceivedLabel = new JLabel("Bytes Received: 0B"); final JLabel timeSpent = new JLabel("Time: 0ms"); timeSpent.setHorizontalAlignment(SwingConstants.CENTER); JPanel bottomPanel = new JPanel(new BorderLayout(1, 3)); bottomPanel.add(bytesSentLabel, BorderLayout.WEST); bottomPanel.add(timeSpent, BorderLayout.CENTER); bottomPanel.add(bytesReceivedLabel, BorderLayout.EAST); ventanica.setLayout(new BorderLayout(3, 1)); ventanica.add(direccion, BorderLayout.NORTH); ventanica.add(scrollPane, BorderLayout.CENTER); ventanica.add(bottomPanel, BorderLayout.SOUTH); ventanica.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE); ventanica.setVisible(true); final IOService service = new IOService(); direccion.addActionListener(new AbstractAction() { @Override public void actionPerformed(ActionEvent e) { final TCPSocket socket = new TCPSocket(service); resultado.setText(""); bytesSentLabel.setText("Bytes Sent: 0B"); bytesReceivedLabel.setText("Bytes Received: 0B"); timeSpent.setText("Time: 0ms"); direccion.setEnabled(false); String addr = direccion.getText(); String host, path = "/"; int puerto = 80; try { URL url = new URL((addr.startsWith("http://") ? "" : "http://") + addr); host = url.getHost(); path = url.getPath().isEmpty() ? "/" : url.getPath(); puerto = url.getPort() == -1 ? url.getDefaultPort() : url.getPort(); } catch (MalformedURLException e1) { String as[] = addr.split(":"); host = as[0]; if (as.length > 1) { puerto = Integer.parseInt(as[1]); } } final String request = "GET " + path + " HTTP/1.1\r\n" + "Accept-Charset: utf-8\r\n" + "User-Agent: JavaNettyMelchor629\r\n" + "Host: " + host + "\r\n" + "Connection: close\r\n" + "\r\n"; Callback<Future<Void>> l = new Callback<Future<Void>>() { @Override public void call(Future<Void> arg) { final long start = System.currentTimeMillis(); final ByteBuf b = ByteBufAllocator.DEFAULT.buffer(16 * 1024).retain(); socket.onClose().whenDone(new Callback<Future<Void>>() { @Override public void call(Future<Void> arg) { direccion.setEnabled(true); long spent = System.currentTimeMillis() - start; timeSpent.setText(String.format("Time spent: %dms", spent)); b.release(); } }); socket.setOption(ChannelOption.CONNECT_TIMEOUT_MILLIS, 5000); socket.setOption(ChannelOption.SO_TIMEOUT, 5000); socket.sendAsync(ByteBufUtil.writeUtf8(ByteBufAllocator.DEFAULT, request)) .whenDone(new Callback<Future<Void>>() { @Override public void call(Future<Void> arg) { bytesSentLabel.setText("Bytes Sent: " + socket.sendBytes() + "B"); final Callback<Future<Long>> cbk = new Callback<Future<Long>>() { @Override public void call(Future<Long> arg) { bytesReceivedLabel .setText("Bytes Received: " + socket.receivedBytes() + "B"); if (!arg.isSuccessful()) return; byte b1[] = new byte[(int) (long) arg.getValueNow()]; b.getBytes(0, b1); resultado.setText(resultado.getText() + new String(b1).replace("\r", "\\r").replace("\n", "\\n\n") + ""); b.setIndex(0, 0); socket.receiveAsync(b).whenDone(this); } }; socket.receiveAsync(b).whenDone(cbk); } }); } }; try { socket.connectAsync(host, puerto, new OracleJREServerProvider()).whenDone(l); } catch (Throwable ignore) { } } }); ventanica.addWindowListener(new WindowAdapter() { @Override public void windowClosing(WindowEvent e) { service.cancel(); } }); }
From source file:TestSSL.java
License:Open Source License
public static void main(String[] args) { Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize(); int width = (int) screenSize.getWidth(); int height = (int) screenSize.getHeight(); JFrame ventanica = new JFrame("HTTPS test"); ventanica.setBounds((width - 500) / 2, (height - 400) / 2, 500, 400); resultado = new JTextPane(); resultado.setEditable(true);/* ww w .j a v a 2s .c om*/ resultado.setContentType("text/txt"); resultado.setEditable(false); final JTextField direccion = new JTextField(); JScrollPane scrollPane = new JScrollPane(resultado); final JLabel bytesSentLabel = new JLabel("Bytes Sent: 0B"); final JLabel bytesReceivedLabel = new JLabel("Bytes Received: 0B"); final JLabel timeSpent = new JLabel("Time: 0ms"); timeSpent.setHorizontalAlignment(SwingConstants.CENTER); JPanel bottomPanel = new JPanel(new BorderLayout(1, 3)); bottomPanel.add(bytesSentLabel, BorderLayout.WEST); bottomPanel.add(timeSpent, BorderLayout.CENTER); bottomPanel.add(bytesReceivedLabel, BorderLayout.EAST); ventanica.setLayout(new BorderLayout(3, 1)); ventanica.add(direccion, BorderLayout.NORTH); ventanica.add(scrollPane, BorderLayout.CENTER); ventanica.add(bottomPanel, BorderLayout.SOUTH); ventanica.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE); ventanica.setVisible(true); final IOService service = new IOService(); direccion.addActionListener(new AbstractAction() { @Override public void actionPerformed(ActionEvent e) { final SSLSocket socket = new SSLSocket(service); resultado.setText(""); bytesSentLabel.setText("Bytes Sent: 0B"); bytesReceivedLabel.setText("Bytes Received: 0B"); timeSpent.setText("Time: 0ms"); direccion.setEnabled(false); String addr = direccion.getText(); String host, path = "/"; int puerto = 80; try { URL url = new URL((addr.startsWith("https://") ? "" : "https://") + addr); host = url.getHost(); path = url.getPath().isEmpty() ? "/" : url.getPath(); puerto = url.getPort() == -1 ? url.getDefaultPort() : url.getPort(); } catch (MalformedURLException e1) { String as[] = addr.split(":"); host = as[0]; if (as.length > 1) { puerto = Integer.parseInt(as[1]); } } final String request = "GET " + path + " HTTP/1.1\r\n" + "Accept-Charset: utf-8\r\n" + "User-Agent: JavaNettyMelchor629\r\n" + "Host: " + host + "\r\n" + "Connection: close\r\n" + "\r\n"; Callback<Future<Void>> l = new Callback<Future<Void>>() { @Override public void call(Future<Void> arg) { final long start = System.currentTimeMillis(); final ByteBuf b = ByteBufAllocator.DEFAULT.buffer(16 * 1024).retain(); socket.onClose().whenDone(new Callback<Future<Void>>() { @Override public void call(Future<Void> arg) { direccion.setEnabled(true); long spent = System.currentTimeMillis() - start; timeSpent.setText(String.format("Time spent: %dms", spent)); b.release(); } }); socket.setOption(ChannelOption.CONNECT_TIMEOUT_MILLIS, 5000); socket.setOption(ChannelOption.SO_TIMEOUT, 5000); socket.sendAsync(ByteBufUtil.writeUtf8(ByteBufAllocator.DEFAULT, request)) .whenDone(new Callback<Future<Void>>() { @Override public void call(Future<Void> arg) { bytesSentLabel.setText("Bytes Sent: " + socket.sendBytes() + "B"); final Callback<Future<Long>> cbk = new Callback<Future<Long>>() { @Override public void call(Future<Long> arg) { bytesReceivedLabel .setText("Bytes Received: " + socket.receivedBytes() + "B"); if (!arg.isSuccessful()) return; byte b1[] = new byte[(int) (long) arg.getValueNow()]; b.getBytes(0, b1); resultado.setText(resultado.getText() + new String(b1).replace("\r", "\\r").replace("\n", "\\n\n") + ""); b.setIndex(0, 0); socket.receiveAsync(b).whenDone(this); } }; socket.receiveAsync(b).whenDone(cbk); } }); } }; try { socket.connectAsync(host, puerto, new OracleJREServerProvider()).whenDone(l); } catch (Throwable ignore) { } } }); ventanica.addWindowListener(new WindowAdapter() { @Override public void windowClosing(WindowEvent e) { service.cancel(); } }); }
From source file:com.dc.gameserver.ServerCore.Controller.AbstractController.AbstractController.java
License:Apache License
/** * 4// ww w . j a v a2 s . c o m * * @param ID ?? * @param messageLite * @return */ public static ByteBuf wrappedBufferInt(int ID, MessageLite messageLite) { byte[] src = messageLite.toByteArray(); int length = 8 + src.length; ByteBuf buffer = PooledByteBufAllocator.DEFAULT.heapBuffer(length, length); buffer.setIndex(0, 0x4);//writeIndex? //4 buffer.writeByte(ID); // 4 buffer.writeBytes(messageLite.toByteArray()); buffer.setInt(0, buffer.writerIndex() - 0x4); messageLite = null; return buffer; }
From source file:com.dc.gameserver.ServerCore.Controller.AbstractController.AbstractController.java
License:Apache License
/** * ?? </br>//from w w w .jav a 2s .c om * Encoder buffer </br> * ?4 </br> * proto buffer </br> * </br> * * @param arg1 * @param arg2 * @param messageLite * @return + ID+protoBufferData */ public static ByteBuf wrappedBufferInt(int arg1, int arg2, MessageLite messageLite) { byte[] src = messageLite.toByteArray(); int length = 12 + src.length; ByteBuf buffer = PooledByteBufAllocator.DEFAULT.heapBuffer(length, length); buffer.setIndex(0, 0x4);//writeIndex? //4 buffer.writeByte(arg1); // 4 buffer.writeByte(arg2); // 4 buffer.writeBytes(messageLite.toByteArray()); buffer.setInt(0, buffer.writerIndex() - 0x4); messageLite = null; return buffer; }
From source file:com.dc.gameserver.ServerCore.Controller.AbstractController.AbstractController.java
License:Apache License
/** * ?? </br>/* w ww .ja v a2 s . com*/ * Encoder buffer </br> * ?2 </br> * proto buffer </br> * </br> * * @param arg1 * @param arg2 * @param messageLite * @return + ID+protoBufferData */ public static ByteBuf wrappedBufferShort(int arg1, int arg2, MessageLite messageLite) { byte[] src = messageLite.toByteArray(); int length = 10 + src.length; ByteBuf buffer = PooledByteBufAllocator.DEFAULT.heapBuffer(length, length); buffer.setIndex(0, 0x2);//writeIndex? buffer.writeByte(arg1); buffer.writeByte(arg2); buffer.writeBytes(messageLite.toByteArray()); /**?2*/ buffer.setShort(0, buffer.writerIndex() - 0x2); messageLite = null; return buffer; }
From source file:com.dc.gameserver.ServerCore.Controller.AbstractController.AbstractController.java
License:Apache License
/** * Encoder buffer </br> * 2 </br>//from w ww .j a v a2s .co m * * @param ID ?? * @param messageLite byte[] * @return */ public static ByteBuf wrappedBufferShort(int ID, MessageLite messageLite) { byte[] src = messageLite.toByteArray(); int length = 6 + src.length; ByteBuf buffer = PooledByteBufAllocator.DEFAULT.heapBuffer(length, length); buffer.setIndex(0, 0x2);//writeIndex? 2 buffer.writeInt(ID); //? 4 buffer.writeBytes(src); buffer.setShort(0, buffer.writerIndex() - 0x2); // short messageLite = null; //set null ,collection by GC return buffer; }
From source file:com.spotify.netty.handler.codec.zmtp.ZMTPMessageParserTest.java
License:Apache License
private void testParse(final boolean enveloped, final Limit limit, final List<String> input, final ZMTPParsedMessage expected, int version) throws Exception { out.println(format("enveloped=%s limit=%s input=%s expected=%s", enveloped, limit, input, expected)); final ByteBuf serialized = serialize(input, version); final int serializedLength = serialized.readableBytes(); // Test parsing the whole message {/*from www . j a v a 2s .c o m*/ final ZMTPMessageParser parser = new ZMTPMessageParser(enveloped, limit.value, 1); final ZMTPParsedMessage parsed = parser.parse(serialized); serialized.setIndex(0, serializedLength); assertEquals(expected, parsed); } // Prepare for trivial message parsing test final int contentSize = min(limit.value - 1, 10); final List<String> envelope = asList("e", ""); final List<String> content = nCopies(contentSize, "."); final List<String> frames = newArrayList(concat(envelope, content)); final ZMTPMessage trivialMessage = ZMTPMessage.fromStringsUTF8(enveloped, frames); final ByteBuf trivialSerialized = serialize(frames, version); final int trivialLength = trivialSerialized.readableBytes(); // Test parsing fragmented input new Fragmenter(serialized.readableBytes()).fragment(new Fragmenter.Consumer() { @Override public void fragments(final int[] limits, final int count) throws Exception { serialized.setIndex(0, serializedLength); ZMTPParsedMessage parsed = null; final ZMTPMessageParser parser = new ZMTPMessageParser(enveloped, limit.value, 1); for (int i = 0; i < count; i++) { final int limit = limits[i]; serialized.writerIndex(limit); parsed = parser.parse(serialized); // Verify that the parser did not return a message for incomplete input if (limit < serializedLength) { assertNull(parsed); } } assertEquals(expected, parsed); // Verify that the parser can be reused to parse the same message serialized.setIndex(0, serializedLength); final ZMTPParsedMessage reparsed = parser.parse(serialized); assertEquals(expected, reparsed); // Verify that the parser can be reused to parse a well-behaved message trivialSerialized.setIndex(0, trivialLength); final ZMTPParsedMessage parsedTrivial = parser.parse(trivialSerialized); assertFalse(parsedTrivial.isTruncated()); assertEquals(trivialMessage, parsedTrivial.getMessage()); } }); }
From source file:com.spotify.netty4.handler.codec.zmtp.ZMTPParserTest.java
License:Apache License
@Theory public void testParse(@FromDataPoints("frames") final String[] frames, @FromDataPoints("versions") final ZMTPVersion version) throws Exception { final List<String> input = asList(frames); final ZMTPWireFormat wireFormat = wireFormat(version); final ZMTPMessage inputMessage = ZMTPMessage.fromUTF8(ALLOC, input); final ExpectedOutput expected = new ExpectedOutput(inputMessage); final ByteBuf serialized = inputMessage.write(ALLOC, version); final int serializedLength = serialized.readableBytes(); // Test parsing the whole message {/*from www . j av a 2 s. c om*/ final VerifyingDecoder verifier = new VerifyingDecoder(expected); final ZMTPFramingDecoder decoder = new ZMTPFramingDecoder(wireFormat, verifier); decoder.decode(ctx, serialized, null); verifier.assertFinished(); serialized.setIndex(0, serializedLength); } // Prepare for trivial message parsing test final ZMTPMessage trivial = ZMTPMessage.fromUTF8(ALLOC, "e", "", "a", "b", "c"); final ByteBuf trivialSerialized = trivial.write(ALLOC, version); final int trivialLength = trivialSerialized.readableBytes(); final ExpectedOutput trivialExpected = new ExpectedOutput(trivial); // Test parsing fragmented input final VerifyingDecoder verifier = new VerifyingDecoder(); final ZMTPFramingDecoder decoder = new ZMTPFramingDecoder(wireFormat, verifier); new Fragmenter(serialized.readableBytes()).fragment(new Fragmenter.Consumer() { @Override public void fragments(final int[] limits, final int count) throws Exception { verifier.expect(expected); serialized.setIndex(0, serializedLength); for (int i = 0; i < count; i++) { final int limit = limits[i]; serialized.writerIndex(limit); decoder.decode(ctx, serialized, null); } verifier.assertFinished(); // Verify that the parser can be reused to parse the same message serialized.setIndex(0, serializedLength); decoder.decode(ctx, serialized, null); verifier.assertFinished(); // Verify that the parser can be reused to parse a well-behaved message verifier.expect(trivialExpected); trivialSerialized.setIndex(0, trivialLength); decoder.decode(ctx, trivialSerialized, null); verifier.assertFinished(); } }); }
From source file:dorkbox.network.connection.KryoExtra.java
License:Apache License
/** * This is NOT ENCRYPTED (and is only done on the loopback connection!) */// ww w . j a v a 2 s.com public Object readCompressed(final Connection_ connection, final ByteBuf buffer, int length) throws IOException { // required by RMI and some serializers to determine which connection wrote (or has info about) this object this.rmiSupport = connection.rmiSupport(); //////////////// // Note: we CANNOT write BACK to the buffer as "temp" storage, since there could be additional data on it! //////////////// ByteBuf inputBuf = buffer; // get the decompressed length (at the beginning of the array) final int uncompressedLength = OptimizeUtilsByteBuf.readInt(buffer, true); final int lengthLength = OptimizeUtilsByteArray.intLength(uncompressedLength, true); // because 1-5 bytes for the decompressed size // have to adjust for uncompressed length length = length - lengthLength; ///////// decompress data -- as it's ALWAYS compressed // NOTE: compression and encryption MUST work with byte[] because they use JNI! // Realistically, it is impossible to get the backing arrays out of a Heap Buffer once they are resized and begin to use // sliced. It's lame that there is a "double copy" of bytes here, but I don't know how to avoid it... // see: https://stackoverflow.com/questions/19296386/netty-java-getting-data-from-bytebuf byte[] inputArray; int inputOffset; // Even if a ByteBuf has a backing array (i.e. buf.hasArray() returns true), the using it isn't always possible because // the buffer might be a slice of other buffer or a pooled buffer: //noinspection Duplicates if (inputBuf.hasArray() && inputBuf.array()[0] == inputBuf.getByte(0) && inputBuf.array().length == inputBuf.capacity()) { // we can use it... inputArray = inputBuf.array(); inputArrayLength = -1; // this is so we don't REUSE this array accidentally! inputOffset = inputBuf.arrayOffset() + lengthLength; } else { // we can NOT use it. if (length > inputArrayLength) { inputArrayLength = length; inputArray = new byte[length]; this.inputArray = inputArray; } else { inputArray = this.inputArray; } inputBuf.getBytes(inputBuf.readerIndex(), inputArray, 0, length); inputOffset = 0; } // have to make sure to set the position of the buffer, since our conversion to array DOES NOT set the new reader index. buffer.readerIndex(buffer.readerIndex() + length); ///////// decompress data -- as it's ALWAYS compressed byte[] decompressOutputArray = this.decompressOutput; if (uncompressedLength > decompressOutputLength) { decompressOutputLength = uncompressedLength; decompressOutputArray = new byte[uncompressedLength]; this.decompressOutput = decompressOutputArray; decompressBuf = Unpooled.wrappedBuffer(decompressOutputArray); // so we can read via kryo } inputBuf = decompressBuf; // LZ4 decompress, requires the size of the ORIGINAL length (because we use the FAST decompressor) decompressor.decompress(inputArray, inputOffset, decompressOutputArray, 0, uncompressedLength); inputBuf.setIndex(0, uncompressedLength); // read the object from the buffer. reader.setBuffer(inputBuf); return readClassAndObject(reader); // this properly sets the readerIndex, but only if it's the correct buffer }
From source file:dorkbox.network.connection.KryoExtra.java
License:Apache License
public Object readCrypto(final Connection_ connection, final ByteBuf buffer, int length) throws IOException { // required by RMI and some serializers to determine which connection wrote (or has info about) this object this.rmiSupport = connection.rmiSupport(); //////////////// // Note: we CANNOT write BACK to the buffer as "temp" storage, since there could be additional data on it! //////////////// ByteBuf inputBuf = buffer; final long gcmIVCounter = OptimizeUtilsByteBuf.readLong(buffer, true); int lengthLength = OptimizeUtilsByteArray.longLength(gcmIVCounter, true); // have to adjust for the gcmIVCounter length = length - lengthLength;/*from w w w. jav a2s . c o m*/ /////////// decrypting data // NOTE: compression and encryption MUST work with byte[] because they use JNI! // Realistically, it is impossible to get the backing arrays out of a Heap Buffer once they are resized and begin to use // sliced. It's lame that there is a "double copy" of bytes here, but I don't know how to avoid it... // see: https://stackoverflow.com/questions/19296386/netty-java-getting-data-from-bytebuf byte[] inputArray; int inputOffset; // Even if a ByteBuf has a backing array (i.e. buf.hasArray() returns true), the using it isn't always possible because // the buffer might be a slice of other buffer or a pooled buffer: //noinspection Duplicates if (inputBuf.hasArray() && inputBuf.array()[0] == inputBuf.getByte(0) && inputBuf.array().length == inputBuf.capacity()) { // we can use it... inputArray = inputBuf.array(); inputArrayLength = -1; // this is so we don't REUSE this array accidentally! inputOffset = inputBuf.arrayOffset() + lengthLength; } else { // we can NOT use it. if (length > inputArrayLength) { inputArrayLength = length; inputArray = new byte[length]; this.inputArray = inputArray; } else { inputArray = this.inputArray; } inputBuf.getBytes(inputBuf.readerIndex(), inputArray, 0, length); inputOffset = 0; } // have to make sure to set the position of the buffer, since our conversion to array DOES NOT set the new reader index. buffer.readerIndex(buffer.readerIndex() + length); // this is a threadlocal, so that we don't clobber other threads that are performing crypto on the same connection at the same time final ParametersWithIV cryptoParameters = connection.getCryptoParameters(); BigEndian.Long_.toBytes(gcmIVCounter, cryptoParameters.getIV(), 4); // put our counter into the IV final GCMBlockCipher aes = this.aesEngine; aes.reset(); aes.init(false, cryptoParameters); int cryptoSize = length - 16; // from: aes.getOutputSize(length); // lazy initialize the decrypt output buffer byte[] decryptOutputArray; if (cryptoSize > decryptOutputLength) { decryptOutputLength = cryptoSize; decryptOutputArray = new byte[cryptoSize]; this.decryptOutput = decryptOutputArray; decryptBuf = Unpooled.wrappedBuffer(decryptOutputArray); } else { decryptOutputArray = this.decryptOutput; } int decryptedLength = aes.processBytes(inputArray, inputOffset, length, decryptOutputArray, 0); try { // authentication tag for GCM decryptedLength += aes.doFinal(decryptOutputArray, decryptedLength); } catch (Exception e) { throw new IOException("Unable to AES decrypt the data", e); } ///////// decompress data -- as it's ALWAYS compressed // get the decompressed length (at the beginning of the array) inputArray = decryptOutputArray; final int uncompressedLength = OptimizeUtilsByteArray.readInt(inputArray, true); inputOffset = OptimizeUtilsByteArray.intLength(uncompressedLength, true); // because 1-4 bytes for the decompressed size byte[] decompressOutputArray = this.decompressOutput; if (uncompressedLength > decompressOutputLength) { decompressOutputLength = uncompressedLength; decompressOutputArray = new byte[uncompressedLength]; this.decompressOutput = decompressOutputArray; decompressBuf = Unpooled.wrappedBuffer(decompressOutputArray); // so we can read via kryo } inputBuf = decompressBuf; // LZ4 decompress, requires the size of the ORIGINAL length (because we use the FAST decompressor decompressor.decompress(inputArray, inputOffset, decompressOutputArray, 0, uncompressedLength); inputBuf.setIndex(0, uncompressedLength); // read the object from the buffer. reader.setBuffer(inputBuf); return readClassAndObject(reader); // this properly sets the readerIndex, but only if it's the correct buffer }