List of usage examples for io.netty.buffer ByteBuf alloc
public abstract ByteBufAllocator alloc();
From source file:com.ogarproject.ogar.server.net.packet.Packet.java
License:Open Source License
public static String readUTF16(ByteBuf in) { in = in.order(ByteOrder.BIG_ENDIAN); ByteBuf buffer = in.alloc().buffer(); char chr;/*ww w . j a v a 2 s . co m*/ while (in.readableBytes() > 1 && (chr = in.readChar()) != 0) { buffer.writeChar(chr); } return buffer.toString(Charsets.UTF_16LE); }
From source file:com.turo.pushy.apns.server.MockApnsServerHandler.java
License:Open Source License
@Override public int onDataRead(final ChannelHandlerContext context, final int streamId, final ByteBuf data, final int padding, final boolean endOfStream) { final int bytesProcessed = data.readableBytes() + padding; final Http2Stream stream = this.connection().stream(streamId); if (stream.getProperty(this.payloadPropertyKey) == null) { stream.setProperty(this.payloadPropertyKey, data.alloc().heapBuffer(MAX_CONTENT_LENGTH)); }/*from www. j av a2s . c om*/ ((ByteBuf) stream.getProperty(this.payloadPropertyKey)).writeBytes(data); if (endOfStream) { this.handleEndOfStream(context, stream); } return bytesProcessed; }
From source file:io.atomix.cluster.messaging.impl.MessageEncoder.java
License:Apache License
private void encodeRequest(InternalRequest request, ByteBuf out) { encodeMessage(request, out);//w ww .j a v a2 s . co m final ByteBuf buf = out.alloc().buffer(ByteBufUtil.utf8MaxBytes(request.subject())); try { final int length = ByteBufUtil.writeUtf8(buf, request.subject()); // write length of message type out.writeShort(length); // write message type bytes out.writeBytes(buf); } finally { buf.release(); } }
From source file:io.datty.msgpack.core.AbstractMessageReader.java
License:Apache License
public ByteBuf readBytes(byte b, ByteBuf buffer, boolean copy) { int length = readBinaryHeader(b, buffer); if (length > buffer.readableBytes()) { throw new MessageParseException( "insufficient buffer length: " + buffer.readableBytes() + ", required length: " + length); }//w w w.j av a 2 s . co m if (copy) { ByteBuf dst = buffer.alloc().buffer(length); buffer.readBytes(dst, length); return dst; } else { ByteBuf slice = buffer.slice(buffer.readerIndex(), length); buffer.readerIndex(buffer.readerIndex() + length); return slice; } }
From source file:io.datty.msgpack.core.ValueMessageReader.java
License:Apache License
@Override public ByteBuf skipValue(ByteBuf source, boolean copy) { if (!hasNext(source)) { return null; }// ww w .ja v a 2s. c o m int startIndex = source.readerIndex(); skipValue(source); int endIndex = source.readerIndex(); int length = endIndex - startIndex; if (copy) { ByteBuf dst = source.alloc().buffer(length); source.getBytes(startIndex, dst, length); return dst; } else { return source.slice(startIndex, length); } }
From source file:io.datty.msgpack.core.ValueMessageReader.java
License:Apache License
public ByteBuf readBinary(ByteBuf source, boolean copy) { int length = readBinaryHeader(source); if (length > source.readableBytes()) { throw new MessageParseException( "insufficient buffer length: " + source.readableBytes() + ", required length: " + length); }//w w w . ja v a 2s .c o m if (copy) { ByteBuf dst = source.alloc().buffer(length); source.readBytes(dst, length); return dst; } else { ByteBuf slice = source.slice(source.readerIndex(), length); source.skipBytes(length); return slice; } }
From source file:io.datty.msgpack.test.CompositeByteBufTest.java
License:Apache License
@Test public void testTwoComponents() { ByteBuf first = Unpooled.wrappedBuffer("a".getBytes()); ByteBuf second = Unpooled.wrappedBuffer("b".getBytes()); CompositeByteBuf result = first.alloc().compositeBuffer(); result.addComponent(true, first);/*from ww w. j a va 2 s . c o m*/ result.addComponent(true, second); byte[] actual = ByteBufUtil.getBytes(result); Assert.assertEquals(2, actual.length); Assert.assertEquals('a', actual[0]); Assert.assertEquals('b', actual[1]); }
From source file:io.datty.msgpack.test.CompositeByteBufTest.java
License:Apache License
@Test public void testCompositeWrite() { ByteBuf first = Unpooled.buffer(); first.writeByte('a'); CompositeByteBuf result = first.alloc().compositeBuffer(); result.addComponent(true, first);// w w w.j a v a 2 s. c om result.writeByte('b'); byte[] actual = ByteBufUtil.getBytes(result); Assert.assertEquals(2, actual.length); Assert.assertEquals('a', actual[0]); Assert.assertEquals('b', actual[1]); }
From source file:io.gatling.http.client.body.multipart.impl.MessageEndPartImpl.java
License:Apache License
@Override public void copyInto(ByteBuf target) { copyInto(lazyLoadContentBuffer(target.alloc()), target, PartImplState.DONE); }
From source file:io.gatling.netty.util.ahc.ByteBufUtils.java
License:Apache License
private static CharBuffer decode(ByteBuf src, Charset charset) { int readerIndex = src.readerIndex(); int len = src.readableBytes(); final CharsetDecoder decoder = CharsetUtil.decoder(charset); CharBuffer dst = pooledCharBuffer(len, decoder); if (src.nioBufferCount() == 1) { // Use internalNioBuffer(...) to reduce object creation. // BEWARE: NOT THREAD-SAFE decode(decoder, src.internalNioBuffer(readerIndex, len), dst); } else {/*from w w w . j a v a 2 s. co m*/ // We use a heap buffer as CharsetDecoder is most likely able to use a fast-path if src and dst buffers // are both backed by a byte array. ByteBuf buffer = src.alloc().heapBuffer(len); try { buffer.writeBytes(src, readerIndex, len); // Use internalNioBuffer(...) to reduce object creation. decode(decoder, buffer.internalNioBuffer(buffer.readerIndex(), len), dst); } finally { // Release the temporary buffer again. buffer.release(); } } dst.flip(); return dst; }