@Override
    protected Storage toStorage0() throws IOException {
      if (tail == null)
        return new MemoryStorageProvider.MemoryStorage(head.buffer(), head.length());

      return new ThresholdStorage(head.buffer(), head.length(), tail.toStorage());
    }
    @Override
    protected void write0(byte[] buffer, int offset, int length) throws IOException {
      int remainingHeadSize = thresholdSize - head.length();
      if (remainingHeadSize > 0) {
        int n = Math.min(remainingHeadSize, length);
        head.append(buffer, offset, n);
        offset += n;
        length -= n;
      }

      if (length > 0) {
        if (tail == null) tail = backend.createStorageOutputStream();

        tail.write(buffer, offset, length);
      }
    }