private static void test(BigByteBuffer bbb) throws IOException {
    bbb.write(new byte[] {1, 2}, 0, 2);
    assert bbb.length() == 2;
    bbb.write(new byte[] {3, 4, 5, 6, 7, 8}, 0, 6);
    assert bbb.length() == 8;
    try {
      bbb.getInputStream().read();
      assert false;
    } catch (IllegalStateException x) {
      System.out.println("pass write");
    }

    bbb.doneWriting();
    InputStream in = bbb.getInputStream();
    assert in.read() == 1;
    assert in.read() == 2;
    assert in.read() == 3;
    assert in.read() == 4;
    assert in.read() == 5;
    assert in.read() == 6;
    assert in.read() == 7;
    assert in.read() == 8;
    assert in.read() == -1;
    in.close();
    try {
      in.read();
      assert false;
    } catch (IllegalStateException x) {
      System.out.println("pass read");
    }

    bbb.destroy();
  }
 private static void testBig() throws IOException {
   BigByteBuffer bbb = new BigByteBuffer();
   for (int i = 0; i < DEFAULT_MAX_MEM_BUF_SIZE * 2; ++i) bbb.write((byte) (i & 0xFF));
   bbb.doneWriting();
   assert bbb.length() == DEFAULT_MAX_MEM_BUF_SIZE * 2;
   InputStream in = bbb.getInputStream();
   for (int i = 0; i < DEFAULT_MAX_MEM_BUF_SIZE * 2; ++i)
     assert (byte) in.read() == (byte) (i & 0xFF);
   assert in.read() == -1;
   bbb.destroy();
   System.out.println("big pass");
 }
  @Override
  public void write(int b) throws IOException {
    if (isWritingDone) throw new IllegalStateException("no more writing");

    if (bufSize < maxMemBufSize) {
      bao.write(b);
    } else {
      try {
        if (bufFileOut == null) {
          bufFile = File.createTempFile("BigByteBuffer", null);
          bufFileOut = new BufferedOutputStream(new FileOutputStream(bufFile), STREAM_CHUNK_SIZE);
        }
        bufFileOut.write(b);
      } catch (IOException x) {
        destroy();
      }
    }
    ++bufSize;
  }