List of usage examples for io.netty.buffer ByteBufAllocator buffer
ByteBuf buffer();
From source file:org.apache.bookkeeper.common.allocator.impl.ByteBufAllocatorBuilderTest.java
License:Apache License
@Test public void testOomWithFallbackAndNoMoreHeap() { ByteBufAllocator baseAlloc = mock(ByteBufAllocator.class); when(baseAlloc.directBuffer(anyInt(), anyInt())).thenThrow(outOfDirectMemException); ByteBufAllocator heapAlloc = mock(ByteBufAllocator.class); OutOfMemoryError noHeapError = new OutOfMemoryError("no more heap"); when(heapAlloc.heapBuffer(anyInt(), anyInt())).thenThrow(noHeapError); AtomicReference<OutOfMemoryError> receivedException = new AtomicReference<>(); ByteBufAllocator alloc = ByteBufAllocatorBuilder.create().pooledAllocator(baseAlloc) .unpooledAllocator(heapAlloc).outOfMemoryPolicy(OutOfMemoryPolicy.FallbackToHeap) .outOfMemoryListener((e) -> { receivedException.set(e); }).build();//from www .j ava2 s .co m try { alloc.buffer(); fail("Should have thrown exception"); } catch (OutOfMemoryError e) { // Expected assertEquals(noHeapError, e); } // Ensure the notification was triggered even when exception is thrown assertEquals(noHeapError, receivedException.get()); }
From source file:org.apache.bookkeeper.common.allocator.impl.ByteBufAllocatorBuilderTest.java
License:Apache License
@Test public void testUnpooled() { ByteBufAllocator alloc = ByteBufAllocatorBuilder.create().poolingPolicy(PoolingPolicy.UnpooledHeap).build(); ByteBuf buf = alloc.buffer(); assertEquals(UnpooledByteBufAllocator.DEFAULT, buf.alloc()); assertTrue(buf.hasArray());// w ww.j a v a 2 s .c om ByteBuf buf2 = alloc.directBuffer(); assertEquals(UnpooledByteBufAllocator.DEFAULT, buf2.alloc()); assertFalse(buf2.hasArray()); }
From source file:org.apache.bookkeeper.common.allocator.impl.ByteBufAllocatorBuilderTest.java
License:Apache License
@Test public void testPooled() { PooledByteBufAllocator pooledAlloc = new PooledByteBufAllocator(true); ByteBufAllocator alloc = ByteBufAllocatorBuilder.create().poolingPolicy(PoolingPolicy.PooledDirect) .pooledAllocator(pooledAlloc).build(); assertTrue(alloc.isDirectBufferPooled()); ByteBuf buf1 = alloc.buffer(); assertEquals(pooledAlloc, buf1.alloc()); assertFalse(buf1.hasArray());/*from w w w . j a va 2s . c o m*/ buf1.release(); ByteBuf buf2 = alloc.directBuffer(); assertEquals(pooledAlloc, buf2.alloc()); assertFalse(buf2.hasArray()); buf2.release(); ByteBuf buf3 = alloc.heapBuffer(); assertEquals(pooledAlloc, buf3.alloc()); assertTrue(buf3.hasArray()); buf3.release(); }
From source file:org.apache.bookkeeper.common.allocator.impl.ByteBufAllocatorBuilderTest.java
License:Apache License
@Test public void testPooledWithDefaultAllocator() { ByteBufAllocator alloc = ByteBufAllocatorBuilder.create().poolingPolicy(PoolingPolicy.PooledDirect) .poolingConcurrency(3).build(); assertTrue(alloc.isDirectBufferPooled()); ByteBuf buf1 = alloc.buffer(); assertEquals(PooledByteBufAllocator.class, buf1.alloc().getClass()); assertEquals(3, ((PooledByteBufAllocator) buf1.alloc()).metric().numDirectArenas()); assertFalse(buf1.hasArray());//from w w w . j a va 2 s .com buf1.release(); ByteBuf buf2 = alloc.directBuffer(); assertFalse(buf2.hasArray()); buf2.release(); ByteBuf buf3 = alloc.heapBuffer(); assertTrue(buf3.hasArray()); buf3.release(); }
From source file:org.curioswitch.curiostack.gcloud.storage.StorageClient.java
License:Open Source License
private static HttpData serializeRequest(Object request, ByteBufAllocator alloc) { ByteBuf buf = alloc.buffer(); try (ByteBufOutputStream os = new ByteBufOutputStream(buf)) { OBJECT_MAPPER.writeValue((DataOutput) os, request); } catch (IOException e) { buf.release();//ww w . j a v a 2 s . c o m throw new UncheckedIOException("Could not serialize resource JSON to buffer.", e); } return new ByteBufHttpData(buf, true); }
From source file:org.mobicents.protocols.ss7.m3ua.impl.AspFactoryImpl.java
License:Open Source License
protected void write(M3UAMessage message) { try {/* w w w . j a v a 2 s .c o m*/ ByteBufAllocator byteBufAllocator = this.association.getByteBufAllocator(); ByteBuf byteBuf; if (byteBufAllocator != null) { byteBuf = byteBufAllocator.buffer(); } else { byteBuf = Unpooled.buffer(); } ((M3UAMessageImpl) message).encode(byteBuf); org.mobicents.protocols.api.PayloadData payloadData = null; if (this.m3UAManagementImpl.isSctpLibNettySupport()) { switch (message.getMessageClass()) { case MessageClass.ASP_STATE_MAINTENANCE: case MessageClass.MANAGEMENT: case MessageClass.ROUTING_KEY_MANAGEMENT: payloadData = new org.mobicents.protocols.api.PayloadData(byteBuf.readableBytes(), byteBuf, true, true, SCTP_PAYLOAD_PROT_ID_M3UA, 0); break; case MessageClass.TRANSFER_MESSAGES: PayloadData payload = (PayloadData) message; int seqControl = payload.getData().getSLS(); payloadData = new org.mobicents.protocols.api.PayloadData(byteBuf.readableBytes(), byteBuf, true, false, SCTP_PAYLOAD_PROT_ID_M3UA, this.slsTable[seqControl]); break; default: payloadData = new org.mobicents.protocols.api.PayloadData(byteBuf.readableBytes(), byteBuf, true, true, SCTP_PAYLOAD_PROT_ID_M3UA, 0); break; } this.association.send(payloadData); // congestion control - we will send MTP-PAUSE every 8 messages int congLevel = this.association.getCongestionLevel(); if (message instanceof PayloadData) { PayloadData payloadData2 = (PayloadData) message; if (congLevel > 0) { sendCongestionInfoToMtp3Users(congLevel, payloadData2.getData().getDpc()); } else { sendCongestionEndInfoToMtp3Users(congLevel, payloadData2.getData().getDpc()); } } } else { byte[] bf = new byte[byteBuf.readableBytes()]; byteBuf.readBytes(bf); synchronized (txBuffer) { switch (message.getMessageClass()) { case MessageClass.ASP_STATE_MAINTENANCE: case MessageClass.MANAGEMENT: case MessageClass.ROUTING_KEY_MANAGEMENT: payloadData = new org.mobicents.protocols.api.PayloadData(byteBuf.readableBytes(), bf, true, true, SCTP_PAYLOAD_PROT_ID_M3UA, 0); break; case MessageClass.TRANSFER_MESSAGES: PayloadData payload = (PayloadData) message; int seqControl = payload.getData().getSLS(); payloadData = new org.mobicents.protocols.api.PayloadData(byteBuf.readableBytes(), bf, true, false, SCTP_PAYLOAD_PROT_ID_M3UA, this.slsTable[seqControl]); break; default: payloadData = new org.mobicents.protocols.api.PayloadData(byteBuf.readableBytes(), bf, true, true, SCTP_PAYLOAD_PROT_ID_M3UA, 0); break; } this.association.send(payloadData); } } } catch (Throwable e) { logger.error( String.format("Error while trying to send PayloadData to SCTP layer. M3UAMessage=%s", message), e); } }
From source file:org.restcomm.protocols.ss7.m3ua.impl.AspFactoryImpl.java
License:Open Source License
protected void write(M3UAMessage message) { try {/* w w w . jav a 2 s .c o m*/ ByteBufAllocator byteBufAllocator = this.association.getByteBufAllocator(); ByteBuf byteBuf; if (byteBufAllocator != null) { byteBuf = byteBufAllocator.buffer(); } else { byteBuf = Unpooled.buffer(); } ((M3UAMessageImpl) message).encode(byteBuf); org.mobicents.protocols.api.PayloadData payloadData = null; if (m3UAManagementImpl.getStatisticsEnabled()) { updateTxStatistic(message); } if (this.m3UAManagementImpl.isSctpLibNettySupport()) { switch (message.getMessageClass()) { case MessageClass.ASP_STATE_MAINTENANCE: case MessageClass.MANAGEMENT: case MessageClass.ROUTING_KEY_MANAGEMENT: payloadData = new org.mobicents.protocols.api.PayloadData(byteBuf.readableBytes(), byteBuf, true, true, SCTP_PAYLOAD_PROT_ID_M3UA, 0); break; case MessageClass.TRANSFER_MESSAGES: PayloadData payload = (PayloadData) message; int seqControl = payload.getData().getSLS(); payloadData = new org.mobicents.protocols.api.PayloadData(byteBuf.readableBytes(), byteBuf, true, false, SCTP_PAYLOAD_PROT_ID_M3UA, this.slsTable[seqControl]); break; default: payloadData = new org.mobicents.protocols.api.PayloadData(byteBuf.readableBytes(), byteBuf, true, true, SCTP_PAYLOAD_PROT_ID_M3UA, 0); break; } this.association.send(payloadData); // congestion control - we will send MTP-PAUSE every 8 messages int congLevel = this.association.getCongestionLevel(); if (message instanceof PayloadData) { PayloadData payloadData2 = (PayloadData) message; if (congLevel > 0) { sendCongestionInfoToMtp3Users(congLevel, payloadData2.getData().getDpc()); } else { sendCongestionEndInfoToMtp3Users(congLevel, payloadData2.getData().getDpc()); } } } else { byte[] bf = new byte[byteBuf.readableBytes()]; byteBuf.readBytes(bf); synchronized (txBuffer) { switch (message.getMessageClass()) { case MessageClass.ASP_STATE_MAINTENANCE: case MessageClass.MANAGEMENT: case MessageClass.ROUTING_KEY_MANAGEMENT: payloadData = new org.mobicents.protocols.api.PayloadData(byteBuf.readableBytes(), bf, true, true, SCTP_PAYLOAD_PROT_ID_M3UA, 0); break; case MessageClass.TRANSFER_MESSAGES: PayloadData payload = (PayloadData) message; int seqControl = payload.getData().getSLS(); payloadData = new org.mobicents.protocols.api.PayloadData(byteBuf.readableBytes(), bf, true, false, SCTP_PAYLOAD_PROT_ID_M3UA, this.slsTable[seqControl]); break; default: payloadData = new org.mobicents.protocols.api.PayloadData(byteBuf.readableBytes(), bf, true, true, SCTP_PAYLOAD_PROT_ID_M3UA, 0); break; } this.association.send(payloadData); } } } catch (Throwable e) { logger.error( String.format("Error while trying to send PayloadData to SCTP layer. M3UAMessage=%s", message), e); } }
From source file:org.springframework.cloud.gateway.rsocket.support.Metadata.java
License:Apache License
public static ByteBuf encode(ByteBufAllocator allocator, String name, Map<String, String> properties) { Assert.hasText(name, "name may not be empty"); Assert.notNull(properties, "properties may not be null"); Assert.notNull(allocator, "allocator may not be null"); ByteBuf byteBuf = allocator.buffer(); encodeString(byteBuf, name);//from ww w. j ava 2s .co m properties.entrySet().stream().forEach(entry -> { encodeString(byteBuf, entry.getKey()); encodeString(byteBuf, entry.getValue()); }); return byteBuf; }
From source file:ratpack.sse.internal.ServerSentEventEncoder.java
License:Apache License
public ByteBuf encode(Event<?> event, ByteBufAllocator bufferAllocator) throws Exception { ByteBuf buffer = bufferAllocator.buffer(); OutputStream outputStream = new ByteBufOutputStream(buffer); Writer writer = new OutputStreamWriter(outputStream, UTF_8); String eventType = event.getEvent(); if (eventType != null) { outputStream.write(EVENT_TYPE_PREFIX); writer.append(eventType).flush(); outputStream.write(NEWLINE);/* w ww . j a v a2 s .co m*/ } String eventData = event.getData(); if (eventData != null) { outputStream.write(EVENT_DATA_PREFIX); for (Character character : Lists.charactersOf(eventData)) { if (character == '\n') { outputStream.write(NEWLINE); outputStream.write(EVENT_DATA_PREFIX); } else { writer.append(character).flush(); } } outputStream.write(NEWLINE); } String eventId = event.getId(); if (eventId != null) { outputStream.write(EVENT_ID_PREFIX); writer.append(eventId).flush(); outputStream.write(NEWLINE); } outputStream.write(NEWLINE); writer.flush(); writer.close(); return buffer; }
From source file:reactor.ipc.netty.ByteBufFlux.java
License:Open Source License
/** * Open a {@link java.nio.channels.FileChannel} from a path and stream * {@link ByteBuf }chunks with/*from w w w . j av a 2 s .co m*/ * a given maximum size into the returned {@link ByteBufFlux} * * @param path the path to the resource to stream * @param maxChunkSize the maximum per-item ByteBuf size * @param allocator the channel {@link ByteBufAllocator} * * @return a {@link ByteBufFlux} */ public static ByteBufFlux fromPath(Path path, int maxChunkSize, ByteBufAllocator allocator) { Objects.requireNonNull(path, "path"); Objects.requireNonNull(allocator, "allocator"); if (maxChunkSize < 1) { throw new IllegalArgumentException("chunk size must be strictly positive, " + "was: " + maxChunkSize); } return new ByteBufFlux(Flux.generate(() -> FileChannel.open(path), (fc, sink) -> { try { ByteBuf buf = allocator.buffer(); long pos; if ((pos = buf.writeBytes(fc, maxChunkSize)) < 0) { sink.complete(); } else { fc.position(pos + fc.position()); sink.next(buf); } } catch (IOException e) { sink.error(e); } return fc; }), allocator); }