Example usage for io.netty.buffer UnpooledByteBufAllocator UnpooledByteBufAllocator

List of usage examples for io.netty.buffer UnpooledByteBufAllocator UnpooledByteBufAllocator

Introduction

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

Prototype

public UnpooledByteBufAllocator(boolean preferDirect) 

Source Link

Document

Create a new instance which uses leak-detection for direct buffers.

Usage

From source file:com.streamsets.pipeline.lib.parser.udp.netflow.TestNetflowParser.java

License:Apache License

@Test
public void testV5() throws Exception {
    UnpooledByteBufAllocator allocator = new UnpooledByteBufAllocator(false);
    NetflowParser netflowParser = makeNetflowParser();
    byte[] bytes = Resources.toByteArray(Resources.getResource(TEN_PACKETS));
    ByteBuf buf = allocator.buffer(bytes.length);
    buf.writeBytes(bytes);//from   ww w .  j av a 2s  . c  o  m
    List<Record> records = netflowParser.parse(buf, null, null);
    NetflowTestUtil.assertRecordsForTenPackets(records);
}

From source file:com.streamsets.pipeline.lib.parser.udp.syslog.TestSyslogParser.java

License:Apache License

@Test
public void testParseFailure() throws Exception {
    SyslogParser parser = new SyslogParser(getContext(), StandardCharsets.UTF_8);
    String msg = "<123>                    ";
    byte[] bytes = msg.getBytes(StandardCharsets.UTF_8);
    UnpooledByteBufAllocator allocator = new UnpooledByteBufAllocator(false);
    ByteBuf buffer = allocator.buffer(bytes.length);
    buffer.writeBytes(bytes);//from  w  w w . j a v  a 2 s  .  c  o m
    try {
        parser.parse(buffer, InetSocketAddress.createUnresolved("localhost", 5000),
                InetSocketAddress.createUnresolved("localhost", 50000));
        Assert.fail("Expected OnRecordErrorException");
    } catch (OnRecordErrorException ex) {
        Record record = ex.getRecord();
        Assert.assertEquals(msg, record.get().getValueAsString());
    }
}

From source file:com.streamsets.pipeline.lib.parser.udp.syslog.TestSyslogParser.java

License:Apache License

@Test
public void testMessageParsing() throws Exception {
    SyslogParser parser = new SyslogParser(getContext(), StandardCharsets.UTF_8);
    List<String> messages = getTestMessageStrings();

    UnpooledByteBufAllocator allocator = new UnpooledByteBufAllocator(false);
    // test with default keepFields = false
    for (String msg : messages) {
        byte[] bytes = msg.getBytes(StandardCharsets.UTF_8);
        ByteBuf buffer = allocator.buffer(bytes.length);
        buffer.writeBytes(bytes);//w  ww.jav  a 2 s  .c  om
        List<Record> records = parser.parse(buffer, InetSocketAddress.createUnresolved("localhost", 5000),
                InetSocketAddress.createUnresolved("localhost", 50000));
        Assert.assertEquals(1, records.size());
        Assert.assertEquals("Failure to parse known-good syslog message", msg,
                records.get(0).get("/raw").getValueAsString());
        Assert.assertEquals("Failure to parse known-good syslog message", "localhost:5000",
                records.get(0).get("/receiverAddr").getValueAsString());
        Assert.assertEquals("Failure to parse known-good syslog message", "localhost:50000",
                records.get(0).get("/senderAddr").getValueAsString());
        Assert.assertNotNull("Failure to parse known-good syslog message",
                records.get(0).get("/host").getValueAsString());
    }
}

From source file:com.streamsets.pipeline.lib.parser.udp.syslog.TestSyslogParser.java

License:Apache License

