[Patch] Improve/fix gnu.java.net.LineInputStream...

David Daney ddaney@avtrex.com
Mon Sep 12 19:34:00 GMT 2005


gnu.java.net.LineInputStream has at least one bug in it, but think its 
whole approach is incorrect.

First the bug:

              len = in.available();
              len = (len < MIN_LENGTH) ? MIN_LENGTH : len;

I think the idea was to read all available bytes into its buffer (but 
not more that the size of the buffer).  However the conditional was 
reversed leading it to try to read more than is available.  This can 
cause LineInputStream to block even if enough data is available to form 
an entire line.

I have not fully researched it but this was causing HTTP connections to 
block for several seconds.  I think under the proper circumstances they 
could block forever, but I am not positive about this.

The main problem I have with LineInputStream, is that in all cases I 
know about it is either reading from a raw socket or a BufferedInputStream.

In the raw socket case LineInputStream reads one character at a time (as 
is required).  If the stream supports mark/reset then LineInputStream 
reads big blocks of data and then resets if it finds that it read too far.

My problem with this is that if the stream supports mark/reset, then it 
is already buffered and additional buffering is unlikely to produce any 
additional benefit.  An as an added bad point you are using more memory 
for the redundant buffer.

I did take the liberty of adding my own micro-optimization, in that if 
the encoding is US-ASCII, we can skip using String's character encoding 
system and just request hibyte of 0.  I did this because a year ago with 
  libgcj-3.4.3 we were seeing a vast increase in speed doing this in a 
different situation.

Note that this micro-optimization is dependent on my 
ByteArrayOutputStream patch.


Bootstrapped and tested with make -k check in libjava on i686-pc-linux-gnu.

2005-09-12  David Daney  <ddaney@avtrex.com>

	* classpath/gnu/java/net/LineInputStream.java (blockReads): Removed.
	(Constructor): Don't initialize blockReads.
	(bufToString): New method.
	(readLine): Removed block reading logic.
	(indexOf): Removed.

I suppose if this is OK that I would commit it to classpath (when my CVS 
access is enabled).

Opinions?


David Daney.
-------------- next part --------------
An embedded and charset-unspecified text was scrubbed...
Name: lis.d
URL: <http://gcc.gnu.org/pipermail/java-patches/attachments/20050912/9fc2f56f/attachment.ksh>


More information about the Java-patches mailing list