/** * Write {@link Long#BYTES} bytes to the stream representing encoded long value. * * @param v The long value to write to the stream. * * @return current output stream. */ public OutputStream writeLong(long v) { writeBytes(Unpooled.copyLong(v)); return this; }
@Override public long transferTo(WritableByteChannel target, long position) throws IOException { for (int i = 0; i < writesPerCall; i++) { ByteBuf buf = Unpooled.copyLong((position / 8) + i); ByteBuffer nio = buf.nioBuffer(); while (nio.remaining() > 0) { target.write(nio); } buf.release(); written++; } return 8 * writesPerCall; }
@Override public long transferTo(WritableByteChannel target, long position) throws IOException { for (int i = 0; i < writesPerCall; i++) { ByteBuf buf = Unpooled.copyLong((position / 8) + i); ByteBuffer nio = buf.nioBuffer(); while (nio.remaining() > 0) { target.write(nio); } buf.release(); written++; } return 8 * writesPerCall; }
@Override public void set(String key, String value, Handler<AsyncResult<Void>> completionHandler) { // Create the key buffer ByteBuf keyBuf = Unpooled.copiedBuffer(key, StandardCharsets.UTF_8); // Create the value buffer ByteBuf valueBuf = Unpooled.copiedBuffer(value, StandardCharsets.UTF_8); // Extra : deadbeef + 2 hours expiration time ByteBuf extra = Unpooled.copyLong(0xdeadbeef00001c20L); // Create the memcached request FullBinaryMemcacheRequest request = new DefaultFullBinaryMemcacheRequest(keyBuf, extra, valueBuf); // Set the memcached operation opcode to perform a SET request.setOpcode(BinaryMemcacheOpcodes.SET); // Execute the request writeRequest(request, ar -> { if (ar.succeeded()) { processSetResponse(ar.result(), completionHandler); } else { // Network error completionHandler.handle(Future.failedFuture(ar.cause())); } }); }
private void nextChunk() throws IOException { byteChannel.reset(); if (isByteBuf) { int copied = byteChannel.write(buf.nioBuffer()); buf.skipBytes(copied); } else { region.transferTo(byteChannel, region.transfered()); } byte[] encrypted = backend.wrap(byteChannel.getData(), 0, byteChannel.length()); this.currentChunk = ByteBuffer.wrap(encrypted); this.currentChunkSize = encrypted.length; this.currentHeader = Unpooled.copyLong(8 + currentChunkSize); this.unencryptedChunkSize = byteChannel.length(); }
private void nextChunk() throws IOException { byteChannel.reset(); if (isByteBuf) { int copied = byteChannel.write(buf.nioBuffer()); buf.skipBytes(copied); } else { region.transferTo(byteChannel, region.transfered()); } byte[] encrypted = backend.wrap(byteChannel.getData(), 0, byteChannel.length()); this.currentChunk = ByteBuffer.wrap(encrypted); this.currentChunkSize = encrypted.length; this.currentHeader = Unpooled.copyLong(8 + currentChunkSize); this.unencryptedChunkSize = byteChannel.length(); }
private void nextChunk() throws IOException { if (byteChannel == null) { byteChannel = new ByteArrayWritableChannel(maxOutboundBlockSize); } byteChannel.reset(); if (isByteBuf) { int copied = byteChannel.write(buf.nioBuffer()); buf.skipBytes(copied); } else { region.transferTo(byteChannel, region.transferred()); } byte[] encrypted = backend.wrap(byteChannel.getData(), 0, byteChannel.length()); this.currentChunk = ByteBuffer.wrap(encrypted); this.currentChunkSize = encrypted.length; this.currentHeader = Unpooled.copyLong(8 + currentChunkSize); this.unencryptedChunkSize = byteChannel.length(); }
private void nextChunk() throws IOException { if (byteChannel == null) { byteChannel = new ByteArrayWritableChannel(maxOutboundBlockSize); } byteChannel.reset(); if (isByteBuf) { int copied = byteChannel.write(buf.nioBuffer()); buf.skipBytes(copied); } else { region.transferTo(byteChannel, region.transferred()); } byte[] encrypted = backend.wrap(byteChannel.getData(), 0, byteChannel.length()); this.currentChunk = ByteBuffer.wrap(encrypted); this.currentChunkSize = encrypted.length; this.currentHeader = Unpooled.copyLong(8 + currentChunkSize); this.unencryptedChunkSize = byteChannel.length(); }
@Test public void testByteBufBody() throws Exception { testByteBufBody(Unpooled.copyLong(42)); }
private void testInvalidFrame(long size) throws Exception { TransportFrameDecoder decoder = new TransportFrameDecoder(); ChannelHandlerContext ctx = mock(ChannelHandlerContext.class); ByteBuf frame = Unpooled.copyLong(size); try { decoder.channelRead(ctx, frame); } finally { release(frame); } }
private void testInvalidFrame(long size) throws Exception { TransportFrameDecoder decoder = new TransportFrameDecoder(); ChannelHandlerContext ctx = mock(ChannelHandlerContext.class); ByteBuf frame = Unpooled.copyLong(size); try { decoder.channelRead(ctx, frame); } finally { release(frame); } }
private void testFileRegionBody(int totalWrites, int writesPerCall) throws Exception { ByteBuf header = Unpooled.copyLong(42); int headerLength = header.readableBytes(); TestFileRegion region = new TestFileRegion(totalWrites, writesPerCall); MessageWithHeader msg = new MessageWithHeader(null, header, region, region.count()); ByteBuf result = doWrite(msg, totalWrites / writesPerCall); assertEquals(headerLength + region.count(), result.readableBytes()); assertEquals(42, result.readLong()); for (long i = 0; i < 8; i++) { assertEquals(i, result.readLong()); } assertTrue(msg.release()); }
@Test public void testDeallocateReleasesManagedBuffer() throws Exception { ByteBuf header = Unpooled.copyLong(42); ManagedBuffer managedBuf = Mockito.spy(new TestManagedBuffer(84)); ByteBuf body = (ByteBuf) managedBuf.convertToNetty(); assertEquals(2, body.refCnt()); MessageWithHeader msg = new MessageWithHeader(managedBuf, header, body, body.readableBytes()); assertTrue(msg.release()); Mockito.verify(managedBuf, Mockito.times(1)).release(); assertEquals(0, body.refCnt()); }
private void testFileRegionBody(int totalWrites, int writesPerCall) throws Exception { ByteBuf header = Unpooled.copyLong(42); int headerLength = header.readableBytes(); TestFileRegion region = new TestFileRegion(totalWrites, writesPerCall); MessageWithHeader msg = new MessageWithHeader(null, header, region, region.count()); ByteBuf result = doWrite(msg, totalWrites / writesPerCall); assertEquals(headerLength + region.count(), result.readableBytes()); assertEquals(42, result.readLong()); for (long i = 0; i < 8; i++) { assertEquals(i, result.readLong()); } assertTrue(msg.release()); }
@Test public void testDeallocateReleasesManagedBuffer() throws Exception { ByteBuf header = Unpooled.copyLong(42); ManagedBuffer managedBuf = Mockito.spy(new TestManagedBuffer(84)); ByteBuf body = (ByteBuf) managedBuf.convertToNetty(); assertEquals(2, body.refCnt()); MessageWithHeader msg = new MessageWithHeader(managedBuf, header, body, body.readableBytes()); assertTrue(msg.release()); Mockito.verify(managedBuf, Mockito.times(1)).release(); assertEquals(0, body.refCnt()); }
@Test public void testCompositeByteBufBodySingleBuffer() throws Exception { ByteBuf header = Unpooled.copyLong(42); CompositeByteBuf compositeByteBuf = Unpooled.compositeBuffer(); compositeByteBuf.addComponent(true, header); assertEquals(1, compositeByteBuf.nioBufferCount()); testByteBufBody(compositeByteBuf); }
@Test public void testByteBufBody() throws Exception { ByteBuf header = Unpooled.copyLong(42); ByteBuf bodyPassedToNettyManagedBuffer = Unpooled.copyLong(84); assertEquals(1, header.refCnt()); assertEquals(1, bodyPassedToNettyManagedBuffer.refCnt()); ManagedBuffer managedBuf = new NettyManagedBuffer(bodyPassedToNettyManagedBuffer); Object body = managedBuf.convertToNetty(); assertEquals(2, bodyPassedToNettyManagedBuffer.refCnt()); assertEquals(1, header.refCnt()); MessageWithHeader msg = new MessageWithHeader(managedBuf, header, body, managedBuf.size()); ByteBuf result = doWrite(msg, 1); assertEquals(msg.count(), result.readableBytes()); assertEquals(42, result.readLong()); assertEquals(84, result.readLong()); assertTrue(msg.release()); assertEquals(0, bodyPassedToNettyManagedBuffer.refCnt()); assertEquals(0, header.refCnt()); }
@Test public void testCompositeByteBufBodyMultipleBuffers() throws Exception { ByteBuf header = Unpooled.copyLong(42); CompositeByteBuf compositeByteBuf = Unpooled.compositeBuffer(); compositeByteBuf.addComponent(true, header.retainedSlice(0, 4)); compositeByteBuf.addComponent(true, header.slice(4, 4)); assertEquals(2, compositeByteBuf.nioBufferCount()); testByteBufBody(compositeByteBuf); }
static void serializeLinkAttributes(final LinkAttributesCase linkAttributesCase, final ByteBuf output) { final LinkAttributes linkAttributes = linkAttributesCase.getLinkAttributes(); LOG.trace("Started serializing Link Attributes"); ifPresentApply(linkAttributes.getLocalIpv4RouterId(), value -> TlvUtil.writeTLV(TlvUtil.LOCAL_IPV4_ROUTER_ID, Ipv4Util.byteBufForAddress((Ipv4Address) value), output)); ifPresentApply(linkAttributes.getLocalIpv6RouterId(), value -> TlvUtil.writeTLV(TlvUtil.LOCAL_IPV6_ROUTER_ID, Ipv6Util.byteBufForAddress((Ipv6Address) value), output)); ifPresentApply(linkAttributes.getRemoteIpv4RouterId(), value -> TlvUtil.writeTLV(REMOTE_IPV4_ROUTER_ID, Ipv4Util.byteBufForAddress((Ipv4Address) value), output)); ifPresentApply(linkAttributes.getRemoteIpv6RouterId(), value -> TlvUtil.writeTLV(REMOTE_IPV6_ROUTER_ID, Ipv6Util.byteBufForAddress((Ipv6Address) value), output)); ifPresentApply(linkAttributes.getAdminGroup(), value -> TlvUtil.writeTLV(ADMIN_GROUP, Unpooled.copyInt((((AdministrativeGroup) value).getValue()).intValue()), output)); ifPresentApply(linkAttributes.getMaxLinkBandwidth(), value -> TlvUtil.writeTLV(MAX_BANDWIDTH, Unpooled.wrappedBuffer(((Bandwidth) value).getValue()), output)); ifPresentApply(linkAttributes.getMaxReservableBandwidth(), value -> TlvUtil.writeTLV(MAX_RESERVABLE_BANDWIDTH, Unpooled.wrappedBuffer(((Bandwidth) value).getValue()), output)); serializeUnreservedBw(linkAttributes.getUnreservedBandwidth(), output); ifPresentApply(linkAttributes.getTeMetric(), value -> TlvUtil.writeTLV(TE_METRIC, Unpooled.copyLong(((TeMetric) value).getValue()), output)); ifPresentApply(linkAttributes.getLinkProtection(), value -> TlvUtil.writeTLV(LINK_PROTECTION_TYPE, Unpooled.copyShort(((LinkProtectionType) value).getIntValue()), output)); serializeMplsProtocolMask(linkAttributes.getMplsProtocol(), output); ifPresentApply(linkAttributes.getMetric(), value -> TlvUtil.writeTLV(METRIC, Unpooled.copyMedium(((Metric) value).getValue().intValue()), output)); serializeSrlg(linkAttributes.getSharedRiskLinkGroups(), output); ifPresentApply(linkAttributes.getLinkName(), value -> TlvUtil.writeTLV(LINK_NAME, Unpooled.wrappedBuffer(StandardCharsets.UTF_8.encode((String) value)), output)); ifPresentApply(linkAttributes.getSrAdjIds(), value -> SrLinkAttributesParser.serializeAdjacencySegmentIdentifiers((List<SrAdjIds>) value, SR_ADJ_ID, output)); ifPresentApply(linkAttributes.getSrLanAdjIds(), value -> SrLinkAttributesParser.serializeLanAdjacencySegmentIdentifiers((List<SrLanAdjIds>) value, output)); ifPresentApply(linkAttributes.getPeerNodeSid(), value -> TlvUtil.writeTLV(PEER_NODE_SID_CODE, SrLinkAttributesParser.serializeAdjacencySegmentIdentifier((PeerNodeSid) value), output)); ifPresentApply(linkAttributes.getPeerAdjSid(), value -> TlvUtil.writeTLV(PEER_ADJ_SID_CODE, SrLinkAttributesParser.serializeAdjacencySegmentIdentifier((PeerAdjSid) value), output)); ifPresentApply(linkAttributes.getPeerSetSids(), value -> SrLinkAttributesParser.serializeAdjacencySegmentIdentifiers((List<PeerSetSids>) value, PEER_SET_SID_CODE, output)); LOG.trace("Finished serializing Link Attributes"); }
/** * Test writing a {@link MessageWithHeader} using the given {@link ByteBuf} as header. * * @param header the header to use. * @throws Exception thrown on error. */ private void testByteBufBody(ByteBuf header) throws Exception { long expectedHeaderValue = header.getLong(header.readerIndex()); ByteBuf bodyPassedToNettyManagedBuffer = Unpooled.copyLong(84); assertEquals(1, header.refCnt()); assertEquals(1, bodyPassedToNettyManagedBuffer.refCnt()); ManagedBuffer managedBuf = new NettyManagedBuffer(bodyPassedToNettyManagedBuffer); Object body = managedBuf.convertToNetty(); assertEquals(2, bodyPassedToNettyManagedBuffer.refCnt()); assertEquals(1, header.refCnt()); MessageWithHeader msg = new MessageWithHeader(managedBuf, header, body, managedBuf.size()); ByteBuf result = doWrite(msg, 1); assertEquals(msg.count(), result.readableBytes()); assertEquals(expectedHeaderValue, result.readLong()); assertEquals(84, result.readLong()); assertTrue(msg.release()); assertEquals(0, bodyPassedToNettyManagedBuffer.refCnt()); assertEquals(0, header.refCnt()); }