Java Code Examples for io.netty.buffer.ByteBuf#setIndex()

The following examples show how to use io.netty.buffer.ByteBuf#setIndex() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: ChannelBufferWrapper.java    From activemq-artemis with Apache License 2.0 6 votes vote down vote up
@Override
public ActiveMQBuffer readSlice(final int length) {
   if ( isPooled ) {
      ByteBuf fromBuffer = buffer.readSlice(length);
      ByteBuf newNettyBuffer = Unpooled.buffer(fromBuffer.capacity());
      int read = fromBuffer.readerIndex();
      int writ = fromBuffer.writerIndex();
      fromBuffer.readerIndex(0);
      fromBuffer.readBytes(newNettyBuffer,0,writ);
      newNettyBuffer.setIndex(read,writ);
      ActiveMQBuffer returnBuffer = new ChannelBufferWrapper(newNettyBuffer,releasable,false);
      returnBuffer.setIndex(read,writ);
      return returnBuffer;
   }
   return new ChannelBufferWrapper(buffer.readSlice(length), releasable, isPooled);
}
 
Example 2
Source File: GetBlobOperation.java    From ambry with Apache License 2.0 6 votes vote down vote up
/**
 * Slice this chunk's data to only include the bytes within the operation's specified byte range.
 * @param buf the {@link ByteBuf} representing the content of this chunk.
 * @return A {@link ByteBuf} that only includes bytes within the operation's specified byte range.
 */
protected ByteBuf filterChunkToRange(ByteBuf buf) {
  if (options.getBlobOptions.getRange() == null) {
    return buf;
  }
  if (resolvedByteRange.getRangeSize() == 0) {
    buf.clear();
  } else {
    long relativeOffset = offset;
    if (options.getBlobOptions.hasBlobSegmentIdx()) {
      relativeOffset = 0;
    }
    long startOffsetInThisChunk = chunkIndex == 0 ? resolvedByteRange.getStartOffset() - relativeOffset : 0;
    long endOffsetInThisChunkExclusive =
        chunkIndex == (numChunksTotal - 1) ? resolvedByteRange.getEndOffset() - relativeOffset + 1 : chunkSize;
    buf.setIndex(buf.readerIndex() + (int) startOffsetInThisChunk,
        buf.readerIndex() + (int) endOffsetInThisChunkExclusive);
  }
  return buf;
}
 
Example 3
Source File: HttpInvalidMessageTest.java    From netty-4.1.22 with Apache License 2.0 5 votes vote down vote up
private void ensureInboundTrafficDiscarded(EmbeddedChannel ch) {
    // Generate a lot of random traffic to ensure that it's discarded silently.
    byte[] data = new byte[1048576];
    rnd.nextBytes(data);

    ByteBuf buf = Unpooled.wrappedBuffer(data);
    for (int i = 0; i < 4096; i ++) {
        buf.setIndex(0, data.length);
        ch.writeInbound(buf.retain());
        ch.checkException();
        assertNull(ch.readInbound());
    }
    buf.release();
}
 
Example 4
Source File: HttpInvalidMessageTest.java    From netty4.0.27Learn with Apache License 2.0 5 votes vote down vote up
private void ensureInboundTrafficDiscarded(EmbeddedChannel ch) {
    // Generate a lot of random traffic to ensure that it's discarded silently.
    byte[] data = new byte[1048576];
    rnd.nextBytes(data);

    ByteBuf buf = Unpooled.wrappedBuffer(data);
    for (int i = 0; i < 4096; i ++) {
        buf.setIndex(0, data.length);
        ch.writeInbound(buf.retain());
        ch.checkException();
        assertNull(ch.readInbound());
    }
    buf.release();
}
 
Example 5
Source File: PacketImpl.java    From activemq-artemis with Apache License 2.0 5 votes vote down vote up
protected static ByteBuf copyMessageBuffer(ByteBuf buffer, int skipBytes) {

      ByteBuf newNettyBuffer = Unpooled.buffer(buffer.capacity() - PACKET_HEADERS_SIZE - skipBytes);

      int read = buffer.readerIndex();
      int writ = buffer.writerIndex();
      buffer.readerIndex(PACKET_HEADERS_SIZE);

      newNettyBuffer.writeBytes(buffer, buffer.readableBytes() - skipBytes);
      buffer.setIndex( read, writ );
      newNettyBuffer.setIndex( 0, writ - PACKET_HEADERS_SIZE - skipBytes);

      return newNettyBuffer;
   }
 
