patches to slove an InputStreamReader problem

ito at htk.hitachi-cable.co.jp ito at htk.hitachi-cable.co.jp
Sun May 14 22:26:49 PDT 2000


I made patches that may solve the problem:
  URL: http://rufus.w3.org/tools/Kaffe/messages/5936.html
  Subject: ByteToCharUTF8.java cannot handle long strings
  From: ito at htk.hitachi-cable.co.jp
  Date: Fri Mar 03 2000 - 06:16:19 EST

In some cases, bytes left in the ByteToCharConverter's buffer
must be reprocessed with new input bytes, and cannot be
just flushed.

So I prepared a new method "withdraw" in ByteToCharConverter
which gives back the bytes left in the buffer to the caller
of the method.

Using "withdraw", InputStreamReader can concatenate the
unprocessed bytes and newly read bytes so that they can
be successfully converted to a string. 

Attached patches:

Patch for kaffe/io/ByteToCharConverter.java

*** kaffe/io/ByteToCharConverter.java.orig	Thu Nov 11 01:56:17 1999
--- kaffe/io/ByteToCharConverter.java	Mon May 15 13:26:01 2000
***************
*** 84,89 ****
--- 84,99 ----
  	}
  }
  
+ public int withdraw ( byte[] to, int tpos, int tlen ) {
+ 	int n = (tlen < blen ? tlen : blen);
+ 	System.arraycopy( buf, 0, to, 0, n);
+ 	if ( n < blen ) {
+ 		System.arraycopy( buf, n, buf, 0, blen - n);
+ 	}
+ 	blen -= n;
+ 	return n;
+ }
+ 
  private static ByteToCharConverter getConverterInternal ( String enc ) {
  	Class cls = (Class)cache.get(enc);
  	if (cls == noConverter) {

Patch for java/io/InputStreamReader.java

*** java/io/InputStreamReader.java.orig	Thu Dec  9 11:05:26 1999
--- java/io/InputStreamReader.java	Mon May 15 13:30:14 2000
***************
*** 43,63 ****
  
  public int read ( char cbuf[], int off, int len ) throws IOException {
  	int outlen = 0;
- 	int olen;
  
  	synchronized ( lock ) {
- 		// First we return anything left in the converter
- 		outlen = encoding.flush(cbuf, off, len);
  		while (len > outlen) {
  			int n = len - outlen;
! 			if (n > inbuf.length) {
! 				n = inbuf.length;
  			}
! 			int inlen = strm.read(inbuf, 0, n);
  			if (inlen < 0) {
! 				break;
  			}
! 			outlen += encoding.convert(inbuf, 0, inlen, cbuf, off+outlen, len-outlen);
  			if (inlen < n) {
  				break;
  			}
--- 43,63 ----
  
  public int read ( char cbuf[], int off, int len ) throws IOException {
  	int outlen = 0;
  
  	synchronized ( lock ) {
  		while (len > outlen) {
+ 			// First we return anything left in the converter
+ 			int inpos = encoding.withdraw(inbuf, 0, inbuf.length);
  			int n = len - outlen;
! 			int m = inbuf.length - inpos;
! 			if (n > m) {
! 				n = m;
  			}
! 			int inlen = strm.read(inbuf, inpos, n);
  			if (inlen < 0) {
! 				inlen = 0;
  			}
! 			outlen += encoding.convert(inbuf, 0, inpos+inlen, cbuf, off+outlen, len-outlen);
  			if (inlen < n) {
  				break;
  			}


More information about the kaffe mailing list