@Test
public void testMessageParsingIPv6() throws Exception {
    SyslogParser parser = new SyslogParser(getContext(), StandardCharsets.UTF_8);
    List<String> messages = getTestMessageStrings();

    UnpooledByteBufAllocator allocator = new UnpooledByteBufAllocator(false);
    // test with default keepFields = false
    for (String msg : messages) {
        byte[] bytes = msg.getBytes(StandardCharsets.UTF_8);
        ByteBuf buffer = allocator.buffer(bytes.length);
        buffer.writeBytes(bytes);/*from  w  w  w  .  j  a va2  s .  co  m*/
        List<Record> records = parser.parse(buffer, InetSocketAddress.createUnresolved("::1", 5000),
                InetSocketAddress.createUnresolved("2001:db8::ff00:42:8329", 50000));
        Assert.assertEquals(1, records.size());
        Assert.assertEquals("Failure to parse known-good syslog message", msg,
                records.get(0).get("/raw").getValueAsString());
        Assert.assertEquals("Failure to parse known-good syslog message", "[::1]:5000",
                records.get(0).get("/receiverAddr").getValueAsString());
        Assert.assertEquals("Failure to parse known-good syslog message", "[2001:db8::ff00:42:8329]:50000",
                records.get(0).get("/senderAddr").getValueAsString());
        Assert.assertNotNull("Failure to parse known-good syslog message",
                records.get(0).get("/host").getValueAsString());
    }
}

From source file:io.vertx.ext.web.client.impl.HttpRequestImpl.java

License:Open Source License

private void send(String contentType, Object body, Handler<AsyncResult<HttpResponse<T>>> handler) {

    Future<HttpClientResponse> responseFuture = Future.<HttpClientResponse>future().setHandler(ar -> {
        if (ar.succeeded()) {
            HttpClientResponse resp = ar.result();
            Future<HttpResponse<T>> fut = Future.future();
            fut.setHandler(handler);/*from   w w w . j  a  va2 s .c  o  m*/
            resp.exceptionHandler(err -> {
                if (!fut.isComplete()) {
                    fut.fail(err);
                }
            });
            resp.pause();
            codec.create(ar2 -> {
                resp.resume();
                if (ar2.succeeded()) {
                    BodyStream<T> stream = ar2.result();
                    stream.exceptionHandler(err -> {
                        if (!fut.isComplete()) {
                            fut.fail(err);
                        }
                    });
                    resp.endHandler(v -> {
                        if (!fut.isComplete()) {
                            stream.end();
                            if (stream.result().succeeded()) {
                                fut.complete(new HttpResponseImpl<>(resp, null, stream.result().result()));
                            } else {
                                fut.fail(stream.result().cause());
                            }
                        }
                    });
                    Pump responsePump = Pump.pump(resp, stream);
                    responsePump.start();
                } else {
                    handler.handle(Future.failedFuture(ar2.cause()));
                }
            });
        } else {
            handler.handle(Future.failedFuture(ar.cause()));
        }
    });

    HttpClientRequest req;
    String requestURI;
    if (params != null && params.size() > 0) {
        QueryStringEncoder enc = new QueryStringEncoder(uri);
        params.forEach(param -> {
            enc.addParam(param.getKey(), param.getValue());
        });
        requestURI = enc.toString();
    } else {
        requestURI = uri;
    }
    if (ssl != options.isSsl()) {
        req = client.request(method,
                new RequestOptions().setSsl(ssl).setHost(host).setPort(port).setURI(requestURI));
    } else {
        req = client.request(method, port, host, requestURI);
    }
    req.setFollowRedirects(followRedirects);
    if (headers != null) {
        req.headers().addAll(headers);
    }
    req.exceptionHandler(err -> {
        if (!responseFuture.isComplete()) {
            responseFuture.fail(err);
        }
    });
    req.handler(resp -> {
        if (!responseFuture.isComplete()) {
            responseFuture.complete(resp);
        }
    });
    if (timeout > 0) {
        req.setTimeout(timeout);
    }
    if (body != null) {
        if (contentType != null) {
            String prev = req.headers().get(HttpHeaders.CONTENT_TYPE);
            if (prev == null) {
                req.putHeader(HttpHeaders.CONTENT_TYPE, contentType);
            } else {
                contentType = prev;
            }
        }
        if (body instanceof ReadStream<?>) {
            ReadStream<Buffer> stream = (ReadStream<Buffer>) body;
            if (headers == null || !headers.contains(HttpHeaders.CONTENT_LENGTH)) {
                req.setChunked(true);
            }
            Pump pump = Pump.pump(stream, req);
            stream.exceptionHandler(err -> {
                req.reset();
                if (!responseFuture.isComplete()) {
                    responseFuture.fail(err);
                }
            });
            stream.endHandler(v -> {
                pump.stop();
                req.end();
            });
            pump.start();
        } else {
            Buffer buffer;
            if (body instanceof Buffer) {
                buffer = (Buffer) body;
            } else if (body instanceof MultiMap) {
                try {
                    MultiMap attributes = (MultiMap) body;
                    boolean multipart = "multipart/form-data".equals(contentType);
                    DefaultFullHttpRequest request = new DefaultFullHttpRequest(HttpVersion.HTTP_1_1,
                            io.netty.handler.codec.http.HttpMethod.POST, "/");
                    HttpPostRequestEncoder encoder = new HttpPostRequestEncoder(request, multipart);
                    for (Map.Entry<String, String> attribute : attributes) {
                        encoder.addBodyAttribute(attribute.getKey(), attribute.getValue());
                    }
                    encoder.finalizeRequest();
                    for (String headerName : request.headers().names()) {
                        req.putHeader(headerName, request.headers().get(headerName));
                    }
                    if (encoder.isChunked()) {
                        buffer = Buffer.buffer();
                        while (true) {
                            HttpContent chunk = encoder.readChunk(new UnpooledByteBufAllocator(false));
                            ByteBuf content = chunk.content();
                            if (content.readableBytes() == 0) {
                                break;
                            }
                            buffer.appendBuffer(Buffer.buffer(content));
                        }
                    } else {
                        ByteBuf content = request.content();
                        buffer = Buffer.buffer(content);
                    }
                } catch (Exception e) {
                    throw new VertxException(e);
                }
            } else if (body instanceof JsonObject) {
                buffer = Buffer.buffer(((JsonObject) body).encode());
            } else {
                buffer = Buffer.buffer(Json.encode(body));
            }
            req.end(buffer);
        }
    } else {
        req.end();
    }
}

