/** * Creates a byte array from the given ByteBuffer, the position property of input * {@link ByteBuffer} remains unchanged. * * @param buf source ByteBuffer * @return a newly created byte array */ public static byte[] newByteArrayFromByteBuffer(ByteBuffer buf) { final int length = buf.remaining(); byte[] bytes = new byte[length]; // transfer bytes from this buffer into the given destination array buf.duplicate().get(bytes, 0, length); return bytes; }
@Override public void aggregate(ByteBuffer buf, int position) { SerializablePairLongString pair = (SerializablePairLongString) selector.getObject(); if (pair != null && pair.lhs != null) { ByteBuffer mutationBuffer = buf.duplicate(); mutationBuffer.position(position); long lastTime = mutationBuffer.getLong(position); if (pair.lhs < lastTime) { mutationBuffer.putLong(position, pair.lhs); if (pair.rhs != null) { byte[] valueBytes = StringUtils.toUtf8(pair.rhs); mutationBuffer.putInt(position + Long.BYTES, valueBytes.length); mutationBuffer.position(position + Long.BYTES + Integer.BYTES); mutationBuffer.put(valueBytes); } else { mutationBuffer.putInt(position + Long.BYTES, 0); } } } }
@Override public Object get(ByteBuffer buf, int position) { ByteBuffer mutationBuffer = buf.duplicate(); mutationBuffer.position(position); Long timeValue = mutationBuffer.getLong(position); Integer stringSizeBytes = mutationBuffer.getInt(position + Long.BYTES); SerializablePairLongString serializablePair; if (stringSizeBytes > 0) { byte[] valueBytes = new byte[stringSizeBytes]; mutationBuffer.position(position + Long.BYTES + Integer.BYTES); mutationBuffer.get(valueBytes, 0, stringSizeBytes); serializablePair = new SerializablePairLongString(timeValue, StringUtils.fromUtf8(valueBytes)); } else { serializablePair = new SerializablePairLongString(timeValue, null); } return serializablePair; }
@Override public StaticBuffer getStaticBufferFlipBytes(int from, int to) { ByteBuffer b = buffer.duplicate(); b.flip(); Preconditions.checkArgument(from>=0 && from<=to); Preconditions.checkArgument(to<=b.limit()); for (int i=from;i<to;i++) b.put(i,(byte)~b.get(i)); return StaticArrayBuffer.of(b); } }
@Override public Object get(ByteBuffer buf, int position) { ByteBuffer mutationBuffer = buf.duplicate(); mutationBuffer.position(position); // | k (byte) | numLongs (int) | bitset (long[numLongs]) | int sizeBytes = 1 + Integer.BYTES + (buf.getInt(position + 1) * Long.BYTES); mutationBuffer.limit(position + sizeBytes); return mutationBuffer.slice(); }
@Override public DefaultDataBuffer read(byte[] destination, int offset, int length) { Assert.notNull(destination, "Byte array must not be null"); assertIndex(this.readPosition <= this.writePosition - length, "readPosition %d and length %d should be smaller than writePosition %d", this.readPosition, length, this.writePosition); ByteBuffer tmp = this.byteBuffer.duplicate(); int limit = this.readPosition + length; ((Buffer) tmp).clear().position(this.readPosition).limit(limit); tmp.get(destination, offset, length); this.readPosition += length; return this; }
@Override public void _parseDetails(ByteBuffer content) { parseVersionAndFlags(content); data = content.slice(); content.position(content.position() + content.remaining()); try { data.rewind(); descriptor = ObjectDescriptorFactory.createFrom(-1, data.duplicate()); } catch (IOException e) { LOG.warn("Error parsing ObjectDescriptor", e); //that's why we copied it ;) } catch (IndexOutOfBoundsException e) { LOG.warn("Error parsing ObjectDescriptor", e); //that's why we copied it ;) } }
@Override public void getBytes(int index, ChannelBuffer dst, int dstIndex, int length) { if (dst instanceof ByteBufferBackedChannelBuffer) { ByteBufferBackedChannelBuffer bbdst = (ByteBufferBackedChannelBuffer) dst; ByteBuffer data = bbdst.buffer.duplicate(); data.limit(dstIndex + length).position(dstIndex); getBytes(index, data); } else if (buffer.hasArray()) { dst.setBytes(dstIndex, buffer.array(), index + buffer.arrayOffset(), length); } else { dst.setBytes(dstIndex, this, index, length); } }
@Override public DefaultDataBuffer write(byte[] source, int offset, int length) { Assert.notNull(source, "Byte array must not be null"); ensureCapacity(length); ByteBuffer tmp = this.byteBuffer.duplicate(); int limit = this.writePosition + length; ((Buffer) tmp).clear().position(this.writePosition).limit(limit); tmp.put(source, offset, length); this.writePosition += length; return this; }