List of usage examples for io.netty.buffer ByteBuf readRetainedSlice
public abstract ByteBuf readRetainedSlice(int length);
From source file:com.chat.common.netty.handler.decode.ProtobufVarint32FrameDecoder.java
License:Apache License
@Override protected void decode(ChannelHandlerContext ctx, ByteBuf in, List<Object> out) throws Exception { in.markReaderIndex();/*from w ww . j ava 2 s .com*/ int preIndex = in.readerIndex(); int length = readRawVarint32(in); if (preIndex == in.readerIndex()) { return; } if (length < 0) { throw new CorruptedFrameException("negative length: " + length); } if (in.readableBytes() < length) { in.resetReaderIndex(); } else { out.add(in.readRetainedSlice(length)); } }
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.curioswitch.curiostack.gcloud.storage.FileWriter.java
License:Open Source License
public ListenableFuture<Void> write(ByteBuf nextBuf) { final ByteBuf buf; if (unfinishedChunk == null) { buf = nextBuf;//w w w . ja va 2s . c o m } else { buf = alloc.compositeBuffer(2).addComponent(true, unfinishedChunk).addComponent(true, nextBuf); unfinishedChunk = null; } int alignedWritableBytes = alignedSize(buf.readableBytes()); if (alignedWritableBytes == buf.readableBytes()) { return CompletableFuturesExtra.toListenableFuture(uploadChunk(buf, false)); } if (alignedWritableBytes == 0) { // Not enough data for a chunk, so copy it for next time. copyUnfinishedBuffer(buf); return immediateFuture(null); } ByteBuf nextChunk = buf.readRetainedSlice(alignedWritableBytes); copyUnfinishedBuffer(buf); return CompletableFuturesExtra.toListenableFuture(uploadChunk(nextChunk, false)); }
From source file:org.curioswitch.gradle.plugins.gcloud.buildcache.CloudStorageBuildCacheService.java
License:Open Source License
@Override public void store(BuildCacheKey buildCacheKey, BuildCacheEntryWriter buildCacheEntryWriter) { ByteBuf buf = PooledByteBufAllocator.DEFAULT.buffer((int) buildCacheEntryWriter.getSize()); try {//from w ww.j av a 2 s . c om try (ByteBufOutputStream os = new ByteBufOutputStream(buf)) { buildCacheEntryWriter.writeTo(os); } catch (IOException e) { logger.warn("Couldn't write cache entry to buffer.", e); return; } FileWriter writer = cloudStorage.createFile(buildCacheKey.getHashCode(), ImmutableMap.of()).join(); while (buf.readableBytes() > 0) { ByteBuf chunk = buf.readRetainedSlice(Math.min(buf.readableBytes(), 10 * 4 * 256 * 1000)); if (buf.readableBytes() > 0) { getUnchecked(writer.write(chunk)); } else { writer.writeAndClose(chunk).join(); } } } catch (Throwable t) { logger.warn("Exception writing to cloud storage, ignoring.", t); } finally { buf.release(); } }
From source file:org.graylog2.inputs.syslog.tcp.SyslogOctetCountFrameDecoder.java
License:Open Source License
@Override protected void decode(ChannelHandlerContext ctx, ByteBuf buffer, List<Object> out) throws Exception { final int frameSizeValueLength = findFrameSizeValueLength(buffer); // We have not found the frame length value byte size yet. if (frameSizeValueLength <= 0) { return;// w ww. ja v a 2 s. c om } // Convert the frame length value bytes into an integer without mutating the buffer reader index. final String lengthString = buffer.slice(buffer.readerIndex(), frameSizeValueLength) .toString(StandardCharsets.UTF_8); final int length = Integer.parseInt(lengthString); final int skipLength = frameSizeValueLength + 1; // Frame length value bytes and the whitespace that follows it. // We have to take the skipped bytes (frame size value length + whitespace) into account when checking if // the buffer has enough data to read the complete message. if (buffer.readableBytes() - skipLength < length) { // We cannot read the complete frame yet. return; } else { // Skip the frame length value bytes and the whitespace that follows it. buffer.skipBytes(skipLength); } final ByteBuf frame = buffer.readRetainedSlice(length); out.add(frame); }
From source file:org.graylog2.inputs.transports.netty.LenientDelimiterBasedFrameDecoder.java
License:Open Source License
/** * Create a frame out of the {@link ByteBuf} and return it. * * @param ctx the {@link ChannelHandlerContext} which this {@link ByteToMessageDecoder} belongs to * @param buffer the {@link ByteBuf} from which to read data * @return frame the {@link ByteBuf} which represent the frame or {@code null} if no frame could * be created.//from w ww . j a va 2 s .co m */ protected Object decode(ChannelHandlerContext ctx, ByteBuf buffer) throws Exception { if (lineBasedDecoder != null) { return lineBasedDecoder.decode(ctx, buffer); } // Try all delimiters and choose the delimiter which yields the shortest frame. int minFrameLength = Integer.MAX_VALUE; ByteBuf minDelim = null; for (ByteBuf delim : delimiters) { int frameLength = indexOf(buffer, delim); if (frameLength >= 0 && frameLength < minFrameLength) { minFrameLength = frameLength; minDelim = delim; } } if (minDelim != null) { int minDelimLength = minDelim.capacity(); ByteBuf frame; if (discardingTooLongFrame) { // We've just finished discarding a very large frame. // Go back to the initial state. discardingTooLongFrame = false; buffer.skipBytes(minFrameLength + minDelimLength); int tooLongFrameLength = this.tooLongFrameLength; this.tooLongFrameLength = 0; if (!failFast) { fail(tooLongFrameLength); } return null; } if (minFrameLength > maxFrameLength) { // Discard read frame. buffer.skipBytes(minFrameLength + minDelimLength); fail(minFrameLength); return null; } if (stripDelimiter) { frame = buffer.readRetainedSlice(minFrameLength); buffer.skipBytes(minDelimLength); } else { frame = buffer.readRetainedSlice(minFrameLength + minDelimLength); } return frame; } else if (emitLastLineWithoutDelimiter && !ctx.channel().isActive()) { minFrameLength = buffer.readableBytes(); ByteBuf frame; if (discardingTooLongFrame) { // We've just finished discarding a very large frame. // Go back to the initial state. discardingTooLongFrame = false; buffer.skipBytes(minFrameLength); int tooLongFrameLength = this.tooLongFrameLength; this.tooLongFrameLength = 0; if (!failFast) { fail(tooLongFrameLength); } return null; } if (minFrameLength > maxFrameLength) { // Discard read frame. buffer.skipBytes(minFrameLength); fail(minFrameLength); return null; } frame = buffer.readRetainedSlice(minFrameLength); return frame; } else { if (!discardingTooLongFrame) { if (buffer.readableBytes() > maxFrameLength) { // Discard the content of the buffer until a delimiter is found. tooLongFrameLength = buffer.readableBytes(); buffer.skipBytes(buffer.readableBytes()); discardingTooLongFrame = true; if (failFast) { fail(tooLongFrameLength); } } } else { // Still discarding the buffer since a delimiter is not found. tooLongFrameLength += buffer.readableBytes(); buffer.skipBytes(buffer.readableBytes()); } return null; } }
From source file:org.graylog2.inputs.transports.netty.LenientLineBasedFrameDecoder.java
License:Open Source License
/** * Create a frame out of the {@link ByteBuf} and return it. * * @param ctx the {@link ChannelHandlerContext} which this {@link ByteToMessageDecoder} belongs to * @param buffer the {@link ByteBuf} from which to read data * @return frame the {@link ByteBuf} which represent the frame or {@code null} if no frame could * be created.//w w w .j av a2 s .c o m */ protected Object decode(ChannelHandlerContext ctx, ByteBuf buffer) throws Exception { final int eol = findEndOfLine(buffer); if (!discarding) { if (eol < 0 && emitLastLineWithoutDelimiter && !ctx.channel().isActive()) { final ByteBuf frame; final int length = buffer.readableBytes(); if (length > maxLength) { buffer.readerIndex(length); fail(ctx, length); return null; } frame = buffer.readRetainedSlice(length); return frame; } else if (eol >= 0) { final ByteBuf frame; final int length = eol - buffer.readerIndex(); final int delimLength = buffer.getByte(eol) == '\r' ? 2 : 1; if (length > maxLength) { buffer.readerIndex(eol + delimLength); fail(ctx, length); return null; } if (stripDelimiter) { frame = buffer.readRetainedSlice(length); buffer.skipBytes(delimLength); } else { frame = buffer.readRetainedSlice(length + delimLength); } return frame; } else { final int length = buffer.readableBytes(); if (length > maxLength) { discardedBytes = length; buffer.readerIndex(buffer.writerIndex()); discarding = true; offset = 0; if (failFast) { fail(ctx, "over " + discardedBytes); } } return null; } } else { if (eol >= 0) { final int length = discardedBytes + eol - buffer.readerIndex(); final int delimLength = buffer.getByte(eol) == '\r' ? 2 : 1; buffer.readerIndex(eol + delimLength); discardedBytes = 0; discarding = false; if (!failFast) { fail(ctx, length); } } else { discardedBytes += buffer.readableBytes(); buffer.readerIndex(buffer.writerIndex()); } return null; } }
From source file:org.traccar.protocol.AlematicsFrameDecoder.java
License:Apache License
@Override protected Object decode(ChannelHandlerContext ctx, ByteBuf buf) throws Exception { if (buf.readableBytes() < MESSAGE_MINIMUM_LENGTH) { return null; }//from w ww . j ava 2s. co m if (buf.getUnsignedShort(buf.readerIndex()) == 0xFAF8) { ByteBuf heartbeat = buf.readRetainedSlice(12); if (ctx != null && ctx.channel() != null) { ctx.channel().writeAndFlush(new NetworkMessage(heartbeat, ctx.channel().remoteAddress())); } } return super.decode(ctx, buf); }
From source file:org.traccar.protocol.AplicomFrameDecoder.java
License:Apache License
@Override protected Object decode(ChannelHandlerContext ctx, Channel channel, ByteBuf buf) throws Exception { // Skip Alive message while (buf.isReadable() && Character.isDigit(buf.getByte(buf.readerIndex()))) { buf.readByte();/* w w w . j a v a 2 s . c om*/ } // Check minimum length if (buf.readableBytes() < 11) { return null; } // Read flags int version = buf.getUnsignedByte(buf.readerIndex() + 1); int offset = 1 + 1 + 3; if ((version & 0x80) != 0) { offset += 4; } // Get data length int length = buf.getUnsignedShort(buf.readerIndex() + offset); offset += 2; if ((version & 0x40) != 0) { offset += 3; } length += offset; // add header // Return buffer if (buf.readableBytes() >= length) { return buf.readRetainedSlice(length); } return null; }
From source file:org.traccar.protocol.At2000FrameDecoder.java
License:Apache License
@Override protected Object decode(ChannelHandlerContext ctx, Channel channel, ByteBuf buf) throws Exception { if (buf.readableBytes() < 5) { return null; }//from w w w.ja v a2 s . c om int length; if (firstPacket) { firstPacket = false; length = buf.getUnsignedMediumLE(buf.readerIndex() + 2); } else { length = buf.getUnsignedMediumLE(buf.readerIndex() + 1); } length += BLOCK_LENGTH; if (length % BLOCK_LENGTH != 0) { length = (length / BLOCK_LENGTH + 1) * BLOCK_LENGTH; } if ((buf.readableBytes() >= length || buf.readableBytes() % ACK_LENGTH == 0) && (buf != currentBuffer || buf.readableBytes() > acknowledgedBytes)) { sendResponse(channel); currentBuffer = buf; acknowledgedBytes = buf.readableBytes(); } if (buf.readableBytes() >= length) { return buf.readRetainedSlice(length); } return null; }