List of usage examples for io.netty.buffer ByteBuf readerIndex
public abstract ByteBuf readerIndex(int readerIndex);
From source file:net.tomp2p.storage.AlternativeCompositeByteBuf.java
License:Apache License
public List<ByteBuf> decompose(int offset, int length) { checkIndex(offset, length);//w ww.j a v a 2s . c om if (length == 0) { return Collections.emptyList(); } int componentId = findIndex(offset); List<ByteBuf> slice = new ArrayList<ByteBuf>(components.size()); // The first component Component firstC = components.get(componentId); ByteBuf first = firstC.buf.duplicate(); first.readerIndex(offset - firstC.offset); ByteBuf buf = first; int bytesToSlice = length; do { int readableBytes = buf.readableBytes(); if (bytesToSlice <= readableBytes) { // Last component buf.writerIndex(buf.readerIndex() + bytesToSlice); slice.add(buf); break; } else { // Not the last component slice.add(buf); bytesToSlice -= readableBytes; componentId++; // Fetch the next component. buf = components.get(componentId).buf.duplicate(); } } while (bytesToSlice > 0); // Slice all components because only readable bytes are interesting. for (int i = 0; i < slice.size(); i++) { slice.set(i, slice.get(i).slice()); } return slice; }
From source file:nl.matsv.viabackwards.protocol.protocol1_9_4to1_10.chunks.Chunk1_10Type.java
License:Open Source License
@Override public void write(ByteBuf output, ClientWorld world, Chunk input) throws Exception { if (!(input instanceof Chunk1_10)) throw new IllegalClassException( "Tried to send the wrong chunk type from 1.9.3-4 chunk: " + input.getClass()); Chunk1_10 chunk = (Chunk1_10) input; output.writeInt(chunk.getX());// w ww .j av a 2 s . c om output.writeInt(chunk.getZ()); output.writeBoolean(chunk.isGroundUp()); Type.VAR_INT.write(output, chunk.getBitmask()); ByteBuf buf = Unpooled.buffer(); for (int i = 0; i < 16; i++) { ChunkSection section = chunk.getSections()[i]; if (section == null) continue; // Section not set section.writeBlocks(buf); section.writeBlockLight(buf); if (!section.hasSkyLight()) continue; // No sky light, we're done here. section.writeSkyLight(buf); } buf.readerIndex(0); Type.VAR_INT.write(output, buf.readableBytes() + (chunk.isBiomeData() ? 256 : 0)); output.writeBytes(buf); buf.release(); // release buffer // Write biome data if (chunk.isBiomeData()) { output.writeBytes(chunk.getBiomeData()); } Type.NBT_ARRAY.write(output, chunk.getBlockEntities().toArray(new CompoundTag[0])); }
From source file:nl.matsv.viabackwards.protocol.protocol1_9_4to1_10.chunks.ChunkSection1_10.java
License:Open Source License
private int countBytes(int value) throws Exception { // Count amount of bytes that would be sent if the value were sent as a VarInt ByteBuf buf = Unpooled.buffer(); Type.VAR_INT.write(buf, value); buf.readerIndex(0); int bitCount = buf.readableBytes(); buf.release();//ww w . j a v a 2 s . c o m return bitCount; }
From source file:openbns.commons.net.codec.sts.HttpObjectDecoder.java
License:Apache License
private static void skipControlCharacters(ByteBuf buffer) { for (;;) {/*from ww w . ja va2 s . c o m*/ char c = (char) buffer.readUnsignedByte(); if (!Character.isISOControl(c) && !Character.isWhitespace(c)) { buffer.readerIndex(buffer.readerIndex() - 1); break; } } }
From source file:org.aotorrent.common.Piece.java
License:Apache License
public ByteBuf read(int offset, int length) throws IOException { //LOGGER.debug("READ is complete = " + isComplete()); if (isComplete()) { ByteBuf bb = (softBuffer != null) ? softBuffer.get() : null; if (bb == null) { bb = torrent.getFileStorage().read(index, pieceLength); softBuffer = new SoftReference<>(bb); torrent.missCache();//from ww w.j a v a 2s .c o m } else { torrent.hitCache(); } ByteBuf buf = Unpooled.buffer(length, length); bb.readerIndex(offset); bb.readBytes(buf); return buf; } else { return Unpooled.EMPTY_BUFFER; } }
From source file:org.apache.activemq.artemis.core.message.impl.CoreMessage.java
License:Apache License
/** * Recast the message as an 1.4 message//w ww . j ava 2 s . co m */ @Override public void sendBuffer_1X(ByteBuf sendBuffer) { checkEncode(); ByteBuf tmpBuffer = buffer.duplicate(); sendBuffer.writeInt(endOfBodyPosition + DataConstants.SIZE_INT); tmpBuffer.readerIndex(DataConstants.SIZE_INT); tmpBuffer.readBytes(sendBuffer, endOfBodyPosition - BUFFER_HEADER_SPACE); sendBuffer.writeInt(tmpBuffer.writerIndex() + DataConstants.SIZE_INT + BUFFER_HEADER_SPACE); tmpBuffer.readBytes(sendBuffer, tmpBuffer.readableBytes()); sendBuffer.readerIndex(0); }
From source file:org.apache.activemq.artemis.core.message.impl.MessageImpl.java
License:Apache License
@Override public synchronized ActiveMQBuffer getBodyBufferDuplicate() { // Must copy buffer before sending it ByteBuf byteBuf = ChannelBufferWrapper.unwrap(getBodyBuffer().byteBuf()); byteBuf = byteBuf.duplicate();// w ww .ja va 2 s. c om byteBuf.readerIndex(getBodyBuffer().readerIndex()); byteBuf.writerIndex(getBodyBuffer().writerIndex()); return new ResetLimitWrappedActiveMQBuffer(BODY_OFFSET, byteBuf, null); }
From source file:org.apache.activemq.artemis.message.CoreMessageTest.java
License:Apache License
public void testChangeBodyString(String newString) { CoreMessage coreMessage = decodeMessage(); coreMessage.putStringProperty("newProperty", "newValue"); ActiveMQBuffer legacyBuffer = coreMessage.getBodyBuffer(); legacyBuffer.resetWriterIndex();/*from ww w . ja va 2 s .c o m*/ legacyBuffer.clear(); TextMessageUtil.writeBodyText(legacyBuffer, SimpleString.toSimpleString(newString)); ByteBuf newbuffer = Unpooled.buffer(150000); coreMessage.sendBuffer(newbuffer, 0); newbuffer.readerIndex(0); CoreMessage newCoreMessage = new CoreMessage(); newCoreMessage.receiveBuffer(newbuffer); SimpleString newText = TextMessageUtil.readBodyText(newCoreMessage.getReadOnlyBodyBuffer()); Assert.assertEquals(newString, newText.toString()); // coreMessage.putStringProperty() }