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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T10:19:44+00:00 2026-06-11T10:19:44+00:00

What the difference between byte[] buffer = new byte[1024]; // this: if (inputStream.read(buffer) >

  • 0

What the difference between

byte[] buffer = new byte[1024];
// this:
if (inputStream.read(buffer) > 0) { /*...*/ }
// and:
if (inputStream.read(buffer) != -1) { /*...*/ }

Can both determine the network stream terminate?

  • 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-11T10:19:45+00:00Added an answer on June 11, 2026 at 10:19 am

    The Javadocs for InputStream.read() say:

    If the length of b is zero, then no bytes are read and 0 is returned

    In normal use, this should never happen, so there’s not much point to testing for this condition explicitly. (If you want to avoid looping forever because the buffer is zero-length and fail-fast in this situation, just test the length of the buffer.)

    Further on, there’s:

    Returns: the total number of bytes read into the buffer, or -1 if there is no more data because the end of the stream has been reached.

    If you want to test for end-of-file (or network stream, or whatever), use the test:

    if ( inputStream.read(buffer) != -1 ) ...
    

    A non-buggy Java implementation will never return anything else to indicate there’s no more data available.

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

Sidebar

Related Questions

what is the difference between this two Byte i1=new Byte(1);//complier error byte b=1;//ok my
Pretty simple question: what's the performance difference between a Byte Stream and a Character
What is the difference between byte[] and char[]? Differentiate more from usage perspective. Can
What is the difference between Big Endian and Little Endian Byte order ? Both
Can any one explain difference between position and anchor point in cocos-2D with some
Is there a difference between this.form and document.forms (document[forms]) or, are they similar? Here
What's the difference between #1 and #2: Code 1 (compiled ok): byte[] GetSomeBytes() {
Can anyone explain the difference between calling GetPreamble() on a newly instantiated utf8 encoding
What's the difference between a string and a byte string? When is it appropriate
What's the difference - performance-wise - between reading from a socket 1 byte a

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.