Java Code Examples for io.netty.buffer.ByteBuf#capacity()
The following examples show how to use
io.netty.buffer.ByteBuf#capacity() .
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: ByteBufWrappingBytes.java From incubator-tuweni with Apache License 2.0 | 6 votes |
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 File: ResultDecoder.java From extract with MIT License | 6 votes |
@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 3
Source File: ByteBufOutput.java From turbo-rpc with Apache License 2.0 | 6 votes |
/** * 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 4
Source File: DelimiterBasedFrameDecoder.java From netty4.0.27Learn with Apache License 2.0 | 6 votes |
/** * 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 5
Source File: ByteBufWrappingBytes.java From cava with Apache License 2.0 | 6 votes |
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 6
Source File: AbstractGryoMessageSerializerV1d0.java From tinkerpop with Apache License 2.0 | 5 votes |
@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 File: AbstractGryoMessageSerializerV3d0.java From tinkerpop with Apache License 2.0 | 5 votes |
@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 8
Source File: InboundEnvelopeDecoderTest.java From stratosphere with Apache License 2.0 | 5 votes |
/** * 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 9
Source File: DynamicByteBufferTest.java From netty.book.kor with MIT License | 5 votes |
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 10
Source File: ByteBufLineReader.java From tajo with Apache License 2.0 | 5 votes |
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 11
Source File: MsgEchoPeerHandler.java From netty-4.1.22 with Apache License 2.0 | 5 votes |
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 12
Source File: FileRegionEncoder.java From rocketmq-read with Apache License 2.0 | 5 votes |
/** * 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 13
Source File: MQTTPublishManager.java From activemq-artemis with Apache License 2.0 | 5 votes |
/** * 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 14
Source File: FileRegionEncoder.java From rocketmq-4.3.0 with Apache License 2.0 | 5 votes |
/** * 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 15
Source File: FileRegionEncoder.java From DDMQ with Apache License 2.0 | 5 votes |
/** * 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 16
Source File: SpoolingRawBatchBuffer.java From Bats with Apache License 2.0 | 5 votes |
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 17
Source File: ClientPacketDecoder.java From sctalk with Apache License 2.0 | 4 votes |
@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 18
Source File: BulkReply.java From hanboDB with Apache License 2.0 | 4 votes |
public BulkReply(ByteBuf bytes) { this.bytes = bytes; capacity = bytes.capacity(); }
Example 19
Source File: ByteBufLineReader.java From tajo with Apache License 2.0 | 4 votes |
public ByteBufLineReader(ByteBufInputChannel channel, ByteBuf buf) { this.readBytes = 0; this.channel = channel; this.buffer = buf; this.bufferSize = buf.capacity(); }
Example 20
Source File: MutableBytes.java From cava with Apache License 2.0 | 3 votes |
/** * 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); }