This week's giveaway is in the Android forum.
We're giving away four copies of Android Security Essentials Live Lessons and have Godfrey Nolan on-line!
See this thread for details.
The moose likes Servlets and the fly likes Efficient I/O in Servlets Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Java » Servlets
Bookmark "Efficient I/O in Servlets" Watch "Efficient I/O in Servlets" New topic

Efficient I/O in Servlets

tom walters
Ranch Hand

Joined: Feb 14, 2002
Posts: 34
What is an efficient way to read from an input stream and write to an outputstream when you do not know the number of bytes in the inputstream?
I am currently doing the following:
InputStream is = getInputStream();//helper method
ServletOutputStream sos = response.getOutputStream();
byte b;
while((b = (byte) != -1)
William Brogden
Author and all-around good cowpoke

Joined: Mar 22, 2000
Posts: 12761
Although the method you are using is safe and simple, the prefered method is to use a BufferedInputStream / BufferedOutputStream or one of the related classes for reader and writer.
You can use the available() method of BufferedInputStream to see how many bytes you can safely read into a byte[]
Whether it is worth the trouble to use a different method would depend on how often you have to do this and how much data is transferred.
I agree. Here's the link:
subject: Efficient I/O in Servlets
Similar Threads
Accessing parent directory
Convert InputStream to byte array
Simple stream read question ...
BufferedReader in J2ME
Converting from BLOB to string