public byte[] toByteArray() {
    ByteBuffer bb = toByteBuffer();
    if (bb.hasArray() && bb.arrayOffset() == 0 && bb.limit() == bb.capacity()) {
      return bb.array();
    } else {
      // create a new buffer of just the right size and copy the old buffer into it
      ByteBuffer tmp = ByteBuffer.allocate(bb.remaining());
      tmp.put(bb);
      tmp.flip();
      buffer = tmp;
      return buffer.array();
    }
  }
gets the contents of this stream as a byte[]. The stream should not be written to past this
point until it has been reset.
geode-serialization/src/main/java/org/apache/geode/internal/serialization/BufferDataOutputStream.java