You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@harmony.apache.org by "Mikhail Markov (JIRA)" <ji...@apache.org> on 2007/06/09 15:41:26 UTC

[jira] Commented: (HARMONY-3854) [classlib][luni] InputStreamReader.read(char[] buffer) doesn't fill in all the buffer when input is still available

    [ https://issues.apache.org/jira/browse/HARMONY-3854?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel#action_12503074 ] 

Mikhail Markov commented on HARMONY-3854:
-----------------------------------------

I've tried to fix the problem by improving Spark's patch but have figured out that there is a major problem with InputStreamReader.read(char[], int, int) method:
The method is designed to work with relatively small buffers (8K), but in the beginning of the method it's trying to reallocate the buffer to the larger size (what for?) and there is a error there - it allocates the buffer with the size of bytes available in the stream, which could be potentially large if reading from large files. And because of this bug all read operations are now occuring in this sequence:
1) the large buffer equal to in.available stream is created
2) the buffer is filled with 1 read operation
3) all subsequent read() method calls uses the data in this buffer

This scheme works fine (and i've found quick fix to let the last from 3 tests above pass) but the memory occupied by the buffer depends on input size which is not so good. Also all the functionality related with slicing the input was hidden due to this feature and contains a some errors which was not tested.
So, real fix requires much more efforts - i'm working on it now.


> [classlib][luni] InputStreamReader.read(char[] buffer) doesn't fill in all the buffer when input is still available
> -------------------------------------------------------------------------------------------------------------------
>
>                 Key: HARMONY-3854
>                 URL: https://issues.apache.org/jira/browse/HARMONY-3854
>             Project: Harmony
>          Issue Type: Bug
>          Components: Classlib
>            Reporter: Elena Sayapina
>            Assignee: Paulex Yang
>         Attachments: InputStreamReader_3854.patch, readTest.zip, readTest2.zip, readTest3.zip
>
>
> Method read of InputStreamReader works incorrectly in some cases.
> If call method read(char[] buffer) with the non-tiny buffer more than two times it reads chars incorrectly, 
> i.e. doesn't fill in all the buffer when some input is still available.
> Please, consider the following piece of code (note that data.txt contains about 14000 chars):
> import java.io.File;
> import java.io.FileInputStream;
> import java.io.IOException;
> import java.io.InputStream;
> import java.io.InputStreamReader;
> public class readTest {
> 	public static void main(String[] args) throws IOException {
> 		
> 		String filename = new String("data.txt");
> 		System.out.println("Data file length: " + new File(filename).length());
> 		InputStream stream = new FileInputStream(new File(filename));
> 		InputStreamReader reader = new InputStreamReader(stream);
> 		
> 		int read = 0;
> 		char[] buffer = new char[4];
> 		System.out.println("Char buffer capacity: " + buffer.length);
> 		try {
> 			read = reader.read(buffer);
> 		} catch (Exception e) {
> 		    e.printStackTrace();
> 		    System.out.println("TEST FAILED: unexpected" + e);
> 		}
> 		System.out.println("Number of chars read: " + read);
> 		read = 0;
> 		buffer = new char[9000];
> 		System.out.println("Char buffer capacity: " + buffer.length);
> 		try {
> 			read = reader.read(buffer);
> 		} catch (Exception e) {
> 			e.printStackTrace();
> 			System.out.println("TEST FAILED: unexpected" + e);
> 		}
> 		System.out.println("Number of chars read: " + read);
> 	    
> 	    System.out.println("buffer[8999]: " + buffer[8999]);
> 	    System.out.println("buffer[8998]: " + buffer[8998]);
> 	    System.out.println("buffer[8189]: " + buffer[8189]);
> 	    System.out.println("buffer[8188]: " + buffer[8188]);
> 	    System.out.println("buffer[8187]: " + buffer[8187]);
> 	    
> 	    if (read == buffer.length) System.out.println("TEST PASSED");
> 	    else System.out.println("TEST FAILED");
> 	}
> }
> Output on Harmony-r537585:
> Apache Harmony Launcher : (c) Copyright 1991, 2006 The Apache Software Foundation or its licensors,
> as applicable.
> java version "1.5.0"
> pre-alpha : not complete or compatible
> svn = r537585, (May 13 2007), Windows/ia32/msvc 1310, debug build
> http://incubator.apache.org/harmony
> Data file length: 14000
> Char buffer capacity: 4
> Number of chars read: 4
> Char buffer capacity: 9000
> Number of chars read: 8188
> buffer[8999]:
> buffer[8998]:
> buffer[8189]:
> buffer[8188]:
> buffer[8187]: t
> TEST FAILED
> Output on HotSpot:
> java version "1.5.0_11"
> Java(TM) 2 Runtime Environment, Standard Edition (build 1.5.0_11-b03)
> Java HotSpot(TM) Client VM (build 1.5.0_11-b03, mixed mode)
> Data file length: 14000
> Char buffer capacity: 4
> Number of chars read: 4
> Char buffer capacity: 9000
> Number of chars read: 9000
> buffer[8999]: t
> buffer[8998]: s
> buffer[8189]: e
> buffer[8188]: T
> buffer[8187]: t
> TEST PASSED
> NOTE that if remove first try..catch block with reader.read(buffer) then test passes,
> or if reduce buffer size from 9000 to 8188 test also passes.
> Please, use the data from attached readTest.zip to reproduce the failure.

-- 
This message is automatically generated by JIRA.
-
You can reply to this email to add a comment to the issue online.