List of usage examples for io.netty.buffer ByteBuf getBytes
public abstract ByteBuf getBytes(int index, ByteBuffer dst);
From source file:org.apache.activemq.artemis.tests.unit.core.journal.impl.TimedBufferTest.java
License:Apache License
@Test public void testFillBuffer() { final ArrayList<ByteBuffer> buffers = new ArrayList<>(); final AtomicInteger flushTimes = new AtomicInteger(0); class TestObserver implements TimedBufferObserver { @Override//w ww . j a va2 s .c o m public void flushBuffer(final ByteBuf byteBuf, final boolean sync, final List<IOCallback> callbacks) { final ByteBuffer buffer = ByteBuffer.allocate(byteBuf.readableBytes()); buffer.limit(byteBuf.readableBytes()); byteBuf.getBytes(byteBuf.readerIndex(), buffer); buffer.flip(); buffers.add(buffer); flushTimes.incrementAndGet(); } @Override public int getRemainingBytes() { return 1024 * 1024; } } TimedBuffer timedBuffer = new TimedBuffer(null, 100, TimedBufferTest.ONE_SECOND_IN_NANOS, false); timedBuffer.start(); try { timedBuffer.setObserver(new TestObserver()); int x = 0; for (int i = 0; i < 10; i++) { byte[] bytes = new byte[10]; for (int j = 0; j < 10; j++) { bytes[j] = ActiveMQTestBase.getSamplebyte(x++); } ActiveMQBuffer buff = ActiveMQBuffers.wrappedBuffer(bytes); timedBuffer.checkSize(10); timedBuffer.addBytes(buff, false, dummyCallback); } timedBuffer.checkSize(1); Assert.assertEquals(1, flushTimes.get()); ByteBuffer flushedBuffer = buffers.get(0); Assert.assertEquals(100, flushedBuffer.limit()); Assert.assertEquals(100, flushedBuffer.capacity()); flushedBuffer.rewind(); for (int i = 0; i < 100; i++) { Assert.assertEquals(ActiveMQTestBase.getSamplebyte(i), flushedBuffer.get()); } } finally { timedBuffer.stop(); } }
From source file:org.apache.activemq.artemis.tests.unit.core.journal.impl.TimedBufferTest.java
License:Apache License
@Test public void testTimeOnTimedBuffer() throws Exception { final ReusableLatch latchFlushed = new ReusableLatch(0); final AtomicInteger flushes = new AtomicInteger(0); class TestObserver implements TimedBufferObserver { @Override/* w ww. j a va 2 s . c o m*/ public void flushBuffer(final ByteBuf byteBuf, final boolean sync, final List<IOCallback> callbacks) { final ByteBuffer buffer = ByteBuffer.allocate(byteBuf.readableBytes()); buffer.limit(byteBuf.readableBytes()); byteBuf.getBytes(byteBuf.readerIndex(), buffer); for (IOCallback callback : callbacks) { callback.done(); } } @Override public int getRemainingBytes() { return 1024 * 1024; } } TimedBuffer timedBuffer = new TimedBuffer(null, 100, TimedBufferTest.ONE_SECOND_IN_NANOS / 2, false); timedBuffer.start(); TestObserver observer = new TestObserver(); timedBuffer.setObserver(observer); int x = 0; byte[] bytes = new byte[10]; for (int j = 0; j < 10; j++) { bytes[j] = ActiveMQTestBase.getSamplebyte(x++); } ActiveMQBuffer buff = ActiveMQBuffers.wrappedBuffer(bytes); IOCallback callback = new IOCallback() { @Override public void done() { System.out.println("done"); latchFlushed.countDown(); } @Override public void onError(int errorCode, String errorMessage) { } }; try { latchFlushed.setCount(2); // simulating a low load period timedBuffer.addBytes(buff, true, callback); Thread.sleep(1000); timedBuffer.addBytes(buff, true, callback); Assert.assertTrue(latchFlushed.await(5, TimeUnit.SECONDS)); latchFlushed.setCount(5); flushes.set(0); // Sending like crazy... still some wait (1 millisecond) between each send.. long time = System.currentTimeMillis(); for (int i = 0; i < 5; i++) { timedBuffer.addBytes(buff, true, callback); Thread.sleep(1); } Assert.assertTrue(latchFlushed.await(5, TimeUnit.SECONDS)); // The purpose of the timed buffer is to batch writes up to a millisecond.. or up to the size of the buffer. Assert.assertTrue( "Timed Buffer is not batching accordingly, it was expected to take at least 500 seconds batching multiple writes while it took " + (System.currentTimeMillis() - time) + " milliseconds", System.currentTimeMillis() - time >= 450); // ^^ there are some discounts that can happen inside the timed buffer that are still considered valid (like discounting the time it took to perform the operation itself // for that reason the test has been failing (before this commit) at 499 or 480 milliseconds. So, I'm using a reasonable number close to 500 milliseconds that would still be valid for the test // it should be in fact only writing once.. // i will set for 3 just in case there's a GC or anything else happening on the test Assert.assertTrue("Too many writes were called", flushes.get() <= 3); } finally { timedBuffer.stop(); } }
From source file:org.apache.activemq.artemis.tests.unit.core.journal.impl.TimedBufferTest.java
License:Apache License
@Test public void testTimingAndFlush() throws Exception { final ArrayList<ByteBuffer> buffers = new ArrayList<>(); final AtomicInteger flushTimes = new AtomicInteger(0); class TestObserver implements TimedBufferObserver { @Override/*from w ww .j a v a 2s .c o m*/ public void flushBuffer(final ByteBuf byteBuf, final boolean sync, final List<IOCallback> callbacks) { final ByteBuffer buffer = ByteBuffer.allocate(byteBuf.readableBytes()); buffer.limit(byteBuf.readableBytes()); byteBuf.getBytes(byteBuf.readerIndex(), buffer); buffer.flip(); buffers.add(buffer); flushTimes.incrementAndGet(); } @Override public int getRemainingBytes() { return 1024 * 1024; } } TimedBuffer timedBuffer = new TimedBuffer(null, 100, TimedBufferTest.ONE_SECOND_IN_NANOS / 10, false); timedBuffer.start(); try { timedBuffer.setObserver(new TestObserver()); int x = 0; byte[] bytes = new byte[10]; for (int j = 0; j < 10; j++) { bytes[j] = ActiveMQTestBase.getSamplebyte(x++); } ActiveMQBuffer buff = ActiveMQBuffers.wrappedBuffer(bytes); timedBuffer.checkSize(10); timedBuffer.addBytes(buff, false, dummyCallback); Thread.sleep(200); Assert.assertEquals(0, flushTimes.get()); bytes = new byte[10]; for (int j = 0; j < 10; j++) { bytes[j] = ActiveMQTestBase.getSamplebyte(x++); } buff = ActiveMQBuffers.wrappedBuffer(bytes); timedBuffer.checkSize(10); timedBuffer.addBytes(buff, true, dummyCallback); Thread.sleep(500); Assert.assertEquals(1, flushTimes.get()); ByteBuffer flushedBuffer = buffers.get(0); Assert.assertEquals(20, flushedBuffer.limit()); Assert.assertEquals(20, flushedBuffer.capacity()); flushedBuffer.rewind(); for (int i = 0; i < 20; i++) { Assert.assertEquals(ActiveMQTestBase.getSamplebyte(i), flushedBuffer.get()); } } finally { timedBuffer.stop(); } }
From source file:org.apache.bookkeeper.proto.ProtocolBenchmark.java
License:Apache License
@Benchmark public void testAddEntryV3() throws Exception { // Build the request and calculate the total size to be included in the packet. BKPacketHeader.Builder headerBuilder = BKPacketHeader.newBuilder().setVersion(ProtocolVersion.VERSION_THREE) .setOperation(OperationType.ADD_ENTRY).setTxnId(0L); ByteBuf toSend = entry.slice(); byte[] toSendArray = new byte[toSend.readableBytes()]; toSend.getBytes(toSend.readerIndex(), toSendArray); AddRequest.Builder addBuilder = AddRequest.newBuilder().setLedgerId(ledgerId).setEntryId(entryId) .setMasterKey(ByteString.copyFrom(masterKey)).setBody(ByteString.copyFrom(toSendArray)) .setFlag(AddRequest.Flag.RECOVERY_ADD); Request request = Request.newBuilder().setHeader(headerBuilder).setAddRequest(addBuilder).build(); Object res = this.reqEnDeV3.encode(request, ByteBufAllocator.DEFAULT); ReferenceCountUtil.release(res);//from w ww . ja v a 2s . c o m }
From source file:org.apache.bookkeeper.proto.ProtocolBenchmark.java
License:Apache License
@Benchmark public void testAddEntryV3WithMdc() throws Exception { MDC.put("parent_id", "LetsPutSomeLongParentRequestIdHere"); MDC.put("request_id", "LetsPutSomeLongRequestIdHere"); // Build the request and calculate the total size to be included in the packet. BKPacketHeader.Builder headerBuilder = BKPacketHeader.newBuilder().setVersion(ProtocolVersion.VERSION_THREE) .setOperation(OperationType.ADD_ENTRY).setTxnId(0L); ByteBuf toSend = entry.slice(); byte[] toSendArray = new byte[toSend.readableBytes()]; toSend.getBytes(toSend.readerIndex(), toSendArray); AddRequest.Builder addBuilder = AddRequest.newBuilder().setLedgerId(ledgerId).setEntryId(entryId) .setMasterKey(ByteString.copyFrom(masterKey)).setBody(ByteString.copyFrom(toSendArray)) .setFlag(AddRequest.Flag.RECOVERY_ADD); Request request = PerChannelBookieClient.appendRequestContext(Request.newBuilder()).setHeader(headerBuilder) .setAddRequest(addBuilder).build(); Object res = this.reqEnDeV3.encode(request, ByteBufAllocator.DEFAULT); ReferenceCountUtil.release(res);/*from w w w . java 2 s.com*/ MDC.clear(); }
From source file:org.apache.bookkeeper.proto.ProtocolBenchmark.java
License:Apache License
@Benchmark public void testAddEntryV3WithExtraContextDataNoMdc() throws Exception { // Build the request and calculate the total size to be included in the packet. BKPacketHeader.Builder headerBuilder = BKPacketHeader.newBuilder().setVersion(ProtocolVersion.VERSION_THREE) .setOperation(OperationType.ADD_ENTRY).setTxnId(0L); ByteBuf toSend = entry.slice(); byte[] toSendArray = new byte[toSend.readableBytes()]; toSend.getBytes(toSend.readerIndex(), toSendArray); AddRequest.Builder addBuilder = AddRequest.newBuilder().setLedgerId(ledgerId).setEntryId(entryId) .setMasterKey(ByteString.copyFrom(masterKey)).setBody(ByteString.copyFrom(toSendArray)) .setFlag(AddRequest.Flag.RECOVERY_ADD); Request request = appendRequestContextNoMdc(Request.newBuilder()).setHeader(headerBuilder) .setAddRequest(addBuilder).build(); Object res = this.reqEnDeV3.encode(request, ByteBufAllocator.DEFAULT); ReferenceCountUtil.release(res);/* w w w.j a v a 2 s . c om*/ }
From source file:org.apache.camel.component.hl7.HL7MLLPNettyDecoder.java
License:Apache License
private byte[] asByteArray(ByteBuf msg) { byte[] bytes = new byte[msg.readableBytes()]; msg.getBytes(0, bytes); if (config.isConvertLFtoCR()) { for (int i = 0; i < bytes.length; i++) { if (bytes[i] == (byte) '\n') { bytes[i] = (byte) '\r'; }/*w w w. j a v a 2 s .c o m*/ } } return bytes; }
From source file:org.apache.camel.component.netty4.NettyConverter.java
License:Apache License
@Converter public static byte[] toByteArray(ByteBuf buffer, Exchange exchange) { byte[] bytes = new byte[buffer.readableBytes()]; int readerIndex = buffer.readerIndex(); buffer.getBytes(readerIndex, bytes); return bytes; }
From source file:org.apache.distributedlog.common.util.ByteBufUtils.java
License:Apache License
public static byte[] getArray(ByteBuf buffer) { if (buffer.hasArray() && buffer.arrayOffset() == 0 && buffer.writableBytes() == 0) { return buffer.array(); }/*from www.ja v a 2 s . co m*/ byte[] data = new byte[buffer.readableBytes()]; buffer.getBytes(buffer.readerIndex(), data); return data; }
From source file:org.apache.jackrabbit.oak.plugins.segment.standby.codec.SegmentDecoder.java
License:Apache License
@Override protected Object decode(ChannelHandlerContext ctx, ByteBuf in) throws Exception { ByteBuf frame = (ByteBuf) super.decode(ctx, in); if (frame == null) { return null; }/*from w w w . j av a 2 s . c o m*/ int len = frame.readInt(); byte type = frame.readByte(); long msb = frame.readLong(); long lsb = frame.readLong(); long hash = frame.readLong(); byte[] segment = new byte[len - 25]; frame.getBytes(29, segment); Hasher hasher = Hashing.murmur3_32().newHasher(); long check = hasher.putBytes(segment).hash().padToLong(); if (hash == check) { SegmentId id = new SegmentId(store.getTracker(), msb, lsb); Segment s = new Segment(store.getTracker(), id, ByteBuffer.wrap(segment)); log.debug("received type {} with id {} and size {}", type, id, s.size()); return s; } log.debug("received corrupted segment {}, ignoring", new UUID(msb, lsb)); return null; }