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

The following examples show how to use io.netty.buffer.ByteBuf#capacity() . These examples are extracted from open source projects. 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
ByteBufWrappingBytes(ByteBuf byteBuf, int offset, int length) {
  checkArgument(length >= 0, "Invalid negative length");
  int bufferLength = byteBuf.capacity();
  checkElementIndex(offset, bufferLength + 1);
  checkArgument(
      offset + length <= bufferLength,
      "Provided length %s is too big: the buffer has size %s and has only %s bytes from %s",
      length,
      bufferLength,
      bufferLength - offset,
      offset);

  if (offset == 0 && length == bufferLength) {
    this.byteBuf = byteBuf;
  } else {
    this.byteBuf = byteBuf.slice(offset, length);
  }
}
 
Example 2
Source Project: cava   File: ByteBufWrappingBytes.java    License: Apache License 2.0 6 votes vote down vote up
ByteBufWrappingBytes(ByteBuf byteBuf, int offset, int length) {
  checkArgument(length >= 0, "Invalid negative length");
  int bufferLength = byteBuf.capacity();
  checkElementIndex(offset, bufferLength + 1);
  checkArgument(
      offset + length <= bufferLength,
      "Provided length %s is too big: the buffer has size %s and has only %s bytes from %s",
      length,
      bufferLength,
      bufferLength - offset,
      offset);

  if (offset == 0 && length == bufferLength) {
    this.byteBuf = byteBuf;
  } else {
    this.byteBuf = byteBuf.slice(offset, length);
  }
}
 
Example 3
/**
 * Returns the number of bytes between the readerIndex of the haystack and
 * the first needle found in the haystack.  -1 is returned if no needle is
 * found in the haystack.
 */
private static int indexOf(ByteBuf haystack, ByteBuf needle) {
    for (int i = haystack.readerIndex(); i < haystack.writerIndex(); i ++) {
        int haystackIndex = i;
        int needleIndex;
        for (needleIndex = 0; needleIndex < needle.capacity(); needleIndex ++) {
            if (haystack.getByte(haystackIndex) != needle.getByte(needleIndex)) {
                break;
            } else {
                haystackIndex ++;
                if (haystackIndex == haystack.writerIndex() &&
                    needleIndex != needle.capacity() - 1) {
                    return -1;
                }
            }
        }

        if (needleIndex == needle.capacity()) {
            // Found the needle from the haystack!
            return i - haystack.readerIndex();
        }
    }
    return -1;
}
 
Example 4
Source Project: turbo-rpc   File: ByteBufOutput.java    License: Apache License 2.0 6 votes vote down vote up
/**
 * Sets the buffer that will be written to. The byte order, position and
 * capacity are set to match the specified buffer. The total is set to 0. The
 * {@link #setOutputStream(OutputStream) OutputStream} is set to null.
 * 
 * @param maxBufferSize
 *            The buffer is doubled as needed until it exceeds maxCapacity and
 *            an exception is thrown.
 */
public void setBuffer(ByteBuf buffer, int maxBufferSize) {
	if (buffer == null) {
		return;
	}

	if (maxBufferSize < -1) {
		throw new IllegalArgumentException("maxBufferSize cannot be < -1: " + maxBufferSize);
	}

	this.byteBuf = buffer;
	this.maxCapacity = maxBufferSize == -1 ? MAX_SAFE_ARRAY_SIZE : maxBufferSize;
	capacity = buffer.capacity();
	position = buffer.writerIndex();
	total = 0;
	outputStream = null;
}
 
Example 5
Source Project: extract   File: ResultDecoder.java    License: MIT License 6 votes vote down vote up
@Override
public Object decode(final ByteBuf buffer, final State state) throws IOException {
	String result = new String (new char[] {(char) buffer.readByte()});
	ExtractionStatus extractionStatus = ExtractionStatus.parse(result);
	if (buffer.capacity() == 1) {
		return new Report(extractionStatus);
	} else {
		buffer.readByte(); // pipe char |
		ByteBuf exceptionPayload = buffer.getBytes(0, new byte[buffer.capacity() - 2]);
		try (ObjectInputStream objectInputStream = new ObjectInputStream(new ByteBufInputStream(exceptionPayload))) {
			Exception ex = (Exception) objectInputStream.readObject();
			return new Report(extractionStatus, ex);
		} catch (ClassNotFoundException e) {
			logger.warn("cannot read object : ", e);
			return new Report(extractionStatus);
		}
	}
}
 
