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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 22, 20262026-05-22T14:31:40+00:00 2026-05-22T14:31:40+00:00

I have a piece of code to read a InputStream and write the content

  • 0

I have a piece of code to read a InputStream and write the content into an OutputStream:

BufferedInputStream in = new BufferedInputStream(...);
FileOutputStream outStream = new FileOutputStream outStream(...);

int read = in.read(buffer, 0, bufferSize);
while (read != -1) {
    outStream.write(buffer, 0, read);
    read = in.read(buffer, 0, bufferSize);
}

It works, but i don’t like it since the variable read is declared out of the loop, and read() method is written twice.
The revised version:

 for (int read = 0; read != -1; read = in.read(buffer, 0, bufferSize)) {
      outStream.write(buffer, 0, read);
 }

It looks better but not good enough because the first iteration is useless (and maybe harmful) with read=0.

Do you have a better solution?

  • 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-05-22T14:31:41+00:00Added an answer on May 22, 2026 at 2:31 pm

    Personally I break the normal “no side-effect in a condition” rule for this sort of thing:

    int bytesRead;
    while ((bytesRead = in.read(buffer, 0, bufferSize)) != -1)
    {
        outStream.write(buffer, 0, bytesRead);
    }
    

    EDIT: As noted, it does involve declaring read outside the loop, but it only calls read() once. I’ve never found it to be a problem – while I generally prefer to declare variables with as small a scope as possible, that’s more a general cleanliness thing. If you want to limit the scope further you can put the whole thing in braces, or extract it to its own method, like Alan’s approach. Here’s how I’d implement it though:

    public static void copyStream(InputStream input, OutputStream output)
        throws IOException {
      byte[] buffer = new byte[1024 * 16]; // Reasonable general size
    
      int bytesRead;
      while ((bytesRead = in.read(buffer, 0, buffer.length)) != -1) {
        outStream.write(buffer, 0, bytesRead);
      }
    }
    

    Alternatively you could provide the buffer length as a parameter. Note that this can now go off into a utility library, and you need never write the code again.

    Alternatively, you could use the fact that it’s already available in other utility libraries, such as Guava as ByteStreams.copy

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

Sidebar

Related Questions

I have a piece of code for stream copying. OutputStream os = ...; InputStream
I have a piece of code that reads the content from a non-empty InputStream.
I have this piece of code using standard sockets: void set_fds(int sock1, int sock2,
So, I have this piece of code, which will just read the message from
I have found the following piece of code which uses XmlHttpRequest to read JSON
I have problem running and debugging this piece of code: bool readSectionHeaders(char* path, int
I have a mostly-working piece of code to read a directory, output each folder
I have a piece of Javascript code I'm trying to understand // read big-endian
I have the following piece of code to read Japanese Kanji characters from UTF-8
I have a piece of code that can read current state of serial port

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.