From source file:io.vertx.webclient.impl.HttpRequestImpl.java

License:Open Source License

private <R> void send(String contentType, Object body, BodyCodec<R> codec,
        Handler<AsyncResult<HttpResponse<R>>> handler) {

    Future<HttpClientResponse> responseFuture = Future.<HttpClientResponse>future().setHandler(ar -> {
        if (ar.succeeded()) {
            HttpClientResponse resp = ar.result();
            Future<HttpResponse<R>> fut = Future.future();
            fut.setHandler(handler);/*w w w.j ava 2  s .c o  m*/
            resp.exceptionHandler(err -> {
                if (!fut.isComplete()) {
                    fut.fail(err);
                }
            });
            resp.pause();
            codec.create(ar2 -> {
                resp.resume();
                if (ar2.succeeded()) {
                    BodyStream<R> stream = ar2.result();
                    stream.exceptionHandler(err -> {
                        if (!fut.isComplete()) {
                            fut.fail(err);
                        }
                    });
                    resp.endHandler(v -> {
                        if (!fut.isComplete()) {
                            stream.end();
                            if (stream.result().succeeded()) {
                                fut.complete(new HttpResponseImpl<>(resp, null, stream.result().result()));
                            } else {
                                fut.fail(stream.result().cause());
                            }
                        }
                    });
                    Pump responsePump = Pump.pump(resp, stream);
                    responsePump.start();
                } else {
                    handler.handle(Future.failedFuture(ar2.cause()));
                }
            });
        } else {
            handler.handle(Future.failedFuture(ar.cause()));
        }
    });

    HttpClientRequest req;
    String requestURI;
    if (params != null && params.size() > 0) {
        QueryStringEncoder enc = new QueryStringEncoder(uri);
        params.forEach(param -> {
            enc.addParam(param.getKey(), param.getValue());
        });
        requestURI = enc.toString();
    } else {
        requestURI = uri;
    }
    if (port != -1) {
        if (host != null) {
            req = client.request(method, port, host, requestURI);
        } else {
            throw new IllegalStateException("Both host and port must be set with an explicit port");
        }
    } else {
        if (host != null) {
            req = client.request(method, host, requestURI);
        } else {
            req = client.request(method, requestURI);
        }
    }
    if (headers != null) {
        req.headers().addAll(headers);
    }
    req.exceptionHandler(err -> {
        if (!responseFuture.isComplete()) {
            responseFuture.fail(err);
        }
    });
    req.handler(resp -> {
        if (!responseFuture.isComplete()) {
            responseFuture.complete(resp);
        }
    });
    if (timeout > 0) {
        req.setTimeout(timeout);
    }
    if (body != null) {
        if (contentType != null) {
            String prev = req.headers().get(HttpHeaders.CONTENT_TYPE);
            if (prev == null) {
                req.putHeader(HttpHeaders.CONTENT_TYPE, contentType);
            } else {
                contentType = prev;
            }
        }
        if (body instanceof ReadStream<?>) {
            ReadStream<Buffer> stream = (ReadStream<Buffer>) body;
            if (headers == null || !headers.contains(HttpHeaders.CONTENT_LENGTH)) {
                req.setChunked(true);
            }
            Pump pump = Pump.pump(stream, req);
            stream.exceptionHandler(err -> {
                req.reset();
                if (!responseFuture.isComplete()) {
                    responseFuture.fail(err);
                }
            });
            stream.endHandler(v -> {
                pump.stop();
                req.end();
            });
            pump.start();
        } else {
            Buffer buffer;
            if (body instanceof Buffer) {
                buffer = (Buffer) body;
            } else if (body instanceof MultiMap) {
                try {
                    MultiMap attributes = (MultiMap) body;
                    boolean multipart = "multipart/form-data".equals(contentType);
                    DefaultFullHttpRequest request = new DefaultFullHttpRequest(HttpVersion.HTTP_1_1,
                            io.netty.handler.codec.http.HttpMethod.POST, "/");
                    HttpPostRequestEncoder encoder = new HttpPostRequestEncoder(request, multipart);
                    for (Map.Entry<String, String> attribute : attributes) {
                        encoder.addBodyAttribute(attribute.getKey(), attribute.getValue());
                    }
                    encoder.finalizeRequest();
                    for (String headerName : request.headers().names()) {
                        req.putHeader(headerName, request.headers().get(headerName));
                    }
                    if (encoder.isChunked()) {
                        buffer = Buffer.buffer();
                        while (true) {
                            HttpContent chunk = encoder.readChunk(new UnpooledByteBufAllocator(false));
                            ByteBuf content = chunk.content();
                            if (content.readableBytes() == 0) {
                                break;
                            }
                            buffer.appendBuffer(Buffer.buffer(content));
                        }
                    } else {
                        ByteBuf content = request.content();
                        buffer = Buffer.buffer(content);
                    }
                } catch (Exception e) {
                    throw new VertxException(e);
                }
            } else if (body instanceof JsonObject) {
                buffer = Buffer.buffer(((JsonObject) body).encode());
            } else {
                buffer = Buffer.buffer(Json.encode(body));
            }
            req.end(buffer);
        }
    } else {
        req.end();
    }
}

