Du kan inte välja fler än 25 ämnen Ämnen måste starta med en bokstav eller siffra, kan innehålla bindestreck ('-') och vara max 35 tecken långa.

HWPFTestDataSamples.java 6.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171
  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.hwpf;
  16. import java.io.ByteArrayInputStream;
  17. import java.io.ByteArrayOutputStream;
  18. import java.io.IOException;
  19. import java.io.InputStream;
  20. import java.net.URL;
  21. import java.util.zip.ZipInputStream;
  22. import org.apache.poi.POIDataSamples;
  23. import org.apache.poi.poifs.filesystem.POIFSFileSystem;
  24. import org.apache.poi.util.IOUtils;
  25. import org.apache.poi.util.POILogFactory;
  26. import org.apache.poi.util.POILogger;
  27. public class HWPFTestDataSamples {
  28. private static final POILogger logger = POILogFactory
  29. .getLogger( HWPFTestDataSamples.class );
  30. public static HWPFDocument openSampleFile(String sampleFileName) {
  31. try {
  32. InputStream is = POIDataSamples.getDocumentInstance().openResourceAsStream(sampleFileName);
  33. return new HWPFDocument(is);
  34. } catch (IOException e) {
  35. throw new RuntimeException(e);
  36. }
  37. }
  38. public static HWPFDocument openSampleFileFromArchive( String sampleFileName )
  39. {
  40. final long start = System.currentTimeMillis();
  41. try
  42. {
  43. ZipInputStream is = new ZipInputStream( POIDataSamples
  44. .getDocumentInstance()
  45. .openResourceAsStream( sampleFileName ) );
  46. try
  47. {
  48. is.getNextEntry();
  49. ByteArrayOutputStream baos = new ByteArrayOutputStream();
  50. try
  51. {
  52. IOUtils.copy( is, baos );
  53. }
  54. finally
  55. {
  56. baos.close();
  57. }
  58. final long endUnzip = System.currentTimeMillis();
  59. byte[] byteArray = baos.toByteArray();
  60. logger.log( POILogger.DEBUG, "Unzipped in ",
  61. Long.valueOf( endUnzip - start ), " ms -- ",
  62. Long.valueOf( byteArray.length ), " byte(s)" );
  63. ByteArrayInputStream bais = new ByteArrayInputStream( byteArray );
  64. HWPFDocument doc = new HWPFDocument( bais );
  65. final long endParse = System.currentTimeMillis();
  66. logger.log( POILogger.DEBUG, "Parsed in ",
  67. Long.valueOf( endParse - start ), " ms" );
  68. return doc;
  69. }
  70. finally
  71. {
  72. is.close();
  73. }
  74. }
  75. catch ( IOException e )
  76. {
  77. throw new RuntimeException( e );
  78. }
  79. }
  80. /**
  81. * Open a remote sample from URL. opening is performd in two phases:
  82. * (1) download content into a byte array
  83. * (2) construct HWPFDocument
  84. *
  85. * @param sampleFileUrl the url to open
  86. */
  87. public static HWPFDocument openRemoteFile( String sampleFileUrl )
  88. {
  89. final long start = System.currentTimeMillis();
  90. try
  91. {
  92. logger.log(POILogger.DEBUG, "Downloading ", sampleFileUrl, " ...");
  93. InputStream is = new URL( sampleFileUrl ).openStream();
  94. try
  95. {
  96. ByteArrayOutputStream baos = new ByteArrayOutputStream();
  97. try
  98. {
  99. IOUtils.copy( is, baos );
  100. }
  101. finally
  102. {
  103. baos.close();
  104. }
  105. final long endDownload = System.currentTimeMillis();
  106. byte[] byteArray = baos.toByteArray();
  107. logger.log( POILogger.DEBUG, "Downloaded in ",
  108. Long.valueOf( endDownload - start ), " ms -- ",
  109. Long.valueOf( byteArray.length ), " byte(s)" );
  110. ByteArrayInputStream bais = new ByteArrayInputStream( byteArray );
  111. HWPFDocument doc = new HWPFDocument( bais );
  112. final long endParse = System.currentTimeMillis();
  113. logger.log( POILogger.DEBUG, "Parsed in ",
  114. Long.valueOf( endParse - start ), " ms" );
  115. return doc;
  116. }
  117. finally
  118. {
  119. is.close();
  120. }
  121. }
  122. catch ( IOException e )
  123. {
  124. throw new RuntimeException( e );
  125. }
  126. }
  127. public static HWPFOldDocument openOldSampleFile(String sampleFileName) {
  128. try {
  129. InputStream is = POIDataSamples.getDocumentInstance().openResourceAsStream(sampleFileName);
  130. return new HWPFOldDocument(new POIFSFileSystem(is));
  131. } catch (IOException e) {
  132. throw new RuntimeException(e);
  133. }
  134. }
  135. /**
  136. * Writes a spreadsheet to a <tt>ByteArrayOutputStream</tt> and reads it back
  137. * from a <tt>ByteArrayInputStream</tt>.<p/>
  138. * Useful for verifying that the serialisation round trip
  139. */
  140. public static HWPFDocument writeOutAndReadBack(HWPFDocument original) {
  141. try {
  142. ByteArrayOutputStream baos = new ByteArrayOutputStream(4096);
  143. original.write(baos);
  144. ByteArrayInputStream bais = new ByteArrayInputStream(baos.toByteArray());
  145. return new HWPFDocument(bais);
  146. } catch (IOException e) {
  147. throw new RuntimeException(e);
  148. }
  149. }
  150. }