You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

StreamHelper.java 3.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  1. /* ====================================================================
  2. Licensed to the Apache Software Foundation (ASF) under one or more
  3. contributor license agreements. See the NOTICE file distributed with
  4. this work for additional information regarding copyright ownership.
  5. The ASF licenses this file to You under the Apache License, Version 2.0
  6. (the "License"); you may not use this file except in compliance with
  7. the License. You may obtain a copy of the License at
  8. http://www.apache.org/licenses/LICENSE-2.0
  9. Unless required by applicable law or agreed to in writing, software
  10. distributed under the License is distributed on an "AS IS" BASIS,
  11. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. See the License for the specific language governing permissions and
  13. limitations under the License.
  14. ==================================================================== */
  15. package org.apache.poi.openxml4j.opc;
  16. import java.io.FilterOutputStream;
  17. import java.io.IOException;
  18. import java.io.InputStream;
  19. import java.io.OutputStream;
  20. import javax.xml.transform.OutputKeys;
  21. import javax.xml.transform.Result;
  22. import javax.xml.transform.Source;
  23. import javax.xml.transform.Transformer;
  24. import javax.xml.transform.TransformerException;
  25. import javax.xml.transform.dom.DOMSource;
  26. import javax.xml.transform.stream.StreamResult;
  27. import org.apache.poi.util.IOUtils;
  28. import org.apache.poi.util.XMLHelper;
  29. import org.w3c.dom.Document;
  30. public final class StreamHelper {
  31. private StreamHelper() {
  32. // Do nothing
  33. }
  34. /**
  35. * Save the document object in the specified output stream.
  36. *
  37. * @param xmlContent
  38. * The XML document.
  39. * @param outStream
  40. * The OutputStream in which the XML document will be written.
  41. * @return <b>true</b> if the xml is successfully written in the stream,
  42. * else <b>false</b>.
  43. */
  44. public static boolean saveXmlInStream(Document xmlContent,
  45. OutputStream outStream) {
  46. try {
  47. Transformer trans = XMLHelper.newTransformer();
  48. Source xmlSource = new DOMSource(xmlContent);
  49. // prevent close of stream by transformer:
  50. Result outputTarget = new StreamResult(new FilterOutputStream(
  51. outStream) {
  52. @Override
  53. public void write(byte[] b, int off, int len)
  54. throws IOException {
  55. out.write(b, off, len);
  56. }
  57. @Override
  58. public void close() throws IOException {
  59. out.flush(); // only flush, don't close!
  60. }
  61. });
  62. // xmlContent.setXmlStandalone(true);
  63. trans.setOutputProperty(OutputKeys.ENCODING, "UTF-8");
  64. // don't indent xml documents, the indent will cause errors in calculating the xml signature
  65. // because of different handling of linebreaks in Windows/Unix
  66. trans.setOutputProperty(OutputKeys.INDENT, "no");
  67. trans.setOutputProperty(OutputKeys.STANDALONE, "yes");
  68. trans.transform(xmlSource, outputTarget);
  69. } catch (TransformerException e) {
  70. return false;
  71. }
  72. return true;
  73. }
  74. /**
  75. * Copy the input stream into the output stream.
  76. *
  77. * @param inStream
  78. * The source stream.
  79. * @param outStream
  80. * The destination stream.
  81. * @return <b>true</b> if the operation succeed, else return <b>false</b>.
  82. */
  83. public static boolean copyStream(InputStream inStream, OutputStream outStream) {
  84. try {
  85. IOUtils.copy(inStream, outStream);
  86. return true;
  87. } catch (Exception e) {
  88. return false;
  89. }
  90. }
  91. }