Skip to Main Content

Java APIs

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!

SocketInputStream.read(byte[], int, int) extremely slow

843790Feb 21 2009 — edited Feb 23 2009
Hi everyone,

I'm trying to send several commands to a server and for each command read one or more lines as response. To do that I'm using a good old Socket connection. I'm reading the response lines with BufferedReader.readLine, but it's very slow. VisualVM says, that
SocketInputStream.read(byte[], int, int)
is consuming most of the cpu time. A perl script, which does the same job, finishes in no time. So it's not a problem with the server.

I'm runnning
java version "1.6.0_12"
Java(TM) SE Runtime Environment (build 1.6.0_12-b04)
Java HotSpot(TM) Server VM (build 11.2-b01, mixed mode)
on
Linux henni 2.6.25-gentoo-r7 #3 SMP PREEMPT Sat Jul 26 19:35:54 CEST 2008 i686 Intel(R) Core(TM)2 Duo CPU E6550 @ 2.33GHz GenuineIntel GNU/Linux
and here's my code
private List<Response> readResponse() throws IOException {
        List<Response> responses = new ArrayList<Response>();
        String line = "";
        while ( (line = in.readLine()) != null) {
            int code = -1;
            try {
                code = Integer.parseInt(line.substring(0, 3));
                line = line.substring(4);
            } catch (Exception e) {
                code = -1;
            }
            
            // TODO create different response objects ?!?
            NotImplemented res = new NotImplemented(code, line);
            responses.add(res);
            
            // we received an "end of response" line and can stop reading from the socket
            if(code >= 200 && code < 300) {
                break;
            }
        }
        return responses;
    }
Any hints are appreciated.

Best regards,
Henrik
Comments
Locked Post
New comments cannot be posted to this locked post.
Post Details
Locked on Mar 23 2009
Added on Feb 21 2009
67 comments
3,449 views