Vous ne pouvez pas sélectionner plus de 25 sujets Les noms de sujets doivent commencer par une lettre ou un nombre, peuvent contenir des tirets ('-') et peuvent comporter jusqu'à 35 caractères.

Array.java 3.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  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.hpsf;
  16. import org.apache.poi.util.IOUtils;
  17. import org.apache.poi.util.Internal;
  18. import org.apache.poi.util.LittleEndianByteArrayInputStream;
  19. @Internal
  20. public class Array {
  21. private static final int MAX_NUMBER_OF_ARRAY_SCALARS = 100_000;
  22. static class ArrayDimension {
  23. private long _size;
  24. @SuppressWarnings("unused")
  25. private int _indexOffset;
  26. void read( LittleEndianByteArrayInputStream lei ) {
  27. _size = lei.readUInt();
  28. _indexOffset = lei.readInt();
  29. }
  30. }
  31. static class ArrayHeader {
  32. private ArrayDimension[] _dimensions;
  33. private int _type;
  34. void read( LittleEndianByteArrayInputStream lei ) {
  35. _type = lei.readInt();
  36. long numDimensionsUnsigned = lei.readUInt();
  37. if ( !( 1 <= numDimensionsUnsigned && numDimensionsUnsigned <= 31 ) ) {
  38. String msg = "Array dimension number "+numDimensionsUnsigned+" is not in [1; 31] range";
  39. throw new IllegalPropertySetDataException(msg);
  40. }
  41. int numDimensions = (int) numDimensionsUnsigned;
  42. _dimensions = new ArrayDimension[numDimensions];
  43. for ( int i = 0; i < numDimensions; i++ ) {
  44. ArrayDimension ad = new ArrayDimension();
  45. ad.read(lei);
  46. _dimensions[i] = ad;
  47. }
  48. }
  49. long getNumberOfScalarValues() {
  50. long result = 1;
  51. for ( ArrayDimension dimension : _dimensions ) {
  52. result *= dimension._size;
  53. }
  54. return result;
  55. }
  56. int getType() {
  57. return _type;
  58. }
  59. }
  60. private final ArrayHeader _header = new ArrayHeader();
  61. private TypedPropertyValue[] _values;
  62. public void read( LittleEndianByteArrayInputStream lei ) {
  63. _header.read(lei);
  64. long numberOfScalarsLong = _header.getNumberOfScalarValues();
  65. if ( numberOfScalarsLong > Integer.MAX_VALUE ) {
  66. String msg =
  67. "Sorry, but POI can't store array of properties with size of " +
  68. numberOfScalarsLong + " in memory";
  69. throw new UnsupportedOperationException(msg);
  70. }
  71. int numberOfScalars = (int) numberOfScalarsLong;
  72. IOUtils.safelyAllocateCheck(numberOfScalars, MAX_NUMBER_OF_ARRAY_SCALARS);
  73. _values = new TypedPropertyValue[numberOfScalars];
  74. int paddedType = (_header._type == Variant.VT_VARIANT) ? 0 : _header._type;
  75. for ( int i = 0; i < numberOfScalars; i++ ) {
  76. TypedPropertyValue typedPropertyValue = new TypedPropertyValue(paddedType, null);
  77. typedPropertyValue.read(lei);
  78. _values[i] = typedPropertyValue;
  79. if (paddedType != 0) {
  80. TypedPropertyValue.skipPadding(lei);
  81. }
  82. }
  83. }
  84. public TypedPropertyValue[] getValues(){
  85. return _values;
  86. }
  87. }