Example 6
@Override
public ResponseMessage deserializeResponse(final ByteBuf msg) throws SerializationException {
    try {
        final Kryo kryo = kryoThreadLocal.get();
        final byte[] payload = new byte[msg.capacity()];
        msg.readBytes(payload);
        try (final Input input = new Input(payload)) {
            final UUID requestId = kryo.readObjectOrNull(input, UUID.class);
            final int status = input.readShort();
            final String statusMsg = input.readString();
            final Map<String,Object> statusAttributes = (Map<String,Object>) kryo.readClassAndObject(input);
            final Object result = kryo.readClassAndObject(input);
            final Map<String,Object> metaAttributes = (Map<String,Object>) kryo.readClassAndObject(input);

            return ResponseMessage.build(requestId)
                    .code(ResponseStatusCode.getFromValue(status))
                    .statusMessage(statusMsg)
                    .statusAttributes(statusAttributes)
                    .result(result)
                    .responseMetaData(metaAttributes)
                    .create();
        }
    } catch (Exception ex) {
        logger.warn(String.format("Response [%s] could not be deserialized by %s.", msg, AbstractGryoMessageSerializerV1d0.class.getName()), ex);
        throw new SerializationException(ex);
    }
}
 
Example 7
Source Project: Bats   File: SpoolingRawBatchBuffer.java    License: Apache License 2.0 5 votes vote down vote up
public void writeToStream(FSDataOutputStream stream) throws IOException {
  Stopwatch watch = Stopwatch.createStarted();
  available = false;
  check = ThreadLocalRandom.current().nextLong();
  start = stream.getPos();
  logger.debug("Writing check value {} at position {}", check, start);
  stream.writeLong(check);
  batch.getHeader().writeDelimitedTo(stream);
  ByteBuf buf = batch.getBody();
  if (buf != null) {
    bodyLength = buf.capacity();
  } else {
    bodyLength = 0;
  }
  if (bodyLength > 0) {
    buf.getBytes(0, stream, bodyLength);
  }
  stream.hsync();
  FileStatus status = fs.getFileStatus(path);
  long len = status.getLen();
  logger.debug("After spooling batch, stream at position {}. File length {}", stream.getPos(), len);
  batch.sendOk();
  latch.countDown();
  long t = watch.elapsed(TimeUnit.MICROSECONDS);
  logger.debug("Took {} us to spool {} to disk. Rate {} mb/s", t, bodyLength, bodyLength / t);
  if (buf != null) {
    buf.release();
  }
}
 
Example 8
Source Project: DDMQ   File: FileRegionEncoder.java    License: Apache License 2.0 5 votes vote down vote up
/**
 * Encode a message into a {@link io.netty.buffer.ByteBuf}. This method will be called for each written message that
 * can be handled by this encoder.
 *
 * @param ctx the {@link io.netty.channel.ChannelHandlerContext} which this {@link
 * io.netty.handler.codec.MessageToByteEncoder} belongs to
 * @param msg the message to encode
 * @param out the {@link io.netty.buffer.ByteBuf} into which the encoded message will be written
 * @throws Exception is thrown if an error occurs
 */
@Override
protected void encode(ChannelHandlerContext ctx, FileRegion msg, final ByteBuf out) throws Exception {
    WritableByteChannel writableByteChannel = new WritableByteChannel() {
        @Override
        public int write(ByteBuffer src) throws IOException {
            out.writeBytes(src);
            return out.capacity();
        }

        @Override
        public boolean isOpen() {
            return true;
        }

        @Override
        public void close() throws IOException {
        }
    };

    long toTransfer = msg.count();

    while (true) {
        long transferred = msg.transfered();
        if (toTransfer - transferred <= 0) {
            break;
        }
        msg.transferTo(writableByteChannel, transferred);
    }
}
 
Example 9
/**
 * Encode a message into a {@link io.netty.buffer.ByteBuf}. This method will be called for each written message that
 * can be handled by this encoder.将消息编码到ByteBuf中。对于可由此编码器处理的每个书面消息,将调用此方法。
 *
 * @param ctx the {@link io.netty.channel.ChannelHandlerContext} which this {@link
 * io.netty.handler.codec.MessageToByteEncoder} belongs to
 * @param msg the message to encode
 * @param out the {@link io.netty.buffer.ByteBuf} into which the encoded message will be written
 * @throws Exception is thrown if an error occurs
 */
