I have a xmlStream which I am converting to jsonStream using org.apache.wink.json4j.utils.XML
. Here is the code
public void process(InputStream xmlStream) {
final BufferedInputStream bufferedXmlStream = new BufferedInputStream(xmlStream);
PipedInputStream pipedJsonInputStream = new PipedInputStream();
final PipedOutputStream jsonStream = new PipedOutputStream(pipedJsonInputStream);
Thread xmlToJsonThread = new Thread(new Runnable() {
@Override
public void run() {
// put your code that writes data to the outputstream here.
try {
XML.toJson(bufferedXmlStream, jsonStream, true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
xmlToJsonThread.setDaemon(true);
xmlToJsonThread.start();
//process data from piped stream
BufferedReader reader = new BufferedReader(new InputStreamReader(
pipedJsonInputStream, StandardCharsets.UTF_8));
try {
// use reader to further process json in main thread...
parseJsonStream(reader);
} finally {
reader.close();
jsonStream.close();
}
}
When XML.toJson throws exception I see that main thread does not exit. How do I handle this ? Do you guys think this is a good way of converting XML stream to Json stream for further processing ? I would really appreciate any suggestions. Thanks a lot!