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 7968067
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T07:01:07+00:00 2026-06-04T07:01:07+00:00

This is a bit of an obscure problem that only seems to happen when

  • 0

This is a bit of an obscure problem that only seems to happen when I’m on certain computers.
I was having this issue today on our school’s XP computers and I can’t seem to replicate this on my home computer (W7).

Anyway, reading/writing to sockets in Java tends to be problematic whenever I use this code (where: int avail, InputStream socket, byte[] buffer, String output):

                   while( (avail = input.available()) > 0 )
                    {
                        read = input.read( buffer );
                        output += new String( buffer, 0, read );
                    }

It seems to make sense (reading all the data until no data is available to a temporary buffer, then to a string), but on our school computers (testing it using IE7), the whole thing somehow pauses. I’m thinking input.available() is causing it to somehow block because the thread just keeps running without ever reaching an endpoint… effectively just pausing somewhere.

OH, I forgot to mention: whenever I run this in debug mode and perform each line step-by-step, it works completely like it should… which just confuses me even more.

When I got home to replicate this issue, it works just fine (just using Firefox and IE8). I have no idea what would be a better alternative to this.

PS:
If the buffer is large enough and I just use:

                    read = input.read( buffer );
                    output += new String( buffer, 0, read );

It works just fine, but there’s always a worry that the data sent will exceed the buffer size.

  • 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-04T07:01:08+00:00Added an answer on June 4, 2026 at 7:01 am

    You’re thinking about available() the wrong way. That method tells you approximately how many bytes can be read right now, without blocking. The commonly accepted idiom for what you’re trying to do is

    int length;
    while ((length = in.read(buffer)) != -1) {
        output += new String(buffer, 0, length);
    }
    

    or something along those lines (not compiled/tested).

    Update: I think you misunderstand the concept of “end of the stream”. “End of the stream” doesn’t mean that all the data you want to read has been read. It means that there isn’t, and won’t ever be, anything else to read. For instance, it might mean that you were reading a file and have come to the end of it, or it might mean you were reading from an in-memory byte array and came to the end of that. Those are “end of streams”.

    In your question, you indicated, or at least implied, that you’re reading from a Socket. Are you aware that you’ll never get to the end of that stream until the associated Socket or the remote end of the connection is closed? Just because you received a bit of data from it doesn’t make it the end of the stream.

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

Sidebar

Related Questions

This may be a bit obscure, but I'm developing a viewer app that chunks
This is a bit of an obscure issue, but I'm using jQuery Sortables and
Ok so this is alittle bit obscure and I'm not sure its the best
Apologies if this question is a bit obscure, I've been banging my head against
This bit of code comes with new classes that are subclasses of UITableViewController... -
I've been looking at Sharepoint script files and I've come across this bit that
So I have this bit of python code that runs through a delicious page
The question title is a bit obscure so let me explain. A requirement that
We have a Silverlight solution that worked fine on our old computers. It was
Bit of an obscure one this. My setup is all running on my local

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.