1

Is there a better way to read Strings from an InputStreamReader. In the Profiler im am getting a memory heap there.

public String getClientMessage() throws IOException {
    BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(tempSocket.getInputStream()));     
    char[] buffer = new char[200];
    return new String(buffer, 0, bufferedReader.read(buffer));
}

Thanks in advance.

EDIT: enter image description here

EDIT: Messages are sent with this:

public void sendServerMessage(String action) throws IOException{
    PrintWriter printWriter = new PrintWriter(new OutputStreamWriter(tempSocket.getOutputStream()));
    printWriter.print(action);
    printWriter.flush();
}
6
  • 1
    You're "getting a memory heap" ? What does it means ? Commented Jan 10, 2013 at 8:13
  • I don't see any problems in your screenshot, unless you are executing getClientMessage too much by accident. Commented Jan 10, 2013 at 8:22
  • I am executing getClientMessage every one second Commented Jan 10, 2013 at 8:25
  • Well, Dear, we don't know how those methods are interleaved which each other, so it could be more deep problem. Commented Jan 10, 2013 at 8:30
  • This is so interesting: you read some value from tempSocket and write it back to tempSocket. Some kind of echo service, yes? Commented Jan 10, 2013 at 8:36

2 Answers 2

2

I would suggest you commons-io library for doing such things in a more convenient and simple way. Just use:

return IOUtils.toString(tempSocket.getInputStream());

But this is only a code-style notice. We don't understand what do you mean by the term getting a memory heap. In any case, if you have insufficient memory troubles, you have to increase the memory for you Java application: Memory Management in the Java HotSpot™ Virtual Machine:

Java heap space This indicates that an object could not be allocated in the heap. The issue may be just a configuration problem. You could get this error, for example, if the maximum heap size specified by the –Xmx command line option (or selected by default) is insufficient for the application. It could also be an indication that objects that are no longer needed cannot be garbage collected because the application is unintentionally holding references to them. The HAT tool (see Section 7) can be used to view all reachable objects and understand which references are keeping each one alive. One other potential source of this error could be the excessive use of finalizers by the application such that the thread to invoke the finalizers cannot keep up with the rate of addition of finalizers to the queue. The jconsole management tool can be used to monitor the number of objects that are pending finalization.

Sign up to request clarification or add additional context in comments.

3 Comments

tryed the commons-io but i don't get any message
Try to debug or add printout of variable action in method sendServerMessage.
sendServerMessage actually works, because I can give the Strings out with my Method with the char-Array.. the only Problem is, it is not efficient.
0

You can use IOUtils, but it is easy to write if you can't use that library.

public String getClientMessage() throws IOException {
    Reader r = new InputStreamReader(tempSocket.getInputStream());
    char[] buffer = new char[4096];
    StringBuilder sb = new StringBuilder();
    for(int len; (len = r.read(buffer)) > 0;)
         sb.append(buffer, 0, len);
    return sb.toString();
}

I suspect the problem is you have no way of know from the way you send messages when a message stops. This means you must read until you close the connection which you are not doing. If you don't want to wait until you close you need to add some way of knowing when a message is finished e.g. a newline.

// create this once per socket.
final PrintWriter out = new PrintWriter(
      new OutputStreamWriter(tempSocket.getOutputStream(), "UTF-8"), true);

public void sendServerMessage(String action) {
    // assuming there is no newlines in the message
    printWriter.println(action);  // auto flushed.
}

// create this once per socket
BufferedReader in = new BufferedReader(
    new InputStreamReader(tempSocket.getInputStream(), "UTF-8"));     

public String getClientMessage() throws IOException {
     // read until the end of a line, which is the end of a message.
      return in.readLine();
}

7 Comments

tryed this, same.. can't print anything out.
Well, Dear, we don't know how those methods are interleaved which each other in your code, so it could be more deep problem.
@IlendemliMuhammet This method doesn't print anything. Where are you expecting something to print?
I suspect the problem is you have no way of know from the way you send messages when a message stops. This means you must read until you close the connection which you are not doing. If you don't want to wait until you close you need to add some way of knowing when a message is finished e.g. a newline.
This Method returns a String, i am printing it somewhere else out. The Problem is while reading the Reader (r.read(buffer)).. but I don't know how to solve this.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.