List of usage examples for io.netty.buffer Unpooled copiedBuffer
public static ByteBuf copiedBuffer(ByteBuffer... buffers)
From source file:com.xing.netty.client.DiscardClient.java
License:Apache License
public static void main(String[] args) throws Exception { // Configure SSL. final SslContext sslCtx; if (SSL) {/* w w w. ja v a2 s. c o m*/ sslCtx = SslContext.newClientContext(InsecureTrustManagerFactory.INSTANCE); } else { sslCtx = null; } EventLoopGroup group = new NioEventLoopGroup(); try { Bootstrap b = new Bootstrap(); b.group(group).channel(NioSocketChannel.class).option(ChannelOption.TCP_NODELAY, true) .handler(new ChannelInitializer<Channel>() { @Override protected void initChannel(Channel ch) throws Exception { ChannelPipeline p = ch.pipeline(); // if (sslCtx != null) { // p.addLast(sslCtx.newHandler(ch.alloc(), HOST, // PORT)); // } // p.addLast(new DiscardClientHandler()); ByteBuf delimiter = Unpooled.copiedBuffer("$_".getBytes()); p.addLast(new DelimiterBasedFrameDecoder(1024, delimiter)); // p.addLast(new LineBasedFrameDecoder(1024)); p.addLast(new StringDecoder()); p.addLast(new TimeClientHandler()); } }); // Make the connection attempt. ChannelFuture f = b.connect(HOST, PORT).sync(); // Wait until the connection is closed. f.channel().closeFuture().sync(); } catch (Exception e) { System.out.println(e); } finally { group.shutdownGracefully(); } }
From source file:com.yahoo.pulsar.broker.service.ServerCnxTest.java
License:Apache License
@Test(timeOut = 30000) public void testSendCommand() throws Exception { resetChannel();/*from w w w . j a v a 2 s . c o m*/ setChannelConnected(); ByteBuf clientCommand = Commands.newProducer(successTopicName, 1 /* producer id */, 1 /* request id */, "prod-name"); channel.writeInbound(clientCommand); assertTrue(getResponse() instanceof CommandProducerSuccess); // test SEND success MessageMetadata messageMetadata = MessageMetadata.newBuilder().setPublishTime(System.currentTimeMillis()) .setProducerName("prod-name").setSequenceId(0).build(); ByteBuf data = Unpooled.buffer(1024); clientCommand = Commands.newSend(1, 0, 1, ChecksumType.None, messageMetadata, data); channel.writeInbound(Unpooled.copiedBuffer(clientCommand)); clientCommand.release(); assertTrue(getResponse() instanceof CommandSendReceipt); channel.finish(); }
From source file:com.yahoo.pulsar.client.impl.MessageImpl.java
License:Apache License
MessageImpl(MessageIdData messageId, MessageMetadata msgMetadata, ByteBuf payload, int partitionIndex, ClientCnx cnx) {// w ww . j a va2s. c o m this.msgMetadataBuilder = MessageMetadata.newBuilder(msgMetadata); this.messageId = new MessageIdImpl(messageId.getLedgerId(), messageId.getEntryId(), partitionIndex); this.cnx = cnx; // Need to make a copy since the passed payload is using a ref-count buffer that we don't know when could // release, since the Message is passed to the user. Also, the passed ByteBuf is coming from network and is // backed by a direct buffer which we could not expose as a byte[] this.payload = Unpooled.copiedBuffer(payload); if (msgMetadata.getPropertiesCount() > 0) { Map<String, String> properties = Maps.newTreeMap(); for (KeyValue entry : msgMetadata.getPropertiesList()) { properties.put(entry.getKey(), entry.getValue()); } this.properties = Collections.unmodifiableMap(properties); } else { properties = Collections.emptyMap(); } }
From source file:com.yahoo.pulsar.client.impl.MessageImpl.java
License:Apache License
MessageImpl(BatchMessageIdImpl batchMessageIdImpl, MessageMetadata msgMetadata, PulsarApi.SingleMessageMetadata singleMessageMetadata, ByteBuf payload, ClientCnx cnx) { this.msgMetadataBuilder = MessageMetadata.newBuilder(msgMetadata); this.messageId = batchMessageIdImpl; this.cnx = cnx; this.payload = Unpooled.copiedBuffer(payload); if (singleMessageMetadata.getPropertiesCount() > 0) { Map<String, String> properties = Maps.newTreeMap(); for (KeyValue entry : singleMessageMetadata.getPropertiesList()) { properties.put(entry.getKey(), entry.getValue()); }/*from w ww . j a v a 2 s.c om*/ this.properties = Collections.unmodifiableMap(properties); } else { properties = Collections.emptyMap(); } }
From source file:com.yeetor.androidcontrol.client.LocalClient.java
License:Open Source License
private void sendImage(byte[] data) { if (protocol != null) { System.out.println("thread:" + Thread.currentThread().getId()); protocol.getBroswerSocket().channel() .writeAndFlush(new BinaryWebSocketFrame(Unpooled.copiedBuffer(data))); }/*from w w w . ja v a 2s . c o m*/ }
From source file:com.yeetor.server.WSServer.java
License:Open Source License
private void sendImage(byte[] data) { byte[] head = new byte[2]; head[0] = (BinaryProtocol.Header.SM_JPG) & 0xff; head[1] = (BinaryProtocol.Header.SM_JPG >> 8) & 0xff; int len = data.length; byte[] lenbuf = new byte[4]; lenbuf[0] = (byte) ((len) & 0xff); lenbuf[1] = (byte) ((len >> 8) & 0xff); lenbuf[2] = (byte) ((len >> 16) & 0xff); lenbuf[3] = (byte) ((len >> 24) & 0xff); byte[] d = ArrayUtils.addAll(ArrayUtils.addAll(head, lenbuf), data); channel.writeAndFlush(new BinaryWebSocketFrame(Unpooled.copiedBuffer(d))); }
From source file:com.zextras.modules.chat.server.xmpp.netty.StanzaWriterImp.java
License:Open Source License
@Override public void onEventQueued(EventQueue eventQueue) { try {/*from w w w. ja va 2 s . com*/ final ByteArrayOutputStream out = new ByteArrayOutputStream(4096); for (Event event : eventQueue.popAllEvents()) { LogContext logContext = CurrentLogContext.begin(); if (event.getTarget().getAddresses().size() == 1) { logContext.setAccountName(event.getTarget().toSingleAddress()); } logContext.freeze(); try { out.reset(); XmppEncoder encoder = (XmppEncoder) event.interpret(mEncoderFactory); if (encoder == null) { ChatLog.log.debug("No encoder found for event " + event.getClass().getName()); continue; } final SpecificAddress exposedAddress = mXmppConnectionHandler.getSession().getExposedAddress(); encoder.encode(out, exposedAddress); String stanzaDebug = new String(out.toByteArray(), "UTF-8"); ChatLog.log.debug("writing: " + event.getClass().getName()); ChatLog.log.debug("writing stanza(" + stanzaDebug.length() + "): " + stanzaDebug); ByteBuf stanza = Unpooled.copiedBuffer(out.toByteArray()); ChannelFuture writeFuture = mXmppConnectionHandler.write(stanza); final Event eventToNotify = event; writeFuture.addListener(new ChannelFutureListener() { @Override public void operationComplete(ChannelFuture future) throws Exception { if (future.isSuccess()) { EventInterceptor interceptor = eventToNotify .interpret(mXmppEventInterceptorFactory); interceptor.intercept(mEventManager, exposedAddress); } } }); } finally { CurrentLogContext.end(); } } } catch (Throwable ex) { ChatLog.log.warn("Exception: " + Utils.exceptionToString(ex)); } }
From source file:com.zextras.modules.chat.server.xmpp.StartTLSHandler.java
License:Open Source License
@Override public List<ChatOperation> handle() { if (mConnectionStatus.getSession().isUsingSSL()) { ChatLog.log.info("Requested TLS initialization when TLS is already initialized"); } else {//w w w.ja va2 s . c o m try { ByteBuf buffer = Unpooled .copiedBuffer("<proceed xmlns=\"urn:ietf:params:xml:ns:xmpp-tls\"/>".getBytes("UTF-8")); mConnectionStatus.startTLS(buffer); } catch (UnsupportedEncodingException ex) { throw new RuntimeException(); } } return Collections.emptyList(); }
From source file:com.zhang.client.NettyHttpRequest.java
License:Apache License
public NettyHttpRequest content(byte[] content) { if (null == content) { throw new NullPointerException("content"); }/*ww w . java 2 s . c o m*/ this.content = Unpooled.copiedBuffer(content); return this; }
From source file:com.zhaopeng.timeserver.netty.delimiter.EchoClient.java
License:Apache License
public void connect(int port, String host) throws Exception { // ?NIO// www. j a v a 2 s.c o m EventLoopGroup group = new NioEventLoopGroup(); try { Bootstrap b = new Bootstrap(); b.group(group).channel(NioSocketChannel.class).option(ChannelOption.TCP_NODELAY, true) .handler(new ChannelInitializer<SocketChannel>() { @Override public void initChannel(SocketChannel ch) throws Exception { ByteBuf delimiter = Unpooled.copiedBuffer("$_".getBytes()); ch.pipeline().addLast(new DelimiterBasedFrameDecoder(1024, delimiter)); ch.pipeline().addLast(new StringDecoder()); ch.pipeline().addLast(new EchoClientHandler()); } }); // ?? ChannelFuture f = b.connect(host, port).sync(); System.out.println(f); // f.channel().closeFuture().sync(); } finally { // NIO group.shutdownGracefully(); } }