Restore buf-to-buf hex encode
@@ -45,41 +45,41 @@ public HexEncoder()
initialiseDecodingTable();
}
- /**
- * encode the input data producing a Hex output stream.
- *
- * @return the number of bytes produced.
- */
- public int encode(
- byte[] data,
- int off,
- int length,
- OutputStream out)
- throws IOException
+ public int encode(byte[] inBuf, int inOff, int inLen, byte[] outBuf, int outOff) throws IOException
{
- byte[] buf = new byte[72];
- int bufOff = 0;
+ int inPos = inOff;
+ int inEnd = inOff + inLen;
+ int outPos = outOff;
- for (int i = off; i < (off + length); i++)
+ while (inPos < inEnd)
{
- int v = data[i] & 0xff;
+ int b = inBuf[inPos++] & 0xFF;
- buf[bufOff++] = encodingTable[(v >>> 4)];
- buf[bufOff++] = encodingTable[v & 0xf];
-
- if (bufOff == buf.length)
- {
- out.write(buf);
- bufOff = 0;
+ outBuf[outPos++] = encodingTable[b >>> 4];
+ outBuf[outPos++] = encodingTable[b & 0xF];
}
+
+ return outPos - outOff;
}
- if (bufOff > 0)
+ /**
+ * encode the input data producing a Hex output stream.
+ *
+ * @return the number of bytes produced.
+ */
+ public int encode(byte[] buf, int off, int len, OutputStream out)
+ throws IOException
{
- out.write(buf, 0, bufOff);
+ byte[] tmp = new byte[72];
+ while (len > 0)
+ {
+ int inLen = Math.min(36, len);
+ int outLen = encode(buf, off, inLen, tmp, 0);
+ out.write(tmp, 0, outLen);
+ off += inLen;
+ len -= inLen;
}
-
- return length * 2;
+ return len * 2;
}
private static boolean ignore(
