diff options
author | Nick Burch <nick@apache.org> | 2010-05-05 16:45:58 +0000 |
---|---|---|
committer | Nick Burch <nick@apache.org> | 2010-05-05 16:45:58 +0000 |
commit | 7a0aa138858e21c451df683d1bc8fef48ea19d20 (patch) | |
tree | facb54ce6e1dfba74bd73615f0d35be666638fad /src/java/org/apache/poi/hssf/util | |
parent | 476a526e26676c82fbe6f2d77223f23648533542 (diff) | |
download | poi-7a0aa138858e21c451df683d1bc8fef48ea19d20.tar.gz poi-7a0aa138858e21c451df683d1bc8fef48ea19d20.zip |
Apply patch from Trejkaz from bug #49050 - Improve performance of AbstractEscherHolderRecord when there are lots of Continue Records
git-svn-id: https://svn.apache.org/repos/asf/poi/trunk@941379 13f79535-47bb-0310-9956-ffa450edef68
Diffstat (limited to 'src/java/org/apache/poi/hssf/util')
-rw-r--r-- | src/java/org/apache/poi/hssf/util/LazilyConcatenatedByteArray.java | 83 |
1 files changed, 83 insertions, 0 deletions
diff --git a/src/java/org/apache/poi/hssf/util/LazilyConcatenatedByteArray.java b/src/java/org/apache/poi/hssf/util/LazilyConcatenatedByteArray.java new file mode 100644 index 0000000000..e7ca949e79 --- /dev/null +++ b/src/java/org/apache/poi/hssf/util/LazilyConcatenatedByteArray.java @@ -0,0 +1,83 @@ +/* ==================================================================== + Licensed to the Apache Software Foundation (ASF) under one or more + contributor license agreements. See the NOTICE file distributed with + this work for additional information regarding copyright ownership. + The ASF licenses this file to You under the Apache License, Version 2.0 + (the "License"); you may not use this file except in compliance with + the License. You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + + Unless required by applicable law or agreed to in writing, software + distributed under the License is distributed on an "AS IS" BASIS, + WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + See the License for the specific language governing permissions and + limitations under the License. +==================================================================== */ + +package org.apache.poi.hssf.util; + +import java.util.ArrayList; +import java.util.List; + +/** + * Utility for delaying the concatenation of multiple byte arrays. Doing this up-front + * causes significantly more copying, which for a large number of byte arrays can cost + * a large amount of time. + */ +public class LazilyConcatenatedByteArray { + private final List<byte[]> arrays = new ArrayList<byte[]>(1); + + /** + * Clears the array (sets the concatenated length back to zero. + */ + public void clear() { + arrays.clear(); + } + + /** + * Concatenates an array onto the end of our array. + * This is a relatively fast operation. + * + * @param array the array to concatenate. + * @throws IllegalArgumentException if {@code array} is {@code null}. + */ + public void concatenate(byte[] array) { + if (array == null) { + throw new IllegalArgumentException("array cannot be null"); + } + arrays.add(array); + } + + /** + * Gets the concatenated contents as a single byte array. + * + * This is a slower operation, but the concatenated array is stored off as a single + * array again so that subsequent calls will not perform additional copying. + * + * @return the byte array. Returns {@code null} if no data has been placed into it. + */ + public byte[] toArray() { + if (arrays.isEmpty()) { + return null; + } else if (arrays.size() > 1) { + int totalLength = 0; + for (byte[] array : arrays) { + totalLength += array.length; + } + + byte[] concatenated = new byte[totalLength]; + int destPos = 0; + for (byte[] array : arrays) { + System.arraycopy(array, 0, concatenated, destPos, array.length); + destPos += array.length; + } + + arrays.clear(); + arrays.add(concatenated); + } + + return arrays.get(0); + } +} + |