public byte[] toByteArray()
  {
    if (this._bufferList.peekFirst() == null)
    {
      // Return an empty array if the output stream is empty
      return new byte[0];
    }
    else
    {
      int totalSize = size();
      byte[] targetBuffer = new byte[totalSize];
      int pos = 0;
      Iterator<byte[]> iter = this._bufferList.iterator();
      while (iter.hasNext())
      {
        byte[] buffer = iter.next();
        if (iter.hasNext())
        {
          // If it has next buffer, we know this buffer is full and
          // we copy the whole buffer to the new buffer.
          System.arraycopy(buffer, 0, targetBuffer, pos, buffer.length);
          pos += buffer.length;
        }
        else
        {
          // If this is the last buffer, we only copy valid content based on _index
          System.arraycopy(buffer, 0, targetBuffer, pos, this._index);
        }
      }
      return targetBuffer;
    }
  }
@Test
  public void testToByteArray()
  {
    FastByteArrayOutputStream testStream = new FastByteArrayOutputStream();
    byte[] inputArray = new byte[] {0b1, 0b1, 0b1, 0b1, 0b1, 0b1, 0b1, 0b1};
    testStream.write(inputArray, 0, inputArray.length);

    byte[] safeArray1 = testStream.toByteArray();
    byte[] safeArray2 = testStream.toByteArray();

    Assert.assertEquals(testStream.size(), inputArray.length);
    Assert.assertEquals(safeArray1, inputArray);
    Assert.assertEquals(safeArray2, inputArray);

    // The two byte array has the same content but different identities.
    Assert.assertEquals(safeArray1, safeArray2);
    Assert.assertNotSame(safeArray1, safeArray2);
  }