You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@harmony.apache.org by Jesse Wilson <je...@google.com> on 2009/10/08 20:14:13 UTC

Re: [classlib][archive] Several archive bugfixes and optimizations (HARMONY-6346)

On Wed, Oct 7, 2009 at 1:26 PM, Tim Ellison <t....@gmail.com> wrote:

> I know that the InputStream here can only be a RAFStream or
> InflaterInputStream, and both of them return available() as 1 so long as
> the stream is not at the end, but in general I guess it is better to
> read to the -1 eof marker rather than check available().
>
> Then it uses a ByteArrayOutputStream every time, which by default has a
> 32-byte backing array, that grows aggressively, so putting anything near
> 1K bytes in will result in a ~2K backing array.
>
> So how about we fast track the case (esp. RAFStream) where we get all
> the bytes in one read...?
>
>  // Initial read
>  byte[] buffer = new byte[1024];
>  int count = is.read(buffer);
>  int nextByte = is.read();
>
>  // Did we get it all in one read?
>  if (nextByte == -1) {
>    byte[] dest = new byte[count];
>    System.arraycopy(buffer, 0, dest, 0, count);
>    return dest;
>  }
>
>  // Requires additional reads
>  ByteArrayOutputStream baos = new ByteArrayOutputStream(count * 2);
>  baos.write(buffer, 0, count);
>  baos.write(nextByte);
>  while (true) {
>    count = is.read(buffer);
>    if (count == -1) {
>        return baos.toByteArray();
>    }
>    baos.write(buffer, 0, count);
>  }
>
>
>
> The same holds true for Manifest#readFully(InputStream).  Even more so
> since if we get an eof we don't have to scan for a line ending.
>
> WDYT?
>

That sounds fine. Or perhaps just right-size the ByteArrayOutputStream to
something reasonable, like 256 bytes.