@Override
protected void encode(ChannelHandlerContext ctx, FileRegion msg, final ByteBuf out) throws Exception {
    WritableByteChannel writableByteChannel = new WritableByteChannel() {
        @Override
        public int write(ByteBuffer src) throws IOException {
            out.writeBytes(src);
            return out.capacity();
        }

        @Override
        public boolean isOpen() {
            return true;
        }

        @Override
        public void close() throws IOException {
        }
    };

    long toTransfer = msg.count();

    while (true) {
        long transferred = msg.transfered();
        if (toTransfer - transferred <= 0) {
            break;
        }
        msg.transferTo(writableByteChannel, transferred);
    }
}
 
Example 10
/**
 * Sends a message either on behalf of the client or on behalf of the broker (Will Messages)
 * @param messageId
 * @param topic
 * @param qos
 * @param payload
 * @param retain
 * @param internal if true means on behalf of the broker (skips authorisation) and does not return ack.
 * @throws Exception
 */
void sendInternal(int messageId, String topic, int qos, ByteBuf payload, boolean retain, boolean internal) throws Exception {
   synchronized (lock) {
      Message serverMessage = MQTTUtil.createServerMessageFromByteBuf(session, topic, retain, qos, payload);

      if (qos > 0) {
         serverMessage.setDurable(MQTTUtil.DURABLE_MESSAGES);
      }

      if (qos < 2 || !state.getPubRec().contains(messageId)) {
         if (qos == 2 && !internal)
            state.getPubRec().add(messageId);

         Transaction tx = session.getServerSession().newTransaction();
         try {
            if (internal) {
               session.getServer().getPostOffice().route(serverMessage, tx, true);
            } else {
               session.getServerSession().send(tx, serverMessage, true, false);
            }

            if (retain) {
               boolean reset = payload instanceof EmptyByteBuf || payload.capacity() == 0;
               session.getRetainMessageManager().handleRetainedMessage(serverMessage, topic, reset, tx);
            }
            tx.commit();
         } catch (Throwable t) {
            logger.warn(t.getMessage(), t);
            tx.rollback();
            throw t;
         }
         createMessageAck(messageId, qos, internal);
      }
   }
}
 
Example 11
Source Project: rocketmq-read   File: FileRegionEncoder.java    License: Apache License 2.0 5 votes vote down vote up
/**
 * Encode a message into a {@link io.netty.buffer.ByteBuf}. This method will be called for each written message that
 * can be handled by this encoder.
 *
 * @param ctx the {@link io.netty.channel.ChannelHandlerContext} which this {@link
 * io.netty.handler.codec.MessageToByteEncoder} belongs to
 * @param msg the message to encode
 * @param out the {@link io.netty.buffer.ByteBuf} into which the encoded message will be written
 * @throws Exception is thrown if an error occurs
 */
@Override
protected void encode(ChannelHandlerContext ctx, FileRegion msg, final ByteBuf out) throws Exception {
    WritableByteChannel writableByteChannel = new WritableByteChannel() {
        @Override
        public int write(ByteBuffer src) throws IOException {
            out.writeBytes(src);
            return out.capacity();
        }

        @Override
        public boolean isOpen() {
            return true;
        }

        @Override
        public void close() throws IOException {
        }
    };

    long toTransfer = msg.count();

    while (true) {
        long transferred = msg.transfered();
        if (toTransfer - transferred <= 0) {
            break;
        }
        msg.transferTo(writableByteChannel, transferred);
    }
}
 
Example 12
Source Project: netty-4.1.22   File: MsgEchoPeerHandler.java    License: Apache License 2.0 5 votes vote down vote up
public MsgEchoPeerHandler(final int messageSize) {
    super(false);
    final ByteBuf byteBuf = Unpooled.buffer(messageSize);
    for (int i = 0; i < byteBuf.capacity(); i++) {
        byteBuf.writeByte((byte) i);
    }
    message = new UdtMessage(byteBuf);
}
 
Example 13
Source Project: tajo   File: ByteBufLineReader.java    License: Apache License 2.0 5 votes vote down vote up
public ByteBufLineReader(InputChannel channel, ByteBuf buf) {
  this.readBytes = 0;
  this.channel = channel;
  this.buffer = buf;
  this.bufferSize = buf.capacity();
  if (channel instanceof SeekableChannel) {
    seekableChannel = (SeekableChannel) channel;
  } else {
    seekableChannel = null;
  }
}
 
