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 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

Related Questions

I currently have this method: -(void)seperateGuides { DLog(GetGuideListCount: %i, [[appDelegate getGuideList] count]); columnOneArray =
i currently have a method that checks what is around the centre item in
Currently I have created a ABCFactory class that has a single method creating ABC
I have a method in Java that concatenates 2 Strings. It currently works correctly,
All, I have a method that is currently used to invoke DLLs of return
Currently I have a timer with an alarm (local notification). I want to create
I currently have this method header: public virtual void SetupGrid<T>() where T : class,
I currently have an extension method on System.Windows.Forms.Control like this: public static void ExampleMethod(this
I currently have this method in a standard web service: [WebMethod] public void addGame(int
I currently have this working but it requires me to have a static method

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.