Skip buffer copy when possible
@@ -96,23 +96,32 @@ public void write(
public void write(byte[] b, int off, int len) throws IOException
{
- while (len > 0)
+ int bufLen = _buf.length;
+ int available = bufLen - _off;
+ if (len < available)
{
- int numToCopy = Math.min(len, _buf.length - _off);
- System.arraycopy(b, off, _buf, _off, numToCopy);
+ System.arraycopy(b, off, _buf, _off, len);
+ _off += len;
+ return;
+ }
- _off += numToCopy;
- if (_off < _buf.length)
+ int count = 0;
+ if (_off > 0)
{
- break;
+ System.arraycopy(b, off, _buf, _off, available);
+ count += available;
+ DEROctetString.encode(_derOut, true, _buf, 0, bufLen);
}
- DEROctetString.encode(_derOut, true, _buf, 0, _buf.length);
- _off = 0;
-
- off += numToCopy;
- len -= numToCopy;
+ int remaining;
+ while ((remaining = (len - count)) >= bufLen)
+ {
+ DEROctetString.encode(_derOut, true, b, off + count, bufLen);
+ count += bufLen;
}
+
+ System.arraycopy(b, off + count, _buf, 0, remaining);
+ this._off = remaining;
}
public void close()
