/**
   * Constructs an {@link InterruptibleByteChannel} from which the deserializer to be tested can
   * read its data.
   *
   * @param readInterruptPositions the positions after which the byte stream shall be interrupted
   * @param testBufferSize the size of the test buffer to create
   * @return an {@link InterruptibleByteChannel} holding the serialized data in memory
   * @throws IOException thrown if an error occurs while serializing the original data
   */
  private ReadableByteChannel createByteChannel(
      final int[] readInterruptPositions, final int testBufferSize) throws IOException {

    final TransferEnvelope te = new TransferEnvelope(SEQUENCE_NUMBER, JOB_ID, CHANNEL_ID);

    if (testBufferSize >= 0) {

      if (testBufferSize > 100) {
        throw new IllegalStateException("Test buffer size can be 100 bytes at most");
      }

      final Queue<MemorySegment> bufferPool = new ArrayDeque<MemorySegment>();
      final MemorySegment ms = new MemorySegment(new byte[TEST_BUFFER_CAPACITY]);

      final MemoryBuffer buffer =
          BufferFactory.createFromMemory(ms.size(), ms, new BufferPoolConnector(bufferPool));

      final ByteBuffer srcBuffer = ByteBuffer.allocate(testBufferSize);
      for (int i = 0; i < testBufferSize; ++i) {
        srcBuffer.put((byte) i);
      }
      srcBuffer.flip();

      buffer.write(srcBuffer);
      buffer.flip();
      te.setBuffer(buffer);
    }

    final DefaultSerializer ds = new DefaultSerializer();
    ds.setTransferEnvelope(te);

    final InterruptibleByteChannel ibc = new InterruptibleByteChannel(null, readInterruptPositions);

    while (ds.write(ibc)) ;

    ibc.switchToReadPhase();

    return ibc;
  }