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.

XSSFPictureData.java 4.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  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.usermodel;
  16. import java.io.IOException;
  17. import java.io.InputStream;
  18. import org.apache.poi.ooxml.POIXMLDocumentPart;
  19. import org.apache.poi.ooxml.POIXMLException;
  20. import org.apache.poi.ooxml.POIXMLRelation;
  21. import org.apache.poi.openxml4j.opc.PackagePart;
  22. import org.apache.poi.ss.usermodel.PictureData;
  23. import org.apache.poi.ss.usermodel.Workbook;
  24. import org.apache.poi.util.IOUtils;
  25. /**
  26. * Raw picture data, normally attached to a SpreadsheetML Drawing.
  27. * As a rule, pictures are stored in the /xl/media/ part of a SpreadsheetML package.
  28. */
  29. public class XSSFPictureData extends POIXMLDocumentPart implements PictureData {
  30. private static final int DEFAULT_MAX_IMAGE_SIZE = 100_000_000;
  31. private static int MAX_IMAGE_SIZE = DEFAULT_MAX_IMAGE_SIZE;
  32. /**
  33. * @param length the max image size allowed for XSSF pictures
  34. */
  35. public static void setMaxImageSize(int length) {
  36. MAX_IMAGE_SIZE = length;
  37. }
  38. /**
  39. * @return the max image size allowed for XSSF pictures
  40. */
  41. public static int getMaxImageSize() {
  42. return MAX_IMAGE_SIZE;
  43. }
  44. /**
  45. * Relationships for each known picture type
  46. */
  47. protected static final POIXMLRelation[] RELATIONS;
  48. static {
  49. RELATIONS = new POIXMLRelation[13];
  50. RELATIONS[Workbook.PICTURE_TYPE_EMF] = XSSFRelation.IMAGE_EMF;
  51. RELATIONS[Workbook.PICTURE_TYPE_WMF] = XSSFRelation.IMAGE_WMF;
  52. RELATIONS[Workbook.PICTURE_TYPE_PICT] = XSSFRelation.IMAGE_PICT;
  53. RELATIONS[Workbook.PICTURE_TYPE_JPEG] = XSSFRelation.IMAGE_JPEG;
  54. RELATIONS[Workbook.PICTURE_TYPE_PNG] = XSSFRelation.IMAGE_PNG;
  55. RELATIONS[Workbook.PICTURE_TYPE_DIB] = XSSFRelation.IMAGE_DIB;
  56. RELATIONS[XSSFWorkbook.PICTURE_TYPE_GIF] = XSSFRelation.IMAGE_GIF;
  57. RELATIONS[XSSFWorkbook.PICTURE_TYPE_TIFF] = XSSFRelation.IMAGE_TIFF;
  58. RELATIONS[XSSFWorkbook.PICTURE_TYPE_EPS] = XSSFRelation.IMAGE_EPS;
  59. RELATIONS[XSSFWorkbook.PICTURE_TYPE_BMP] = XSSFRelation.IMAGE_BMP;
  60. RELATIONS[XSSFWorkbook.PICTURE_TYPE_WPG] = XSSFRelation.IMAGE_WPG;
  61. }
  62. /**
  63. * Create a new XSSFPictureData node
  64. *
  65. * @see org.apache.poi.xssf.usermodel.XSSFWorkbook#addPicture(byte[], int)
  66. */
  67. protected XSSFPictureData() {
  68. super();
  69. }
  70. /**
  71. * Construct XSSFPictureData from a package part
  72. *
  73. * @param part the package part holding the drawing data,
  74. *
  75. * @since POI 3.14-Beta1
  76. */
  77. protected XSSFPictureData(PackagePart part) {
  78. super(part);
  79. }
  80. /**
  81. * Gets the picture data as a byte array.
  82. * <p>
  83. * Note, that this call might be expensive since all the picture data is copied into a temporary byte array.
  84. * You can grab the picture data directly from the underlying package part as follows:
  85. * <br>
  86. * <code>
  87. * InputStream is = getPackagePart().getInputStream();
  88. * </code>
  89. * </p>
  90. *
  91. * @return the picture data.
  92. */
  93. public byte[] getData() {
  94. try (InputStream inputStream = getPackagePart().getInputStream()) {
  95. return IOUtils.toByteArrayWithMaxLength(inputStream, getMaxImageSize());
  96. } catch(IOException e) {
  97. throw new POIXMLException(e);
  98. }
  99. }
  100. @Override
  101. public String suggestFileExtension() {
  102. return getPackagePart().getPartName().getExtension();
  103. }
  104. @Override
  105. public int getPictureType(){
  106. String contentType = getPackagePart().getContentType();
  107. for (int i = 0; i < RELATIONS.length; i++) {
  108. if(RELATIONS[i] == null) continue;
  109. if(RELATIONS[i].getContentType().equals(contentType)){
  110. return i;
  111. }
  112. }
  113. return 0;
  114. }
  115. public String getMimeType() {
  116. return getPackagePart().getContentType();
  117. }
  118. /**
  119. * *PictureData objects store the actual content in the part directly without keeping a
  120. * copy like all others therefore we need to handle them differently.
  121. */
  122. @Override
  123. protected void prepareForCommit() {
  124. // do not clear the part here
  125. }
  126. }