io/src/main/java/com/stanfy/enroscar/io/PoolableBufferedOutputStream.java
@Override
  public synchronized void write(final byte[] buffer, final int offset, final int length) throws IOException {
    checkNotClosed();

    if (buffer == null) {
      throw new NullPointerException("buffer == null");
    }

    final byte[] internalBuffer = buf;
    if (length >= internalBuffer.length) {
      flushInternal();
      out.write(buffer, offset, length);
      return;
    }

    if ((offset | length) < 0 || offset > buffer.length || buffer.length - offset < length) {
      throw new ArrayIndexOutOfBoundsException("length=" + buffer.length + "; regionStart=" + offset
          + "; regionLength=" + length);
    }

    // flush the internal buffer first if we have not enough space left
    if (length > (internalBuffer.length - count)) {
      flushInternal();
    }

    System.arraycopy(buffer, offset, internalBuffer, count, length);
    count += length;
  }
@Override
  public synchronized void write(final byte[] buffer, final int offset, final int length) throws IOException {
    checkNotClosed();

    if (buffer == null) {
      throw new NullPointerException("buffer == null");
    }

    final byte[] internalBuffer = buf;
    if (length >= internalBuffer.length) {
      flushInternal();
      out.write(buffer, offset, length);
      return;
    }

    if ((offset | length) < 0 || offset > buffer.length || buffer.length - offset < length) {
      throw new ArrayIndexOutOfBoundsException("length=" + buffer.length + "; regionStart=" + offset
          + "; regionLength=" + length);
    }

    // flush the internal buffer first if we have not enough space left
    if (length > (internalBuffer.length - count)) {
      flushInternal();
    }

    System.arraycopy(buffer, offset, internalBuffer, count, length);
    count += length;
  }
Writes {@code count} bytes from the byte array {@code buffer} starting at
{@code offset} to this stream. If there is room in the buffer to hold the
bytes, they are copied in. If not, the buffered bytes plus the bytes in
{@code buffer} are written to the target stream, the target is flushed,
and the buffer is cleared.

@param buffer
the buffer to be written.
@param offset
the start position in {@code buffer} from where to get bytes.
@param length
the number of bytes from {@code buffer} to write to this
stream.
@throws IndexOutOfBoundsException
if {@code offset < 0} or {@code length < 0}, or if
{@code offset + length} is greater than the size of
{@code buffer}.
@throws IOException
if an error occurs attempting to write to this stream.
@throws NullPointerException
if {@code buffer} is {@code null}.
@throws ArrayIndexOutOfBoundsException
If offset or count is outside of bounds.