List of usage examples for io.netty.buffer ByteBuf array
public abstract byte[] array();
From source file:sas.systems.imflux.test.packet.rtcp.ByePacketTest.java
License:Apache License
@Test public void testEncodeDecode() throws Exception { ByePacket packet = new ByePacket(); packet.addSsrc(0x45);//w w w .java 2 s .c o m packet.addSsrc(0x46); packet.setReasonForLeaving("So long, cruel world."); ByteBuf buffer = packet.encode(); assertEquals(36, buffer.readableBytes()); System.out.println(ByteUtils.writeArrayAsHex(buffer.array(), true)); assertEquals(0, buffer.readableBytes() % 4); ControlPacket controlPacket = ControlPacket.decode(buffer); assertEquals(ControlPacket.Type.BYE, controlPacket.getType()); ByePacket byePacket = (ByePacket) controlPacket; assertNotNull(byePacket.getSsrcList()); assertEquals(2, byePacket.getSsrcList().size()); assertEquals(new Long(0x45), byePacket.getSsrcList().get(0)); assertEquals(new Long(0x46), byePacket.getSsrcList().get(1)); assertEquals("So long, cruel world.", byePacket.getReasonForLeaving()); assertEquals(0, buffer.readableBytes()); }
From source file:sas.systems.imflux.test.packet.rtcp.ByePacketTest.java
License:Apache License
@Test public void testEncodeDecodeWithFixedBlockSize64() throws Exception { ByePacket packet = new ByePacket(); packet.addSsrc(0x45);//from ww w . j a va 2 s. c o m packet.addSsrc(0x46); packet.setReasonForLeaving("So long, cruel world."); ByteBuf buffer = packet.encode(0, 64); assertEquals(64, buffer.readableBytes()); byte[] bufferArray = buffer.array(); System.out.println(ByteUtils.writeArrayAsHex(bufferArray, true)); assertEquals(0, buffer.readableBytes() % 4); ControlPacket controlPacket = ControlPacket.decode(buffer); assertEquals(ControlPacket.Type.BYE, controlPacket.getType()); ByePacket byePacket = (ByePacket) controlPacket; assertNotNull(byePacket.getSsrcList()); assertEquals(2, byePacket.getSsrcList().size()); assertEquals(new Long(0x45), byePacket.getSsrcList().get(0)); assertEquals(new Long(0x46), byePacket.getSsrcList().get(1)); assertEquals("So long, cruel world.", byePacket.getReasonForLeaving()); // Size without fixed block size would be 36 so padding is 64 - 36 assertEquals(64 - 36, bufferArray[bufferArray.length - 1]); assertEquals(0, buffer.readableBytes()); }
From source file:sas.systems.imflux.test.packet.rtcp.ByePacketTest.java
License:Apache License
@Test public void testEncodeDecodeWithFixedBlockSize64AndCompound() throws Exception { ByePacket packet = new ByePacket(); packet.addSsrc(0x45);/*from www .ja va 2 s . c om*/ packet.addSsrc(0x46); packet.setReasonForLeaving("So long, cruel world."); ByteBuf buffer = packet.encode(60, 64); // Alignment would be to 128 bytes *with* the other RTCP packets. So this packet is sized at 128 - 60 = 68 assertEquals(68, buffer.readableBytes()); byte[] bufferArray = buffer.array(); System.out.println(ByteUtils.writeArrayAsHex(bufferArray, true)); assertEquals(0, buffer.readableBytes() % 4); ControlPacket controlPacket = ControlPacket.decode(buffer); assertEquals(ControlPacket.Type.BYE, controlPacket.getType()); ByePacket byePacket = (ByePacket) controlPacket; assertNotNull(byePacket.getSsrcList()); assertEquals(2, byePacket.getSsrcList().size()); assertEquals(new Long(0x45), byePacket.getSsrcList().get(0)); assertEquals(new Long(0x46), byePacket.getSsrcList().get(1)); assertEquals("So long, cruel world.", byePacket.getReasonForLeaving()); // Size without fixed block size would be 36 so padding is 128 - (60 + 36) because current compound length is 60 assertEquals(128 - (60 + 36), bufferArray[bufferArray.length - 1]); assertEquals(0, buffer.readableBytes()); }
From source file:sas.systems.imflux.test.packet.rtcp.SdesChunkItemsTest.java
License:Apache License
@Test public void testEncodeNull() throws Exception { ByteBuf buffer = SdesChunkItems.encode(SdesChunkItems.NULL_ITEM); assertEquals(1, buffer.capacity());/*from w ww . j a v a 2s.co m*/ assertEquals(0x00, buffer.array()[0]); }
From source file:sas.systems.imflux.test.packet.rtcp.SourceDescriptionPacketTest.java
License:Apache License
@Test public void testEncode() throws Exception { SourceDescriptionPacket packet = new SourceDescriptionPacket(); SdesChunk chunk = new SdesChunk(); chunk.setSsrc(0x45);/*from w w w. j ava 2 s. c o m*/ chunk.addItem(SdesChunkItems.createCnameItem("karma")); chunk.addItem(SdesChunkItems.createNameItem("Earl")); chunk.addItem(SdesChunkItems.createNoteItem("Hey crabman")); packet.addItem(chunk); chunk = new SdesChunk(); chunk.setSsrc(0x46); chunk.addItem(SdesChunkItems.createCnameItem("Randy")); packet.addItem(chunk); ByteBuf encoded = packet.encode(); System.out.println(ByteUtils.writeArrayAsHex(encoded.array(), true)); assertEquals(0, encoded.readableBytes() % 4); ControlPacket decoded = ControlPacket.decode(encoded); assertEquals(packet.getType(), decoded.getType()); SourceDescriptionPacket decodedSdes = (SourceDescriptionPacket) decoded; assertNotEmpty(decodedSdes.getChunks()); assertEquals(2, decodedSdes.getChunks().size()); assertEquals(0x45, decodedSdes.getChunks().get(0).getSsrc()); assertNotEmpty(decodedSdes.getChunks().get(0).getItems()); assertEquals(SdesChunkItem.Type.CNAME, decodedSdes.getChunks().get(0).getItems().get(0).getType()); assertEquals("karma", decodedSdes.getChunks().get(0).getItems().get(0).getValue()); assertEquals(SdesChunkItem.Type.NAME, decodedSdes.getChunks().get(0).getItems().get(1).getType()); assertEquals("Earl", decodedSdes.getChunks().get(0).getItems().get(1).getValue()); assertEquals(SdesChunkItem.Type.NOTE, decodedSdes.getChunks().get(0).getItems().get(2).getType()); assertEquals("Hey crabman", decodedSdes.getChunks().get(0).getItems().get(2).getValue()); assertEquals(0x46, decodedSdes.getChunks().get(1).getSsrc()); assertNotEmpty(decodedSdes.getChunks().get(1).getItems()); assertEquals(SdesChunkItem.Type.CNAME, decodedSdes.getChunks().get(1).getItems().get(0).getType()); assertEquals("Randy", decodedSdes.getChunks().get(1).getItems().get(0).getValue()); assertEquals(0, encoded.readableBytes()); }
From source file:sas.systems.imflux.test.packet.rtcp.SourceDescriptionPacketTest.java
License:Apache License
@Test public void testEncodeAsPartOfCompound() throws Exception { SourceDescriptionPacket packet = new SourceDescriptionPacket(); SdesChunk chunk = new SdesChunk(); chunk.setSsrc(0x45);// w w w.j a v a 2s. c o m chunk.addItem(SdesChunkItems.createCnameItem("karma")); chunk.addItem(SdesChunkItems.createNameItem("Earl")); packet.addItem(chunk); chunk = new SdesChunk(); chunk.setSsrc(0x46); chunk.addItem(SdesChunkItems.createCnameItem("Randy")); packet.addItem(chunk); // 36 bytes ByteBuf encoded = packet.encode(); System.out.println(ByteUtils.writeArrayAsHex(encoded.array(), true)); System.out.println("simple encoding length: " + encoded.readableBytes()); assertEquals(0, encoded.readableBytes() % 4); // assuming previous 20 bytes, 36 bytes of normally encoded packet thus become 44 (+8 for padding, 20+36+8 = 64) encoded = packet.encode(20, 64); System.out.println("compound encoding length: " + encoded.readableBytes()); // 20 encoded.skipBytes(encoded.readableBytes() - 1); assertEquals(8, encoded.readByte()); }
From source file:se.sics.gvod.cc.op.CCAddOverlayOp.java
License:Open Source License
@Override public HandleStatus handleEvent(CCOpResponse event) { if (phase.equals(Phase.GET) && event.opResp instanceof GetResponse) { GetResponse resp = (GetResponse) event.opResp; if (!resp.id.equals(pendingMsgId)) { return Operation.HandleStatus.NOT_HANDLED; }/* w w w. j a va2 s.c o m*/ if (resp.code.equals(ResponseCode.SUCCESS)) { if (resp.data != null) { FileMetadata fileMetadata = (FileMetadata) Serializers.lookupSerializer(FileMetadata.class) .fromBinary(Unpooled.wrappedBuffer(resp.data), Optional.absent()); if (req.fileMeta.equals(fileMetadata)) { result = req.success(); } else { result = req.fail(); } } else { phase = Phase.ADD; pendingMsgId = UUID.randomUUID(); ByteBuf buf = Unpooled.buffer(); Serializers.lookupSerializer(FileMetadata.class).toBinary(req.fileMeta, buf); //TODO Alex optimization - do I need to cut short the backing array? pendingMsg = new CCOpRequest(new PutRequest(pendingMsgId, resp.key, buf.array()), resp.key); } } else { result = req.timeout(); } return Operation.HandleStatus.HANDLED; } else if (phase.equals(Phase.ADD) && event.opResp instanceof PutResponse) { PutResponse resp = (PutResponse) event.opResp; if (!resp.id.equals(pendingMsgId)) { return Operation.HandleStatus.NOT_HANDLED; } if (resp.code.equals(ResponseCode.SUCCESS)) { result = req.success(); } else { result = req.timeout(); } return Operation.HandleStatus.HANDLED; } return Operation.HandleStatus.NOT_HANDLED; }
From source file:se.sics.kompics.network.netty.serialization.ProtobufSerializer.java
License:Open Source License
@Override public Object fromBinary(ByteBuf msg, Optional<Class> hint) { final byte[] array; final int offset; final int length = msg.readableBytes(); if (msg.hasArray()) { array = msg.array(); offset = msg.arrayOffset() + msg.readerIndex(); } else {//from ww w.ja v a 2s . co m array = new byte[length]; msg.getBytes(msg.readerIndex(), array, 0, length); offset = 0; } Object o = null; try { if (extensionRegistry == null) { if (HAS_PARSER) { o = prototype.getParserForType().parseFrom(array, offset, length); } else { o = prototype.newBuilderForType().mergeFrom(array, offset, length).build(); } } else { if (HAS_PARSER) { o = prototype.getParserForType().parseFrom(array, offset, length, extensionRegistry); } else { o = prototype.newBuilderForType().mergeFrom(array, offset, length, extensionRegistry).build(); } } } catch (InvalidProtocolBufferException ex) { Serializers.LOG.error("ProtobufSerializer: Couldn't deserialize object.", ex); } return o; }
From source file:se.sics.ktoolbox.cc.heartbeat.util.CCKeyFactory.java
License:Open Source License
public static Key getHeartbeatKey(byte[] schemaPrefix, Identifier overlayId, int slot) { Key.KeyBuilder key = new Key.KeyBuilder(schemaPrefix); ByteBuf buf = Unpooled.buffer(); Serializers.toBinary(overlayId, buf); byte[] oId = Arrays.copyOfRange(buf.array(), 0, buf.readableBytes()); key.append(new byte[] { (byte) oId.length }); key.append(oId);/*from w ww .j a v a2 s . c o m*/ key.append(Ints.toByteArray(slot)); return key.get(); }
From source file:se.sics.ktoolbox.cc.heartbeat.util.CCKeyFactory.java
License:Open Source License
public static KeyRange getHeartbeatRange(byte[] schemaPrefix, Identifier overlayId) { Key.KeyBuilder prefix = new Key.KeyBuilder(schemaPrefix); ByteBuf buf = Unpooled.buffer(); Serializers.toBinary(overlayId, buf); byte[] oId = Arrays.copyOfRange(buf.array(), 0, buf.readableBytes()); prefix.append(new byte[] { (byte) oId.length }); prefix.append(oId);//from ww w. j a v a 2s . co m return KeyRange.prefix(prefix.get()); }