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

  • Home
  • SEARCH
  • 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 1062087
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T18:34:13+00:00 2026-05-16T18:34:13+00:00

Currently, I have a method : void method(InputStream stream) { // Create UTF-8 reader

  • 0

Currently, I have a method :

void method(InputStream stream) {
    // Create UTF-8 reader by wrapping up the stream.
}

The reason I do not want to have method to accept Reader is that, I want my own method to have own control to decide what type of encoding should be used.

The problem is, whenever I close the Reader, InputStream will be closed as well. This is not my intention. As InputStream is “opened” by the caller. Hence, the closing operation on InputStream shall be done at caller side.

Is there any way I can close the Reader in “method”, without closing the InputStream passed by caller?

Thanks.

  • 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-16T18:34:14+00:00Added an answer on May 16, 2026 at 6:34 pm

    If you don’t want to close the input stream, just don’t close the reader. Closing the reader will serve no purpose other than to close the input stream, after all.

    That’s assuming your method would be closing the reader… if it wouldn’t be, then overriding close() in your own Reader implementation (e.g. deriving from InputStreamReader) or overriding close() in a FilterInputStream as per the other answers should be fine.

    EDIT: The comments have expressed some concerns about resource leakage. It’s true that in theory, InputStreamReader (which is the kind of reader you’re most likely to be using) could be doing something else behind the scenes… writing the output to a file as it goes, for example. In reality, that’s simply not going to happen. close() is just going to close the stream (via a StreamDecoder, in fact – at least in the JDK 6 implementation). The only unmanaged resource involved is the stream itself… and you want to effectively leak that.

    For an input stream, there’s no such concept as “flush” – what would it even do? There’s basically nothing to clean up apart from the input stream and structures in memory… the GC will take care of the memory, so you’re done.

    Using a FilterInputStream is a generally cleaner approach, I’ll certainly concede that – but for this specific case I’d just not close the reader. Heck, that’ll make the code simpler than normal, as you’d usually want a try/finally block to make sure you closed it in all situations. All of that can just go.

    Note that if you were publishing the reader to any other code, all of the above logic would be null and void – you’d want to close the reader to stop the other code from potentially using it when they shouldn’t. In this case though, I’m anticipating that you’ll construct the reader, read from it, and then just let it get garbage collected, never publishing the reference elsewhere.

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

Sidebar

Ask A Question

Stats

  • Questions 517k
  • Answers 517k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer Short answer - yes this could be checked using constraint… May 16, 2026 at 7:58 pm
  • Editorial Team
    Editorial Team added an answer This is bad design. You should provide a callback function… May 16, 2026 at 7:58 pm
  • Editorial Team
    Editorial Team added an answer No, you don't need to define them again - you… May 16, 2026 at 7:58 pm

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

Related Questions

I currently have this method header: public virtual void SetupGrid<T>() where T : class,
Currently I've found that it's most convenient to have a separate public static void
I currently have a Service that runs fine when I start it but when
I currently have a LINQ query implemented as a method of my DataContext class.
I have a method that takes an array of queries, and I need to
I have a method that I will use in the following contexts: 1. User
If I have a method that hides a button, I would probably call it
I have this method called LoadToDictionary. It accepts a dictionary and a filepath. Currently,
I currently have an object called Station defined as: @interface RFStation : NSObject {
I'd like to get the currently executing NUnit test in a helper method I'm

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.