Altova Mailing List Archives>Archive Index >comp.text.xml Archive Home >Recent entries >Thread Prev - Re: Java SAX parser. How to get the raw XML code of the currently parsing event [Thread Next] Re: Java SAX parser. How to get the raw XML code of the currently parsing eventTo: NULL Date: 7/3/2008 12:39:00 PM On 3 =E0=CC=C9, 17:34, "Joseph J. Kesselman" <keshlam-nos...@comcast.net>
wrote:
> > Socket -> InputStreamReader -> BufferedReader -> InputSource -> then
> > it goes to .parse(InputSource)....of the SAX.
>
> > What node on this chain can be cloned so I can read the data two
> > times.....once for the parser, once for my debugging log.
>
> I'd suggest dealing with it at the Java level. Find or write a "wrapper"
> reader implementation which saves a copy of the data passing through it
> off to a data structure or scratch file for rereading, or perhaps which
> writes the data direct to your log as it passes through. Plug that in
> somewhere between the InputStreamReader and the InputSource -- whether
> upstream, downstream, or in place of the BufferedReader depends on the
> details of how this tee adapter is written.
YES!
I've solved my problem using class RecordingInputStream that wraps the
InputStream
here is the class source code:
import java.io.ByteArrayOutputStream;
import java.io.FilterInputStream;
import java.io.InputStream;
import java.io.IOException;
/**
*
* @author Unknown
*
*/
class RecordingInputStream extends FilterInputStream {
protected ByteArrayOutputStream sink;
RecordingInputStream(InputStream in) {
this(in, new ByteArrayOutputStream());
}
RecordingInputStream(InputStream in, ByteArrayOutputStream sink)
{
super(in);
this.sink =3D sink;
}
public synchronized int read() throws IOException {
int i =3D in.read();
sink.write(i);
return i;
}
public synchronized int read(byte[] buf, int off, int len) throws
IOException {
int l =3D in.read(buf, off, len);
sink.write(buf, off, l);
return l;
}
public synchronized int read(byte[] buf) throws IOException {
return read(buf, 0, buf.length);
}
public synchronized long skip(long len) throws IOException {
long l =3D 0;
int i =3D 0;
byte[] buf =3D new byte[1024];
while (l < len) {
i =3D read(buf, 0, (int)Math.min((long)buf.length, len -
l));
if (i =3D=3D -1) break;
l +=3D i;
}
return l;
}
byte[] getBytes() {
return sink.toByteArray();
}
void resetSink() {
sink.reset();
}
}
| ||||||
| Company | Legal | Press | Partners | Careers | Sitemap | Contact Us | Altova Blog | Mobile | Full Site | |||
|
