In Java, what's the fastest way to "build" and use a string, character by character?
byte, java, string, variables
Solution
I would probably use an `InputStreamReader` wrapped around a `BufferedInputStream`, which in turn wraps the socket. And write code that processes a message at a time, potentially blocking for input. If the input is bursty, I might run on a background thread and use a concurrent queue to hold the messages.
Reading a buffer at a time and trying to convert it to characters is exactly what `BufferedInputStream/InputStreamReader` does. And it does so while paying attention to encoding, something that (as other people have noted) your solution does not.
I don't know why you're focused on speed, but you'll find that the time to process data coming off a socket is far less than the time it takes to transmit over that socket.
Problem
I have a Java socket connection that is receiving data intermittently. The number of bytes of data received with each burst varies. The data may or may not be terminated by a well-known character (such as CR or LF). The length of each burst of data is variable. I'm attempting to build a string out of each burst of data. What is the fastest way (speed, not memory), to build a string that would later need to be parsed? I began by using a byte array to store the incoming bytes, then converting them to a String with each burst, like so: ``` byte[] message = new byte[1024]; ... message[i] = //byte from socket i++; ... String messageStr = new String(message); ... //parse the string here ``` The obvious disadvantage of this is that some bursts may be longer than 1024. I don't want to arbitrarily create a larger byte array (what if my burst is larger?). What is the best way of doing this? Should I create a StringBuilder object and append() to it? That way I don't have to convert from StringBuilder to String (since the former has most of the methods I need). Again, speed of execution is my biggest concern. TIA.