Advertisement
tjrothwell

Piped IO java transformation

Aug 18th, 2011
557
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 5 1.52 KB | None | 0 0
  1.  
  2.  
  3.        private void identStreamWithThread(T req, final OutputStream ostream) throws IOException, JAXBException {
  4.           final PipedOutputStream pipedOutput = new PipedOutputStream();
  5.           Runnable copyStream = new Runnable() {
  6.              @Override
  7.              public void run() {
  8.                 try {
  9.                    PipedInputStream pipedInput = new PipedInputStream(pipedOutput);
  10.                    TransformerFactory transformerFactory = TransformerFactory.newInstance();
  11.                    Transformer transformer = transformerFactory.newTransformer();
  12.                    transformer.setOutputProperty(OutputKeys.INDENT, "yes");
  13.                    transformer.setOutputProperty("{http://xml.apache.org/xslt}indent-amount", "2");
  14.                    Source source = new StreamSource(pipedInput);
  15.                    Result result = new StreamResult(ostream);
  16.                    transformer.transform(source, result);
  17.                 } catch (IOException e) {
  18.                    e.printStackTrace();
  19.                 } catch (TransformerException e) {
  20.                    e.printStackTrace();
  21.                 }
  22.              }
  23.           };
  24.           Thread t = new Thread(copyStream);
  25.           t.setDaemon(true);
  26.           t.start();
  27.           Marshaller m = jaxbContext.createMarshaller();
  28.           m.marshal(req, pipedOutput);
  29.           pipedOutput.close();
  30.           try {
  31.              t.join();
  32.           } catch (InterruptedException e) {
  33.              throw new IOException(e);
  34.           }
  35.        }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement