Esempio n. 1
0
  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;
  }
Esempio n. 2
0
  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;
  }
Esempio n. 3
0
 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;
 }
Esempio n. 4
0
 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;
 }
Esempio n. 5
0
 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;
 }
Esempio n. 6
0
 private boolean readPartition(ByteBuffer src) {
   if (!isPersistStatusSet(PERSIST_PARTITION)) {
     if (src.remaining() < 4) {
       return false;
     }
     partitionId = src.getInt();
     setPersistStatus(PERSIST_PARTITION);
   }
   return true;
 }
Esempio n. 7
0
 private boolean readHeader(ByteBuffer src) {
   if (!isPersistStatusSet(PERSIST_HEADER)) {
     if (src.remaining() < 2) {
       return false;
     }
     header = src.getShort();
     setPersistStatus(PERSIST_HEADER);
   }
   return true;
 }
Esempio n. 8
0
 private boolean writeVersion(ByteBuffer dst) {
   if (!isPersistStatusSet(PERSIST_VERSION)) {
     if (!dst.hasRemaining()) {
       return false;
     }
     dst.put(VERSION);
     setPersistStatus(PERSIST_VERSION);
   }
   return true;
 }
Esempio n. 9
0
 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;
 }
Esempio n. 10
0
 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;
 }
Esempio n. 11
0
  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;
  }
Esempio n. 12
0
  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;
  }