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.

LazilyConcatenatedByteArray.java 2.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  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.hssf.util;
  16. import java.util.ArrayList;
  17. import java.util.List;
  18. /**
  19. * Utility for delaying the concatenation of multiple byte arrays. Doing this up-front
  20. * causes significantly more copying, which for a large number of byte arrays can cost
  21. * a large amount of time.
  22. */
  23. public class LazilyConcatenatedByteArray {
  24. private final List<byte[]> arrays = new ArrayList<>(1);
  25. /**
  26. * Clears the array (sets the concatenated length back to zero.
  27. */
  28. public void clear() {
  29. arrays.clear();
  30. }
  31. /**
  32. * Concatenates an array onto the end of our array.
  33. * This is a relatively fast operation.
  34. *
  35. * @param array the array to concatenate.
  36. * @throws IllegalArgumentException if {@code array} is {@code null}.
  37. */
  38. public void concatenate(byte[] array) {
  39. if (array == null) {
  40. throw new IllegalArgumentException("array cannot be null");
  41. }
  42. arrays.add(array);
  43. }
  44. public void concatenate(LazilyConcatenatedByteArray other) {
  45. arrays.addAll(other.arrays);
  46. }
  47. /**
  48. * Gets the concatenated contents as a single byte array.
  49. *
  50. * This is a slower operation, but the concatenated array is stored off as a single
  51. * array again so that subsequent calls will not perform additional copying.
  52. *
  53. * @return the byte array. Returns {@code null} if no data has been placed into it.
  54. */
  55. public byte[] toArray() {
  56. if (arrays.isEmpty()) {
  57. return null;
  58. } else if (arrays.size() > 1) {
  59. int totalLength = 0;
  60. for (byte[] array : arrays) {
  61. totalLength += array.length;
  62. }
  63. byte[] concatenated = new byte[totalLength];
  64. int destPos = 0;
  65. for (byte[] array : arrays) {
  66. System.arraycopy(array, 0, concatenated, destPos, array.length);
  67. destPos += array.length;
  68. }
  69. arrays.clear();
  70. arrays.add(concatenated);
  71. }
  72. return arrays.get(0);
  73. }
  74. }