Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is there a simple “tee” filter for Java input streams?

For debugging purposes I would like to dump the content of an input stream into a file while it is processed. The stream is parsed by org.xml.sax.XMLReader which will consume the data.

I would gess one need a filter to archive this and before writing one my self I wanted to ask if there is a ready made version already.

I work on Android.

What I have:

final org.apache.http.HttpEntity responseEntity = response.getEntity ();
final java.io.InputStream content = responseEntity.getContent ();
final java.io.InputStreamReader contentReader =
   new java.io.InputStreamReader (content, "UTF-8");

So I have an java.io.InputStream and an InputStreamReader.

like image 546
Martin Avatar asked May 18 '11 11:05

Martin


People also ask

What is filter input stream?

A FilterInputStream contains some other input stream, which it uses as its basic source of data, possibly transforming the data along the way or providing additional functionality.

How does Java handle input stream?

Methods of InputStreamread() - reads one byte of data from the input stream. read(byte[] array) - reads bytes from the stream and stores in the specified array. available() - returns the number of bytes available in the input stream. mark() - marks the position in the input stream up to which data has been read.

What is buffered input stream in Java?

A BufferedInputStream adds functionality to another input stream-namely, the ability to buffer the input and to support the mark and reset methods. When the BufferedInputStream is created, an internal buffer array is created.


2 Answers

Commons IO to the rescue! Check out TeeInputStream and TeeOutputStream.

like image 54
stevevls Avatar answered Sep 19 '22 22:09

stevevls


Not quite a ready rolled one, but this might be of interest. There is a TeeOutputStream implementation in the examples.

like image 20
DaveH Avatar answered Sep 22 '22 22:09

DaveH