Skip to Main Content

Java Programming

Announcement

For appeals, questions and feedback about Oracle Forums, please email oracle-forums-moderators_us@oracle.com. Technical questions should be asked in the appropriate category. Thank you!

What's the fastest input stream reader?

807603Nov 22 2005 — edited Dec 19 2007
Here's my candidate for the fastest stream reader. My interest and testing is with file input streams, but it's nice to support any stream.
	public static String readStream(InputStream is) throws IOException{
		int i, length = 0;
		StringBuffer s = null;
		char[] c = null;
		InputStreamReader isr = new InputStreamReader(is);
		while(length != -1){
			length = is.available();
			if(c == null) c = new char[length];
			isr.read(c, 0 , length);
			i = isr.read();
			if(i == -1){
				if(s == null) return String.copyValueOf(c);
				else break;
			}else{
				if(s == null) s = new StringBuffer();
				s.append(c, 0, length);
				s.append((char)i);
			}
		}
		return s.toString();
	}
Comments
Locked Post
New comments cannot be posted to this locked post.
Post Details
Locked on Jan 16 2008
Added on Nov 22 2005
22 comments
2,046 views