List of usage examples for io.netty.buffer Unpooled wrappedBuffer
public static ByteBuf wrappedBuffer(ByteBuffer... buffers)
From source file:com.github.gregwhitaker.awspolly.example.PollyReadHandler.java
License:Apache License
@Override public void handle(Context ctx) throws Exception { String voiceId = ctx.getRequest().getQueryParams().get("voiceId"); String text = ctx.getRequest().getQueryParams().get("text"); String outputFormat = ctx.getRequest().getQueryParams().get("outputFormat"); SynthesizeSpeechRequest ssRequest = new SynthesizeSpeechRequest(); ssRequest.setVoiceId(voiceId);/*ww w.j a v a 2s . c om*/ ssRequest.setOutputFormat(outputFormat); ssRequest.setText(text); SynthesizeSpeechResult result = polly.synthesizeSpeech(ssRequest); ctx.getResponse().contentType(result.getContentType()); ctx.getResponse().sendStream(s -> s.onSubscribe(new Subscription() { @Override public void request(long n) { try { byte[] data = new byte[1024]; int bytesRead = result.getAudioStream().read(data); while (bytesRead != -1) { s.onNext(Unpooled.wrappedBuffer(data)); bytesRead = result.getAudioStream().read(data); } } catch (IOException e) { ctx.getResponse().status(500); ctx.getResponse().send(); } finally { s.onComplete(); } } @Override public void cancel() { } })); }
From source file:com.github.http.helloworld.HttpHelloWorldServerHandler.java
License:Apache License
@Override public void channelRead(ChannelHandlerContext ctx, Object msg) { if (msg instanceof HttpRequest) { HttpRequest req = (HttpRequest) msg; if (HttpHeaders.is100ContinueExpected(req)) { ctx.write(new DefaultFullHttpResponse(HTTP_1_1, CONTINUE)); }/* ww w.j a v a2 s. co m*/ boolean keepAlive = HttpHeaders.isKeepAlive(req); FullHttpResponse response = new DefaultFullHttpResponse(HTTP_1_1, OK, Unpooled.wrappedBuffer(CONTENT)); response.headers().set(CONTENT_TYPE, "text/plain"); response.headers().set(CONTENT_LENGTH, response.content().readableBytes()); if (!keepAlive) { ctx.write(response).addListener(ChannelFutureListener.CLOSE); } else { response.headers().set(CONNECTION, Values.KEEP_ALIVE); ctx.write(response); } } }
From source file:com.github.milenkovicm.kafka.FailureTest.java
License:Apache License
@Test public void test_producer() throws Exception { String topic = "test"; createTopic(topic, 2, 2);//from w ww .jav a 2 s .co m ProducerProperties properties = new ProducerProperties(); properties.override(ProducerProperties.NETTY_THREAD_COUNT, 1); properties.override(ProducerProperties.NETTY_DEBUG_PIPELINE, true); properties.override(ProducerProperties.PARTITIONER, new RRPartitioner()); KafkaProducer producer = new KafkaProducer("localhost", START_PORT, topic, properties); producer.connect().sync(); KafkaTopic kafkaTopic = producer.topic(); kafkaTopic.send(Unpooled.wrappedBuffer(new byte[] { 0 }), freeLaterBuffer(TEST_MESSAGE.getBytes())); kafkaTopic.send(Unpooled.wrappedBuffer(new byte[] { 1 }), freeLaterBuffer(TEST_MESSAGE.getBytes())); kafkaServers.get(0).shutdown(); kafkaTopic.send(Unpooled.wrappedBuffer(new byte[] { 2 }), freeLaterBuffer(TEST_MESSAGE.getBytes())); kafkaTopic.send(Unpooled.wrappedBuffer(new byte[] { 3 }), freeLaterBuffer(TEST_MESSAGE.getBytes())); final List<KafkaStream<byte[], byte[]>> consume = consume(topic); final KafkaStream<byte[], byte[]> stream = consume.get(0); final ConsumerIterator<byte[], byte[]> messages = stream.iterator(); Set<Integer> partitions = new TreeSet<>(); MessageAndMetadata<byte[], byte[]> message = messages.next(); Assert.assertThat("message should be equal", new String(message.message()), is(TEST_MESSAGE)); partitions.add(message.partition()); message = messages.next(); Assert.assertThat("message should be equal", new String(message.message()), is(TEST_MESSAGE)); partitions.add(message.partition()); message = messages.next(); Assert.assertThat("message should be equal", new String(message.message()), is(TEST_MESSAGE)); partitions.add(message.partition()); message = messages.next(); Assert.assertThat("message should be equal", new String(message.message()), is(TEST_MESSAGE)); partitions.add(message.partition()); Assert.assertThat("we should have message from all partitions", partitions.toArray(), is((Object[]) new Integer[] { 0, 1 })); producer.disconnect().sync(); }
From source file:com.github.milenkovicm.kafka.KafkaTopicMultiBrokerTest.java
License:Apache License
@Test public void test_producer() throws Exception { String topic = "test"; int numberOfPartition = 4; createTopic(topic, numberOfPartition, 1); ProducerProperties properties = new ProducerProperties(); properties.override(ProducerProperties.NETTY_DEBUG_PIPELINE, true); properties.override(ProducerProperties.PARTITIONER, new RRPartitioner()); KafkaProducer producer = new KafkaProducer("localhost", START_PORT, topic, properties); producer.connect().sync();/*www . jav a 2 s . com*/ KafkaTopic kafkaTopic = producer.topic(); Assert.assertThat("number of partitions do not match", kafkaTopic.numberOfPartitions(), is(numberOfPartition)); kafkaTopic.send(Unpooled.wrappedBuffer(new byte[] { 0 }), freeLaterBuffer(TEST_MESSAGE.getBytes())); kafkaTopic.send(Unpooled.wrappedBuffer(new byte[] { 1 }), freeLaterBuffer(TEST_MESSAGE.getBytes())); kafkaTopic.send(Unpooled.wrappedBuffer(new byte[] { 2 }), freeLaterBuffer(TEST_MESSAGE.getBytes())); kafkaTopic.send(Unpooled.wrappedBuffer(new byte[] { 3 }), freeLaterBuffer(TEST_MESSAGE.getBytes())); final List<KafkaStream<byte[], byte[]>> consume = consume(topic); final KafkaStream<byte[], byte[]> stream = consume.get(0); final ConsumerIterator<byte[], byte[]> messages = stream.iterator(); List<Integer> partitions = new LinkedList<>(); MessageAndMetadata<byte[], byte[]> message = messages.next(); Assert.assertThat("message should be equal", new String(message.message()), is(TEST_MESSAGE)); Assert.assertThat("partition and key should be equal", (byte) message.partition(), is(message.key()[0])); partitions.add(message.partition()); message = messages.next(); Assert.assertThat("message should be equal", new String(message.message()), is(TEST_MESSAGE)); Assert.assertThat("partition and key should be equal", (byte) message.partition(), is(message.key()[0])); partitions.add(message.partition()); message = messages.next(); Assert.assertThat("message should be equal", new String(message.message()), is(TEST_MESSAGE)); Assert.assertThat("partition and key should be equal", (byte) message.partition(), is(message.key()[0])); partitions.add(message.partition()); message = messages.next(); Assert.assertThat("message should be equal", new String(message.message()), is(TEST_MESSAGE)); Assert.assertThat("partition and key should be equal", (byte) message.partition(), is(message.key()[0])); partitions.add(message.partition()); Collections.sort(partitions); Assert.assertThat("we should have message from all partitions", partitions.toArray(), is((Object[]) new Integer[] { 0, 1, 2, 3 })); producer.disconnect().sync(); }
From source file:com.github.nettybook.ch7.junit.Base64EncoderTest.java
License:Apache License
@Test public void testEncoder() { String writeData = ""; ByteBuf request = Unpooled.wrappedBuffer(writeData.getBytes()); Base64Encoder encoder = new Base64Encoder(); EmbeddedChannel embeddedChannel = new EmbeddedChannel(encoder); embeddedChannel.writeOutbound(request); ByteBuf response = (ByteBuf) embeddedChannel.readOutbound(); String expect = "7JWI64WV7ZWY7IS47JqU"; assertEquals(expect, response.toString(Charset.defaultCharset())); embeddedChannel.finish();/*from w ww. ja va2 s.co m*/ }
From source file:com.github.nettybook.ch7.junit.DelimiterBasedFrameDecoderTest.java
License:Apache License
@Test public void testDecoder() { String writeData = "\r\n\r\n"; String firstResponse = "\r\n"; String secondResponse = "\r\n"; DelimiterBasedFrameDecoder decoder = new DelimiterBasedFrameDecoder(8192, false, Delimiters.lineDelimiter()); EmbeddedChannel embeddedChannel = new EmbeddedChannel(decoder); ByteBuf request = Unpooled.wrappedBuffer(writeData.getBytes()); boolean result = embeddedChannel.writeInbound(request); assertTrue(result);//from w w w.j a va 2 s.co m ByteBuf response = null; response = (ByteBuf) embeddedChannel.readInbound(); assertEquals(firstResponse, response.toString(Charset.defaultCharset())); response = (ByteBuf) embeddedChannel.readInbound(); assertEquals(secondResponse, response.toString(Charset.defaultCharset())); embeddedChannel.finish(); }
From source file:com.github.sadikovi.netflowlib.NetFlowReader.java
License:Apache License
/** * [[NetFlowReader]] provides interface to get parsed header and record buffer with chosen * strategy based on columns, predicate and statistics. Metadata, header are parsed as part of * initialization.//from w w w. j a v a 2 s. c o m */ private NetFlowReader(DataInputStream inputStream, int buffer, boolean ignoreCorruptFile) throws IOException { in = inputStream; bufferLength = buffer; ignoreCorrupt = ignoreCorruptFile; byte[] metadata = null; ByteBuf buf = null; try { metadata = new byte[METADATA_LENGTH]; in.read(metadata, 0, METADATA_LENGTH); // Parse metadata, byte order does not really matter, so we go for big endian. Metadata contains // magic numbers to verify consistency of the NetFlow file, byte order encoded as either 1 or 2, // and stream version which affects header parsing (currently only 1 and 3 are supported). buf = Unpooled.wrappedBuffer(metadata).order(ByteOrder.BIG_ENDIAN); short magic1 = buf.getUnsignedByte(0); short magic2 = buf.getUnsignedByte(1); short order = buf.getUnsignedByte(2); short stream = buf.getUnsignedByte(3); // Verify consistency of NetFlow file, also this ensures that we are at the beginning of the // input stream if (magic1 != HEADER_MAGIC1 || magic2 != HEADER_MAGIC2) { throw new IOException("Corrupt NetFlow file. Wrong magic number"); } // Resolve byte order, last case corresponds to incorrect reading from buffer if (order == HEADER_BIG_ENDIAN) { byteOrder = ByteOrder.BIG_ENDIAN; } else if (order == HEADER_LITTLE_ENDIAN) { byteOrder = ByteOrder.LITTLE_ENDIAN; } else { throw new IOException("Could not recognize byte order " + order); } streamVersion = stream; // Check stream version ensureStreamVersion(); // Read header header = getHeader(); } catch (IOException err) { if (ignoreCorrupt) { // we subsume exception and log warning. Set header to null log.warn("Failed to initialize reader, ignoreCorruptFile=" + ignoreCorrupt + ", error=" + err); header = new CorruptNetFlowHeader(); } else { throw err; } } finally { metadata = null; if (buf != null) { buf.release(); buf = null; } } }
From source file:com.github.sadikovi.netflowlib.NetFlowReader.java
License:Apache License
/** Prepare header using provided input stream */ private NetFlowHeader prepareHeader() throws IOException { NetFlowHeader internalHeader;/*from w w w.ja v a 2 s .c o m*/ int numBytesRead = 0; int lenRead = 0; ByteBuf buf; byte[] headerArray; // Read header depending on stream version (different from flow version) if (streamVersion == 1) { // Version 1 has static header // TODO: verify header size for stream version 1 lenRead = NetFlowHeader.S1_HEADER_SIZE - METADATA_LENGTH; internalHeader = new NetFlowHeader(streamVersion, byteOrder); } else { // Version 3 with dynamic header size headerArray = new byte[HEADER_OFFSET_LENGTH]; numBytesRead = in.read(headerArray, 0, HEADER_OFFSET_LENGTH); if (numBytesRead != HEADER_OFFSET_LENGTH) { throw new UnsupportedOperationException("Short read while loading header offset"); } buf = Unpooled.wrappedBuffer(headerArray).order(byteOrder); int headerSize = (int) buf.getUnsignedInt(0); if (headerSize <= 0) { throw new UnsupportedOperationException("Failed to load header of size " + headerSize); } // Actual header length, determine how many bytes to read lenRead = headerSize - METADATA_LENGTH - HEADER_OFFSET_LENGTH; internalHeader = new NetFlowHeader(streamVersion, byteOrder, headerSize); } // allocate buffer for length to read headerArray = new byte[lenRead]; numBytesRead = in.read(headerArray, 0, lenRead); if (numBytesRead != lenRead) { throw new UnsupportedOperationException("Short read while loading header data"); } // build buffer buf = Unpooled.wrappedBuffer(headerArray).order(byteOrder); // resolve stream version (either 1 or 3) if (streamVersion == 1) { internalHeader.setFlowVersion((short) buf.getUnsignedShort(0)); internalHeader.setStartCapture(buf.getUnsignedInt(2)); internalHeader.setEndCapture(buf.getUnsignedInt(6)); internalHeader.setHeaderFlags(buf.getUnsignedInt(10)); internalHeader.setRotation(buf.getUnsignedInt(14)); internalHeader.setNumFlows(buf.getUnsignedInt(18)); internalHeader.setNumDropped(buf.getUnsignedInt(22)); internalHeader.setNumMisordered(buf.getUnsignedInt(26)); // Read hostname fixed bytes byte[] hostnameBytes = new byte[NetFlowHeader.S1_HEADER_HN_LEN]; buf.getBytes(30, hostnameBytes, 0, hostnameBytes.length); internalHeader.setHostname(new String(hostnameBytes)); // Read comments fixed bytes byte[] commentsBytes = new byte[NetFlowHeader.S1_HEADER_CMNT_LEN]; buf.getBytes(30 + hostnameBytes.length, commentsBytes, 0, commentsBytes.length); internalHeader.setComments(new String(commentsBytes)); // Dereference arrays hostnameBytes = null; commentsBytes = null; } else { // Resolve TLV (type-length value) // Set decode pointer to first tlv int dp = 0; int left = lenRead; // Smallest TLV is 2+2+0 (null TLV) // tlv_t - TLV type, tlv_l - TLV length, tlv_v - TLV value int tlv_t = 0; int tlv_l = 0; int tlv_v = 0; // Byte array for holding Strings byte[] pr; while (left >= 4) { // Parse type, store in host byte order tlv_t = buf.getUnsignedShort(dp); dp += 2; left -= 2; // Parse len, store in host byte order tlv_l = buf.getUnsignedShort(dp); dp += 2; left -= 2; // Parse val tlv_v = dp; // Point decode buffer at next tlv dp += tlv_l; left -= tlv_l; // TLV length check if (left < 0) { break; } switch (tlv_t) { // FT_TLV_VENDOR case 0x1: internalHeader.setVendor(buf.getUnsignedShort(tlv_v)); break; // FT_TLV_EX_VER case 0x2: internalHeader.setFlowVersion((short) buf.getUnsignedShort(tlv_v)); break; // FT_TLV_AGG_VER case 0x3: internalHeader.setAggVersion(buf.getUnsignedByte(tlv_v)); break; // FT_TLV_AGG_METHOD case 0x4: internalHeader.setAggMethod(buf.getUnsignedByte(tlv_v)); break; // FT_TLV_EXPORTER_IP case 0x5: internalHeader.setExporterIP(buf.getUnsignedInt(tlv_v)); break; // FT_TLV_CAP_START case 0x6: internalHeader.setStartCapture(buf.getUnsignedInt(tlv_v)); break; // FT_TLV_CAP_END case 0x7: internalHeader.setEndCapture(buf.getUnsignedInt(tlv_v)); break; // FT_TLV_HEADER_FLAGS case 0x8: internalHeader.setHeaderFlags(buf.getUnsignedInt(tlv_v)); break; // FT_TLV_ROT_SCHEDULE case 0x9: internalHeader.setRotation(buf.getUnsignedInt(tlv_v)); break; // FT_TLV_FLOW_COUNT case 0xA: internalHeader.setNumFlows(buf.getUnsignedInt(tlv_v)); break; // FT_TLV_FLOW_LOST case 0xB: internalHeader.setNumDropped(buf.getUnsignedInt(tlv_v)); break; // FT_TLV_FLOW_MISORDERED case 0xC: internalHeader.setNumMisordered(buf.getUnsignedInt(tlv_v)); break; // FT_TLV_PKT_CORRUPT case 0xD: internalHeader.setNumCorrupt(buf.getUnsignedInt(tlv_v)); break; // FT_TLV_SEQ_RESET case 0xE: internalHeader.setSeqReset(buf.getUnsignedInt(tlv_v)); break; // FT_TLV_CAP_HOSTNAME case 0xF: pr = new byte[tlv_l]; buf.getBytes(tlv_v, pr, 0, pr.length); // Expected null-terminated string if (pr[pr.length - 1] != 0) { throw new UnsupportedOperationException("Char sequence is not null-terminated"); } internalHeader.setHostname(new String(pr, 0, pr.length - 1)); break; // FT_TLV_COMMENTS case 0x10: pr = new byte[tlv_l]; buf.getBytes(tlv_v, pr, 0, pr.length); // Expected null-terminated string if (pr[pr.length - 1] != 0) { throw new UnsupportedOperationException("Char sequence is not null-terminated"); } internalHeader.setComments(new String(pr, 0, pr.length - 1)); break; // FT_TLV_IF_NAME case 0x11: // uint32_t, uint16_t, string: // - IP address of device // - ifIndex of interface // - interface name long ip = buf.getUnsignedInt(tlv_v); int ifIndex = buf.getUnsignedShort(tlv_v + 4); pr = new byte[tlv_l - 4 - 2]; buf.getBytes(tlv_v + 4 + 2, pr, 0, pr.length); if (pr[pr.length - 1] != 0) { throw new UnsupportedOperationException("Char sequence is not null-terminated"); } internalHeader.setInterfaceName(ip, ifIndex, new String(pr, 0, pr.length - 1)); break; // FT_TLV_IF_ALIAS case 0x12: // uint32_t, uint16_t, uint16_t, string: // - IP address of device // - ifIndex count // - ifIndex of interface (count times) // - alias name long aliasIP = buf.getUnsignedInt(tlv_v); int aliasIfIndexCnt = buf.getUnsignedShort(tlv_v + 4); int aliasIfIndex = buf.getUnsignedShort(tlv_v + 4 + 2); pr = new byte[tlv_l - 4 - 2 - 2]; buf.getBytes(tlv_v + 4 + 2 + 2, pr, 0, pr.length); if (pr[pr.length - 1] != 0) { throw new UnsupportedOperationException("Char sequence is not null-terminated"); } internalHeader.setInterfaceAlias(aliasIP, aliasIfIndexCnt, aliasIfIndex, new String(pr, 0, pr.length - 1)); break; // Case 0x0 default: break; } } if (buf != null && buf.refCnt() > 0) { buf.release(buf.refCnt()); } buf = null; pr = null; } return internalHeader; }
From source file:com.github.sparkfy.network.buffer.NioManagedBuffer.java
License:Apache License
@Override public InputStream createInputStream() throws IOException { return new ByteBufInputStream(Unpooled.wrappedBuffer(buf)); }
From source file:com.github.sparkfy.network.buffer.NioManagedBuffer.java
License:Apache License
@Override public Object convertToNetty() throws IOException { return Unpooled.wrappedBuffer(buf); }