Example 14
Source Project: netty.book.kor   File: DynamicByteBufferTest.java    License: MIT License 5 votes vote down vote up
private void testBuffer(ByteBuf buf, boolean isDirect) {
        assertEquals(11, buf.capacity());
        assertEquals(isDirect, buf.isDirect());
        
        String sourceData = "hello world";

        buf.writeBytes(sourceData.getBytes());
        assertEquals(11, buf.readableBytes());
        assertEquals(0, buf.writableBytes());

        assertEquals(sourceData, buf.toString(Charset.defaultCharset()));

        buf.capacity(6);
        assertEquals("hello ", buf.toString(Charset.defaultCharset()));

        assertEquals(6, buf.capacity());

        buf.capacity(13);
        assertEquals("hello ", buf.toString(Charset.defaultCharset()));

        buf.writeBytes("world".getBytes());
        assertEquals(sourceData, buf.toString(Charset.defaultCharset()));

        assertEquals(13, buf.capacity());
        assertEquals(2, buf.writableBytes());

        // FIXME expected raised exception but just passed.
//        assertNotNull(buf.writeBytes("hello world test".getBytes()));
    }
 
Example 15
/**
 * Returns slices with the specified sizes of the given buffer.
 * <p/>
 * 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;
}
 
Example 16
@Override
public ResponseMessage deserializeResponse(final ByteBuf msg) throws SerializationException {
    try {
        final Kryo kryo = kryoThreadLocal.get();
        final byte[] payload = new byte[msg.capacity()];
        msg.readBytes(payload);
        try (final Input input = new Input(payload)) {
            return kryo.readObject(input, ResponseMessage.class);
        }
    } catch (Exception ex) {
        logger.warn(String.format("Response [%s] could not be deserialized by %s.", msg, AbstractGryoMessageSerializerV3d0.class.getName()), ex);
        throw new SerializationException(ex);
    }
}
 
Example 17
Source Project: tajo   File: ByteBufLineReader.java    License: Apache License 2.0 4 votes vote down vote up
public ByteBufLineReader(ByteBufInputChannel channel, ByteBuf buf) {
  this.readBytes = 0;
  this.channel = channel;
  this.buffer = buf;
  this.bufferSize = buf.capacity();
}
 
Example 18
Source Project: hanboDB   File: BulkReply.java    License: Apache License 2.0 4 votes vote down vote up
public BulkReply(ByteBuf bytes) {
  this.bytes = bytes;
  capacity = bytes.capacity();
}
 
Example 19
Source Project: sctalk   File: ClientPacketDecoder.java    License: Apache License 2.0 4 votes vote down vote up
@Override
protected void decode(final ChannelHandlerContext ctx, final ByteBuf in, final List<Object> out) throws Exception {

    try {

        logger.trace("Protobuf decode started.");
        in.markReaderIndex();
        if (in.readableBytes() < 4) {
            logger.debug("Readable Bytes length less than 4 bytes, ignored");
            in.resetReaderIndex();
            return;
        }

        DataBuffer dataBuf = new DataBuffer(in);

        IMHeader header = new IMHeader();
        header.decode(dataBuf);

        if (header.getLength() < 0) {
            ctx.close();
            logger.error("message length less than 0, channel closed");
            return;
        }

        ByteBuf byteBuf = ctx.alloc().buffer(header.getLength() - SysConstant.PROTOCOL_HEADER_LENGTH);

        in.readBytes(byteBuf);
        byte[] body;
        if (byteBuf.hasArray()) {
            body = byteBuf.array();
        } else {
            body = new byte[byteBuf.capacity()];
            byteBuf.readBytes(body);
        }

        ClientMessage<ByteString> protoMessage = new ClientMessage<>(header, ByteString.copyFrom(body));
        out.add(protoMessage);
        
        logger.trace("Received protobuf : length={}, commandId={}", header.getLength(), header.getCommandId());
    } catch (Exception e) {
        logger.error(ctx.channel().remoteAddress() + ",decode failed.", e);
    } finally {
        logger.trace("Protobuf decode finished.");
    }
}
 
Example 20
Source Project: cava   File: MutableBytes.java    License: Apache License 2.0 3 votes vote down vote up
/**
 * Wrap a full Netty {@link ByteBuf} as a {@link MutableBytes} value.
 *
 * <p>
 * Note that any change to the content of the buffer may be reflected in the returned value.
 *
 * @param byteBuf The {@link ByteBuf} to wrap.
 * @return A {@link MutableBytes} value.
 */
static MutableBytes wrapByteBuf(ByteBuf byteBuf) {
  checkNotNull(byteBuf);
  if (byteBuf.capacity() == 0) {
    return EMPTY;
  }
  return new MutableByteBufWrappingBytes(byteBuf);
}