List of usage examples for io.netty.buffer ByteBuf readLong
public abstract long readLong();
From source file:org.apache.jackrabbit.oak.plugins.segment.standby.codec.ReplyDecoder.java
License:Apache License
private IdArrayBasedBlob decodeBlob(ByteBuf in, int length, byte type) { int inIdLen = in.readInt(); byte[] bid = new byte[inIdLen]; in.readBytes(bid);//from w ww .ja v a 2 s .co m String id = new String(bid, Charset.forName("UTF-8")); long hash = in.readLong(); // #readBytes throws a 'REPLAY' exception if there are not enough bytes // available for reading ByteBuf data = in.readBytes(length); byte[] blob; if (data.hasArray()) { blob = data.array(); } else { blob = new byte[length]; data.readBytes(blob); } Hasher hasher = Hashing.murmur3_32().newHasher(); long check = hasher.putBytes(blob).hash().padToLong(); if (hash == check) { log.debug("received blob with id {} and size {}", id, blob.length); return new IdArrayBasedBlob(blob, id); } log.debug("received corrupted binary {}, ignoring", id); return null; }
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; }/* ww 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; }
From source file:org.apache.servicecomb.transport.highway.TestHighwayClient.java
License:Apache License
@Test public void testCreateLogin(@Mocked NetClientWrapper netClientWrapper) throws Exception { ProtobufCompatibleUtils.init();//from ww w . j a va2 s .c o m HighwayClientConnection connection = new HighwayClientConnection(null, netClientWrapper, "highway://127.0.0.1:7890"); TcpOutputStream os = connection.createLogin(); ByteBuf buf = os.getBuffer().getByteBuf(); byte[] magic = new byte[TcpParser.TCP_MAGIC.length]; buf.readBytes(magic); Assert.assertArrayEquals(TcpParser.TCP_MAGIC, magic); Assert.assertEquals(os.getMsgId(), buf.readLong()); int start = TcpParser.TCP_HEADER_LENGTH; int totalLen = buf.readInt(); int headerLen = buf.readInt(); Buffer headerBuffer = os.getBuffer().slice(start, start + headerLen); int end = start + totalLen; start += headerLen; Buffer bodyBuffer = os.getBuffer().slice(start, end); RequestHeader header = RequestHeader.readObject(headerBuffer); Assert.assertEquals(MsgType.LOGIN, header.getMsgType()); LoginRequest login = LoginRequest.readObject(bodyBuffer); Assert.assertEquals(Const.HIGHWAY, login.getProtocol()); }
From source file:org.apache.spark.network.protocol.MessageWithHeaderSuite.java
License:Apache License
@Test public void testByteBufBody() throws Exception { ByteBuf header = Unpooled.copyLong(42); ByteBuf bodyPassedToNettyManagedBuffer = Unpooled.copyLong(84); assertEquals(1, header.refCnt());/*from w ww .java 2s . c o m*/ 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()); }
From source file:org.apache.spark.network.protocol.MessageWithHeaderSuite.java
License:Apache License
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()); }/* w ww. j a v a 2 s . c om*/ assertTrue(msg.release()); }
From source file:org.apache.spark.network.protocol.UploadStream.java
License:Apache License
public static UploadStream decode(ByteBuf buf) { long requestId = buf.readLong(); int metaSize = buf.readInt(); ManagedBuffer meta = new NettyManagedBuffer(buf.readRetainedSlice(metaSize)); long bodyByteCount = buf.readLong(); // This is called by the frame decoder, so the data is still null. We need a StreamInterceptor // to read the data. return new UploadStream(requestId, meta, bodyByteCount); }
From source file:org.apache.spark.network.shuffle.protocol.mesos.RegisterDriver.java
License:Apache License
public static RegisterDriver decode(ByteBuf buf) { String appId = Encoders.Strings.decode(buf); long heartbeatTimeout = buf.readLong(); return new RegisterDriver(appId, heartbeatTimeout); }
From source file:org.apache.spark.network.shuffle.protocol.StreamHandle.java
License:Apache License
public static StreamHandle decode(ByteBuf buf) { long streamId = buf.readLong(); int numChunks = buf.readInt(); return new StreamHandle(streamId, numChunks); }
From source file:org.blockartistry.mod.DynSurround.network.PacketAurora.java
License:MIT License
public void fromBytes(final ByteBuf buf) { this.dimension = buf.readInt(); this.seed = buf.readLong(); this.posX = buf.readInt(); this.posZ = buf.readInt(); this.colorSet = buf.readByte(); this.preset = buf.readByte(); }
From source file:org.blockartistry.mod.DynSurround.network.PacketHealthChange.java
License:MIT License
@Override public void fromBytes(final ByteBuf buf) { this.entityId = new UUID(buf.readLong(), buf.readLong()); this.posX = buf.readFloat(); this.posY = buf.readFloat(); this.posZ = buf.readFloat(); this.isCritical = buf.readBoolean(); this.amount = buf.readInt(); }