Example usage for io.netty.buffer ByteBufAllocator directBuffer

List of usage examples for io.netty.buffer ByteBufAllocator directBuffer

Introduction

In this page you can find the example usage for io.netty.buffer ByteBufAllocator directBuffer.

Prototype

ByteBuf directBuffer(int initialCapacity, int maxCapacity);

Source Link

Document

Allocate a direct ByteBuf with the given initial capacity and the given maximal capacity.

Usage

From source file:org.apache.bookkeeper.common.allocator.impl.ByteBufAllocatorBuilderTest.java

License:Apache License

@Test
public void testOomWithException() {
    ByteBufAllocator baseAlloc = mock(ByteBufAllocator.class);
    when(baseAlloc.directBuffer(anyInt(), anyInt())).thenThrow(outOfDirectMemException);

    AtomicReference<OutOfMemoryError> receivedException = new AtomicReference<>();

    ByteBufAllocator alloc = ByteBufAllocatorBuilder.create().pooledAllocator(baseAlloc)
            .outOfMemoryPolicy(OutOfMemoryPolicy.ThrowException).outOfMemoryListener((e) -> {
                receivedException.set(e);
            }).build();//  w ww  .j  ava  2s .  c o  m

    try {
        alloc.buffer();
        fail("Should have thrown exception");
    } catch (OutOfMemoryError e) {
        // Expected
        assertEquals(outOfDirectMemException, e);
    }

    // Ensure the notification was triggered even when exception is thrown
    assertEquals(outOfDirectMemException, receivedException.get());
}

From source file:org.apache.bookkeeper.common.allocator.impl.ByteBufAllocatorBuilderTest.java

License:Apache License

@Test
public void testOomWithFallback() {
    ByteBufAllocator baseAlloc = mock(ByteBufAllocator.class);
    when(baseAlloc.directBuffer(anyInt(), anyInt())).thenThrow(outOfDirectMemException);

    AtomicReference<OutOfMemoryError> receivedException = new AtomicReference<>();

    ByteBufAllocator alloc = ByteBufAllocatorBuilder.create().pooledAllocator(baseAlloc)
            .unpooledAllocator(UnpooledByteBufAllocator.DEFAULT)
            .outOfMemoryPolicy(OutOfMemoryPolicy.FallbackToHeap).outOfMemoryListener((e) -> {
                receivedException.set(e);
            }).build();/*from  w  ww. j a  v a 2  s .com*/

    // Should not throw exception
    ByteBuf buf = alloc.buffer();
    assertEquals(UnpooledByteBufAllocator.DEFAULT, buf.alloc());

    // No notification should have been triggered
    assertEquals(null, receivedException.get());
}

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   w  w  w . j  a  v  a 2 s  .  c om*/

    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 testOomUnpooledDirect() {
    ByteBufAllocator heapAlloc = mock(ByteBufAllocator.class);
    OutOfMemoryError noMemError = new OutOfMemoryError("no more direct mem");
    when(heapAlloc.directBuffer(anyInt(), anyInt())).thenThrow(noMemError);

    AtomicReference<OutOfMemoryError> receivedException = new AtomicReference<>();

    ByteBufAllocator alloc = ByteBufAllocatorBuilder.create().poolingPolicy(PoolingPolicy.UnpooledHeap)
            .unpooledAllocator(heapAlloc).outOfMemoryPolicy(OutOfMemoryPolicy.FallbackToHeap)
            .outOfMemoryListener((e) -> {
                receivedException.set(e);
            }).build();//from   w w w.  j  av a 2s .co m

    try {
        alloc.directBuffer();
        fail("Should have thrown exception");
    } catch (OutOfMemoryError e) {
        // Expected
        assertEquals(noMemError, e);
    }

    // Ensure the notification was triggered even when exception is thrown
    assertEquals(noMemError, receivedException.get());
}

From source file:ratpack.util.internal.IoUtils.java

License:Apache License

public static ByteBuf read(ByteBufAllocator allocator, Path path) throws IOException {
    try (SeekableByteChannel sbc = Files.newByteChannel(path); InputStream in = Channels.newInputStream(sbc)) {
        int size = Ints.checkedCast(sbc.size());
        ByteBuf byteBuf = allocator.directBuffer(size, size);
        byteBuf.writeBytes(in, size);/*w  ww  .j  av  a2 s .c o m*/
        return byteBuf;
    }
}