public synchronized void setReady(State... expectedCurrentState) {
    if (this.checksum != 0) {
      throw new IllegalStateException("Checksum cannot be changed once set");
    }

    this.buffer = this.buffer.asReadOnlyBuffer();
    this.checksum = getChecksum(this.buffer);
    this.buffer.rewind();
    this.updateState(State.READY, expectedCurrentState);
  }
@Test
  public void testSetReady() throws Exception {
    byte[] bytes1 = new byte[5];
    initBytes(bytes1);

    ByteBuffer buffer = ByteBuffer.allocate(10);
    buffer.put(bytes1);
    buffer.limit(bytes1.length);
    BufferData data = new BufferData(1, buffer);
    assertNotEquals(BufferData.State.READY, data.getState());
    assertEquals(0, data.getChecksum());

    data.setReady(BufferData.State.BLANK);
    assertEquals(BufferData.State.READY, data.getState());
    assertNotEquals(0, data.getChecksum());

    // Verify that buffer cannot be modified once in READY state.
    ExceptionAsserts.assertThrows(
        ReadOnlyBufferException.class,
        null,
        () -> data.getBuffer().put(bytes1));

    // Verify that buffer cannot be set to READY state more than once.
    ExceptionAsserts.assertThrows(
        IllegalStateException.class,
        "Checksum cannot be changed once set",
        () -> data.setReady(BufferData.State.BLANK));

    // Verify that we detect post READY buffer modification.
    buffer.array()[2] = (byte) 42;
    ExceptionAsserts.assertThrows(
        IllegalStateException.class,
        "checksum changed after setReady()",
        () -> data.setDone());
  }