List of usage examples for io.netty.buffer ByteBuf writeBytes
public abstract ByteBuf writeBytes(ByteBuffer src);
From source file:com.gemstone.gemfire.internal.redis.Coder.java
License:Apache License
public static ByteBuf getBulkStringArrayResponseOfValues(ByteBufAllocator alloc, Collection<?> items) { Iterator<?> it = items.iterator(); ByteBuf response = alloc.buffer();// w w w . jav a2 s .co m response.writeByte(Coder.ARRAY_ID); ByteBuf tmp = alloc.buffer(); int size = 0; while (it.hasNext()) { Object next = it.next(); ByteArrayWrapper nextWrapper = null; if (next instanceof Entry) { try { nextWrapper = (ByteArrayWrapper) ((Entry<?, ?>) next).getValue(); } catch (EntryDestroyedException e) { continue; } } else if (next instanceof Struct) { nextWrapper = (ByteArrayWrapper) ((Struct) next).getFieldValues()[1]; } if (nextWrapper != null) { tmp.writeByte(Coder.BULK_STRING_ID); tmp.writeBytes(intToBytes(nextWrapper.length())); tmp.writeBytes(Coder.CRLFar); tmp.writeBytes(nextWrapper.toBytes()); tmp.writeBytes(Coder.CRLFar); } else { tmp.writeBytes(Coder.bNIL); } size++; } response.writeBytes(intToBytes(size)); response.writeBytes(Coder.CRLFar); response.writeBytes(tmp); tmp.release(); return response; }
From source file:com.gemstone.gemfire.internal.redis.Coder.java
License:Apache License
public static ByteBuf zRangeResponse(ByteBufAllocator alloc, Collection<?> list, boolean withScores) { if (list.isEmpty()) return Coder.getEmptyArrayResponse(alloc); ByteBuf buffer = alloc.buffer();/* ww w .ja v a 2s .c o m*/ buffer.writeByte(Coder.ARRAY_ID); ByteBuf tmp = alloc.buffer(); int size = 0; for (Object entry : list) { ByteArrayWrapper key; DoubleWrapper score; if (entry instanceof Entry) { try { key = (ByteArrayWrapper) ((Entry<?, ?>) entry).getKey(); score = (DoubleWrapper) ((Entry<?, ?>) entry).getValue(); } catch (EntryDestroyedException e) { continue; } } else { Object[] fieldVals = ((Struct) entry).getFieldValues(); key = (ByteArrayWrapper) fieldVals[0]; score = (DoubleWrapper) fieldVals[1]; } byte[] byteAr = key.toBytes(); tmp.writeByte(Coder.BULK_STRING_ID); tmp.writeBytes(intToBytes(byteAr.length)); tmp.writeBytes(Coder.CRLFar); tmp.writeBytes(byteAr); tmp.writeBytes(Coder.CRLFar); size++; if (withScores) { String scoreString = score.toString(); byte[] scoreAr = stringToBytes(scoreString); tmp.writeByte(Coder.BULK_STRING_ID); tmp.writeBytes(intToBytes(scoreString.length())); tmp.writeBytes(Coder.CRLFar); tmp.writeBytes(scoreAr); tmp.writeBytes(Coder.CRLFar); size++; } } buffer.writeBytes(intToBytes(size)); buffer.writeBytes(Coder.CRLFar); buffer.writeBytes(tmp); tmp.release(); return buffer; }
From source file:com.gemstone.gemfire.internal.redis.Coder.java
License:Apache License
public static ByteBuf getArrayOfNils(ByteBufAllocator alloc, int length) { ByteBuf response = alloc.buffer(); response.writeByte(Coder.ARRAY_ID);/*from w w w .j a v a 2 s . c o m*/ response.writeBytes(intToBytes(length)); response.writeBytes(Coder.CRLFar); for (int i = 0; i < length; i++) response.writeBytes(bNIL); return response; }
From source file:com.gemstone.gemfire.internal.redis.executor.sortedset.ZRangeByLexExecutor.java
License:Apache License
private final ByteBuf getCustomBulkStringArrayResponse(Collection<ByteArrayWrapper> items, ExecutionHandlerContext context) { Iterator<ByteArrayWrapper> it = items.iterator(); ByteBuf response = context.getByteBufAllocator().buffer(); response.writeByte(Coder.ARRAY_ID);/*from w w w .j a v a 2 s .com*/ response.writeBytes(Coder.intToBytes(items.size())); response.writeBytes(Coder.CRLFar); while (it.hasNext()) { ByteArrayWrapper next = it.next(); byte[] byteAr = next.toBytes(); response.writeByte(Coder.BULK_STRING_ID); response.writeBytes(Coder.intToBytes(byteAr.length)); response.writeBytes(Coder.CRLFar); response.writeBytes(byteAr); response.writeBytes(Coder.CRLFar); } return response; }
From source file:com.gemstone.gemfire.internal.redis.executor.TimeExecutor.java
License:Apache License
@Override public void executeCommand(Command command, ExecutionHandlerContext context) { long timeStamp = System.currentTimeMillis(); long seconds = timeStamp / 1000; long microSeconds = (timeStamp - (seconds * 1000)) * 1000; byte[] secAr = Coder.longToBytes(seconds); byte[] micAr = Coder.longToBytes(microSeconds); ByteBuf response = context.getByteBufAllocator().buffer(50); response.writeByte(Coder.ARRAY_ID);/* w w w . jav a 2 s.co m*/ response.writeByte(50); // #2 response.writeBytes(Coder.CRLFar); response.writeByte(Coder.BULK_STRING_ID); response.writeBytes(Coder.intToBytes(secAr.length)); response.writeBytes(Coder.CRLFar); response.writeBytes(secAr); response.writeBytes(Coder.CRLFar); response.writeByte(Coder.BULK_STRING_ID); response.writeBytes(Coder.intToBytes(micAr.length)); response.writeBytes(Coder.CRLFar); response.writeBytes(micAr); response.writeBytes(Coder.CRLFar); command.setResponse(response); }
From source file:com.gemstone.gemfire.internal.redis.executor.transactions.ExecExecutor.java
License:Apache License
private ByteBuf constructResponseExec(ExecutionHandlerContext context) { Queue<Command> cQ = context.getTransactionQueue(); ByteBuf response = context.getByteBufAllocator().buffer(); response.writeByte(Coder.ARRAY_ID);// w w w .jav a 2 s . c o m response.writeBytes(Coder.intToBytes(cQ.size())); response.writeBytes(Coder.CRLFar); for (Command c : cQ) { ByteBuf r = c.getResponse(); response.writeBytes(r); } return response; }
From source file:com.github.gregwhitaker.requestreply.Client.java
License:Apache License
public void start() throws Exception { Publisher<ClientTcpDuplexConnection> publisher = ClientTcpDuplexConnection.create(remoteAddress, new NioEventLoopGroup(1)); ClientTcpDuplexConnection duplexConnection = RxReactiveStreams.toObservable(publisher).toBlocking().last(); ReactiveSocket reactiveSocket = DefaultReactiveSocket.fromClientConnection(duplexConnection, ConnectionSetupPayload.create("UTF-8", "UTF-8"), t -> t.printStackTrace()); reactiveSocket.startAndWait();//from w w w .j a v a2s . co m // Create an observable that emits messages at a specific interval. Publisher<Payload> requestStream = RxReactiveStreams.toPublisher( Observable.interval(1_000, TimeUnit.MILLISECONDS).onBackpressureDrop().map(i -> new Payload() { @Override public ByteBuffer getData() { return ByteBuffer.wrap(("YO " + i).getBytes()); } @Override public ByteBuffer getMetadata() { return Frame.NULL_BYTEBUFFER; } })); final CountDownLatch latch = new CountDownLatch(Integer.MAX_VALUE); requestStream.subscribe(new Subscriber<Payload>() { @Override public void onSubscribe(Subscription s) { s.request(Long.MAX_VALUE); } @Override public void onNext(Payload payload) { ByteBuf buffer = Unpooled.buffer(payload.getData().capacity()); buffer.writeBytes(payload.getData()); byte[] bytes = new byte[buffer.capacity()]; buffer.readBytes(bytes); System.out.println("Client Sent: " + new String(bytes)); reactiveSocket.requestResponse(payload).subscribe(new Subscriber<Payload>() { @Override public void onSubscribe(Subscription s) { s.request(Long.MAX_VALUE); } @Override public void onNext(Payload payload) { ByteBuf buffer = Unpooled.buffer(payload.getData().capacity()); buffer.writeBytes(payload.getData()); byte[] bytes = new byte[buffer.capacity()]; buffer.readBytes(bytes); System.out.println("Client Received: " + new String(bytes)); latch.countDown(); } @Override public void onError(Throwable t) { latch.countDown(); } @Override public void onComplete() { latch.countDown(); } }); } @Override public void onError(Throwable t) { } @Override public void onComplete() { } }); latch.await(); System.exit(0); }
From source file:com.github.jrialland.ajpclient.impl.ForwardImpl.java
License:Apache License
protected static void sendRequest(final Channel channel, final ForwardRequest request) throws IOException { // start by writing message payload, header will be appended afterwards final ByteArrayOutputStream baos = new ByteArrayOutputStream(MAX_MESSAGE_SIZE); final DataOutputStream tmp = new DataOutputStream(baos); // request type tmp.writeByte(PREFIX_FORWARD_REQUEST); // payload/*from w ww . ja v a2 s .c o m*/ tmp.writeByte(request.getMethod().getCode()); writeString(request.getProtocol(), tmp); writeString(request.getRequestUri(), tmp); writeString(request.getRemoteAddress(), tmp); writeString(request.getRemoteHost(), tmp); writeString(request.getServerName(), tmp); tmp.writeShort(request.getServerPort()); tmp.writeBoolean(request.isSsl()); tmp.writeShort(request.getHeaders().size()); // headers for (final Header header : request.getHeaders()) { final Integer code = RequestHeader.getKeyCode(header.getKey()); if (code == null) { writeString(header.getKey(), tmp); } else { tmp.writeShort(code); } writeString(header.getValue(), tmp); } // attributes for (final Entry<Attribute, String> attr : request.getAttributes().entrySet()) { tmp.writeByte(attr.getKey().getCode()); writeString(attr.getValue(), tmp); } // request terminator tmp.write(REQUEST_TERMINATOR); tmp.flush(); // now prepare the whole message final byte[] data = baos.toByteArray(); if (data.length + 4 > MAX_MESSAGE_SIZE) { throw new IllegalArgumentException("Message size is larger than " + MAX_MESSAGE_SIZE + " bytes."); } final ByteBuf buf = Buffers.makeBuffer(4 + data.length); buf.writeBytes(CLIENT_MAGIC); buf.writeShort(data.length); buf.writeBytes(data); channel.writeAndFlush(buf); getLog().debug( "Sent : FORWARDREQUEST (" + PREFIX_FORWARD_REQUEST + "), payload size = " + data.length + " bytes"); final InputStream requestBody = request.getRequestBody(); if (requestBody != null) { sendChunk(true, requestBody, MAX_SEND_CHUNK_SIZE, channel); } }
From source file:com.github.lburgazzoli.quickfixj.transport.netty.codec.NettyMessageEncoder.java
License:Apache License
/** * * @param ctx// ww w . j av a2s . c o m * @param msg * @param out * @return * @throws Exception */ @Override protected void encode(ChannelHandlerContext ctx, byte[] msg, ByteBuf out) throws Exception { if (msg.length != 0) { out.writeBytes(msg); } }
From source file:com.github.milenkovicm.kafka.CrcTest.java
License:Apache License
@Test public void test_crc6() { ByteBuf buf = getByteBuf(4090);/*from w w w . java 2 s . c o m*/ long crc = calculateCrc(buf); ByteBuf buf1 = getByteBuf(18); buf1.writeBytes(buf); System.out.println(crc); Assert.assertEquals(crc, calculateJCrc(buf1, 18)); }