Skip to main content
Notice removed Canonical answer required by david
Bounty Ended with coderodde's answer chosen by david
Notice added Canonical answer required by david
Bounty Started worth 50 reputation by david
edited tags
Link
200_success
  • 145.6k
  • 22
  • 191
  • 481
deleted 60 characters in body; edited title
Source Link
Jamal
  • 35.2k
  • 13
  • 134
  • 238

Serialize data into one byte array using ByteBuffer?

I have a below class in which I am passing certain parameters through the constructor and then using those parameters I am makingto make one final byte array with a proper format (header + data) as shown below:

I am opting for code reviewwould like to see whetherknow if it can be improved in any way or any better way?. As you can see right now, I am allocating ByteBuffer with predefined size of 70000. MaybeIs there is a better way by which I can allocate the size I am using while making ByteBuffer instead of using a hardcoded 70000?

Serialize data into one byte array using ByteBuffer?

I have a below class in which I am passing certain parameters through constructor and then using those parameters I am making one final byte array with proper format (header + data) as shown below:

I am opting for code review to see whether it can be improved in any way or any better way? As you can see right now, I am allocating ByteBuffer with predefined size of 70000. Maybe there is a better way by which I can allocate the size I am using while making ByteBuffer instead of using hardcoded 70000?

Serialize data into one byte array using ByteBuffer

I have a class in which I am passing certain parameters through the constructor and then using those parameters to make one final byte array with a proper format (header + data):

I would like to know if it can be improved in any way. As you can see right now, I am allocating ByteBuffer with predefined size of 70000. Is there a better way by which I can allocate the size I am using while making ByteBuffer instead of using a hardcoded 70000?

Source Link
david
  • 2k
  • 8
  • 32
  • 48

Serialize data into one byte array using ByteBuffer?

I have a below class in which I am passing certain parameters through constructor and then using those parameters I am making one final byte array with proper format (header + data) as shown below:

public final class Frame {
  private final byte addressedCenter;
  private final byte version;
  private final Map<byte[], byte[]> keyDataHolder;
  private final long location;
  private final long locationFrom;
  private final long locationOrigin;
  private final byte partition;
  private final byte copy;

  public Frame(byte addressedCenter, byte version,
      Map<byte[], byte[]> keyDataHolder, long location, long locationFrom,
      long locationOrigin, byte partition, byte copy) {
    this.addressedCenter = addressedCenter;
    this.version = version;
    this.keyDataHolder = keyDataHolder;
    this.location = location;
    this.locationFrom = locationFrom;
    this.locationOrigin = locationOrigin;
    this.partition = partition;
    this.copy = copy;
  }

  public byte[] serialize() {
    // All of the data is embedded in a binary array with fixed maximum size 70000
    ByteBuffer byteBuffer = ByteBuffer.allocate(70000);
    byteBuffer.order(ByteOrder.BIG_ENDIAN);

    int numOfRecords = keyDataHolder.size();
    int bufferUsed = getBufferUsed(keyDataHolder); // 36 + dataSize + 1 + 1 + keyLength + 8 + 2;

    // header layout
    byteBuffer.put(addressedCenter); // byte
    byteBuffer.put(version); // byte
    byteBuffer.putInt(numOfRecords); // int
    byteBuffer.putInt(bufferUsed); // int
    byteBuffer.putLong(location); // long
    byteBuffer.putLong(locationFrom); // long
    byteBuffer.putLong(locationOrigin); // long
    byteBuffer.put(partition); // byte
    byteBuffer.put(copy); // byte

    // now the data layout
    for (Map.Entry<byte[], byte[]> entry : keyDataHolder.entrySet()) {
      byte keyType = 0;
      byte keyLength = (byte) entry.getKey().length;
      byte[] key = entry.getKey();
      byte[] data = entry.getValue();
      short dataSize = (short) data.length;
      
      ByteBuffer dataBuffer = ByteBuffer.wrap(data);
      long timestamp = 0;

      if (dataSize > 10) {
        timestamp = dataBuffer.getLong(2);              
      }       

      byteBuffer.put(keyType);
      byteBuffer.put(keyLength);
      byteBuffer.put(key);
      byteBuffer.putLong(timestamp);
      byteBuffer.putShort(dataSize);
      byteBuffer.put(data);
    }
    return byteBuffer.array();
  }
  
  private int getBufferUsed(final Map<byte[], byte[]> keyDataHolder) {
    int size = 36;
    for (Map.Entry<byte[], byte[]> entry : keyDataHolder.entrySet()) {
      size += 1 + 1 + 8 + 2;
      size += entry.getKey().length;
      size += entry.getValue().length;
    }
    return size;
  }  
}

I am opting for code review to see whether it can be improved in any way or any better way? As you can see right now, I am allocating ByteBuffer with predefined size of 70000. Maybe there is a better way by which I can allocate the size I am using while making ByteBuffer instead of using hardcoded 70000?