From source file:mysql.client.Session_Old.java

/**
 * ?mysql server ?//from  ww  w  .  j a va  2  s . c om
 * @return
 * @throws IOException
 */
private ByteBuf readPacket() throws IOException {
    try {
        //mysqlio?DataInputStream.readFully,?
        //?read(bytes)readFully(bytes),????,??
        io.readFully(packetHeaderBuf);
    } catch (EOFException e) {
        //mysql ?,
        io.close();
        return null;
    }
    //        if(count < packetHeaderBuf.length){
    //            //mysql ?,
    //            io.close();
    //            return null;
    //        }

    int packetLength = (this.packetHeaderBuf[0] & 0xff) + ((this.packetHeaderBuf[1] & 0xff) << 8)
            + ((this.packetHeaderBuf[2] & 0xff) << 16);

    byte[] bytes = new byte[packetLength + 1];
    int realReadCount = io.read(bytes);
    bytes[packetLength] = 0;//??packetLength, 0, c++ /0 ?
    if (realReadCount != packetLength) {
        io.close();
        throw new IllegalStateException("mysql ??,length??");
    }
    ByteBufAllocator allocator = new UnpooledByteBufAllocator(false);
    ByteBuf byteBuf = allocator.heapBuffer(bytes.length);
    byteBuf.writeBytes(bytes);
    byteBuf = byteBuf.order(ByteOrder.LITTLE_ENDIAN);//mysql ????
    return byteBuf;
}

