/** * @see ByteBuffer#hasRemaining() */ public boolean hasRemaining() { return remaining() > 0; }
@Override public Integer decode(IoBuffer input) { if (input.remaining() < 4) { return null; } int out = 0; for (int i = 0; i < 32; i += 8) { out |= (input.get() & 0xff) << (bo == ByteOrder.BIG_ENDIAN ? 24 - i : i); } return out; } }
@Override public int read(byte[] b, int off, int len) throws IOException { if (!hasRemaining()) { return -1; } int toRead = Math.min(remaining(), len); get(b, off, toRead); return toRead; } };
public <L extends TBase<?, ?>> L get(Class<L> clazz) throws InstantiationException, IllegalAccessException, TException { L object = clazz.newInstance(); byte array[] = new byte[buffer.remaining()]; buffer.get(array); deserializer.deserialize(object, array); return object; } }
/** * @see ByteBuffer#put(ByteBuffer) */ public IoBuffer put(ByteBuffer src) { if (remaining() < src.remaining()) { throw new BufferOverflowException(); } if (isReadOnly()) { throw new ReadOnlyBufferException(); } while (src.hasRemaining()) { put(src.get()); } return this; }
/** * @see ByteBuffer#put(ByteBuffer) */ public IoBuffer put(IoBuffer src) { if (src == this) { // NOSONAR, checking the instance throw new IllegalArgumentException(); } if (remaining() < src.remaining()) { throw new BufferOverflowException(); } if (isReadOnly()) { throw new ReadOnlyBufferException(); } while (src.hasRemaining()) { put(src.get()); } return this; }
/** * {@inheritDoc} */ @Override public OUTPUT decode(IoBuffer input) { OUTPUT object; try { byte array[] = new byte[input.remaining()]; input.get(array); object = clazz.newInstance(); deserializer.deserialize(object, array); return object; } catch (TException e) { throw new ProtocolDecoderException(e); } catch (InstantiationException e) { throw new ProtocolDecoderException(e); } catch (IllegalAccessException e) { throw new ProtocolDecoderException(e); } }
/** * @see ByteBuffer#get(byte[], int,int) */ public IoBuffer get(byte[] dst, int offset, int length) { if (remaining() < length) { throw new BufferUnderflowException(); } int remainsToCopy = length; int currentOffset = offset; while (remainsToCopy > 0) { position.updatePos(); position.getNode().getBuffer().position(position.getPositionInNode()); ByteBuffer currentBuffer = position.getNode().getBuffer(); int blocksize = Math.min(remainsToCopy, currentBuffer.remaining()); position.getNode().getBuffer().get(dst, currentOffset, blocksize); currentOffset += blocksize; remainsToCopy -= blocksize; position.incrementPosition(blocksize); position.getNode().getBuffer().position(0); } return this; }
/** * @see ByteBuffer#put(byte[], int, int) */ public IoBuffer put(byte[] src, int offset, int length) { if (readonly) { throw new ReadOnlyBufferException(); } if (remaining() < length) { throw new BufferUnderflowException(); } int remainsToCopy = length; int currentOffset = offset; position.getNode().getBuffer().position(position.getPositionInNode()); while (remainsToCopy > 0) { position.updatePos(); ByteBuffer currentBuffer = position.getNode().getBuffer(); int blocksize = Math.min(remainsToCopy, currentBuffer.remaining()); position.getNode().getBuffer().put(src, currentOffset, blocksize); currentOffset += blocksize; remainsToCopy -= blocksize; position.incrementPosition(blocksize); } position.getNode().getBuffer().position(0); return this; }
/** * @see ByteBuffer#compact() */ public IoBuffer compact() { for (int i = 0; i < remaining(); i++) { put(i, get(i + position.getPosition())); } position(limit() - position()); limit(capacity); mark = null; return this; }
/** * {@inheritDoc} */ @Override public boolean equals(Object ob) { if (this == ob) { return true; } if (!(ob instanceof IoBuffer)) { return false; } IoBuffer that = (IoBuffer) ob; if (this.remaining() != that.remaining()) { return false; } int p = this.position(); int q = that.position(); while (this.hasRemaining() && that.hasRemaining()) { if (this.get() != that.get()) { this.position(p); that.position(q); return false; } } this.position(p); that.position(q); return true; }
@Test public void testSlice() { ByteBuffer bb1 = ByteBuffer.allocate(5); bb1.put("012".getBytes()); bb1.flip(); ByteBuffer bb2 = ByteBuffer.allocate(5); bb2.put("345".getBytes()); bb2.flip(); ByteBuffer bb3 = ByteBuffer.allocate(5); bb3.put("6789".getBytes()); bb3.flip(); IoBuffer ioBuffer = IoBuffer.newInstance(); ioBuffer.add(bb1, bb2).add(bb3); ioBuffer.position(2); ioBuffer.limit(8); IoBuffer slice = ioBuffer.slice(); assertEquals(6, slice.remaining()); assertEquals(0, slice.position()); assertEquals(6, slice.limit()); byte seg[] = "234567".getBytes(); for (int i = 0; i < 6; i++) { assertEquals(seg[i], slice.get(i)); } }
@Override public OUT decode(IoBuffer input, MutableInt nextBlockSize) { OUT output = null; if (nextBlockSize.getValue() == null) { nextBlockSize.setValue(sizeDecoder.decode(input)); } if (nextBlockSize.isDefined() && (input.remaining() >= nextBlockSize.getValue())) { IoBuffer buffer = input.slice(); buffer.limit(buffer.position() + nextBlockSize.getValue()); output = payloadDecoder.decode(buffer); nextBlockSize.reset(); } return output; }
@Test public void testExtendedValues() { for (int value : new int[] { 0, 1, 127, 128, 65536, 198649, Integer.MAX_VALUE }) { ByteBuffer buffer = encoder.encode(value); for (int i = 1; i < 5; i++) { int size = buffer.remaining() + i; IoBuffer extendedBuffer = IoBuffer.wrap(ByteBuffer.allocate(size)); int start = extendedBuffer.position(); extendedBuffer.put(buffer.slice()); extendedBuffer.position(start); extendedBuffer.limit(start + size); try { decoder.decode(extendedBuffer); assertEquals(i, extendedBuffer.remaining()); } catch (ProtocolDecoderException e) { fail("Should not throw exception"); } } } }
@Test public void testTruncatedValues() { for (int value : new int[] { 0, 1, 127, 128, 65536, 198649, Integer.MAX_VALUE }) { IoBuffer buffer = IoBuffer.wrap(encoder.encode(value)); for (int i = 0; i < buffer.remaining(); i++) { IoBuffer partialBuffer = buffer.slice(); partialBuffer.limit(partialBuffer.position() + i); try { assertNull(decoder.decode(partialBuffer)); } catch (ProtocolDecoderException e) { fail("Should not throw exception"); } } } }