private boolean writeValue(ByteBuffer dst) { if (!isPersistStatusSet(PERSIST_VALUE)) { if (size > 0) { // the number of bytes that can be written to the bb. int bytesWritable = dst.remaining(); // the number of bytes that need to be written. int bytesNeeded = size - valueOffset; int bytesWrite; boolean done; if (bytesWritable >= bytesNeeded) { // All bytes for the value are available. bytesWrite = bytesNeeded; done = true; } else { // Not all bytes for the value are available. So lets write as much as is available. bytesWrite = bytesWritable; done = false; } byte[] byteArray = toByteArray(); dst.put(byteArray, valueOffset, bytesWrite); valueOffset += bytesWrite; if (!done) { return false; } } setPersistStatus(PERSIST_VALUE); } return true; }
private boolean readValue(ByteBuffer src) { if (!isPersistStatusSet(PERSIST_VALUE)) { if (payload == null) { payload = new byte[size]; } if (size > 0) { int bytesReadable = src.remaining(); int bytesNeeded = size - valueOffset; boolean done; int bytesRead; if (bytesReadable >= bytesNeeded) { bytesRead = bytesNeeded; done = true; } else { bytesRead = bytesReadable; done = false; } // read the data from the byte-buffer into the bytes-array. src.get(payload, valueOffset, bytesRead); valueOffset += bytesRead; if (!done) { return false; } } setPersistStatus(PERSIST_VALUE); } return true; }
private boolean writePartition(ByteBuffer dst) { if (!isPersistStatusSet(PERSIST_PARTITION)) { if (dst.remaining() < Bits.INT_SIZE_IN_BYTES) { return false; } dst.putInt(partitionId); setPersistStatus(PERSIST_PARTITION); } return true; }
private boolean readSize(ByteBuffer src) { if (!isPersistStatusSet(PERSIST_SIZE)) { if (src.remaining() < INT_SIZE_IN_BYTES) { return false; } size = src.getInt(); setPersistStatus(PERSIST_SIZE); } return true; }
private boolean writeHeader(ByteBuffer dst) { if (!isPersistStatusSet(PERSIST_HEADER)) { if (dst.remaining() < Bits.SHORT_SIZE_IN_BYTES) { return false; } dst.putShort(header); setPersistStatus(PERSIST_HEADER); } return true; }
private boolean readPartition(ByteBuffer src) { if (!isPersistStatusSet(PERSIST_PARTITION)) { if (src.remaining() < 4) { return false; } partitionId = src.getInt(); setPersistStatus(PERSIST_PARTITION); } return true; }
private boolean readHeader(ByteBuffer src) { if (!isPersistStatusSet(PERSIST_HEADER)) { if (src.remaining() < 2) { return false; } header = src.getShort(); setPersistStatus(PERSIST_HEADER); } return true; }
private boolean writeVersion(ByteBuffer dst) { if (!isPersistStatusSet(PERSIST_VERSION)) { if (!dst.hasRemaining()) { return false; } dst.put(VERSION); setPersistStatus(PERSIST_VERSION); } return true; }
private boolean writeSize(ByteBuffer dst) { if (!isPersistStatusSet(PERSIST_SIZE)) { if (dst.remaining() < INT_SIZE_IN_BYTES) { return false; } size = totalSize(); dst.putInt(size); setPersistStatus(PERSIST_SIZE); } return true; }
private boolean readVersion(ByteBuffer src) { if (!isPersistStatusSet(PERSIST_VERSION)) { if (!src.hasRemaining()) { return false; } byte version = src.get(); setPersistStatus(PERSIST_VERSION); if (VERSION != version) { throw new IllegalArgumentException( "Packet versions are not matching! Expected -> " + VERSION + ", Incoming -> " + version); } } return true; }
public boolean readFrom(ByteBuffer src) { if (!readVersion(src)) { return false; } if (!readHeader(src)) { return false; } if (!readPartition(src)) { return false; } if (!readSize(src)) { return false; } if (!readValue(src)) { return false; } setPersistStatus(PERSIST_COMPLETED); return true; }
public boolean writeTo(ByteBuffer dst) { if (!writeVersion(dst)) { return false; } if (!writeHeader(dst)) { return false; } if (!writePartition(dst)) { return false; } if (!writeSize(dst)) { return false; } if (!writeValue(dst)) { return false; } setPersistStatus(PERSIST_COMPLETED); return true; }