harmony-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Jesse Wilson <jessewil...@google.com>
Subject Re: [classlib][archive] Several archive bugfixes and optimizations (HARMONY-6346)
Date Thu, 08 Oct 2009 18:14:13 GMT
On Wed, Oct 7, 2009 at 1:26 PM, Tim Ellison <t.p.ellison@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.

Mime
  • Unnamed multipart/alternative (inline, None, 0 bytes)
View raw message