Java SAXParser - keep InputStream open
Asked Answered
L

2

4

I've a BufferedInputStream from which I want to parse XML with SAXParser but then reuse it again (eg. mark(int) & reset()). However this stream is closed in parse() method. Is it possible to somehow tell SAXParser to leave it open? The last resort is to wrap this stream with un-closeable stream.

Thank you.

Larrisa answered 1/12, 2011 at 12:50 Comment(0)
B
8

How about something like:

class WontCloseBufferedInputStream extends BufferedInputStream {
  public void close () {
    // Do nothing.
  }

  public void reallyClose() {
    super.close ();
  }
}
Boggle answered 1/12, 2011 at 13:5 Comment(2)
I am interested to know why you see this as a last resort. It does exactly what you want.Boggle
Because it's ugly. IMO library shouldn't close passed in InputStream/Reader.Larrisa
E
-1

You can pass InputSource object rather than InputStream object to SAXParser

sample code

SAXParser parser = // saxpaser object
        InputSource isource = new InputSource();
        InputStream istream = //your inputstream
        isource.setByteStream(istream);
        parser.parse(isource, handler);
Excitor answered 1/12, 2011 at 13:9 Comment(2)
Is the InputSource closed when the parser completes? If so this is unhelpful.Boggle
But then this post doesn't solve the problem neither hints the solution :o)Redheaded

© 2022 - 2024 — McMap. All rights reserved.