/** * @see ByteBuffer#get(byte[]) */ public IoBuffer get(byte[] dst) { get(dst, 0, dst.length); return this; }
/** * @see ByteBuffer#get(int) */ public byte get(int index) { if (index >= limit.getPosition()) { throw new IndexOutOfBoundsException(); } return get(getPointerByPosition(index)); }
private short getShort(Pointer pos) { if (pos.getPosition() > capacity - Short.SIZE / Byte.SIZE) { throw new BufferUnderflowException(); } if (bo == ByteOrder.BIG_ENDIAN) { return (short) ((get(pos) & BYTE_MASK) << Byte.SIZE | (get(pos) & BYTE_MASK)); } else { return (short) ((get(pos) & BYTE_MASK) | (get(pos) & BYTE_MASK) << Byte.SIZE); } }
@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() throws IOException { return hasRemaining() ? get() & BYTE_MASK : -1; }
@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#get() */ public byte get() { if (!hasRemaining()) { throw new BufferUnderflowException(); } return get(position); }
private int getInt(Pointer pos) { if (pos.getPosition() > capacity - Integer.SIZE / Byte.SIZE) { throw new BufferUnderflowException(); } int out = 0; for (int i = 0; i < Integer.SIZE; i += Byte.SIZE) { out |= (get(pos) & BYTE_MASK) << (bo == ByteOrder.BIG_ENDIAN ? (Integer.SIZE - Byte.SIZE) - i : i); } return out; }
private long getLong(Pointer pos) { if (pos.getPosition() > capacity - Long.SIZE / Byte.SIZE) { throw new BufferUnderflowException(); } long out = 0; for (int i = 0; i < Long.SIZE; i += Byte.SIZE) { out |= (get(pos) & BYTE_MASK_L) << (bo == ByteOrder.BIG_ENDIAN ? (Long.SIZE - Byte.SIZE) - i : i); } return out; }
/** * {@inheritDoc} */ @Override public int hashCode() { int hash = 0; Pointer oldPos = position.duplicate(); while (hasRemaining()) { hash *= 31; // NOSONAR, standard way of hashing hash += get(); } position = oldPos; return hash; }
/** * {@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); } }
/** * Test set position method over a buffer */ @Test public void testSetPositionBuffer() { ByteBuffer bb1 = ByteBuffer.allocate(4); bb1.put("012".getBytes()); bb1.flip(); ByteBuffer bb2 = ByteBuffer.allocate(4); bb2.put("3456".getBytes()); bb2.flip(); ByteBuffer bb3 = ByteBuffer.allocate(4); bb3.put("789".getBytes()); bb3.flip(); // The resulting buffer will be seen as "0123456789" IoBuffer ioBuffer = IoBuffer.wrap(bb1, bb2, bb3); // Check with random positions for (int i : new int[] { 4, 6, 7, 8, 3, 9, 1, 5, 0, 2 }) { ioBuffer.position(i); assertEquals('0' + i, ioBuffer.get()); } }
/** * Test the position method over a buffer */ @Test public void testPositionBuffer() { ByteBuffer bb1 = ByteBuffer.allocate(4); bb1.put("012".getBytes()); bb1.flip(); ByteBuffer bb2 = ByteBuffer.allocate(4); bb2.put("3456".getBytes()); bb2.flip(); ByteBuffer bb3 = ByteBuffer.allocate(4); bb3.put("789".getBytes()); bb3.flip(); // The resulting buffer will be seen as "0123456789" IoBuffer ioBuffer = IoBuffer.wrap(bb1, bb2, bb3); // Iterate and check the position for (int i = 0; i < ioBuffer.limit(); i++) { assertEquals(i, ioBuffer.position()); ioBuffer.get(); } }
/** * @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; }
/** * Test the flip() method */ @Test public void testFlip() { ByteBuffer bb1 = ByteBuffer.allocate(4); bb1.put("0123".getBytes()); bb1.flip(); ByteBuffer bb2 = ByteBuffer.allocate(4); bb2.put("4567".getBytes()); bb2.flip(); IoBuffer ioBuffer = IoBuffer.wrap(bb1, bb2); // Move forward a bit ioBuffer.get(); ioBuffer.get(); // Clear ioBuffer.clear(); // We should be back to the origin assertEquals(0, ioBuffer.position()); assertEquals(8, ioBuffer.limit()); }
@Test public void testGet() { 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); byte block[] = new byte[6]; ioBuffer.get(block); byte seg[] = "234567".getBytes(); for (int i = 0; i < 6; i++) { assertEquals(seg[i], block[i]); } }
/** * @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; }
/** * Test the clear() method */ @Test public void testClearEmptyBuffer() { ByteBuffer bb1 = ByteBuffer.allocate(4); bb1.put("012".getBytes()); bb1.flip(); ByteBuffer bb2 = ByteBuffer.allocate(4); bb2.put("345".getBytes()); bb2.flip(); IoBuffer ioBuffer = IoBuffer.wrap(bb1, bb2); assertEquals(6, ioBuffer.limit()); // Move forward a bit ioBuffer.get(); ioBuffer.get(); ioBuffer.limit(3); // Clear ioBuffer.clear(); // We should be back to the origin assertEquals(0, ioBuffer.position()); // The limit must back to the available size assertEquals(6, ioBuffer.limit()); }
/** * Test the get() method on a IoBuffer containing one ByteBuffer with 0 * bytes */ @Test public void testGetOneBuffer0Bytes() { ByteBuffer bb = ByteBuffer.allocate(0); IoBuffer ioBuffer = IoBuffer.wrap(bb); assertEquals(0, ioBuffer.position()); assertEquals(0, ioBuffer.limit()); try { assertFalse(ioBuffer.hasRemaining()); ioBuffer.get(); fail(); } catch (BufferUnderflowException bufe) { // expected assertEquals(0, ioBuffer.position()); } }