Refine search
@Override public byte[] getData() { if (payload.arrayOffset() == 0 && payload.capacity() == payload.array().length) { return payload.array(); } else { // Need to copy into a smaller byte array byte[] data = new byte[payload.readableBytes()]; payload.readBytes(data); return data; } }
public ByteArrayInputStream getPacketByteArray( ByteBuf stream, int readerIndex, int readableBytes ) { final ByteArrayInputStream bytes; if( stream.hasArray() ) { bytes = new ByteArrayInputStream( stream.array(), readerIndex, readableBytes ); } else { byte[] data = new byte[stream.capacity()]; stream.getBytes( readerIndex, data, 0, readableBytes ); bytes = new ByteArrayInputStream( data ); } return bytes; }
public NettyByteBufBuffer(ByteBuf buf) { super(buf.array(), buf.arrayOffset(), buf.capacity()); this.buf = buf; }
/** * This builds a tag with the actual data that should be sent to the client for update syncs. * If the tile entity doesn't need update syncs, it returns null. */ private NBTTagCompound writeUpdateData() { final NBTTagCompound data = new NBTTagCompound(); final ByteBuf stream = Unpooled.buffer(); try { this.writeToStream( stream ); if( stream.readableBytes() == 0 ) { return null; } } catch( final Throwable t ) { AELog.debug( t ); } stream.capacity( stream.readableBytes() ); data.setByteArray( "X", stream.array() ); return data; }
@Override protected void encode( ChannelHandlerContext ctx, MessageNano msg, List<Object> out) throws Exception { final int size = msg.getSerializedSize(); final ByteBuf buffer = ctx.alloc().heapBuffer(size, size); final byte[] array = buffer.array(); CodedOutputByteBufferNano cobbn = CodedOutputByteBufferNano.newInstance(array, buffer.arrayOffset(), buffer.capacity()); msg.writeTo(cobbn); buffer.writerIndex(size); out.add(buffer); } }
public ByteArrayInputStream getPacketByteArray(ByteBuf stream, int readerIndex, int readableBytes) { final ByteArrayInputStream bytes; if (stream.hasArray()) { bytes = new ByteArrayInputStream(stream.array(), readerIndex, readableBytes); } else { byte[] data = new byte[stream.capacity()]; stream.getBytes(readerIndex, data, 0, readableBytes); bytes = new ByteArrayInputStream(data); } return bytes; }
@Override protected void encode( ChannelHandlerContext ctx, MessageNano msg, List<Object> out) throws Exception { final int size = msg.getSerializedSize(); final ByteBuf buffer = ctx.alloc().heapBuffer(size, size); final byte[] array = buffer.array(); CodedOutputByteBufferNano cobbn = CodedOutputByteBufferNano.newInstance(array, buffer.arrayOffset(), buffer.capacity()); msg.writeTo(cobbn); buffer.writerIndex(size); out.add(buffer); } }
@Before public void before() { BYTE_ENCODE = Unpooled.wrappedBuffer(Base64.decode(STRING_ENCODE, Base64.DONT_BREAK_LINES | Base64.URL_SAFE)); // some extra caution here, nothing else, to make sure we would get the same encoding back Assert.assertEquals(STRING_ENCODE, encodeString(BYTE_ENCODE.array())); BYTE_ENCODE.readerIndex(0).writerIndex(BYTE_ENCODE.capacity()); }
/** * Return an array of the underlying storage from {@code buf} into a byte array. * The copy will start at {@code start} and copy {@code length} bytes. * If {@code copy} is true a copy will be made of the memory. * If {@code copy} is false the underlying storage will be shared, if possible. */ public static byte[] getBytes(ByteBuf buf, int start, int length, boolean copy) { int capacity = buf.capacity(); if (isOutOfBounds(start, length, capacity)) { throw new IndexOutOfBoundsException("expected: " + "0 <= start(" + start + ") <= start + length(" + length + ") <= " + "buf.capacity(" + capacity + ')'); } if (buf.hasArray()) { if (copy || start != 0 || length != capacity) { int baseOffset = buf.arrayOffset() + start; return Arrays.copyOfRange(buf.array(), baseOffset, baseOffset + length); } else { return buf.array(); } } byte[] v = PlatformDependent.allocateUninitializedArray(length); buf.getBytes(start, v); return v; }
/** * Return an array of the underlying storage from {@code buf} into a byte array. * The copy will start at {@code start} and copy {@code length} bytes. * If {@code copy} is true a copy will be made of the memory. * If {@code copy} is false the underlying storage will be shared, if possible. */ public static byte[] getBytes(ByteBuf buf, int start, int length, boolean copy) { if (isOutOfBounds(start, length, buf.capacity())) { throw new IndexOutOfBoundsException("expected: " + "0 <= start(" + start + ") <= start + length(" + length + ") <= " + "buf.capacity(" + buf.capacity() + ')'); } if (buf.hasArray()) { if (copy || start != 0 || length != buf.capacity()) { int baseOffset = buf.arrayOffset() + start; return Arrays.copyOfRange(buf.array(), baseOffset, baseOffset + length); } else { return buf.array(); } } byte[] v = new byte[length]; buf.getBytes(start, v); return v; }
@Override protected void encode( ChannelHandlerContext ctx, MessageNano msg, List<Object> out) throws Exception { final int size = msg.getSerializedSize(); final ByteBuf buffer = ctx.alloc().heapBuffer(size, size); final byte[] array = buffer.array(); CodedOutputByteBufferNano cobbn = CodedOutputByteBufferNano.newInstance(array, buffer.arrayOffset(), buffer.capacity()); msg.writeTo(cobbn); buffer.writerIndex(size); out.add(buffer); } }
/** * Return an array of the underlying storage from {@code buf} into a byte array. * The copy will start at {@code start} and copy {@code length} bytes. * If {@code copy} is true a copy will be made of the memory. * If {@code copy} is false the underlying storage will be shared, if possible. */ public static byte[] getBytes(ByteBuf buf, int start, int length, boolean copy) { int capacity = buf.capacity(); if (isOutOfBounds(start, length, capacity)) { throw new IndexOutOfBoundsException("expected: " + "0 <= start(" + start + ") <= start + length(" + length + ") <= " + "buf.capacity(" + capacity + ')'); } if (buf.hasArray()) { if (copy || start != 0 || length != capacity) { int baseOffset = buf.arrayOffset() + start; return Arrays.copyOfRange(buf.array(), baseOffset, baseOffset + length); } else { return buf.array(); } } byte[] v = PlatformDependent.allocateUninitializedArray(length); buf.getBytes(start, v); return v; }
@Override public ByteBuf setBytes(int index, ByteBuf src, int srcIndex, int length) { checkSrcIndex(index, length, srcIndex, src.capacity()); if (src.hasMemoryAddress()) { PlatformDependent.copyMemory(src.memoryAddress() + srcIndex, array, index, length); } else if (src.hasArray()) { setBytes(index, src.array(), src.arrayOffset() + srcIndex, length); } else { src.getBytes(srcIndex, array, index, length); } return this; }
@Override public ByteBuf getBytes(int index, ByteBuf dst, int dstIndex, int length) { checkDstIndex(index, length, dstIndex, dst.capacity()); if (dst.hasMemoryAddress()) { PlatformDependent.copyMemory(array, index, dst.memoryAddress() + dstIndex, length); } else if (dst.hasArray()) { getBytes(index, dst.array(), dst.arrayOffset() + dstIndex, length); } else { dst.setBytes(dstIndex, array, index, length); } return this; }
@Override public ByteBuf getBytes(int index, ByteBuf dst, int dstIndex, int length) { checkDstIndex(index, length, dstIndex, dst.capacity()); if (dst.hasArray()) { getBytes(index, dst.array(), dst.arrayOffset() + dstIndex, length); } else if (dst.nioBufferCount() > 0) { for (ByteBuffer bb: dst.nioBuffers(dstIndex, length)) { int bbLen = bb.remaining(); getBytes(index, bb); index += bbLen; } } else { dst.setBytes(dstIndex, this, index, length); } return this; }
@Override public ByteBuf getBytes(int index, ByteBuf dst, int dstIndex, int length) { checkDstIndex(index, length, dstIndex, dst.capacity()); if (dst.hasArray()) { getBytes(index, dst.array(), dst.arrayOffset() + dstIndex, length); } else if (dst.nioBufferCount() > 0) { for (ByteBuffer bb: dst.nioBuffers(dstIndex, length)) { int bbLen = bb.remaining(); getBytes(index, bb); index += bbLen; } } else { dst.setBytes(dstIndex, this, index, length); } return this; }
@Override public ByteBuf getBytes(int index, ByteBuf dst, int dstIndex, int length) { checkDstIndex(index, length, dstIndex, dst.capacity()); if (dst.hasArray()) { getBytes(index, dst.array(), dst.arrayOffset() + dstIndex, length); } else if (dst.nioBufferCount() > 0) { for (ByteBuffer bb: dst.nioBuffers(dstIndex, length)) { int bbLen = bb.remaining(); getBytes(index, bb); index += bbLen; } } else { dst.setBytes(dstIndex, this, index, length); } return this; }
@Override public ByteBuf setBytes(int index, ByteBuf src, int srcIndex, int length) { checkSrcIndex(index, length, srcIndex, src.capacity()); if (src.hasArray()) { setBytes(index, src.array(), src.arrayOffset() + srcIndex, length); } else if (src.nioBufferCount() > 0) { for (ByteBuffer bb: src.nioBuffers(srcIndex, length)) { int bbLen = bb.remaining(); setBytes(index, bb); index += bbLen; } } else { src.getBytes(srcIndex, this, index, length); } return this; }
static void getBytes(AbstractByteBuf buf, long addr, int index, ByteBuf dst, int dstIndex, int length) { buf.checkIndex(index, length); checkNotNull(dst, "dst"); if (isOutOfBounds(dstIndex, length, dst.capacity())) { throw new IndexOutOfBoundsException("dstIndex: " + dstIndex); } if (dst.hasMemoryAddress()) { PlatformDependent.copyMemory(addr, dst.memoryAddress() + dstIndex, length); } else if (dst.hasArray()) { PlatformDependent.copyMemory(addr, dst.array(), dst.arrayOffset() + dstIndex, length); } else { dst.setBytes(dstIndex, buf, index, length); } }
@Override public ByteBuf getBytes(int index, ByteBuf dst, int dstIndex, int length) { checkIndex(index, length); if (dst == null) { throw new NullPointerException("dst"); } if (dstIndex < 0 || dstIndex > dst.capacity() - length) { throw new IndexOutOfBoundsException("dstIndex: " + dstIndex); } if (dst.hasMemoryAddress()) { PlatformDependent.copyMemory(addr(index), dst.memoryAddress() + dstIndex, length); } else if (dst.hasArray()) { PlatformDependent.copyMemory(addr(index), dst.array(), dst.arrayOffset() + dstIndex, length); } else { dst.setBytes(dstIndex, this, index, length); } return this; }