Java Code Examples for io.netty.buffer.ByteBufAllocator#directBuffer()
The following examples show how to use
io.netty.buffer.ByteBufAllocator#directBuffer() .
You can vote up the ones you like or vote down the ones you don't like,
and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: DirectByteBufInStringOutPayloadTest.java From x-pipe with Apache License 2.0 | 6 votes |
@Test public void testContinuouslyInput() throws Exception { DirectByteBufInStringOutPayload payload = new DirectByteBufInStringOutPayload(); StringBuilder randomStr = new StringBuilder(); payload.startInput(); ByteBufAllocator allocator = new PooledByteBufAllocator(); for(int i = 0; i < 100; i ++) { String delta = randomString(100); ByteBuf byteBuf = allocator.directBuffer(delta.length()); byteBuf.writeBytes(delta.getBytes()); randomStr.append(delta); payload.in(byteBuf); // byteBuf.release(); } payload.endInput(); Assert.assertEquals(randomStr.toString(), payload.toString()); sleep(5000); }
Example 2
Source File: UnixChannelUtilTest.java From netty-4.1.22 with Apache License 2.0 | 6 votes |
private static void testIsBufferCopyNeededForWrite(ByteBufAllocator alloc) { ByteBuf byteBuf = alloc.directBuffer(); assertFalse(isBufferCopyNeededForWrite(byteBuf, IOV_MAX)); assertFalse(isBufferCopyNeededForWrite(byteBuf.asReadOnly(), IOV_MAX)); assertTrue(byteBuf.release()); byteBuf = alloc.heapBuffer(); assertTrue(isBufferCopyNeededForWrite(byteBuf, IOV_MAX)); assertTrue(isBufferCopyNeededForWrite(byteBuf.asReadOnly(), IOV_MAX)); assertTrue(byteBuf.release()); assertCompositeByteBufIsBufferCopyNeededForWrite(alloc, 2, 0, false); assertCompositeByteBufIsBufferCopyNeededForWrite(alloc, IOV_MAX + 1, 0, true); assertCompositeByteBufIsBufferCopyNeededForWrite(alloc, 0, 2, true); assertCompositeByteBufIsBufferCopyNeededForWrite(alloc, 1, 1, true); }
Example 3
Source File: ServletOutputStream.java From spring-boot-protocol with Apache License 2.0 | 5 votes |
/** * Allocation buffer * @param allocator allocator distributor * @param len The required byte length * @return ByteBuf */ protected ByteBuf allocByteBuf(ByteBufAllocator allocator, int len){ ByteBuf ioByteBuf; if(len > responseWriterChunkMaxHeapByteLength){ if(PlatformDependent.usedDirectMemory() + len >= PlatformDependent.maxDirectMemory() * 0.8F){ ioByteBuf = allocator.heapBuffer(len); }else { ioByteBuf = allocator.directBuffer(len); } }else { ioByteBuf = allocator.heapBuffer(len); } return ioByteBuf; }
Example 4
Source File: PemPrivateKey.java From netty-4.1.22 with Apache License 2.0 | 5 votes |
/** * Creates a {@link PemEncoded} value from the {@link PrivateKey}. */ static PemEncoded toPEM(ByteBufAllocator allocator, boolean useDirect, PrivateKey key) { // We can take a shortcut if the private key happens to be already // PEM/PKCS#8 encoded. This is the ideal case and reason why all // this exists. It allows the user to pass pre-encoded bytes straight // into OpenSSL without having to do any of the extra work. if (key instanceof PemEncoded) { return ((PemEncoded) key).retain(); } ByteBuf encoded = Unpooled.wrappedBuffer(key.getEncoded()); try { ByteBuf base64 = SslUtils.toBase64(allocator, encoded); try { int size = BEGIN_PRIVATE_KEY.length + base64.readableBytes() + END_PRIVATE_KEY.length; boolean success = false; final ByteBuf pem = useDirect ? allocator.directBuffer(size) : allocator.buffer(size); try { pem.writeBytes(BEGIN_PRIVATE_KEY); pem.writeBytes(base64); pem.writeBytes(END_PRIVATE_KEY); PemValue value = new PemValue(pem, true); success = true; return value; } finally { // Make sure we never leak that PEM ByteBuf if there's an Exception. if (!success) { SslUtils.zerooutAndRelease(pem); } } } finally { SslUtils.zerooutAndRelease(base64); } } finally { SslUtils.zerooutAndRelease(encoded); } }
Example 5
Source File: SslHandler.java From netty-4.1.22 with Apache License 2.0 | 5 votes |
@Override protected ByteBuf composeFirst(ByteBufAllocator allocator, ByteBuf first) { if (first instanceof CompositeByteBuf) { CompositeByteBuf composite = (CompositeByteBuf) first; first = allocator.directBuffer(composite.readableBytes()); try { first.writeBytes(composite); } catch (Throwable cause) { first.release(); PlatformDependent.throwException(cause); } composite.release(); } return first; }
Example 6
Source File: SslHandler.java From netty4.0.27Learn with Apache License 2.0 | 5 votes |
/** * Always prefer a direct buffer when it's pooled, so that we reduce the number of memory copies * in {@link OpenSslEngine}. */ private ByteBuf allocate(ChannelHandlerContext ctx, int capacity) { ByteBufAllocator alloc = ctx.alloc(); if (wantsDirectBuffer) { return alloc.directBuffer(capacity); } else { return alloc.buffer(capacity); } }
Example 7
Source File: SslHandler.java From netty4.0.27Learn with Apache License 2.0 | 4 votes |
private SSLEngineResult wrap(ByteBufAllocator alloc, SSLEngine engine, ByteBuf in, ByteBuf out) throws SSLException { ByteBuf newDirectIn = null; try { int readerIndex = in.readerIndex(); int readableBytes = in.readableBytes(); // We will call SslEngine.wrap(ByteBuffer[], ByteBuffer) to allow efficient handling of // CompositeByteBuf without force an extra memory copy when CompositeByteBuffer.nioBuffer() is called. final ByteBuffer[] in0; if (in.isDirect() || !wantsDirectBuffer) { // As CompositeByteBuf.nioBufferCount() can be expensive (as it needs to check all composed ByteBuf // to calculate the count) we will just assume a CompositeByteBuf contains more then 1 ByteBuf. // The worst that can happen is that we allocate an extra ByteBuffer[] in CompositeByteBuf.nioBuffers() // which is better then walking the composed ByteBuf in most cases. if (!(in instanceof CompositeByteBuf) && in.nioBufferCount() == 1) { in0 = singleBuffer; // We know its only backed by 1 ByteBuffer so use internalNioBuffer to keep object allocation // to a minimum. in0[0] = in.internalNioBuffer(readerIndex, readableBytes); } else { in0 = in.nioBuffers(); } } else { // We could even go further here and check if its a CompositeByteBuf and if so try to decompose it and // only replace the ByteBuffer that are not direct. At the moment we just will replace the whole // CompositeByteBuf to keep the complexity to a minimum newDirectIn = alloc.directBuffer(readableBytes); newDirectIn.writeBytes(in, readerIndex, readableBytes); in0 = singleBuffer; in0[0] = newDirectIn.internalNioBuffer(0, readableBytes); } for (;;) { ByteBuffer out0 = out.nioBuffer(out.writerIndex(), out.writableBytes()); SSLEngineResult result = engine.wrap(in0, out0); in.skipBytes(result.bytesConsumed()); out.writerIndex(out.writerIndex() + result.bytesProduced()); switch (result.getStatus()) { case BUFFER_OVERFLOW: out.ensureWritable(maxPacketBufferSize); break; default: return result; } } } finally { // Null out to allow GC of ByteBuffer singleBuffer[0] = null; if (newDirectIn != null) { newDirectIn.release(); } } }
Example 8
Source File: AltsTsiFrameProtector.java From grpc-java with Apache License 2.0 | 4 votes |
Unprotector(ChannelCrypterNetty crypter, ByteBufAllocator alloc) { this.crypter = crypter; this.suffixBytes = crypter.getSuffixLength(); this.header = alloc.directBuffer(HEADER_BYTES); this.firstFrameTag = alloc.directBuffer(suffixBytes); }
Example 9
Source File: KryoByteBufTest.java From turbo-rpc with Apache License 2.0 | 4 votes |
public static void main(String[] args) { Kryo kryo = new Kryo(); // kryo.setWarnUnregisteredClasses(true); // kryo.setReferences(false); kryo.register(IntObj.class); kryo.register(StringUTF8Obj.class); kryo.register(LocalDateTimeObj.class); kryo.register(ComplexObj.class); kryo.register(int[].class); kryo.register(ArrayList.class); kryo.register(ArrayListObj.class); ByteBufAllocator allocator = new PooledByteBufAllocator(true); ByteBuf buffer = allocator.directBuffer(2, 1024 * 1024 * 8); System.out.println("buffer.nioBufferCount: " + buffer.nioBufferCount()); System.out.println(buffer.nioBuffer(0, buffer.capacity())); ByteBufOutput output = new ByteBufOutput(buffer); UserService userService = new UserServiceServerImpl(); User user = userService.getUser(Long.MAX_VALUE).join(); while(true) { buffer.clear(); output.setBuffer(buffer); kryo.writeObject(output, user); ByteBufInput input = new ByteBufInput(buffer); User u = kryo.readObject(input, User.class); System.out.println(u); } // buffer.clear(); // output.setBuffer(buffer); // kryo.writeObject(output, user); // System.out.println("user writeObject: " + output.total()); // // ByteBufInput input = new ByteBufInput(buffer); // System.out.println("user readableBytes: " + buffer.readableBytes()); // System.out.println("user readerIndex: " + buffer.readerIndex()); // System.out.println(kryo.readObject(input, User.class)); }
Example 10
Source File: AbstractEpollChannel.java From netty-4.1.22 with Apache License 2.0 | 4 votes |
private static ByteBuf newDirectBuffer0(Object holder, ByteBuf buf, ByteBufAllocator alloc, int capacity) { final ByteBuf directBuf = alloc.directBuffer(capacity); directBuf.writeBytes(buf, buf.readerIndex(), capacity); ReferenceCountUtil.safeRelease(holder); return directBuf; }
Example 11
Source File: AbstractKQueueChannel.java From netty-4.1.22 with Apache License 2.0 | 4 votes |
private static ByteBuf newDirectBuffer0(Object holder, ByteBuf buf, ByteBufAllocator alloc, int capacity) { final ByteBuf directBuf = alloc.directBuffer(capacity); directBuf.writeBytes(buf, buf.readerIndex(), capacity); ReferenceCountUtil.safeRelease(holder); return directBuf; }
Example 12
Source File: AltsTsiFrameProtector.java From grpc-nebula-java with Apache License 2.0 | 4 votes |
@SuppressWarnings("BetaApi") // verify is stable in Guava private ByteBuf handleUnprotected(List<ByteBuf> unprotectedBufs, ByteBufAllocator alloc) throws GeneralSecurityException { long unprotectedBytes = 0; for (ByteBuf buf : unprotectedBufs) { unprotectedBytes += buf.readableBytes(); } // Empty plaintext not allowed since this should be handled as no-op in layer above. checkArgument(unprotectedBytes > 0); // Compute number of frames and allocate a single buffer for all frames. long frameNum = unprotectedBytes / maxUnprotectedBytesPerFrame + 1; int lastFrameUnprotectedBytes = (int) (unprotectedBytes % maxUnprotectedBytesPerFrame); if (lastFrameUnprotectedBytes == 0) { frameNum--; lastFrameUnprotectedBytes = maxUnprotectedBytesPerFrame; } long protectedBytes = frameNum * (HEADER_BYTES + suffixBytes) + unprotectedBytes; ByteBuf protectedBuf = alloc.directBuffer(Ints.checkedCast(protectedBytes)); try { int bufferIdx = 0; for (int frameIdx = 0; frameIdx < frameNum; ++frameIdx) { int unprotectedBytesLeft = (frameIdx == frameNum - 1) ? lastFrameUnprotectedBytes : maxUnprotectedBytesPerFrame; // Write header (at most LIMIT_MAX_ALLOWED_FRAME_BYTES). protectedBuf.writeIntLE(unprotectedBytesLeft + HEADER_TYPE_FIELD_BYTES + suffixBytes); protectedBuf.writeIntLE(HEADER_TYPE_DEFAULT); // Ownership of the backing buffer remains with protectedBuf. ByteBuf frameOut = writeSlice(protectedBuf, unprotectedBytesLeft + suffixBytes); List<ByteBuf> framePlain = new ArrayList<>(); while (unprotectedBytesLeft > 0) { // Ownership of the buffer backing in remains with unprotectedBufs. ByteBuf in = unprotectedBufs.get(bufferIdx); if (in.readableBytes() <= unprotectedBytesLeft) { // The complete buffer belongs to this frame. framePlain.add(in); unprotectedBytesLeft -= in.readableBytes(); bufferIdx++; } else { // The remainder of in will be part of the next frame. framePlain.add(in.readSlice(unprotectedBytesLeft)); unprotectedBytesLeft = 0; } } crypter.encrypt(frameOut, framePlain); verify(!frameOut.isWritable()); } protectedBuf.readerIndex(0); protectedBuf.writerIndex(protectedBuf.capacity()); return protectedBuf.retain(); } finally { protectedBuf.release(); } }
Example 13
Source File: AltsTsiFrameProtector.java From grpc-java with Apache License 2.0 | 4 votes |
private ByteBuf handleUnprotected(List<ByteBuf> unprotectedBufs, ByteBufAllocator alloc) throws GeneralSecurityException { long unprotectedBytes = 0; for (ByteBuf buf : unprotectedBufs) { unprotectedBytes += buf.readableBytes(); } // Empty plaintext not allowed since this should be handled as no-op in layer above. checkArgument(unprotectedBytes > 0); // Compute number of frames and allocate a single buffer for all frames. long frameNum = unprotectedBytes / maxUnprotectedBytesPerFrame + 1; int lastFrameUnprotectedBytes = (int) (unprotectedBytes % maxUnprotectedBytesPerFrame); if (lastFrameUnprotectedBytes == 0) { frameNum--; lastFrameUnprotectedBytes = maxUnprotectedBytesPerFrame; } long protectedBytes = frameNum * (HEADER_BYTES + suffixBytes) + unprotectedBytes; ByteBuf protectedBuf = alloc.directBuffer(Ints.checkedCast(protectedBytes)); try { int bufferIdx = 0; for (int frameIdx = 0; frameIdx < frameNum; ++frameIdx) { int unprotectedBytesLeft = (frameIdx == frameNum - 1) ? lastFrameUnprotectedBytes : maxUnprotectedBytesPerFrame; // Write header (at most LIMIT_MAX_ALLOWED_FRAME_BYTES). protectedBuf.writeIntLE(unprotectedBytesLeft + HEADER_TYPE_FIELD_BYTES + suffixBytes); protectedBuf.writeIntLE(HEADER_TYPE_DEFAULT); // Ownership of the backing buffer remains with protectedBuf. ByteBuf frameOut = writeSlice(protectedBuf, unprotectedBytesLeft + suffixBytes); List<ByteBuf> framePlain = new ArrayList<>(); while (unprotectedBytesLeft > 0) { // Ownership of the buffer backing in remains with unprotectedBufs. ByteBuf in = unprotectedBufs.get(bufferIdx); if (in.readableBytes() <= unprotectedBytesLeft) { // The complete buffer belongs to this frame. framePlain.add(in); unprotectedBytesLeft -= in.readableBytes(); bufferIdx++; } else { // The remainder of in will be part of the next frame. framePlain.add(in.readSlice(unprotectedBytesLeft)); unprotectedBytesLeft = 0; } } crypter.encrypt(frameOut, framePlain); verify(!frameOut.isWritable()); } protectedBuf.readerIndex(0); protectedBuf.writerIndex(protectedBuf.capacity()); return protectedBuf.retain(); } finally { protectedBuf.release(); } }
Example 14
Source File: SslHandler.java From netty-4.1.22 with Apache License 2.0 | 4 votes |
private SSLEngineResult wrap(ByteBufAllocator alloc, SSLEngine engine, ByteBuf in, ByteBuf out) throws SSLException { ByteBuf newDirectIn = null; try { int readerIndex = in.readerIndex(); int readableBytes = in.readableBytes(); // We will call SslEngine.wrap(ByteBuffer[], ByteBuffer) to allow efficient handling of // CompositeByteBuf without force an extra memory copy when CompositeByteBuffer.nioBuffer() is called. final ByteBuffer[] in0; if (in.isDirect() || !engineType.wantsDirectBuffer) { // As CompositeByteBuf.nioBufferCount() can be expensive (as it needs to check all composed ByteBuf // to calculate the count) we will just assume a CompositeByteBuf contains more then 1 ByteBuf. // The worst that can happen is that we allocate an extra ByteBuffer[] in CompositeByteBuf.nioBuffers() // which is better then walking the composed ByteBuf in most cases. if (!(in instanceof CompositeByteBuf) && in.nioBufferCount() == 1) { in0 = singleBuffer; // We know its only backed by 1 ByteBuffer so use internalNioBuffer to keep object allocation // to a minimum. in0[0] = in.internalNioBuffer(readerIndex, readableBytes); } else { in0 = in.nioBuffers(); } } else { // We could even go further here and check if its a CompositeByteBuf and if so try to decompose it and // only replace the ByteBuffer that are not direct. At the moment we just will replace the whole // CompositeByteBuf to keep the complexity to a minimum newDirectIn = alloc.directBuffer(readableBytes); newDirectIn.writeBytes(in, readerIndex, readableBytes); in0 = singleBuffer; in0[0] = newDirectIn.internalNioBuffer(newDirectIn.readerIndex(), readableBytes); } for (;;) { ByteBuffer out0 = out.nioBuffer(out.writerIndex(), out.writableBytes()); SSLEngineResult result = engine.wrap(in0, out0); in.skipBytes(result.bytesConsumed()); out.writerIndex(out.writerIndex() + result.bytesProduced()); switch (result.getStatus()) { case BUFFER_OVERFLOW: out.ensureWritable(engine.getSession().getPacketBufferSize()); break; default: return result; } } } finally { // Null out to allow GC of ByteBuffer singleBuffer[0] = null; if (newDirectIn != null) { newDirectIn.release(); } } }
Example 15
Source File: AbstractEpollChannel.java From netty4.0.27Learn with Apache License 2.0 | 4 votes |
private static ByteBuf newDirectBuffer0(Object holder, ByteBuf buf, ByteBufAllocator alloc, int capacity) { final ByteBuf directBuf = alloc.directBuffer(capacity); directBuf.writeBytes(buf, buf.readerIndex(), capacity); ReferenceCountUtil.safeRelease(holder); return directBuf; }
Example 16
Source File: AbstractSslHandlerThroughputBenchmark.java From netty-4.1.22 with Apache License 2.0 | 4 votes |
@Override ByteBuf newBuffer(ByteBufAllocator allocator, int size) { return allocator.directBuffer(size); }
Example 17
Source File: AltsTsiFrameProtector.java From grpc-nebula-java with Apache License 2.0 | 4 votes |
Unprotector(ChannelCrypterNetty crypter, ByteBufAllocator alloc) { this.crypter = crypter; this.suffixBytes = crypter.getSuffixLength(); this.header = alloc.directBuffer(HEADER_BYTES); this.firstFrameTag = alloc.directBuffer(suffixBytes); }
Example 18
Source File: ProtostuffSerializerTest.java From turbo-rpc with Apache License 2.0 | 3 votes |
public static void main(String[] args) throws IOException { ProtostuffSerializer serializer = new ProtostuffSerializer(); UserService userService = new UserServiceServerImpl(); ByteBufAllocator allocator = new UnpooledByteBufAllocator(true); ByteBuf byteBuf = allocator.directBuffer(16, 1024 * 1024 * 8); Request request = new Request(); request.setRequestId(123); request.setServiceId(8); //request.setParams(new Object[] { Integer.valueOf(1), LocalDate.now(), userService.getUser(999).join() }); serializer.writeRequest(byteBuf, request); byteBuf.readerIndex(4); System.out.println(serializer.readRequest(byteBuf)); byteBuf.clear(); Response response = new Response(); response.setRequestId(321); response.setStatusCode((byte) 1); response.setResult(userService.listUser(0).join()); serializer.writeResponse(byteBuf, response); byteBuf.readerIndex(4); System.out.println(serializer.readResponse(byteBuf)); }
Example 19
Source File: MoreByteBufUtils.java From Velocity with MIT License | 2 votes |
/** * Creates a {@link ByteBuf} that will have the best performance with the specified * {@code nativeStuff}. * * @param alloc the {@link ByteBufAllocator} to use * @param nativeStuff the native we are working with * @return a buffer compatible with the native */ public static ByteBuf preferredBuffer(ByteBufAllocator alloc, Native nativeStuff) { return nativeStuff.isNative() ? alloc.directBuffer() : alloc.heapBuffer(); }
Example 20
Source File: MoreByteBufUtils.java From Velocity with MIT License | 2 votes |
/** * Creates a {@link ByteBuf} that will have the best performance with the specified * {@code nativeStuff}. * * @param alloc the {@link ByteBufAllocator} to use * @param nativeStuff the native we are working with * @param initialCapacity the initial capacity to allocate * @return a buffer compatible with the native */ public static ByteBuf preferredBuffer(ByteBufAllocator alloc, Native nativeStuff, int initialCapacity) { return nativeStuff.isNative() ? alloc.directBuffer(initialCapacity) : alloc .heapBuffer(initialCapacity); }