user15317996
user15317996

Reputation:

Java. Socket, InputStream, InputStreamReader. How can I find a value for the amount of data in the buffer

I have a data stream of data aquisition info coming in like a burst pipe. (Actually 3 of them).

I can read and println the data to the console, and never miss any data.

I am now saving the data to a database and this is of course a bottleneck. I have this running at an impressive speed also.

The backlog is causing the socket to stop receiving data and the sender stops sending data. The program continues to work through the buffer for some time.

Is there a way to get the buffer size for this stream? I would like to clear the buffer after having read enough data.

I'm not sure if this would be in the socket, the socket, the InputStream or the InputStreamReader.

InputStream inputStream1 = socket1.getInputStream();
InputStreamReader inputStreamReader1 = new InputStreamReader(inputStream1, StandardCharsets.UTF_8);

if (inputStreamReader.ready()) {
    recMsg(inputStreamReader, inputStream, (LinkedList) msgFifo);  // Get more data
    //System.out.print("Got More data: ");
}
public static void recMsg(InputStreamReader inputStreamReader, InputStream inputStream,LinkedList msgFifo) throws IOException {
    int x;
    int loop = 0;
    boolean ready;
    while ((x = inputStreamReader.read()) != -1) {
        char result = (char)x;
        msgFifo.add(x);
        loop += 1;
        ready = inputStreamReader.ready();

        if (loop >= bufferSize * 2) break; //bufferSize is 2048
        if(!ready) break;
    }

I'm working on learning Java and I know some of the code is not great.

Upvotes: 0

Views: 61

Answers (0)

Related Questions