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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T04:14:06+00:00 2026-06-11T04:14:06+00:00

I am getting stream data from an http connection. I want to log the

  • 0

I am getting stream data from an http connection.
I want to log the stream to log files using log4j.

I need this stream further to do some other operations (must be retained)

How can I do that?

HttpURLConnection connection = (HttpURLConnection) url.openConnection();
connection.setRequestMethod("GET");
connection.setRequestProperty("Accept", "application/xml");
InputStream xml = connection.getInputStream();

I tried this one:

StreamUtils.copy(xml, new LogOutputStreamUtil(log, Level.INFO));

where LogOutputStreamUtil from http://www.java2s.com/Open-Source/Java/Testing/jacareto/jacareto/toolkit/log4j/LogOutputStream.java.htm

But as soon as it got logged. Stream is getting closed 🙁

  • 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-11T04:14:07+00:00Added an answer on June 11, 2026 at 4:14 am

    Simple solution would be to write your own inputStream wrapper

    Something like:

    class LogingInputStream extends BufferedStream {
        ByteArrayOutputStream copy4log = new ByteArrayOutputStream();
        InputStream source;
    
        public LogingInputStream( InputStream source ) { this.source = source; }
    
        public int read() {
            int value = source.read()
            logCopy.write( value );
            return value;
        }
    
        public void close() {
            source.close();
            StreamUtils.copy(xml, new LogOutputStreamUtil(copy4log, Level.INFO));
            copy4log.close();
        }
    
        .....more code here
    }
    

    Anyway, general idea is that you need to intercept inputstream read methods.

    There are other ways to accomplish this like copying source inputStream into a buffer (bytearray, string or whatever suits you), logging that buffer, and creating another inputStream around buffer and returning it to a caller instead source inputStream. It is simpler to do, but is it right solution depends on your usecase.

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

Sidebar

Related Questions

I'm getting a stream from HttpWebResponse.GetResponseStream() where I'm reading data from. Now I want
It is like continue getting data from mysql using PHP and simultaneously stream out
I am getting a stream of byte data from a telnet session via TcpClient.GetStream().ReadByte()
I am POSTING some data from an IPhone application want want to be able
I have a WCF service and Im looking to stream some files from server
I'm developing a recorder in silverlight and I need to upload data from stream
Getting red warning message that expected identifier for this statement CALayer = ImageView.layer; Using
I am trying to understand some example code on this web page: ( http://www.csharp-station.com/HowTo/HttpWebFetch.aspx
I am reading data from a TCP/IP stream and am successfully receiving a byte
I am trying to use the HTML Agility pack to scrape some data from

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.