Example 6
Source File: CSVLineDeserializer.java    From tajo with Apache License 2.0 5 votes vote down vote up
@Override
public void deserialize(final ByteBuf lineBuf, Tuple output) throws IOException, TextLineParsingError {
  int[] projection = targetColumnIndexes;
  if (lineBuf == null || targetColumnIndexes == null || targetColumnIndexes.length == 0) {
    return;
  }

  final int rowLength = lineBuf.readableBytes();
  int start = 0, fieldLength = 0, end = 0;

  //Projection
  int currentTarget = 0;
  int currentIndex = 0;

  while (end != -1) {
    end = lineBuf.forEachByte(start, rowLength - start, processor);

    if (end < 0) {
      fieldLength = rowLength - start;
    } else {
      fieldLength = end - start - delimiterCompensation;
    }

    if (projection.length > currentTarget && currentIndex == projection[currentTarget]) {
      lineBuf.setIndex(start, start + fieldLength);
      Datum datum = fieldSerDer.deserialize(lineBuf, schema.getColumn(currentIndex).getDataType(), nullChars);
      output.put(currentIndex, datum);
      currentTarget++;
    }

    if (projection.length == currentTarget) {
      break;
    }

    start = end + 1;
    currentIndex++;
  }
}
 
Example 7
Source File: BaseChannelTest.java    From netty-4.1.22 with Apache License 2.0 4 votes vote down vote up
static ByteBuf createTestBuf(int len) {
    ByteBuf buf = Unpooled.buffer(len, len);
    buf.setIndex(0, len);
    return buf;
}
 
Example 8
Source File: BaseChannelTest.java    From netty4.0.27Learn with Apache License 2.0 4 votes vote down vote up
static ByteBuf createTestBuf(int len) {
    ByteBuf buf = Unpooled.buffer(len, len);
    buf.setIndex(0, len);
    return buf;
}
 
Example 9
Source File: CSVLineDeserializer.java    From tajo with Apache License 2.0 4 votes vote down vote up
public void deserialize(final ByteBuf lineBuf, Tuple output) throws IOException, TextLineParsingError {
  if (lineBuf == null || targetColumnIndexes == null || targetColumnIndexes.length == 0) {
    return;
  }
  int[] projection = targetColumnIndexes;

  final int rowLength = lineBuf.readableBytes();
  int start = 0, fieldLength = 0, end = 0;

  // Projection
  int currentTarget = 0;
  int currentIndex = 0;

  while (end != -1) {
    end = lineBuf.forEachByte(start, rowLength - start, processor);

    if (end < 0) {
      fieldLength = rowLength - start;
    } else {
      fieldLength = end - start - delimiterCompensation;
    }

    if (projection.length > currentTarget && currentIndex == projection[currentTarget]) {
      final int terminalOffset = start + fieldLength;
      lineBuf.setIndex(start, terminalOffset);

      // See the issue TAJO-1955. This routine strips quote if the property 'quote_char' is specified
      if (hasQuoteChar) {
        if (lineBuf.getByte(start) == quoteChar && lineBuf.getByte(terminalOffset - 1) == quoteChar) {
          lineBuf.setIndex(start + 1, terminalOffset - 1);
        }
      }

      try {
        Datum datum = fieldSerDer.deserialize(currentIndex, lineBuf, nullChars);
        output.put(currentTarget, datum);
      } catch (Exception e) {
        output.put(currentTarget, NullDatum.get());
      }
      currentTarget++;
    }

    if (projection.length == currentTarget) {
      break;
    }

    start = end + 1;
    currentIndex++;
  }

  /* If a text row is less than table schema size, tuple should set to NullDatum */
  if (projection.length > currentTarget) {
    for (; currentTarget < projection.length; currentTarget++) {
      output.put(currentTarget, NullDatum.get());
    }
  }
}