From source file:org.apache.activemq.core.remoting.impl.netty.NettyConnector.java

License:Apache License

public synchronized void start() {
    if (channelClazz != null) {
        return;//from   ww w.  j  av a2s.  c o m
    }

    int threadsToUse;

    if (nioRemotingThreads == -1) {
        // Default to number of cores * 3

        threadsToUse = Runtime.getRuntime().availableProcessors() * 3;
    } else {
        threadsToUse = this.nioRemotingThreads;
    }

    if (useNioGlobalWorkerPool) {
        channelClazz = NioSocketChannel.class;
        group = SharedNioEventLoopGroup.getInstance(threadsToUse);
    } else {
        channelClazz = NioSocketChannel.class;
        group = new NioEventLoopGroup(threadsToUse);
    }
    // if we are a servlet wrap the socketChannelFactory

    bootstrap = new Bootstrap();
    bootstrap.channel(channelClazz);
    bootstrap.group(group);

    bootstrap.option(ChannelOption.TCP_NODELAY, tcpNoDelay);

    if (connectTimeoutMillis != -1) {
        bootstrap.option(ChannelOption.CONNECT_TIMEOUT_MILLIS, connectTimeoutMillis);
    }
    if (tcpReceiveBufferSize != -1) {
        bootstrap.option(ChannelOption.SO_RCVBUF, tcpReceiveBufferSize);
    }
    if (tcpSendBufferSize != -1) {
        bootstrap.option(ChannelOption.SO_SNDBUF, tcpSendBufferSize);
    }
    bootstrap.option(ChannelOption.SO_KEEPALIVE, true);
    bootstrap.option(ChannelOption.SO_REUSEADDR, true);
    bootstrap.option(ChannelOption.ALLOCATOR, new UnpooledByteBufAllocator(false));
    channelGroup = new DefaultChannelGroup("activemq-connector", GlobalEventExecutor.INSTANCE);

    final SSLContext context;
    if (sslEnabled) {
        try {
            // HORNETQ-680 - override the server-side config if client-side system properties are set
            String realKeyStorePath = keyStorePath;
            String realKeyStoreProvider = keyStoreProvider;
            String realKeyStorePassword = keyStorePassword;
            if (System.getProperty(JAVAX_KEYSTORE_PATH_PROP_NAME) != null) {
                realKeyStorePath = System.getProperty(JAVAX_KEYSTORE_PATH_PROP_NAME);
            }
            if (System.getProperty(JAVAX_KEYSTORE_PASSWORD_PROP_NAME) != null) {
                realKeyStorePassword = System.getProperty(JAVAX_KEYSTORE_PASSWORD_PROP_NAME);
            }

            if (System.getProperty(ACTIVEMQ_KEYSTORE_PROVIDER_PROP_NAME) != null) {
                realKeyStoreProvider = System.getProperty(ACTIVEMQ_KEYSTORE_PROVIDER_PROP_NAME);
            }
            if (System.getProperty(ACTIVEMQ_KEYSTORE_PATH_PROP_NAME) != null) {
                realKeyStorePath = System.getProperty(ACTIVEMQ_KEYSTORE_PATH_PROP_NAME);
            }
            if (System.getProperty(ACTIVEMQ_KEYSTORE_PASSWORD_PROP_NAME) != null) {
                realKeyStorePassword = System.getProperty(ACTIVEMQ_KEYSTORE_PASSWORD_PROP_NAME);
            }

            String realTrustStorePath = trustStorePath;
            String realTrustStoreProvider = trustStoreProvider;
            String realTrustStorePassword = trustStorePassword;
            if (System.getProperty(JAVAX_TRUSTSTORE_PATH_PROP_NAME) != null) {
                realTrustStorePath = System.getProperty(JAVAX_TRUSTSTORE_PATH_PROP_NAME);
            }
            if (System.getProperty(JAVAX_TRUSTSTORE_PASSWORD_PROP_NAME) != null) {
                realTrustStorePassword = System.getProperty(JAVAX_TRUSTSTORE_PASSWORD_PROP_NAME);
            }

            if (System.getProperty(ACTIVEMQ_TRUSTSTORE_PROVIDER_PROP_NAME) != null) {
                realTrustStoreProvider = System.getProperty(ACTIVEMQ_TRUSTSTORE_PROVIDER_PROP_NAME);
            }
            if (System.getProperty(ACTIVEMQ_TRUSTSTORE_PATH_PROP_NAME) != null) {
                realTrustStorePath = System.getProperty(ACTIVEMQ_TRUSTSTORE_PATH_PROP_NAME);
            }
            if (System.getProperty(ACTIVEMQ_TRUSTSTORE_PASSWORD_PROP_NAME) != null) {
                realTrustStorePassword = System.getProperty(ACTIVEMQ_TRUSTSTORE_PASSWORD_PROP_NAME);
            }
            context = SSLSupport.createContext(realKeyStoreProvider, realKeyStorePath, realKeyStorePassword,
                    realTrustStoreProvider, realTrustStorePath, realTrustStorePassword);
        } catch (Exception e) {
            close();
            IllegalStateException ise = new IllegalStateException(
                    "Unable to create NettyConnector for " + host + ":" + port);
            ise.initCause(e);
            throw ise;
        }
    } else {
        context = null; // Unused
    }

    if (context != null && useServlet) {
        // TODO: Fix me
        //bootstrap.setOption("sslContext", context);
    }

    bootstrap.handler(new ChannelInitializer<Channel>() {
        public void initChannel(Channel channel) throws Exception {
            final ChannelPipeline pipeline = channel.pipeline();
            if (sslEnabled && !useServlet) {
                SSLEngine engine = context.createSSLEngine();

                engine.setUseClientMode(true);

                engine.setWantClientAuth(true);

                // setting the enabled cipher suites resets the enabled protocols so we need
                // to save the enabled protocols so that after the customer cipher suite is enabled
                // we can reset the enabled protocols if a customer protocol isn't specified
                String[] originalProtocols = engine.getEnabledProtocols();

                if (enabledCipherSuites != null) {
                    try {
                        engine.setEnabledCipherSuites(
                                SSLSupport.parseCommaSeparatedListIntoArray(enabledCipherSuites));
                    } catch (IllegalArgumentException e) {
                        ActiveMQClientLogger.LOGGER.invalidCipherSuite(SSLSupport
                                .parseArrayIntoCommandSeparatedList(engine.getSupportedCipherSuites()));
                        throw e;
                    }
                }

                if (enabledProtocols != null) {
                    try {
                        engine.setEnabledProtocols(
                                SSLSupport.parseCommaSeparatedListIntoArray(enabledProtocols));
                    } catch (IllegalArgumentException e) {
                        ActiveMQClientLogger.LOGGER.invalidProtocol(
                                SSLSupport.parseArrayIntoCommandSeparatedList(engine.getSupportedProtocols()));
                        throw e;
                    }
                } else {
                    engine.setEnabledProtocols(originalProtocols);
                }

                SslHandler handler = new SslHandler(engine);

                pipeline.addLast(handler);
            }

            if (httpEnabled) {
                pipeline.addLast(new HttpRequestEncoder());

                pipeline.addLast(new HttpResponseDecoder());

                pipeline.addLast(new HttpObjectAggregator(Integer.MAX_VALUE));

                pipeline.addLast(new HttpHandler());
            }

            if (httpUpgradeEnabled) {
                // prepare to handle a HTTP 101 response to upgrade the protocol.
                final HttpClientCodec httpClientCodec = new HttpClientCodec();
                pipeline.addLast(httpClientCodec);
                pipeline.addLast("http-upgrade", new HttpUpgradeHandler(pipeline, httpClientCodec));
            }

            protocolManager.addChannelHandlers(pipeline);

            pipeline.addLast(new ActiveMQClientChannelHandler(channelGroup, handler, new Listener()));
        }
    });

    if (batchDelay > 0) {
        flusher = new BatchFlusher();

        batchFlusherFuture = scheduledThreadPool.scheduleWithFixedDelay(flusher, batchDelay, batchDelay,
                TimeUnit.MILLISECONDS);
    }

    ActiveMQClientLogger.LOGGER.debug("Started Netty Connector version " + TransportConstants.NETTY_VERSION);
}

