Namalak
Namalak

Reputation: 165

Tuning the performance of reading a large InputStream in java

I want to read a large InputStream and return it as a String. This InputStream is a large one. So, normally it takes much time and a lot of memory while it is excuting. The following code is the one that I've developed so far. I need to convert this code as it does the job in a lesser time consuming lesser memory.

Can you give me any idea to do this.

BufferedReader br =
    new BufferedReader(
        new InputStreamReader(
            connection.getInputStream(),
            "UTF-8")
        );

StringBuilder response = new StringBuilder(1000);

char[] buffer = new char[4096];

int n = 0;
while(n >= 0){
    n = br.read(buffer, 0, buffer.length);
    if(n > 0){
        response.append(buffer, 0, n);
    }
}
return response.toString();

Thank you!

Upvotes: 6

Views: 6261

Answers (5)

CurtainDog
CurtainDog

Reputation: 3205

You could run the code in a separate thread... it won't run any faster but at least your program will be able to do some other work instead of waiting for data from the stream.

Upvotes: 0

dogbane
dogbane

Reputation: 274838

Increase the size of your buffer. The bigger the buffer, the faster all the data can be read. If you know (or can work out) how many bytes are available in the stream, you could even allocate a buffer of the same size up-front.

Upvotes: 0

djna
djna

Reputation: 55957

Do you know in advance the likely maxiumum length of your string? You currently specify an intiial capacity of 1000 for your buffer. If what you read is lots bigger than thet you'll pay some cost in allocating larger internal buffers.

If you have control over the life-cycle of what you're reading, perhaps you could allocate a single re-usable byte array as the buffer. Hence avoiding garbage collection.

Upvotes: 0

Steven Schlansker
Steven Schlansker

Reputation: 38536

You may find that for large files on some operating systems, mmaping the file via FileChannel.map will give you better performance - map the file and then create a string out of the mapped ByteBuffer. You'll have to benchmark though, as it may be that 'traditional' IO is faster in some cases.

Upvotes: 1

When you are doing buffered I/O you can just read one char at a time from the buffered reader. Then build up the string, and do a toString() at the end.

Upvotes: 3

Related Questions