List of usage examples for io.netty.buffer ByteBuf slice
public abstract ByteBuf slice(int index, int length);
From source file:com.whizzosoftware.hobson.radiora.api.codec.RadioRaFrameDecoder.java
License:Open Source License
@Override protected Object decode(ChannelHandlerContext ctx, ByteBuf buffer) throws Exception { logger.trace("decode: {}", buffer.toString(CharsetUtil.UTF_8)); ByteBuf frame = (ByteBuf) super.decode(ctx, buffer); if (frame != null) { // if we receive a single byte frame, it should be a '!' if (frame.readableBytes() == 1) { byte b = frame.readByte(); if (b == '!') { return null; } else { throw new CorruptedFrameException("Unexpected single byte frame"); }/*from w w w . j ava2s.c o m*/ // otherwise, we assume it's a command frame } else { int ix = frame.indexOf(frame.readerIndex(), frame.writerIndex(), (byte) ','); if (ix > 0) { String cmdName = frame.slice(0, ix).toString(CharsetUtil.UTF_8); switch (cmdName) { case ZoneMap.TYPE: if (frame.readableBytes() >= ix + 33) { return new ZoneMap(frame.slice(ix + 1, 32).toString(CharsetUtil.UTF_8)); } else { throw new CorruptedFrameException("Received invalid zone map size"); } case LocalZoneChange.TYPE: if (frame.readableBytes() >= ix + 3) { String sZoneNum = frame.slice(ix + 1, 2).toString(CharsetUtil.UTF_8); if (frame.readableBytes() >= ix + 7) { String state = frame.slice(ix + 4, 3).toString(CharsetUtil.UTF_8).trim(); try { return new LocalZoneChange(Integer.parseInt(sZoneNum), LocalZoneChange.State.valueOf(state)); } catch (IllegalArgumentException iae) { throw new CorruptedFrameException("Invalid LZC state string"); } } else { throw new CorruptedFrameException("Invalid LZC size (state)"); } } else { throw new CorruptedFrameException("Invalid LZC size (zoneNum)"); } case LEDMap.TYPE: if (frame.readableBytes() >= ix + 16) { return new LEDMap(frame.slice(ix + 1, 15).toString(CharsetUtil.UTF_8)); } else { throw new CorruptedFrameException("Invalid LED map size"); } default: throw new DecoderException("Unrecognized command: " + cmdName); } } else { throw new CorruptedFrameException("Invalid frame format (no comma)"); } } } else { return null; } }
From source file:com.yahoo.pulsar.client.impl.ProducerImpl.java
License:Apache License
/** * Strips checksum from {@link OpSendMsg} command if present else ignore it. * /* ww w . jav a2 s . co m*/ * @param op */ private void stripChecksum(OpSendMsg op) { op.cmd.markReaderIndex(); int totalMsgBufSize = op.cmd.readableBytes(); DoubleByteBuf msg = getDoubleByteBuf(op.cmd); if (msg != null) { ByteBuf headerFrame = msg.getFirst(); msg.markReaderIndex(); headerFrame.markReaderIndex(); try { headerFrame.skipBytes(4); // skip [total-size] int cmdSize = (int) headerFrame.readUnsignedInt(); // verify if checksum present headerFrame.skipBytes(cmdSize); if (!hasChecksum(headerFrame)) { headerFrame.resetReaderIndex(); return; } int headerSize = 4 + 4 + cmdSize; // [total-size] [cmd-length] [cmd-size] int checksumSize = 4 + 2; // [magic-number] [checksum-size] int checksumMark = (headerSize + checksumSize); // [header-size] [checksum-size] int metaPayloadSize = (totalMsgBufSize - checksumMark); // metadataPayload = totalSize - checksumMark int newTotalFrameSizeLength = 4 + cmdSize + metaPayloadSize; // new total-size without checksum headerFrame.resetReaderIndex(); int headerFrameSize = headerFrame.readableBytes(); headerFrame.setInt(0, newTotalFrameSizeLength); // rewrite new [total-size] ByteBuf metadata = headerFrame.slice(checksumMark, headerFrameSize - checksumMark); // sliced only // metadata headerFrame.writerIndex(headerSize); // set headerFrame write-index to overwrite metadata over checksum metadata.readBytes(headerFrame, metadata.readableBytes()); headerFrame.capacity(headerFrameSize - checksumSize); // reduce capacity by removed checksum bytes headerFrame.resetReaderIndex(); } finally { op.cmd.resetReaderIndex(); } } else { log.warn("[{}] Failed while casting {} into DoubleByteBuf", producerName, op.cmd.getClass().getName()); } }
From source file:com.yahoo.pulsar.common.api.Commands.java
License:Apache License
public static ByteBuf deSerializeSingleMessageInBatch(ByteBuf uncompressedPayload, PulsarApi.SingleMessageMetadata.Builder singleMessageMetadataBuilder, int index, int batchSize) throws IOException { int singleMetaSize = (int) uncompressedPayload.readUnsignedInt(); int writerIndex = uncompressedPayload.writerIndex(); int beginIndex = uncompressedPayload.readerIndex() + singleMetaSize; uncompressedPayload.writerIndex(beginIndex); ByteBufCodedInputStream stream = ByteBufCodedInputStream.get(uncompressedPayload); PulsarApi.SingleMessageMetadata singleMessageMetadata = singleMessageMetadataBuilder.mergeFrom(stream, null) .build();/*from ww w . j a v a 2 s .com*/ int singleMessagePayloadSize = singleMessageMetadata.getPayloadSize(); uncompressedPayload.markReaderIndex(); ByteBuf singleMessagePayload = uncompressedPayload.slice(uncompressedPayload.readerIndex(), singleMessagePayloadSize); singleMessagePayload.retain(); uncompressedPayload.writerIndex(writerIndex); uncompressedPayload.resetReaderIndex(); // reader now points to beginning of payload read; so move it past message payload just read if (index < batchSize) { uncompressedPayload.readerIndex(uncompressedPayload.readerIndex() + singleMessagePayloadSize); } return singleMessagePayload; }
From source file:com.zz.learning.netty5.chap12.codec.MarshallingDecoder.java
License:Apache License
/** * ??//from w w w. ja v a 2s . c om * @param in * @return * @throws Exception */ protected Object decode(ByteBuf in) throws Exception { int objectSize = in.readInt(); /* * ?ByteBuf?? */ ByteBuf buf = in.slice(in.readerIndex(), objectSize); ByteInput input = new ChannelBufferByteInput(buf); try { unmarshaller.start(input); Object obj = unmarshaller.readObject(); unmarshaller.finish(); in.readerIndex(in.readerIndex() + objectSize); // return obj; } finally { unmarshaller.close(); } }
From source file:eu.stratosphere.runtime.io.network.netty.InboundEnvelopeDecoderTest.java
License:Apache License
/** * Returns slices with the specified sizes of the given buffer. * <p/>/* ww w . j ava2s.c om*/ * When given n indexes, n+1 slices will be returned: * <ul> * <li>0 - sliceSizes[0]</li> * <li>sliceSizes[0] - sliceSizes[1]</li> * <li>...</li> * <li>sliceSizes[n-1] - buf.capacity()</li> * </ul> * * @return slices with the specified sizes of the given buffer */ private static ByteBuf[] slice(ByteBuf buf, int... sliceSizes) { if (sliceSizes.length == 0) { throw new IllegalStateException("Need to provide at least one slice size"); } int numSlices = sliceSizes.length; // transform slice sizes to buffer indexes for (int i = 1; i < numSlices; i++) { sliceSizes[i] += sliceSizes[i - 1]; } for (int i = 0; i < sliceSizes.length - 1; i++) { if (sliceSizes[i] >= sliceSizes[i + 1] || sliceSizes[i] <= 0 || sliceSizes[i] >= buf.capacity()) { throw new IllegalStateException( String.format("Slice size %s are off for %s", Arrays.toString(sliceSizes), buf)); } } ByteBuf[] slices = new ByteBuf[numSlices + 1]; // slice at slice indexes slices[0] = buf.slice(0, sliceSizes[0]).retain(); for (int i = 1; i < numSlices; i++) { slices[i] = buf.slice(sliceSizes[i - 1], sliceSizes[i] - sliceSizes[i - 1]).retain(); } slices[numSlices] = buf.slice(sliceSizes[numSlices - 1], buf.capacity() - sliceSizes[numSlices - 1]) .retain(); return slices; }
From source file:io.airlift.drift.transport.netty.codec.TestSimpleFrameInfoDecoder.java
License:Apache License
private static void testDecodeSequenceId(ByteBufAllocator allocator, Protocol protocol) throws TException { FrameInfoDecoder decoder = new SimpleFrameInfoDecoder(FRAMED, protocol, true); ByteBuf message = createTestMessage(allocator, protocol); try {/*from w w w .jav a 2s. com*/ assertFalse(decoder.tryDecodeFrameInfo(message.slice(0, 0)).isPresent()); assertFalse(decoder.tryDecodeFrameInfo(message.slice(0, 1)).isPresent()); assertFalse(decoder.tryDecodeFrameInfo(message.slice(0, 2)).isPresent()); assertFalse(decoder.tryDecodeFrameInfo(message.slice(0, 5)).isPresent()); assertTrue(decoder.tryDecodeFrameInfo(message.slice(0, message.readableBytes())).isPresent()); assertTrue(decoder.tryDecodeFrameInfo(message).isPresent()); assertEquals(decoder.tryDecodeFrameInfo(message).get(), new FrameInfo(METHOD_NAME, CALL, SEQUENCE_ID, FRAMED, protocol, true)); } finally { message.release(); } }
From source file:io.airlift.drift.transport.netty.codec.ThriftUnframedDecoder.java
License:Apache License
@Override protected final void decode(ChannelHandlerContext ctx, ByteBuf buffer, List<Object> out) { int frameOffset = buffer.readerIndex(); TChannelBufferInputTransport transport = new TChannelBufferInputTransport(buffer.retain()); try {/*from w w w .j a v a2 s. co m*/ TProtocolReader protocolReader = protocol.createProtocol(transport); TMessage message = protocolReader.readMessageBegin(); TProtocolUtil.skip(protocolReader, TType.STRUCT); protocolReader.readMessageEnd(); int frameLength = buffer.readerIndex() - frameOffset; if (frameLength > maxFrameSize) { FrameInfo frameInfo = new FrameInfo(message.getName(), message.getType(), message.getSequenceId(), UNFRAMED, protocol, assumeClientsSupportOutOfOrderResponses); ctx.fireExceptionCaught( new FrameTooLargeException(Optional.of(frameInfo), frameLength, maxFrameSize)); } out.add(buffer.slice(frameOffset, frameLength).retain()); } catch (Throwable th) { buffer.readerIndex(frameOffset); } finally { transport.release(); } }
From source file:io.airlift.drift.transport.netty.ThriftUnframedDecoder.java
License:Apache License
private Object decode(ChannelHandlerContext ctx, ByteBuf buffer) { int frameOffset = buffer.readerIndex(); try {/* w w w.ja v a 2s . c o m*/ TTransport transport = new TChannelBufferInputTransport(buffer); TProtocolReader protocol = protocolFactory.getProtocol(transport); protocol.readMessageBegin(); TProtocolUtil.skip(protocol, TType.STRUCT); protocol.readMessageEnd(); int frameLength = buffer.readerIndex() - frameOffset; if (frameLength > maxFrameSize) { ctx.fireExceptionCaught(new TooLongFrameException( "Response message exceeds max size " + maxFrameSize + ": " + frameLength + " - discarded")); } return buffer.slice(frameOffset, frameLength).retain(); } catch (Throwable th) { buffer.readerIndex(frameOffset); return null; } }
From source file:io.datty.msgpack.core.AbstractMessageReader.java
License:Apache License
public ByteBuf readBytes(byte b, ByteBuf buffer, boolean copy) { int length = readBinaryHeader(b, buffer); if (length > buffer.readableBytes()) { throw new MessageParseException( "insufficient buffer length: " + buffer.readableBytes() + ", required length: " + length); }//w w w . j a va 2s.com if (copy) { ByteBuf dst = buffer.alloc().buffer(length); buffer.readBytes(dst, length); return dst; } else { ByteBuf slice = buffer.slice(buffer.readerIndex(), length); buffer.readerIndex(buffer.readerIndex() + length); return slice; } }
From source file:io.datty.msgpack.core.ValueMessageReader.java
License:Apache License
@Override public ByteBuf skipValue(ByteBuf source, boolean copy) { if (!hasNext(source)) { return null; }/*from ww w .j av a 2s . c o m*/ int startIndex = source.readerIndex(); skipValue(source); int endIndex = source.readerIndex(); int length = endIndex - startIndex; if (copy) { ByteBuf dst = source.alloc().buffer(length); source.getBytes(startIndex, dst, length); return dst; } else { return source.slice(startIndex, length); } }