From source file:org.apache.bookkeeper.client.BookieWriteLedgerTest.java

License:Apache License

@Test
@SuppressWarnings("unchecked")
public void testLedgerCreateAdvByteBufRefCnt() throws Exception {
    long ledgerId = rng.nextLong();
    ledgerId &= Long.MAX_VALUE;
    if (!baseConf.getLedgerManagerFactoryClass().equals(LongHierarchicalLedgerManagerFactory.class)) {
        // since LongHierarchicalLedgerManager supports ledgerIds of
        // decimal length upto 19 digits but other
        // LedgerManagers only upto 10 decimals
        ledgerId %= 9999999999L;//from   ww  w.  java 2 s  .c  o  m
    }

    final LedgerHandle lh = bkc.createLedgerAdv(ledgerId, 5, 3, 2, digestType, ledgerPassword, null);

    final List<AbstractByteBufAllocator> allocs = Lists.newArrayList(new PooledByteBufAllocator(true),
            new PooledByteBufAllocator(false), new UnpooledByteBufAllocator(true),
            new UnpooledByteBufAllocator(false));

    long entryId = 0;
    for (AbstractByteBufAllocator alloc : allocs) {
        final ByteBuf data = alloc.buffer(10);
        data.writeBytes(("fragment0" + entryId).getBytes());
        assertEquals("ref count on ByteBuf should be 1", 1, data.refCnt());

        CompletableFuture<Integer> cf = new CompletableFuture<>();
        lh.asyncAddEntry(entryId, data, (rc, handle, eId, qwcLatency, ctx) -> {
            CompletableFuture<Integer> future = (CompletableFuture<Integer>) ctx;
            future.complete(rc);
        }, cf);

        int rc = cf.get();
        assertEquals("rc code is OK", BKException.Code.OK, rc);

        for (int i = 0; i < 10; i++) {
            if (data.refCnt() == 0) {
                break;
            }
            TimeUnit.MILLISECONDS.sleep(250); // recycler runs asynchronously
        }
        assertEquals("writing entry with id " + entryId + ", ref count on ByteBuf should be 0 ", 0,
                data.refCnt());

        org.apache.bookkeeper.client.api.LedgerEntry e = lh.read(entryId, entryId).getEntry(entryId);
        assertEquals("entry data is correct", "fragment0" + entryId, new String(e.getEntryBytes()));
        entryId++;
    }

    bkc.deleteLedger(lh.ledgerId);
}

