Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8662173
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T16:40:10+00:00 2026-06-12T16:40:10+00:00

I am reading from an input stream which I am receiving from a server

  • 0

I am reading from an input stream which I am receiving from a server side of a socket. I have no way of knowing what the size of my response will be and I am reading into a byte array this is the code

public static String readString(InputStream inputStream) throws IOException {

    ByteArrayOutputStream into = new ByteArrayOutputStream();
    byte[] buf = new byte[4096];
    for (int n; 0 < (n = inputStream.read(buf));) {
        System.out.println("Got to this point n is "+n);
        into.write(buf, 0, n); 
    }
    into.close();
    System.out.println("passed this point");
    return new String(into.toByteArray(), AddATudeMessage.CHARENC); 
}

write now the numbers 11 and 235 get printed to the console so I assume the server is still writing output into the input stream. it however gets stuck at 235 and nothing seemes to be happending. I tried pausing the main execution thread for as much as 20 seconds at which point i receive 241 bytes from the server and then the same infinite loop reoccurs anyone know what’s going on

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-12T16:40:11+00:00Added an answer on June 12, 2026 at 4:40 pm

    The way your code is structured, it can work ONLY IF the server closes the socket when it’s done. Otherwise your side can never know if the server is done or is merely slow (or there’s network congestion).

    There are only two ways to solve this problem:

    1. Have the server send the expected length first, then stop reading and proceed when you’ve received the designated number of bytes.
    2. Have the server send a special data sequence when it’s done, some sort of EOF marker that cannot occur in normal data that you can look for to tell you when there’s no more data.

    Solutions using available() are incorrect because it can return zero if the receive buffer is currently empty but there is still data in transit.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am using 2 threads- ReaderThread for reading from the Socket input stream and
OK, so I have a socket InputStream over which the server is sending a
I am writing an application which blocks on input from two istreams . Reading
I'm reading from the standard input using the read() system call but there's a
In my simple custom shell I'm reading commands from the standard input and execute
How I can start reading from a specific byte? I have following code: try
I'm reading from a binary data file which is written by invoking the following
I'm reading from a byte stream that contains a series of variable length descriptors
I have an app in android which is a kind of client-server. I've established
I have a text file in my computer which I am reading form my

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.