public String getUtf8CString() throws IOException
  {
    ArrayList<ByteBuffer> bufferList = null;
    boolean foundZeroByte = false;
    int numBytes = 0;

    while (foundZeroByte == false && advanceBufferIfCurrentBufferHasNoRemaining())
    {
      int position = _currentBuffer.position();
      byte[] array = _currentBuffer.array();
      int arrayOffset = _currentBuffer.arrayOffset();
      int limit = _currentBuffer.limit();
      int arrayLimit = arrayOffset + limit;
      int arrayStart = arrayOffset + position;
      int arrayIndex = arrayStart;
      while (arrayIndex < arrayLimit && array[arrayIndex] != ZERO_BYTE)
      {
        arrayIndex++;
      }

      foundZeroByte = (arrayIndex < arrayLimit);
      int bytesInCurrentBuffer = arrayIndex - arrayStart;
      numBytes += bytesInCurrentBuffer;
      if (foundZeroByte == false || numBytes != bytesInCurrentBuffer)
      {
        bufferList = accummulateByteBuffers(bufferList, bytesInCurrentBuffer);
      }
    }

    if (foundZeroByte == false)
    {
      throw new BufferUnderflowException();
    }
    return bufferToUtf8CString(numBytes, bufferList);
  }
@Test
  public void testGetUTF8CString() throws Exception
  {
    for (Map.Entry<String, String> entry : _strings.entrySet())
    {
      String key = entry.getKey();
      String value = entry.getValue();
      for (int bufferSize : _bufferSizes)
      {
        // out.println("testing " + key + " with buffer size " + bufferSize);

        byte[] bytesFromString = value.getBytes(Data.UTF_8_CHARSET);
        int bytes = bytesFromString.length + 1;
        byte[] bytesInBuffer = new byte[bytes];
        System.arraycopy(bytesFromString, 0, bytesInBuffer, 0, bytes - 1);
        bytesInBuffer[bytes - 1] = 0;

        // must use package privet constructor break bytes into bufferSize byte buffers internally
        BufferChain bufferChain = new BufferChain(BufferChain.DEFAULT_ORDER, bytesInBuffer, bufferSize);

        bufferChain.rewind();
        byte[] bytesFromBufferChain = new byte[bytes];
        bufferChain.get(bytesFromBufferChain, 0, bytes);
        assertEquals(bytesFromBufferChain, bytesInBuffer);

        bufferChain.rewind();
        String stringWithoutLength = bufferChain.getUtf8CString();
        assertEquals(stringWithoutLength, value);

        bufferChain.rewind();
        String stringGetWithLength = bufferChain.getUtf8CString(bytes);
        assertEquals(stringGetWithLength, value);
      }
    }
  }