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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T15:31:51+00:00 2026-06-10T15:31:51+00:00

I have three questions. To explain, I was reviewing someone’s code, and noticed BufferedReader

  • 0

I have three questions.

To explain, I was reviewing someone’s code, and noticed BufferedReaders sometimes aren’t being closed. Usually, Eclipse gives a warning that this is a potential memory leak (and I fix it). However, within a Callable inner class, there is no warning.

class outerClass {
    ...
    public void someMethod() {
        Future<Integer> future = outputThreadPool.submit(new innerClass(this.myProcess.getInputStream(), threadName));
        ...
    }

    class innerClass implements Callable<Integer> {
        private final InputStream stream;
        private final String prepend;

        innerClass(InputStream stream, String prepend) {
            this.stream = stream;
            this.prepend = prepend;
        }

        @Override
        public Integer call() {
            BufferedReader stdOut = new BufferedReader(new InputStreamReader(stream));
            String output = null;
            try {
                while ((output = stdOut.readLine()) != null) {
                    log.info("[" + prepend + "] " + output);
                }

            } catch (IOException ignore) {
            // I have no idea why we're ignoring this... :-|        
            }
            return 0;   
        }
    }
}

The people who wrote the code are experienced Java developers, so my first thought is that it’s intentional… but it could be they were in a hurry when they wrote it and just overlooked it.

My questions are:

  1. Why does Eclipse not highlight this (which may be answered by the answer to the following questions)?

  2. What is the worst that could happen if it’s closed within the call() method? (I can’t think of a good reason… and I’ve been searching for a while… but maybe it was intentional not to close the BufferedReader)

  3. What is the worst that could happen if the BufferedReader is not closed within the inner class?

  • 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-10T15:31:53+00:00Added an answer on June 10, 2026 at 3:31 pm

    I would say that since they’re creating a BufferedReader around a given InputStream, the code is safe not calling close(). The code that calls close() should always be the code that creates the stream and done using try/finally.

    public static void read(String str) throws IOException {
        FileInputStream stream = null
        try {
            stream = new FileInputStream(str);
            readStreamToConsole(stream);
        } finally {
            if (stream != null)
                stream.close();
        }
    }
    
    private static void readStreamToConsole(InputStream stream) {
        BufferedReader stdOut = new BufferedReader(new InputStreamReader(stream));
        String output = null;
        while ((output = stdOut.readLine()) != null)
            System.out.println(output);
    }
    

    Another note: your code appears to be logging output from some other process. You probably wouldn’t be able to close the stream anyway. Without testing it myself, I’m not sure what would happen if you closed a stream from another process.

    Oh, and the IOException isn’t likely to happen because the stream is coming from another process. That’s not likely to happen unless some irrecoverable error occurs. It still wouldn’t be a bad idea to log the exception somehow, though.


    Edit to address your comment about mixed answers:

    Let’s use an output stream and BufferedWriter as an example this time:

    private static final String NEWLINE = System.getProperty("line.separator");
    
    public static void main(String[] args) throws IOException {
        String file = "foo/bar.txt";
        FileOutputStream stream = null;
        try {
            stream = new FileOutputStream(file);
            writeLine(stream, "Line 1");
            writeLine(stream, "Line 2");
        } finally {
            if (stream != null)
                stream.close();
        }
    }
    
    private static void writeLine(OutputStream stream, String line) throws IOException {
        BufferedWriter writer = new BufferedWriter(new InputStreamWriter(stream));
        writer.write(line + NEWLINE);
    }
    

    This works. The writeLine method is used as a delegate to creating writer and actually writing a single line to the file. Of course, this logic could be something more complex, such as turning an object into a String and writing it. This makes the main method a little easier to read too.

    Now, what if instead, we closed the BufferedWriter?

    private static void writeLine(OutputStream stream, String line) throws IOException {
        BufferedWriter writer = null;
        try {
            writer = new BufferedWriter(new InputStreamWriter(stream));
            writer.write(line + NEWLINE);
        } finally {
            if (writer != null)
                writer.close();
        }
    }
    

    Try running it with that, and it will fail every time on the second writeLine call. It’s good practice to always close streams where they’re created, instead of where they’re passed. It may be fine initially, but then trying to change that code later could result in errors. If I started with only 1 writeLine call with the bad method and someone else wanted to add a second one, they’d have to refactor the code so that writeLine didn’t close the stream anyway. Getting close-happy can cause headaches down the road.

    Also note that technically, the BufferedWriter isn’t the actual handle to your system resource, the FileOutputStream is, so you should be closing on the actual resource anyway.

    So, rule of thumb: only close your streams where you create them, and always do the creation and closing in a try/finally block (or Java 7’s awesome try/resource block, which does the closing for you).

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

Sidebar

Related Questions

I have three values that are being generated dynamically in the code-behind of my
I have a three part question based on a dataframe (df is example rows)
There have been questions with answers on how to write rubygems, but what should
As per the title I have three parts to this question... Is db4o object
There have been a few similar questions with solutions, but none answered my question,
There have been several questions posted to SO about floating-point representation. For example, the
There have been several questions over the past few days about the proper use
There have been some similar questions asked regarding Grid views, but none have been
I know there have been many questions on grid and pack in the past
I know there have been some similar questions to this, but they haven't helped

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.