From source file:org.apache.bookkeeper.client.BookieWriteLedgerTest.java

License:Apache License

@Test
@SuppressWarnings("unchecked")
public void testLedgerCreateByteBufRefCnt() throws Exception {
    final LedgerHandle lh = bkc.createLedger(5, 3, 2, digestType, ledgerPassword, null);

    final List<AbstractByteBufAllocator> allocs = Lists.newArrayList(new PooledByteBufAllocator(true),
            new PooledByteBufAllocator(false), new UnpooledByteBufAllocator(true),
            new UnpooledByteBufAllocator(false));

    int entryId = 0;
    for (AbstractByteBufAllocator alloc : allocs) {
        final ByteBuf data = alloc.buffer(10);
        data.writeBytes(("fragment0" + entryId).getBytes());
        assertEquals("ref count on ByteBuf should be 1", 1, data.refCnt());

        CompletableFuture<Integer> cf = new CompletableFuture<>();
        lh.asyncAddEntry(data, (rc, handle, eId, ctx) -> {
            CompletableFuture<Integer> future = (CompletableFuture<Integer>) ctx;
            future.complete(rc);/*from  w ww.  ja  v  a  2s  .c o  m*/
        }, cf);

        int rc = cf.get();
        assertEquals("rc code is OK", BKException.Code.OK, rc);

        for (int i = 0; i < 10; i++) {
            if (data.refCnt() == 0) {
                break;
            }
            TimeUnit.MILLISECONDS.sleep(250); // recycler runs asynchronously
        }
        assertEquals("writing entry with id " + entryId + ", ref count on ByteBuf should be 0 ", 0,
                data.refCnt());

        org.apache.bookkeeper.client.api.LedgerEntry e = lh.read(entryId, entryId).getEntry(entryId);
        assertEquals("entry data is correct", "fragment0" + entryId, new String(e.getEntryBytes()));
        entryId++;
    }

    bkc.deleteLedger(lh.ledgerId);
}