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.

DeferredSXSSFWorkbook.java 5.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170
  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.xssf.streaming;
  16. import java.io.IOException;
  17. import java.io.OutputStream;
  18. import java.util.Iterator;
  19. import org.apache.logging.log4j.LogManager;
  20. import org.apache.logging.log4j.Logger;
  21. import org.apache.poi.ss.usermodel.Sheet;
  22. import org.apache.poi.util.Beta;
  23. import org.apache.poi.util.NotImplemented;
  24. import org.apache.poi.xssf.usermodel.XSSFSheet;
  25. import org.apache.poi.xssf.usermodel.XSSFWorkbook;
  26. /**
  27. * An variant of SXSSFWorkbook that avoids generating a temporary file and writes data directly to
  28. * the provided OutputStream.
  29. *
  30. * This variant is experimental and APIs may change at short notice.
  31. *
  32. * @since 5.0.0
  33. */
  34. @Beta
  35. public class DeferredSXSSFWorkbook extends SXSSFWorkbook {
  36. private static final Logger LOG = LogManager.getLogger(DeferredSXSSFWorkbook.class);
  37. public DeferredSXSSFWorkbook() {
  38. this(null);
  39. }
  40. public DeferredSXSSFWorkbook(int rowAccessWindowSize) { this(null, rowAccessWindowSize); }
  41. public DeferredSXSSFWorkbook(XSSFWorkbook workbook) {
  42. this(workbook, SXSSFWorkbook.DEFAULT_WINDOW_SIZE);
  43. }
  44. public DeferredSXSSFWorkbook(XSSFWorkbook workbook, int rowAccessWindowSize) {
  45. super(workbook, rowAccessWindowSize, false, false);
  46. }
  47. @NotImplemented
  48. @Override
  49. protected SheetDataWriter createSheetDataWriter() throws IOException {
  50. throw new RuntimeException("Not supported by DeferredSXSSFWorkbook");
  51. }
  52. protected StreamingSheetWriter createSheetDataWriter(OutputStream out) throws IOException {
  53. return new StreamingSheetWriter(out);
  54. }
  55. @Override
  56. protected ISheetInjector createSheetInjector(SXSSFSheet sxSheet) throws IOException {
  57. DeferredSXSSFSheet ssxSheet = (DeferredSXSSFSheet) sxSheet;
  58. return (output) -> {
  59. ssxSheet.writeRows(output);
  60. };
  61. }
  62. @Override
  63. SXSSFSheet createAndRegisterSXSSFSheet(XSSFSheet xSheet) {
  64. final DeferredSXSSFSheet sxSheet;
  65. try {
  66. sxSheet = new DeferredSXSSFSheet(this, xSheet);
  67. } catch (IOException ioe) {
  68. throw new RuntimeException(ioe);
  69. }
  70. registerSheetMapping(sxSheet, xSheet);
  71. return sxSheet;
  72. }
  73. @Override
  74. public DeferredSXSSFSheet createSheet() {
  75. return (DeferredSXSSFSheet) super.createSheet();
  76. }
  77. @Override
  78. public DeferredSXSSFSheet createSheet(String sheetname) {
  79. return (DeferredSXSSFSheet) super.createSheet(sheetname);
  80. }
  81. /**
  82. * Returns an iterator of the sheets in the workbook in sheet order. Includes hidden and very hidden sheets.
  83. *
  84. * @return an iterator of the sheets.
  85. */
  86. @Override
  87. public Iterator<Sheet> sheetIterator() {
  88. return new SheetIterator<>();
  89. }
  90. /**
  91. * Gets the sheet at the given index for streaming.
  92. *
  93. * @param index the index
  94. * @return the streaming sheet at
  95. */
  96. public DeferredSXSSFSheet getStreamingSheetAt(int index) {
  97. XSSFSheet xSheet = _wb.getSheetAt(index);
  98. SXSSFSheet sxSheet = getSXSSFSheet(xSheet);
  99. if (sxSheet == null && xSheet != null) {
  100. return (DeferredSXSSFSheet) createAndRegisterSXSSFSheet(xSheet);
  101. } else {
  102. return (DeferredSXSSFSheet) sxSheet;
  103. }
  104. }
  105. public XSSFSheet getXSSFSheet(String name) {
  106. return _wb.getSheet(name);
  107. }
  108. /**
  109. * Gets sheet with the given name for streaming.
  110. *
  111. * @param name the name
  112. * @return the streaming sheet
  113. */
  114. public DeferredSXSSFSheet getStreamingSheet(String name) {
  115. XSSFSheet xSheet = _wb.getSheet(name);
  116. DeferredSXSSFSheet sxSheet = (DeferredSXSSFSheet) getSXSSFSheet(xSheet);
  117. if (sxSheet == null && xSheet != null) {
  118. return (DeferredSXSSFSheet) createAndRegisterSXSSFSheet(xSheet);
  119. } else {
  120. return sxSheet;
  121. }
  122. }
  123. /**
  124. * Removes sheet at the given index
  125. *
  126. * @param index of the sheet to remove (0-based)
  127. */
  128. @Override
  129. public void removeSheetAt(int index) {
  130. // Get the sheet to be removed
  131. XSSFSheet xSheet = _wb.getSheetAt(index);
  132. SXSSFSheet sxSheet = getSXSSFSheet(xSheet);
  133. // De-register it
  134. _wb.removeSheetAt(index);
  135. // The sheet may not be a streaming sheet and is not mapped
  136. if (sxSheet != null) {
  137. deregisterSheetMapping(xSheet);
  138. // Clean up temporary resources
  139. try {
  140. sxSheet.dispose();
  141. } catch (IOException e) {
  142. LOG.atWarn().withThrowable(e).log("Failed to cleanup old sheet");
  143. }
  144. }
  145. }
  146. }