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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T20:00:56+00:00 2026-06-13T20:00:56+00:00

I have a method which accepts an InputStream (of binary data) and serializes it

  • 0

I have a method which accepts an InputStream (of binary data) and serializes it to XML. In order to do so, it wraps the stream with a base64 encoder and a Reader to convert it to character data. However, since the InputStream is passed in as a parameter, I would consider it a harmful side effect to close the stream, and the contract for Reader.close() says it would do just that. If I don’t close the reader, the compiler warns me that I have a

Resource leak: reader is never closed

So, I can add a @SuppressWarnings( "resource" ) to the reader declaration, but is that the right thing to do? Am I missing something?

Here is the actual code:

/**
 * Writes base64 encoded text read from the binary stream.
 * 
 * @param binaryStream
 *            The binary stream to write from
 * @return <code>this</code> XmlWriter (for chaining)
 * @throws IOException
 */
public XmlWriter binary( InputStream binaryStream ) throws IOException {
    Reader reader = new InputStreamReader( 
            new Base64InputStream( binaryStream, true, base64LineLength, base64LineSeparator.getBytes( charset ) ) );
    int bufferSize = 2048;
    int charsRead;
    char[] buffer = new char[bufferSize];
    while ( (charsRead = reader.read( buffer, 0, bufferSize )) >= 0 ) {
        writer.write( buffer, 0, charsRead );
    }

    return this;
}
  • 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-13T20:00:58+00:00Added an answer on June 13, 2026 at 8:00 pm

    If you are a happy Java 7 user, try this:

    try(InputStream binaryStream = /* ... */) {
        xmlWriter.binary(binaryStream);
    }
    

    and stream is closed for you. If you can’t use Java 7, I agree that it’s not the responsibility of binary() method to close() the stream. Just ignore the warning and don’t let tools drive your design. It’s fine.

    As a last resort you can write a lightweight Reader wrapper ignoring close(), but I don’t advice it as it makes following the program flow harder.

    Also let Apache Commons IO help you with IOUtils.copy():

    public XmlWriter binary( InputStream binaryStream ) throws IOException {
        Reader reader = new InputStreamReader( 
                new Base64InputStream( binaryStream, true, base64LineLength, base64LineSeparator.getBytes( charset ) ) );
        IOUtils.copy(reader, writer);
        return this;
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a python webapp which accepts some data via POST. The method which
I have an ASP.NET MVC 3 action method which accepts a HttpFileCollectionBase in the
Assuming I have a method which accepts an IList or similar thing as a
I have a class Exporter which has a generic method which accepts an IEnumerable<T>
I have a method which accepts a filename as a parameter, all filenames should
I have a method which accepts a PDO object as an argument, to allow
I have a application, which accepts xml files, upon uploading a invalid xml file
I have a JAX-RS 1.1 resource FooBar that has a method which accepts a
I have the following issue: I want to create a method which accepts input
I have a C# method which accepts a Predicate<Foo> and returns a list of

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.