private DataInputStream serAndPrepForRead(RequestOrResponse requestOrResponse, int channelSize, boolean isRequest)
      throws IOException {
    DataInputStream stream;
    if (useByteBufContent && requestOrResponse.content() != null) {
      stream = new NettyByteBufDataInputStream(requestOrResponse.content());
    } else {
      if (channelSize == -1) {
        channelSize = (int) (requestOrResponse.sizeInBytes() / 3);
      }
      ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
      int expectedWriteToCount = (int) ((requestOrResponse.sizeInBytes() + channelSize - 1) / channelSize);
      int actualWriteToCount = 0;
      do {
        ByteBufferChannel channel = new ByteBufferChannel(ByteBuffer.allocate(channelSize));
        requestOrResponse.writeTo(channel);
        ByteBuffer underlyingBuf = channel.getBuffer();
        underlyingBuf.flip();
        outputStream.write(underlyingBuf.array(), underlyingBuf.arrayOffset(), underlyingBuf.remaining());
        actualWriteToCount++;
      } while (!requestOrResponse.isSendComplete());
      Assert.assertEquals("Should not have written anything", 0,
          requestOrResponse.writeTo(new ByteBufferChannel(ByteBuffer.allocate(1))));
      Assert.assertEquals("writeTo() should have written out as much as the channel could take in every call",
          expectedWriteToCount, actualWriteToCount);
      stream = new DataInputStream(new ByteArrayInputStream(outputStream.toByteArray()));
    }
    // read length
    stream.readLong();
    if (isRequest) {
      // read version
      Assert.assertEquals(RequestOrResponseType.values()[stream.readShort()], requestOrResponse.getRequestType());
    }
    return stream;
  }
Serializes a {@link RequestOrResponseType} and prepares it for reading.
@param requestOrResponse the {@link RequestOrResponseType} to serialize.
@param channelSize the amount of data that the output channel should read in one iteration. Setting this to -1
                      will set the size of the output channel buffer to 1/3rd the size of {@code requestOrResponse}
@param isRequest {@code true} if {@code requestOrResponse} is a request. {@code false} otherwise.
@return the serialized form of {@code requestOrResponse} as a {@link DataInputStream}.
@throws IOException
ambry-protocol/src/test/java/com/github/ambry/protocol/RequestResponseTest.java