This is an automated email from the ASF dual-hosted git repository. ggregory pushed a commit to branch master in repository https://gitbox.apache.org/repos/asf/commons-imaging.git
The following commit(s) were added to refs/heads/master by this push: new 0209ed7a Refactor for arrays 0209ed7a is described below commit 0209ed7a8947d7833480f1b8ebd5d9dfd36c4480 Author: Gary Gregory <garydgreg...@gmail.com> AuthorDate: Sat May 13 15:34:14 2023 -0400 Refactor for arrays --- .../commons/imaging/common/AllocationChecker.java | 66 --------- .../apache/commons/imaging/common/Allocator.java | 147 +++++++++++++++++++++ .../commons/imaging/common/BasicCParser.java | 2 +- .../commons/imaging/common/BinaryFunctions.java | 7 +- .../commons/imaging/common/ByteConversions.java | 20 +-- .../commons/imaging/common/ImageBuilder.java | 6 +- .../apache/commons/imaging/common/PackBits.java | 2 +- .../apache/commons/imaging/common/ZlibDeflate.java | 2 +- .../common/bytesource/ByteSourceInputStream.java | 4 +- .../common/itu_t4/BitArrayOutputStream.java | 6 +- .../imaging/common/itu_t4/T4AndT6Compression.java | 14 +- .../common/mylzw/BitsToByteInputStream.java | 4 +- .../imaging/common/mylzw/MyLzwDecompressor.java | 4 +- .../imaging/formats/gif/GifImageParser.java | 6 +- .../imaging/formats/icns/Rle24Compression.java | 4 +- .../imaging/formats/ico/IcoImageParser.java | 6 +- .../imaging/formats/jpeg/JpegImageParser.java | 4 +- .../imaging/formats/jpeg/decoder/Block.java | 4 +- .../imaging/formats/jpeg/decoder/JpegDecoder.java | 16 +-- .../imaging/formats/jpeg/iptc/IptcParser.java | 4 +- .../imaging/formats/jpeg/segments/DhtSegment.java | 6 +- .../imaging/formats/jpeg/segments/SofnSegment.java | 4 +- .../imaging/formats/jpeg/segments/SosSegment.java | 4 +- .../imaging/formats/pcx/PcxImageParser.java | 16 +-- .../commons/imaging/formats/pcx/PcxWriter.java | 14 +- .../commons/imaging/formats/png/PngWriter.java | 10 +- .../commons/imaging/formats/png/ScanExpediter.java | 4 +- .../imaging/formats/png/chunks/PngChunkIccp.java | 4 +- .../imaging/formats/png/chunks/PngChunkItxt.java | 4 +- .../imaging/formats/png/chunks/PngChunkPlte.java | 4 +- .../imaging/formats/png/chunks/PngChunkZtxt.java | 4 +- .../psd/datareaders/CompressedDataReader.java | 6 +- .../psd/datareaders/UncompressedDataReader.java | 6 +- .../commons/imaging/formats/rgbe/RgbeInfo.java | 6 +- .../commons/imaging/formats/tiff/TiffField.java | 14 +- .../imaging/formats/tiff/TiffRasterDataFloat.java | 6 +- .../imaging/formats/tiff/TiffRasterDataInt.java | 8 +- .../formats/tiff/datareaders/DataReaderStrips.java | 10 +- .../formats/tiff/datareaders/DataReaderTiled.java | 8 +- .../formats/tiff/datareaders/ImageDataReader.java | 8 +- .../formats/tiff/fieldtypes/FieldTypeAscii.java | 6 +- .../formats/tiff/fieldtypes/FieldTypeDouble.java | 4 +- .../formats/tiff/fieldtypes/FieldTypeFloat.java | 4 +- .../formats/tiff/fieldtypes/FieldTypeLong.java | 4 +- .../formats/tiff/fieldtypes/FieldTypeRational.java | 6 +- .../formats/tiff/fieldtypes/FieldTypeShort.java | 4 +- .../PhotometricInterpreterPalette.java | 4 +- .../formats/tiff/taginfos/TagInfoAscii.java | 4 +- .../formats/tiff/taginfos/TagInfoGpsText.java | 6 +- .../formats/tiff/write/ImageDataOffsets.java | 4 +- .../formats/tiff/write/TiffImageWriterBase.java | 8 +- .../tiff/write/TiffImageWriterLossless.java | 4 +- .../formats/tiff/write/TiffOutputDirectory.java | 6 +- .../imaging/formats/xbm/XbmImageParser.java | 4 +- .../imaging/formats/xpm/XpmImageParser.java | 8 +- .../commons/imaging/icc/IccProfileParser.java | 4 +- .../imaging/palette/MedianCutQuantizer.java | 8 +- .../commons/imaging/palette/PaletteFactory.java | 10 +- .../commons/imaging/palette/QuantizedPalette.java | 4 +- 59 files changed, 328 insertions(+), 248 deletions(-) diff --git a/src/main/java/org/apache/commons/imaging/common/AllocationChecker.java b/src/main/java/org/apache/commons/imaging/common/AllocationChecker.java deleted file mode 100644 index 511a0d75..00000000 --- a/src/main/java/org/apache/commons/imaging/common/AllocationChecker.java +++ /dev/null @@ -1,66 +0,0 @@ -/* - * 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.commons.imaging.common; - -/** - * Checks inputs for meeting allocation limits. - */ -public class AllocationChecker { - - private static final String CANONICAL_NAME = AllocationChecker.class.getCanonicalName(); - - /** One GB. */ - private static final int DEFAULT = 1_073_741_824; - - /** - * Checks a request for meeting allocation limits. - * <p> - * The default limit is {@value #DEFAULT}, override with the system property - * "org.apache.commons.imaging.common.mylzw.AllocationChecker". - * </p> - * - * @param request an allocation request. - * @return the request. - */ - public static int check(final int request) { - // 1 GB limit - final int limit = Integer.getInteger(CANONICAL_NAME, DEFAULT); - if (request > limit) { - throw new AllocationRequestException(DEFAULT, request); - } - return request; - } - - /** - * Checks a request for meeting allocation limits. - * <p> - * The default limit is {@value #DEFAULT}, override with the system property - * "org.apache.commons.imaging.common.mylzw.AllocationChecker". - * </p> - * - * @param request an allocation request. - * @return the request. - */ - public static int check(final long request) { - if (request > Integer.MAX_VALUE) { - throw new AllocationRequestException(DEFAULT, request); - } - return check((int) request); - } - -} diff --git a/src/main/java/org/apache/commons/imaging/common/Allocator.java b/src/main/java/org/apache/commons/imaging/common/Allocator.java new file mode 100644 index 00000000..26a50038 --- /dev/null +++ b/src/main/java/org/apache/commons/imaging/common/Allocator.java @@ -0,0 +1,147 @@ +/* + * 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.commons.imaging.common; + +import java.util.function.IntFunction; + +/** + * Checks inputs for meeting allocation limits and allocates arrays. + */ +public class Allocator { + + private static final String CANONICAL_NAME = Allocator.class.getCanonicalName(); + + /** One GB. */ + private static final int DEFAULT = 1_073_741_824; + private static final int LIMIT; + + static { + LIMIT = Integer.getInteger(CANONICAL_NAME, DEFAULT); + } + + /** + * Allocates an Object array of type T of the requested size. + * + * @param <T> The return array type + * @param request The requested size. + * @param factory The array factory. + * @return a new byte array. + * @throws AllocationRequestException Thrown when the request exceeds the limit. + * @see #check(int) + */ + public static <T> T[] array(final int request, final IntFunction<T[]> factory) { + return factory.apply(check(request)); + } + + /** + * Allocates a byte array of the requested size. + * + * @param request The requested size. + * @return a new byte array. + * @throws AllocationRequestException Thrown when the request exceeds the limit. + * @see #check(int) + */ + public static byte[] byteArray(final int request) { + return new byte[check(request)]; + } + + /** + * Allocates a byte array of the requested size. + * + * @param request The requested size is cast down to an int. + * @return a new byte array. + * @throws AllocationRequestException Thrown when the request exceeds the limit. + * @see #check(int) + */ + public static byte[] byteArray(final long request) { + return new byte[check(request)]; + } + + /** + * Checks a request for meeting allocation limits. + * <p> + * The default limit is {@value #DEFAULT}, override with the system property + * "org.apache.commons.imaging.common.mylzw.AllocationChecker". + * </p> + * + * @param request an allocation request. + * @return the request. + * @throws AllocationRequestException Thrown when the request exceeds the limit. + */ + public static int check(final int request) { + if (request > LIMIT) { + throw new AllocationRequestException(DEFAULT, request); + } + return request; + } + + /** + * Checks a request for meeting allocation limits. + * <p> + * The default limit is {@value #DEFAULT}, override with the system property + * "org.apache.commons.imaging.common.mylzw.AllocationChecker". + * </p> + * + * @param request the allocation request is cast down to an int. + * @return the request. + * @throws AllocationRequestException Thrown when the request exceeds the limit. + */ + public static int check(final long request) { + if (request > Integer.MAX_VALUE) { + throw new AllocationRequestException(DEFAULT, request); + } + return check((int) request); + } + + /** + * Allocates a double array of the requested size. + * + * @param request The requested size. + * @return a new double array. + * @throws AllocationRequestException Thrown when the request exceeds the limit. + * @see #check(int) + */ + public static double[] doubleArray(final int request) { + return new double[check(request)]; + } + + /** + * Allocates a float array of the requested size. + * + * @param request The requested size. + * @return a new float array. + * @throws AllocationRequestException Thrown when the request exceeds the limit. + * @see #check(int) + */ + public static float[] floatArray(final int request) { + return new float[check(request)]; + } + + /** + * Allocates a int array of the requested size. + * + * @param request The requested size. + * @return a new int array. + * @throws AllocationRequestException Thrown when the request exceeds the limit. + * @see #check(int) + */ + public static int[] intArray(final int request) { + return new int[check(request)]; + } + +} diff --git a/src/main/java/org/apache/commons/imaging/common/BasicCParser.java b/src/main/java/org/apache/commons/imaging/common/BasicCParser.java index 91837c05..2b4d67c6 100644 --- a/src/main/java/org/apache/commons/imaging/common/BasicCParser.java +++ b/src/main/java/org/apache/commons/imaging/common/BasicCParser.java @@ -353,7 +353,7 @@ public class BasicCParser { ++numLiveTokens; } } - final String[] liveTokens = new String[AllocationChecker.check(numLiveTokens)]; + final String[] liveTokens = Allocator.array(numLiveTokens, String[]::new); int next = 0; for (final String token : tokens) { if (token != null && !token.isEmpty()) { diff --git a/src/main/java/org/apache/commons/imaging/common/BinaryFunctions.java b/src/main/java/org/apache/commons/imaging/common/BinaryFunctions.java index 6609d6ac..426b731d 100644 --- a/src/main/java/org/apache/commons/imaging/common/BinaryFunctions.java +++ b/src/main/java/org/apache/commons/imaging/common/BinaryFunctions.java @@ -83,7 +83,7 @@ public final class BinaryFunctions { if (length < 0) { throw new IOException(String.format("%s, invalid length: %d", exception, length)); } - final byte[] result = new byte[AllocationChecker.check(length)]; + final byte[] result = Allocator.byteArray(length); raf.seek(pos); @@ -264,8 +264,7 @@ public final class BinaryFunctions { if (length < 0) { throw new IOException(String.format("%s, invalid length: %d", exception, length)); } - AllocationChecker.check(length); - final byte[] result = new byte[AllocationChecker.check(length)]; + final byte[] result = Allocator.byteArray(length); int read = 0; while (read < length) { final int count = is.read(result, read, length - read); @@ -329,7 +328,7 @@ public final class BinaryFunctions { } public static byte[] slice(final byte[] bytes, final int start, final int count) { - final byte[] result = new byte[AllocationChecker.check(count)]; + final byte[] result = Allocator.byteArray(count); System.arraycopy(bytes, start, result, 0, count); return result; } diff --git a/src/main/java/org/apache/commons/imaging/common/ByteConversions.java b/src/main/java/org/apache/commons/imaging/common/ByteConversions.java index 029b2fa0..14fb2963 100644 --- a/src/main/java/org/apache/commons/imaging/common/ByteConversions.java +++ b/src/main/java/org/apache/commons/imaging/common/ByteConversions.java @@ -59,7 +59,7 @@ public final class ByteConversions { private static byte[] toBytes(final double[] values, final int offset, final int length, final ByteOrder byteOrder) { - final byte[] result = new byte[AllocationChecker.check(length * 8)]; + final byte[] result = Allocator.byteArray(length * 8); for (int i = 0; i < length; i++) { toBytes(values[offset + i], byteOrder, result, i * 8); } @@ -92,7 +92,7 @@ public final class ByteConversions { } private static byte[] toBytes(final float[] values, final int offset, final int length, final ByteOrder byteOrder) { - final byte[] result = new byte[AllocationChecker.check(length * 4)]; + final byte[] result = Allocator.byteArray(length * 4); for (int i = 0; i < length; i++) { toBytes(values[offset + i], byteOrder, result, i * 4); } @@ -124,7 +124,7 @@ public final class ByteConversions { } private static byte[] toBytes(final int[] values, final int offset, final int length, final ByteOrder byteOrder) { - final byte[] result = new byte[AllocationChecker.check(length * 4)]; + final byte[] result = Allocator.byteArray(length * 4); for (int i = 0; i < length; i++) { toBytes(values[offset + i], byteOrder, result, i * 4); } @@ -166,7 +166,7 @@ public final class ByteConversions { private static byte[] toBytes(final RationalNumber[] values, final int offset, final int length, final ByteOrder byteOrder) { - final byte[] result = new byte[AllocationChecker.check(length * 8)]; + final byte[] result = Allocator.byteArray(length * 8); for (int i = 0; i < length; i++) { toBytes(values[offset + i], byteOrder, result, i * 8); } @@ -194,7 +194,7 @@ public final class ByteConversions { } private static byte[] toBytes(final short[] values, final int offset, final int length, final ByteOrder byteOrder) { - final byte[] result = new byte[AllocationChecker.check(length * 2)]; + final byte[] result = Allocator.byteArray(length * 2); for (int i = 0; i < length; i++) { toBytes(values[offset + i], byteOrder, result, i * 2); } @@ -233,7 +233,7 @@ public final class ByteConversions { private static double[] toDoubles(final byte[] bytes, final int offset, final int length, final ByteOrder byteOrder) { - final double[] result = new double[AllocationChecker.check(length / 8)]; + final double[] result = Allocator.doubleArray(length / 8); Arrays.setAll(result, i -> toDouble(bytes, offset + 8 * i, byteOrder)); return result; } @@ -262,7 +262,7 @@ public final class ByteConversions { private static float[] toFloats(final byte[] bytes, final int offset, final int length, final ByteOrder byteOrder) { - final float[] result = new float[AllocationChecker.check(length / 4)]; + final float[] result = Allocator.floatArray(length / 4); for (int i = 0; i < result.length; i++) { result[i] = toFloat(bytes, offset + 4 * i, byteOrder); } @@ -290,7 +290,7 @@ public final class ByteConversions { private static int[] toInts(final byte[] bytes, final int offset, final int length, final ByteOrder byteOrder) { - final int[] result = new int[AllocationChecker.check(length / 4)]; + final int[] result = Allocator.intArray(length / 4); Arrays.setAll(result, i -> toInt(bytes, offset + 4 * i, byteOrder)); return result; } @@ -368,7 +368,7 @@ public final class ByteConversions { private static short[] toShorts(final byte[] bytes, final int offset, final int length, final ByteOrder byteOrder) { - final short[] result = new short[AllocationChecker.check(length / 2)]; + final short[] result = new short[Allocator.check(length / 2)]; for (int i = 0; i < result.length; i++) { result[i] = toShort(bytes, offset + 2 * i, byteOrder); } @@ -394,7 +394,7 @@ public final class ByteConversions { private static int[] toUInt16s(final byte[] bytes, final int offset, final int length, final ByteOrder byteOrder) { - final int[] result = new int[AllocationChecker.check(length / 2)]; + final int[] result = Allocator.intArray(length / 2); Arrays.setAll(result, i -> toUInt16(bytes, offset + 2 * i, byteOrder)); return result; } diff --git a/src/main/java/org/apache/commons/imaging/common/ImageBuilder.java b/src/main/java/org/apache/commons/imaging/common/ImageBuilder.java index bf31bc58..fe39904f 100644 --- a/src/main/java/org/apache/commons/imaging/common/ImageBuilder.java +++ b/src/main/java/org/apache/commons/imaging/common/ImageBuilder.java @@ -73,7 +73,7 @@ public class ImageBuilder { public ImageBuilder(final int width, final int height, final boolean hasAlpha) { checkDimensions(width, height); - data = new int[AllocationChecker.check(width * height)]; + data = Allocator.intArray(width * height); this.width = width; this.height = height; this.hasAlpha = hasAlpha; @@ -95,7 +95,7 @@ public class ImageBuilder { public ImageBuilder(final int width, final int height, final boolean hasAlpha, final boolean isAlphaPremultiplied) { checkDimensions(width, height); - data = new int[AllocationChecker.check(width * height)]; + data = Allocator.intArray(width * height); this.width = width; this.height = height; this.hasAlpha = hasAlpha; @@ -203,7 +203,7 @@ public class ImageBuilder { checkBounds(x, y, w, h); // Transcribe the data to an output image array - final int[] argb = new int[AllocationChecker.check(w * h)]; + final int[] argb = Allocator.intArray(w * h); int k = 0; for (int iRow = 0; iRow < h; iRow++) { final int dIndex = (iRow + y) * width + x; diff --git a/src/main/java/org/apache/commons/imaging/common/PackBits.java b/src/main/java/org/apache/commons/imaging/common/PackBits.java index 755909e8..1ed30ca1 100644 --- a/src/main/java/org/apache/commons/imaging/common/PackBits.java +++ b/src/main/java/org/apache/commons/imaging/common/PackBits.java @@ -26,7 +26,7 @@ public class PackBits { public byte[] compress(final byte[] bytes) throws IOException { // max length 1 extra byte for every 128 - try (UnsynchronizedByteArrayOutputStream baos = new UnsynchronizedByteArrayOutputStream(AllocationChecker.check(bytes.length * 2))) { + try (UnsynchronizedByteArrayOutputStream baos = new UnsynchronizedByteArrayOutputStream(Allocator.check(bytes.length * 2))) { int ptr = 0; while (ptr < bytes.length) { int dup = findNextDuplicate(bytes, ptr); diff --git a/src/main/java/org/apache/commons/imaging/common/ZlibDeflate.java b/src/main/java/org/apache/commons/imaging/common/ZlibDeflate.java index 9b8990be..f2180fe7 100644 --- a/src/main/java/org/apache/commons/imaging/common/ZlibDeflate.java +++ b/src/main/java/org/apache/commons/imaging/common/ZlibDeflate.java @@ -71,7 +71,7 @@ public class ZlibDeflate { try { final Inflater inflater = new Inflater(); inflater.setInput(bytes); - final byte[] result = new byte[AllocationChecker.check(expectedSize)]; + final byte[] result = Allocator.byteArray(expectedSize); inflater.inflate(result); return result; } catch (final DataFormatException e) { diff --git a/src/main/java/org/apache/commons/imaging/common/bytesource/ByteSourceInputStream.java b/src/main/java/org/apache/commons/imaging/common/bytesource/ByteSourceInputStream.java index 351cbfd0..c28e6ae1 100644 --- a/src/main/java/org/apache/commons/imaging/common/bytesource/ByteSourceInputStream.java +++ b/src/main/java/org/apache/commons/imaging/common/bytesource/ByteSourceInputStream.java @@ -23,7 +23,7 @@ import java.io.InputStream; import java.util.Arrays; import java.util.Objects; -import org.apache.commons.imaging.common.AllocationChecker; +import org.apache.commons.imaging.common.Allocator; import org.apache.commons.imaging.common.BinaryFunctions; public class ByteSourceInputStream extends ByteSource { @@ -204,7 +204,7 @@ public class ByteSourceInputStream extends ByteSource { final InputStream cis = getInputStream(); BinaryFunctions.skipBytes(cis, blockStart); - final byte[] bytes = new byte[AllocationChecker.check(blockLength)]; + final byte[] bytes = Allocator.byteArray(blockLength); int total = 0; while (true) { final int read = cis.read(bytes, total, bytes.length - total); diff --git a/src/main/java/org/apache/commons/imaging/common/itu_t4/BitArrayOutputStream.java b/src/main/java/org/apache/commons/imaging/common/itu_t4/BitArrayOutputStream.java index 1f830019..ac0d3888 100644 --- a/src/main/java/org/apache/commons/imaging/common/itu_t4/BitArrayOutputStream.java +++ b/src/main/java/org/apache/commons/imaging/common/itu_t4/BitArrayOutputStream.java @@ -19,7 +19,7 @@ package org.apache.commons.imaging.common.itu_t4; import java.io.OutputStream; import java.util.Arrays; -import org.apache.commons.imaging.common.AllocationChecker; +import org.apache.commons.imaging.common.Allocator; /** * Output stream writing to a byte array, and capable @@ -36,7 +36,7 @@ class BitArrayOutputStream extends OutputStream { } BitArrayOutputStream(final int size) { - buffer = new byte[AllocationChecker.check(size)]; + buffer = Allocator.byteArray(size); } @Override @@ -91,7 +91,7 @@ class BitArrayOutputStream extends OutputStream { private void writeByte(final int b) { if (bytesWritten >= buffer.length) { - final byte[] bigger = new byte[AllocationChecker.check(buffer.length * 2)]; + final byte[] bigger = Allocator.byteArray(buffer.length * 2); System.arraycopy(buffer, 0, bigger, 0, bytesWritten); buffer = bigger; } diff --git a/src/main/java/org/apache/commons/imaging/common/itu_t4/T4AndT6Compression.java b/src/main/java/org/apache/commons/imaging/common/itu_t4/T4AndT6Compression.java index 3531d893..f17c0b89 100644 --- a/src/main/java/org/apache/commons/imaging/common/itu_t4/T4AndT6Compression.java +++ b/src/main/java/org/apache/commons/imaging/common/itu_t4/T4AndT6Compression.java @@ -21,7 +21,7 @@ import java.io.IOException; import org.apache.commons.imaging.ImageReadException; import org.apache.commons.imaging.ImageWriteException; -import org.apache.commons.imaging.common.AllocationChecker; +import org.apache.commons.imaging.common.Allocator; import org.apache.commons.imaging.common.itu_t4.T4_T6_Tables.Entry; public final class T4AndT6Compression { @@ -205,8 +205,8 @@ public final class T4AndT6Compression { throws ImageWriteException { final BitInputStreamFlexible inputStream = new BitInputStreamFlexible(new ByteArrayInputStream(uncompressed)); final BitArrayOutputStream outputStream = new BitArrayOutputStream(); - int[] referenceLine = new int[AllocationChecker.check(width)]; - int[] codingLine = new int[AllocationChecker.check(width)]; + int[] referenceLine = Allocator.intArray(width); + int[] codingLine = Allocator.intArray(width); int kCounter = 0; if (hasFill) { T4_T6_Tables.EOL16.writeBits(outputStream); @@ -284,8 +284,8 @@ public final class T4AndT6Compression { try (ByteArrayInputStream bais = new ByteArrayInputStream(uncompressed); BitInputStreamFlexible inputStream = new BitInputStreamFlexible(bais)) { final BitArrayOutputStream outputStream = new BitArrayOutputStream(); - int[] referenceLine = new int[AllocationChecker.check(width)]; - int[] codingLine = new int[AllocationChecker.check(width)]; + int[] referenceLine = Allocator.intArray(width); + int[] codingLine = Allocator.intArray(width); for (int y = 0; y < height; y++) { for (int i = 0; i < width; i++) { try { @@ -435,7 +435,7 @@ public final class T4AndT6Compression { final int height, final boolean hasFill) throws ImageReadException { final BitInputStreamFlexible inputStream = new BitInputStreamFlexible(new ByteArrayInputStream(compressed)); try (BitArrayOutputStream outputStream = new BitArrayOutputStream()) { - final int[] referenceLine = new int[AllocationChecker.check(width)]; + final int[] referenceLine = Allocator.intArray(width); for (int y = 0; y < height; y++) { int rowLength = 0; try { @@ -542,7 +542,7 @@ public final class T4AndT6Compression { throws ImageReadException { final BitInputStreamFlexible inputStream = new BitInputStreamFlexible(new ByteArrayInputStream(compressed)); final BitArrayOutputStream outputStream = new BitArrayOutputStream(); - final int[] referenceLine = new int[AllocationChecker.check(width)]; + final int[] referenceLine = Allocator.intArray(width); for (int y = 0; y < height; y++) { int rowLength = 0; try { diff --git a/src/main/java/org/apache/commons/imaging/common/mylzw/BitsToByteInputStream.java b/src/main/java/org/apache/commons/imaging/common/mylzw/BitsToByteInputStream.java index cbcc2f09..da36c426 100644 --- a/src/main/java/org/apache/commons/imaging/common/mylzw/BitsToByteInputStream.java +++ b/src/main/java/org/apache/commons/imaging/common/mylzw/BitsToByteInputStream.java @@ -19,7 +19,7 @@ package org.apache.commons.imaging.common.mylzw; import java.io.IOException; import java.io.InputStream; -import org.apache.commons.imaging.common.AllocationChecker; +import org.apache.commons.imaging.common.Allocator; public class BitsToByteInputStream extends InputStream { private final MyBitInputStream is; @@ -47,7 +47,7 @@ public class BitsToByteInputStream extends InputStream { } public int[] readBitsArray(final int sampleBits, final int length) throws IOException { - final int[] result = new int[AllocationChecker.check(length)]; + final int[] result = Allocator.intArray(length); for (int i = 0; i < length; i++) { result[i] = readBits(sampleBits); } diff --git a/src/main/java/org/apache/commons/imaging/common/mylzw/MyLzwDecompressor.java b/src/main/java/org/apache/commons/imaging/common/mylzw/MyLzwDecompressor.java index 005b0d1b..3494158d 100644 --- a/src/main/java/org/apache/commons/imaging/common/mylzw/MyLzwDecompressor.java +++ b/src/main/java/org/apache/commons/imaging/common/mylzw/MyLzwDecompressor.java @@ -24,7 +24,7 @@ import java.nio.ByteOrder; import java.util.Arrays; import org.apache.commons.imaging.ImageReadException; -import org.apache.commons.imaging.common.AllocationChecker; +import org.apache.commons.imaging.common.Allocator; public final class MyLzwDecompressor { @@ -112,7 +112,7 @@ public final class MyLzwDecompressor { mbis.setTiffLZWMode(); } - final ByteArrayOutputStream baos = new ByteArrayOutputStream(AllocationChecker.check(expectedLength)); + final ByteArrayOutputStream baos = new ByteArrayOutputStream(Allocator.check(expectedLength)); clearTable(); diff --git a/src/main/java/org/apache/commons/imaging/formats/gif/GifImageParser.java b/src/main/java/org/apache/commons/imaging/formats/gif/GifImageParser.java index 5551ca32..4359f5cf 100644 --- a/src/main/java/org/apache/commons/imaging/formats/gif/GifImageParser.java +++ b/src/main/java/org/apache/commons/imaging/formats/gif/GifImageParser.java @@ -44,7 +44,7 @@ import org.apache.commons.imaging.ImageInfo; import org.apache.commons.imaging.ImageParser; import org.apache.commons.imaging.ImageReadException; import org.apache.commons.imaging.ImageWriteException; -import org.apache.commons.imaging.common.AllocationChecker; +import org.apache.commons.imaging.common.Allocator; import org.apache.commons.imaging.common.BinaryOutputStream; import org.apache.commons.imaging.common.ImageBuilder; import org.apache.commons.imaging.common.ImageMetadata; @@ -349,7 +349,7 @@ public class GifImageParser extends ImageParser<GifImagingParameters> implements } final int length = bytes.length / 3; - final int[] result = new int[AllocationChecker.check(length)]; + final int[] result = Allocator.intArray(length); for (int i = 0; i < length; i++) { final int red = 0xff & bytes[(i * 3) + 0]; @@ -1107,7 +1107,7 @@ public class GifImageParser extends ImageParser<GifImagingParameters> implements lzwMinimumCodeSize, ByteOrder.LITTLE_ENDIAN, false); // GIF // Mode); - final byte[] imageData = new byte[AllocationChecker.check(width * height)]; + final byte[] imageData = Allocator.byteArray(width * height); for (int y = 0; y < height; y++) { for (int x = 0; x < width; x++) { final int argb = src.getRGB(x, y); diff --git a/src/main/java/org/apache/commons/imaging/formats/icns/Rle24Compression.java b/src/main/java/org/apache/commons/imaging/formats/icns/Rle24Compression.java index 949f75c3..d1991701 100644 --- a/src/main/java/org/apache/commons/imaging/formats/icns/Rle24Compression.java +++ b/src/main/java/org/apache/commons/imaging/formats/icns/Rle24Compression.java @@ -16,12 +16,12 @@ */ package org.apache.commons.imaging.formats.icns; -import org.apache.commons.imaging.common.AllocationChecker; +import org.apache.commons.imaging.common.Allocator; final class Rle24Compression { public static byte[] decompress(final int width, final int height, final byte[] data) { final int pixelCount = width * height; - final byte[] result = new byte[AllocationChecker.check(4 * pixelCount)]; + final byte[] result = Allocator.byteArray(4 * pixelCount); // Several ICNS parsers advance by 4 bytes here: // http://code.google.com/p/icns2png/ - when the width is >= 128 diff --git a/src/main/java/org/apache/commons/imaging/formats/ico/IcoImageParser.java b/src/main/java/org/apache/commons/imaging/formats/ico/IcoImageParser.java index 71147e03..00df52b6 100644 --- a/src/main/java/org/apache/commons/imaging/formats/ico/IcoImageParser.java +++ b/src/main/java/org/apache/commons/imaging/formats/ico/IcoImageParser.java @@ -41,7 +41,7 @@ import org.apache.commons.imaging.ImageReadException; import org.apache.commons.imaging.ImageWriteException; import org.apache.commons.imaging.Imaging; import org.apache.commons.imaging.PixelDensity; -import org.apache.commons.imaging.common.AllocationChecker; +import org.apache.commons.imaging.common.Allocator; import org.apache.commons.imaging.common.BinaryOutputStream; import org.apache.commons.imaging.common.ImageMetadata; import org.apache.commons.imaging.common.bytesource.ByteSource; @@ -581,12 +581,12 @@ public class IcoImageParser extends ImageParser<IcoImagingParameters> { try (InputStream is = byteSource.getInputStream()) { final FileHeader fileHeader = readFileHeader(is); - final IconInfo[] fIconInfos = new IconInfo[AllocationChecker.check(fileHeader.iconCount)]; + final IconInfo[] fIconInfos = Allocator.array(fileHeader.iconCount, IconInfo[]::new); for (int i = 0; i < fileHeader.iconCount; i++) { fIconInfos[i] = readIconInfo(is); } - final IconData[] fIconDatas = new IconData[AllocationChecker.check(fileHeader.iconCount)]; + final IconData[] fIconDatas = Allocator.array(fileHeader.iconCount, IconData[]::new); for (int i = 0; i < fileHeader.iconCount; i++) { final byte[] iconData = byteSource.getBlock( fIconInfos[i].imageOffset, fIconInfos[i].imageSize); diff --git a/src/main/java/org/apache/commons/imaging/formats/jpeg/JpegImageParser.java b/src/main/java/org/apache/commons/imaging/formats/jpeg/JpegImageParser.java index f10d747c..8ca31d3e 100644 --- a/src/main/java/org/apache/commons/imaging/formats/jpeg/JpegImageParser.java +++ b/src/main/java/org/apache/commons/imaging/formats/jpeg/JpegImageParser.java @@ -37,7 +37,7 @@ import org.apache.commons.imaging.ImageFormats; import org.apache.commons.imaging.ImageInfo; import org.apache.commons.imaging.ImageParser; import org.apache.commons.imaging.ImageReadException; -import org.apache.commons.imaging.common.AllocationChecker; +import org.apache.commons.imaging.common.Allocator; import org.apache.commons.imaging.common.ImageMetadata; import org.apache.commons.imaging.common.XmpEmbeddable; import org.apache.commons.imaging.common.XmpImagingParameters; @@ -128,7 +128,7 @@ public class JpegImageParser extends ImageParser<JpegImagingParameters> implemen } } - final byte[] result = new byte[AllocationChecker.check(total)]; + final byte[] result = Allocator.byteArray(total); int progress = 0; for (final App2Segment segment : segments) { diff --git a/src/main/java/org/apache/commons/imaging/formats/jpeg/decoder/Block.java b/src/main/java/org/apache/commons/imaging/formats/jpeg/decoder/Block.java index 10d127de..8fa21961 100644 --- a/src/main/java/org/apache/commons/imaging/formats/jpeg/decoder/Block.java +++ b/src/main/java/org/apache/commons/imaging/formats/jpeg/decoder/Block.java @@ -15,7 +15,7 @@ package org.apache.commons.imaging.formats.jpeg.decoder; -import org.apache.commons.imaging.common.AllocationChecker; +import org.apache.commons.imaging.common.Allocator; final class Block { final int[] samples; @@ -23,7 +23,7 @@ final class Block { final int height; Block(final int width, final int height) { - this.samples = new int[AllocationChecker.check(width * height)]; + this.samples = Allocator.intArray(width * height); this.width = width; this.height = height; } diff --git a/src/main/java/org/apache/commons/imaging/formats/jpeg/decoder/JpegDecoder.java b/src/main/java/org/apache/commons/imaging/formats/jpeg/decoder/JpegDecoder.java index 41826c9b..75d9aeee 100644 --- a/src/main/java/org/apache/commons/imaging/formats/jpeg/decoder/JpegDecoder.java +++ b/src/main/java/org/apache/commons/imaging/formats/jpeg/decoder/JpegDecoder.java @@ -33,7 +33,7 @@ import java.util.Properties; import org.apache.commons.imaging.ImageReadException; import org.apache.commons.imaging.color.ColorConversions; -import org.apache.commons.imaging.common.AllocationChecker; +import org.apache.commons.imaging.common.Allocator; import org.apache.commons.imaging.common.BinaryFileParser; import org.apache.commons.imaging.common.bytesource.ByteSource; import org.apache.commons.imaging.formats.jpeg.JpegConstants; @@ -109,7 +109,7 @@ public class JpegDecoder extends BinaryFileParser implements JpegUtils.Visitor { final List<Integer> intervalStarts = getIntervalStartPositions(scanPayload); // get number of intervals in payload to init an array of appropriate length final int intervalCount = intervalStarts.size(); - final JpegInputStream[] streams = new JpegInputStream[AllocationChecker.check(intervalCount)]; + final JpegInputStream[] streams = Allocator.array(intervalCount, JpegInputStream[]::new); for (int i = 0; i < intervalCount; i++) { final int from = intervalStarts.get(i); int to; @@ -142,7 +142,7 @@ public class JpegDecoder extends BinaryFileParser implements JpegUtils.Visitor { private final float[] block = new float[64]; private Block[] allocateMCUMemory() throws ImageReadException { - final Block[] mcu = new Block[AllocationChecker.check(sosSegment.numberOfComponents)]; + final Block[] mcu = Allocator.array(sosSegment.numberOfComponents, Block[]::new); for (int i = 0; i < sosSegment.numberOfComponents; i++) { final SosSegment.Component scanComponent = sosSegment.getComponents(i); SofnSegment.Component frameComponent = null; @@ -379,9 +379,9 @@ public class JpegDecoder extends BinaryFileParser implements JpegUtils.Visitor { } quantizationTables[table.destinationIdentifier] = table; final int mSize = 64; - final int[] quantizationMatrixInt = new int[AllocationChecker.check(mSize)]; + final int[] quantizationMatrixInt = Allocator.intArray(mSize); ZigZag.zigZagToBlock(table.getElements(), quantizationMatrixInt); - final float[] quantizationMatrixFloat = new float[AllocationChecker.check(mSize)]; + final float[] quantizationMatrixFloat = Allocator.floatArray(mSize); for (int j = 0; j < mSize; j++) { quantizationMatrixFloat[j] = quantizationMatrixInt[j]; } @@ -424,7 +424,7 @@ public class JpegDecoder extends BinaryFileParser implements JpegUtils.Visitor { // the payload contains the entropy-encoded segments (or ECS) divided by RST markers // or only one ECS if the entropy-encoded data is not divided by RST markers // length of payload = length of image data - length of data already read - final int[] scanPayload = new int[AllocationChecker.check(imageData.length - segmentLength)]; + final int[] scanPayload = Allocator.intArray(imageData.length - segmentLength); int payloadReadCount = 0; while (payloadReadCount < scanPayload.length) { scanPayload[payloadReadCount] = is.read(); @@ -445,9 +445,9 @@ public class JpegDecoder extends BinaryFileParser implements JpegUtils.Visitor { final int xMCUs = (sofnSegment.width + hSize - 1) / hSize; final int yMCUs = (sofnSegment.height + vSize - 1) / vSize; final Block[] mcu = allocateMCUMemory(); - final Block[] scaledMCU = new Block[AllocationChecker.check(mcu.length)]; + final Block[] scaledMCU = Allocator.array(mcu.length, Block[]::new); Arrays.setAll(scaledMCU, i -> new Block(hSize, vSize)); - final int[] preds = new int[AllocationChecker.check(sofnSegment.numberOfComponents)]; + final int[] preds = Allocator.intArray(sofnSegment.numberOfComponents); ColorModel colorModel; WritableRaster raster; switch (sofnSegment.numberOfComponents) { diff --git a/src/main/java/org/apache/commons/imaging/formats/jpeg/iptc/IptcParser.java b/src/main/java/org/apache/commons/imaging/formats/jpeg/iptc/IptcParser.java index 2df375e1..3bbc16f3 100644 --- a/src/main/java/org/apache/commons/imaging/formats/jpeg/iptc/IptcParser.java +++ b/src/main/java/org/apache/commons/imaging/formats/jpeg/iptc/IptcParser.java @@ -43,7 +43,7 @@ import org.apache.commons.imaging.ImageReadException; import org.apache.commons.imaging.ImageWriteException; import org.apache.commons.imaging.ImagingConstants; import org.apache.commons.imaging.ImagingParameters; -import org.apache.commons.imaging.common.AllocationChecker; +import org.apache.commons.imaging.common.Allocator; import org.apache.commons.imaging.common.BinaryFileParser; import org.apache.commons.imaging.common.BinaryFunctions; import org.apache.commons.imaging.common.BinaryOutputStream; @@ -84,7 +84,7 @@ public class IptcParser extends BinaryFileParser { } catch (final IllegalArgumentException e) { } // check if encoding is a escape sequence // normalize encoding byte sequence - final byte[] codedCharsetNormalized = new byte[AllocationChecker.check(codedCharset.length)]; + final byte[] codedCharsetNormalized = Allocator.byteArray(codedCharset.length); int j = 0; for (final byte element : codedCharset) { if (element != ' ') { diff --git a/src/main/java/org/apache/commons/imaging/formats/jpeg/segments/DhtSegment.java b/src/main/java/org/apache/commons/imaging/formats/jpeg/segments/DhtSegment.java index e93ead44..eeed6afe 100644 --- a/src/main/java/org/apache/commons/imaging/formats/jpeg/segments/DhtSegment.java +++ b/src/main/java/org/apache/commons/imaging/formats/jpeg/segments/DhtSegment.java @@ -24,7 +24,7 @@ import java.util.ArrayList; import java.util.Collections; import java.util.List; -import org.apache.commons.imaging.common.AllocationChecker; +import org.apache.commons.imaging.common.Allocator; public class DhtSegment extends Segment { public static class HuffmanTable { @@ -75,7 +75,7 @@ public class DhtSegment extends Segment { k = 0; int code = 0; int si = huffSize[0]; - huffCode = new int[AllocationChecker.check(lastK)]; + huffCode = Allocator.intArray(lastK); while (true) { if (k >= lastK) { break; @@ -158,7 +158,7 @@ public class DhtSegment extends Segment { length--; bitsSum += bits[i]; } - final int[] huffVal = new int[AllocationChecker.check(bitsSum)]; + final int[] huffVal = Allocator.intArray(bitsSum); for (int i = 0; i < bitsSum; i++) { huffVal[i] = 0xff & readByte("Vij", is, "Not a Valid JPEG File"); length--; diff --git a/src/main/java/org/apache/commons/imaging/formats/jpeg/segments/SofnSegment.java b/src/main/java/org/apache/commons/imaging/formats/jpeg/segments/SofnSegment.java index 0b8012c4..99278825 100644 --- a/src/main/java/org/apache/commons/imaging/formats/jpeg/segments/SofnSegment.java +++ b/src/main/java/org/apache/commons/imaging/formats/jpeg/segments/SofnSegment.java @@ -26,7 +26,7 @@ import java.util.logging.Level; import java.util.logging.Logger; import org.apache.commons.imaging.ImageReadException; -import org.apache.commons.imaging.common.AllocationChecker; +import org.apache.commons.imaging.common.Allocator; import org.apache.commons.imaging.formats.jpeg.JpegConstants; public class SofnSegment extends Segment { @@ -74,7 +74,7 @@ public class SofnSegment extends Segment { if (numberOfComponents < 0) { throw new ImageReadException("The number of components in a SOF0Segment cannot be less than 0!"); } - components = new Component[AllocationChecker.check(numberOfComponents)]; + components = Allocator.array(numberOfComponents, Component[]::new); for (int i = 0; i < numberOfComponents; i++) { final int componentIdentifier = readByte("ComponentIdentifier", is, "Not a Valid JPEG File"); diff --git a/src/main/java/org/apache/commons/imaging/formats/jpeg/segments/SosSegment.java b/src/main/java/org/apache/commons/imaging/formats/jpeg/segments/SosSegment.java index f41834e5..620c885f 100644 --- a/src/main/java/org/apache/commons/imaging/formats/jpeg/segments/SosSegment.java +++ b/src/main/java/org/apache/commons/imaging/formats/jpeg/segments/SosSegment.java @@ -24,7 +24,7 @@ import java.io.InputStream; import java.util.logging.Level; import java.util.logging.Logger; -import org.apache.commons.imaging.common.AllocationChecker; +import org.apache.commons.imaging.common.Allocator; public class SosSegment extends Segment { @@ -68,7 +68,7 @@ public class SosSegment extends Segment { // Debug.debug("number_of_components_in_scan", // numberOfComponents); - components = new Component[AllocationChecker.check(numberOfComponents)]; + components = Allocator.array(numberOfComponents, Component[]::new); for (int i = 0; i < numberOfComponents; i++) { final int scanComponentSelector = readByte("scanComponentSelector", is, "Not a Valid JPEG File"); // Debug.debug("scanComponentSelector", scanComponentSelector); diff --git a/src/main/java/org/apache/commons/imaging/formats/pcx/PcxImageParser.java b/src/main/java/org/apache/commons/imaging/formats/pcx/PcxImageParser.java index a175d14b..6c8f81a7 100644 --- a/src/main/java/org/apache/commons/imaging/formats/pcx/PcxImageParser.java +++ b/src/main/java/org/apache/commons/imaging/formats/pcx/PcxImageParser.java @@ -46,7 +46,7 @@ import org.apache.commons.imaging.ImageInfo; import org.apache.commons.imaging.ImageParser; import org.apache.commons.imaging.ImageReadException; import org.apache.commons.imaging.ImageWriteException; -import org.apache.commons.imaging.common.AllocationChecker; +import org.apache.commons.imaging.common.Allocator; import org.apache.commons.imaging.common.ImageMetadata; import org.apache.commons.imaging.common.bytesource.ByteSource; @@ -305,12 +305,12 @@ public class PcxImageParser extends ImageParser<PcxImagingParameters> { throw new ImageReadException("Unsupported/invalid image encoding " + pcxHeader.encoding); } final int scanlineLength = pcxHeader.bytesPerLine * pcxHeader.nPlanes; - final byte[] scanline = new byte[AllocationChecker.check(scanlineLength)]; + final byte[] scanline = Allocator.byteArray(scanlineLength); if ((pcxHeader.bitsPerPixel == 1 || pcxHeader.bitsPerPixel == 2 || pcxHeader.bitsPerPixel == 4 || pcxHeader.bitsPerPixel == 8) && pcxHeader.nPlanes == 1) { final int bytesPerImageRow = (xSize * pcxHeader.bitsPerPixel + 7) / 8; - final byte[] image = new byte[AllocationChecker.check(ySize * bytesPerImageRow)]; + final byte[] image = Allocator.byteArray(ySize * bytesPerImageRow); for (int y = 0; y < ySize; y++) { rleReader.read(is, scanline); System.arraycopy(scanline, 0, image, y * bytesPerImageRow, @@ -360,7 +360,7 @@ public class PcxImageParser extends ImageParser<PcxImagingParameters> { DataBuffer.TYPE_BYTE); final BufferedImage image = new BufferedImage(xSize, ySize, BufferedImage.TYPE_BYTE_BINARY, colorModel); - final byte[] unpacked = new byte[AllocationChecker.check(xSize)]; + final byte[] unpacked = Allocator.byteArray(xSize); for (int y = 0; y < ySize; y++) { rleReader.read(is, scanline); int nextByte = 0; @@ -380,9 +380,9 @@ public class PcxImageParser extends ImageParser<PcxImagingParameters> { if (pcxHeader.bitsPerPixel == 8 && pcxHeader.nPlanes == 3) { final byte[][] image = new byte[3][]; final int xySize = xSize * ySize; - image[0] = new byte[AllocationChecker.check(xySize)]; - image[1] = new byte[AllocationChecker.check(xySize)]; - image[2] = new byte[AllocationChecker.check(xySize)]; + image[0] = Allocator.byteArray(xySize); + image[1] = Allocator.byteArray(xySize); + image[2] = Allocator.byteArray(xySize); for (int y = 0; y < ySize; y++) { rleReader.read(is, scanline); System.arraycopy(scanline, 0, image[0], y * xSize, xSize); @@ -409,7 +409,7 @@ public class PcxImageParser extends ImageParser<PcxImagingParameters> { + pcxHeader.nPlanes); } final int rowLength = 3 * xSize; - final byte[] image = new byte[AllocationChecker.check(rowLength * ySize)]; + final byte[] image = Allocator.byteArray(rowLength * ySize); for (int y = 0; y < ySize; y++) { rleReader.read(is, scanline); if (pcxHeader.bitsPerPixel == 24) { diff --git a/src/main/java/org/apache/commons/imaging/formats/pcx/PcxWriter.java b/src/main/java/org/apache/commons/imaging/formats/pcx/PcxWriter.java index 415548f0..40c7550a 100644 --- a/src/main/java/org/apache/commons/imaging/formats/pcx/PcxWriter.java +++ b/src/main/java/org/apache/commons/imaging/formats/pcx/PcxWriter.java @@ -21,7 +21,7 @@ import java.io.OutputStream; import java.util.Arrays; import org.apache.commons.imaging.PixelDensity; -import org.apache.commons.imaging.common.AllocationChecker; +import org.apache.commons.imaging.common.Allocator; import org.apache.commons.imaging.common.BinaryOutputStream; import org.apache.commons.imaging.palette.PaletteFactory; import org.apache.commons.imaging.palette.SimplePalette; @@ -183,10 +183,10 @@ class PcxWriter { private void writePixels(final BufferedImage src, final int bitDepth, final int planes, final int bytesPerLine, final SimplePalette palette, final BinaryOutputStream bos) throws IOException { - final byte[] plane0 = new byte[AllocationChecker.check(bytesPerLine)]; - final byte[] plane1 = new byte[AllocationChecker.check(bytesPerLine)]; - final byte[] plane2 = new byte[AllocationChecker.check(bytesPerLine)]; - final byte[] plane3 = new byte[AllocationChecker.check(bytesPerLine)]; + final byte[] plane0 = Allocator.byteArray(bytesPerLine); + final byte[] plane1 = Allocator.byteArray(bytesPerLine); + final byte[] plane2 = Allocator.byteArray(bytesPerLine); + final byte[] plane3 = Allocator.byteArray(bytesPerLine); final byte[][] allPlanes = { plane0, plane1, plane2, plane3 }; for (int y = 0; y < src.getHeight(); y++) { @@ -266,8 +266,8 @@ class PcxWriter { private void writePixels32(final BufferedImage src, final int bytesPerLine, final BinaryOutputStream bos) throws IOException { - final int[] rgbs = new int[AllocationChecker.check(src.getWidth())]; - final byte[] plane = new byte[AllocationChecker.check(4 * bytesPerLine)]; + final int[] rgbs = Allocator.intArray(src.getWidth()); + final byte[] plane = Allocator.byteArray(4 * bytesPerLine); for (int y = 0; y < src.getHeight(); y++) { src.getRGB(0, y, src.getWidth(), 1, rgbs, 0, src.getWidth()); for (int x = 0; x < rgbs.length; x++) { diff --git a/src/main/java/org/apache/commons/imaging/formats/png/PngWriter.java b/src/main/java/org/apache/commons/imaging/formats/png/PngWriter.java index 55f923a7..400b9266 100644 --- a/src/main/java/org/apache/commons/imaging/formats/png/PngWriter.java +++ b/src/main/java/org/apache/commons/imaging/formats/png/PngWriter.java @@ -27,7 +27,7 @@ import java.util.zip.DeflaterOutputStream; import org.apache.commons.imaging.ImageWriteException; import org.apache.commons.imaging.PixelDensity; -import org.apache.commons.imaging.common.AllocationChecker; +import org.apache.commons.imaging.common.Allocator; import org.apache.commons.imaging.internal.Debug; import org.apache.commons.imaging.palette.Palette; import org.apache.commons.imaging.palette.PaletteFactory; @@ -183,7 +183,7 @@ class PngWriter { private void writeChunkPLTE(final OutputStream os, final Palette palette) throws IOException { final int length = palette.length(); - final byte[] bytes = new byte[AllocationChecker.check(length * 3)]; + final byte[] bytes = Allocator.byteArray(length * 3); // Debug.debug("length", length); for (int i = 0; i < length; i++) { @@ -236,7 +236,7 @@ class PngWriter { } private void writeChunkTRNS(final OutputStream os, final Palette palette) throws IOException { - final byte[] bytes = new byte[AllocationChecker.check(palette.length())]; + final byte[] bytes = Allocator.byteArray(palette.length()); for (int i = 0; i < bytes.length; i++) { bytes[i] = (byte) (0xff & (palette.getEntry(i) >> 24)); @@ -471,7 +471,7 @@ class PngWriter { final boolean useAlpha = pngColorType == PngColorType.GREYSCALE_WITH_ALPHA || pngColorType == PngColorType.TRUE_COLOR_WITH_ALPHA; - final int[] row = new int[AllocationChecker.check(width)]; + final int[] row = Allocator.intArray(width); for (int y = 0; y < height; y++) { // Debug.debug("y", y + "/" + height); src.getRGB(0, y, width, 1, row, 0, width); @@ -522,7 +522,7 @@ class PngWriter { final boolean useAlpha = pngColorType == PngColorType.GREYSCALE_WITH_ALPHA || pngColorType == PngColorType.TRUE_COLOR_WITH_ALPHA; - final int[] row = new int[AllocationChecker.check(width)]; + final int[] row = Allocator.intArray(width); for (int y = 0; y < height; y++) { // Debug.debug("y", y + "/" + height); src.getRGB(0, y, width, 1, row, 0, width); diff --git a/src/main/java/org/apache/commons/imaging/formats/png/ScanExpediter.java b/src/main/java/org/apache/commons/imaging/formats/png/ScanExpediter.java index fb1a9fe9..7cf6e2d3 100644 --- a/src/main/java/org/apache/commons/imaging/formats/png/ScanExpediter.java +++ b/src/main/java/org/apache/commons/imaging/formats/png/ScanExpediter.java @@ -23,7 +23,7 @@ import java.io.IOException; import java.io.InputStream; import org.apache.commons.imaging.ImageReadException; -import org.apache.commons.imaging.common.AllocationChecker; +import org.apache.commons.imaging.common.Allocator; import org.apache.commons.imaging.formats.png.chunks.PngChunkPlte; import org.apache.commons.imaging.formats.png.scanlinefilters.ScanlineFilter; import org.apache.commons.imaging.formats.png.scanlinefilters.ScanlineFilterAverage; @@ -209,7 +209,7 @@ abstract class ScanExpediter { final int bytesPerPixel) throws ImageReadException, IOException { final ScanlineFilter filter = getScanlineFilter(filterType, bytesPerPixel); - final byte[] dst = new byte[AllocationChecker.check(src.length)]; + final byte[] dst = Allocator.byteArray(src.length); filter.unfilter(src, dst, prev); return dst; } diff --git a/src/main/java/org/apache/commons/imaging/formats/png/chunks/PngChunkIccp.java b/src/main/java/org/apache/commons/imaging/formats/png/chunks/PngChunkIccp.java index c4d8696b..d1030730 100644 --- a/src/main/java/org/apache/commons/imaging/formats/png/chunks/PngChunkIccp.java +++ b/src/main/java/org/apache/commons/imaging/formats/png/chunks/PngChunkIccp.java @@ -28,7 +28,7 @@ import java.util.logging.Logger; import java.util.zip.InflaterInputStream; import org.apache.commons.imaging.ImageReadException; -import org.apache.commons.imaging.common.AllocationChecker; +import org.apache.commons.imaging.common.Allocator; /** * The PNG iCCP chunk. If "present, the image samples conform to the color space represented by the embedded ICC @@ -84,7 +84,7 @@ public class PngChunkIccp extends PngChunk { compressionMethod = bytes[index + 1]; final int compressedProfileLength = bytes.length - (index + 1 + 1); - compressedProfile = new byte[AllocationChecker.check(compressedProfileLength)]; + compressedProfile = Allocator.byteArray(compressedProfileLength); System.arraycopy(bytes, index + 1 + 1, compressedProfile, 0, compressedProfileLength); if (LOGGER.isLoggable(Level.FINEST)) { diff --git a/src/main/java/org/apache/commons/imaging/formats/png/chunks/PngChunkItxt.java b/src/main/java/org/apache/commons/imaging/formats/png/chunks/PngChunkItxt.java index 40373476..67adc9d1 100644 --- a/src/main/java/org/apache/commons/imaging/formats/png/chunks/PngChunkItxt.java +++ b/src/main/java/org/apache/commons/imaging/formats/png/chunks/PngChunkItxt.java @@ -25,7 +25,7 @@ import java.nio.charset.StandardCharsets; import java.util.zip.InflaterInputStream; import org.apache.commons.imaging.ImageReadException; -import org.apache.commons.imaging.common.AllocationChecker; +import org.apache.commons.imaging.common.Allocator; import org.apache.commons.imaging.formats.png.PngConstants; import org.apache.commons.imaging.formats.png.PngText; @@ -91,7 +91,7 @@ public class PngChunkItxt extends PngTextChunk { if (compressed) { final int compressedTextLength = bytes.length - index; - final byte[] compressedText = new byte[AllocationChecker.check(compressedTextLength)]; + final byte[] compressedText = Allocator.byteArray(compressedTextLength); System.arraycopy(bytes, index, compressedText, 0, compressedTextLength); text = new String(getStreamBytes( diff --git a/src/main/java/org/apache/commons/imaging/formats/png/chunks/PngChunkPlte.java b/src/main/java/org/apache/commons/imaging/formats/png/chunks/PngChunkPlte.java index 4c786222..4ecdab79 100644 --- a/src/main/java/org/apache/commons/imaging/formats/png/chunks/PngChunkPlte.java +++ b/src/main/java/org/apache/commons/imaging/formats/png/chunks/PngChunkPlte.java @@ -23,7 +23,7 @@ import java.io.IOException; import java.util.Arrays; import org.apache.commons.imaging.ImageReadException; -import org.apache.commons.imaging.common.AllocationChecker; +import org.apache.commons.imaging.common.Allocator; import org.apache.commons.imaging.formats.png.GammaCorrection; public class PngChunkPlte extends PngChunk { @@ -41,7 +41,7 @@ public class PngChunkPlte extends PngChunk { final int count = length / 3; - rgb = new int[AllocationChecker.check(count)]; + rgb = Allocator.intArray(count); for (int i = 0; i < count; i++) { final int red = readByte("red[" + i + "]", is, diff --git a/src/main/java/org/apache/commons/imaging/formats/png/chunks/PngChunkZtxt.java b/src/main/java/org/apache/commons/imaging/formats/png/chunks/PngChunkZtxt.java index f08467ee..8c478b1d 100644 --- a/src/main/java/org/apache/commons/imaging/formats/png/chunks/PngChunkZtxt.java +++ b/src/main/java/org/apache/commons/imaging/formats/png/chunks/PngChunkZtxt.java @@ -25,7 +25,7 @@ import java.nio.charset.StandardCharsets; import java.util.zip.InflaterInputStream; import org.apache.commons.imaging.ImageReadException; -import org.apache.commons.imaging.common.AllocationChecker; +import org.apache.commons.imaging.common.Allocator; import org.apache.commons.imaging.formats.png.PngConstants; import org.apache.commons.imaging.formats.png.PngText; @@ -55,7 +55,7 @@ public class PngChunkZtxt extends PngTextChunk { } final int compressedTextLength = bytes.length - index; - final byte[] compressedText = new byte[AllocationChecker.check(compressedTextLength)]; + final byte[] compressedText = Allocator.byteArray(compressedTextLength); System.arraycopy(bytes, index, compressedText, 0, compressedTextLength); text = new String(getStreamBytes(new InflaterInputStream(new ByteArrayInputStream(compressedText))), StandardCharsets.ISO_8859_1); diff --git a/src/main/java/org/apache/commons/imaging/formats/psd/datareaders/CompressedDataReader.java b/src/main/java/org/apache/commons/imaging/formats/psd/datareaders/CompressedDataReader.java index 811c069f..584397b1 100644 --- a/src/main/java/org/apache/commons/imaging/formats/psd/datareaders/CompressedDataReader.java +++ b/src/main/java/org/apache/commons/imaging/formats/psd/datareaders/CompressedDataReader.java @@ -23,7 +23,7 @@ import java.io.InputStream; import java.nio.ByteOrder; import org.apache.commons.imaging.ImageReadException; -import org.apache.commons.imaging.common.AllocationChecker; +import org.apache.commons.imaging.common.Allocator; import org.apache.commons.imaging.common.BinaryFileParser; import org.apache.commons.imaging.common.BinaryFunctions; import org.apache.commons.imaging.common.PackBits; @@ -51,7 +51,7 @@ public class CompressedDataReader implements DataReader { // this.setDebug(true); final int scanlineCount = height * header.channels; - final int[] scanlineByteCounts = new int[AllocationChecker.check(scanlineCount)]; + final int[] scanlineByteCounts = Allocator.intArray(scanlineCount); for (int i = 0; i < scanlineCount; i++) { scanlineByteCounts[i] = BinaryFunctions.read2Bytes("scanline_bytecount[" + i + "]", is, "PSD: bad Image Data", bfp.getByteOrder()); @@ -62,7 +62,7 @@ public class CompressedDataReader implements DataReader { final int depth = header.depth; final int channelCount = dataParser.getBasicChannelsCount(); - final int[][][] data = new int[AllocationChecker.check(channelCount)][AllocationChecker.check(height)][]; + final int[][][] data = new int[Allocator.check(channelCount)][Allocator.check(height)][]; // channels[0] = for (int channel = 0; channel < channelCount; channel++) { for (int y = 0; y < height; y++) { diff --git a/src/main/java/org/apache/commons/imaging/formats/psd/datareaders/UncompressedDataReader.java b/src/main/java/org/apache/commons/imaging/formats/psd/datareaders/UncompressedDataReader.java index 4aa4ee30..6e7a7f24 100644 --- a/src/main/java/org/apache/commons/imaging/formats/psd/datareaders/UncompressedDataReader.java +++ b/src/main/java/org/apache/commons/imaging/formats/psd/datareaders/UncompressedDataReader.java @@ -22,7 +22,7 @@ import java.io.InputStream; import java.nio.ByteOrder; import org.apache.commons.imaging.ImageReadException; -import org.apache.commons.imaging.common.AllocationChecker; +import org.apache.commons.imaging.common.Allocator; import org.apache.commons.imaging.common.BinaryFileParser; import org.apache.commons.imaging.common.mylzw.BitsToByteInputStream; import org.apache.commons.imaging.common.mylzw.MyBitInputStream; @@ -51,8 +51,8 @@ public class UncompressedDataReader implements DataReader { final MyBitInputStream mbis = new MyBitInputStream(is, ByteOrder.BIG_ENDIAN); // we want all samples to be bytes try (BitsToByteInputStream bbis = new BitsToByteInputStream(mbis, 8)) { - final int[][][] data = new int[AllocationChecker.check(channelCount)][AllocationChecker - .check(height)][AllocationChecker.check(width)]; + final int[][][] data = new int[Allocator.check(channelCount)][Allocator + .check(height)][Allocator.check(width)]; for (int channel = 0; channel < channelCount; channel++) { for (int y = 0; y < height; y++) { for (int x = 0; x < width; x++) { diff --git a/src/main/java/org/apache/commons/imaging/formats/rgbe/RgbeInfo.java b/src/main/java/org/apache/commons/imaging/formats/rgbe/RgbeInfo.java index eb81183d..0ca24c1a 100644 --- a/src/main/java/org/apache/commons/imaging/formats/rgbe/RgbeInfo.java +++ b/src/main/java/org/apache/commons/imaging/formats/rgbe/RgbeInfo.java @@ -24,7 +24,7 @@ import java.util.regex.Matcher; import java.util.regex.Pattern; import org.apache.commons.imaging.ImageReadException; -import org.apache.commons.imaging.common.AllocationChecker; +import org.apache.commons.imaging.common.Allocator; import org.apache.commons.imaging.common.BinaryFunctions; import org.apache.commons.imaging.common.ByteConversions; import org.apache.commons.imaging.common.GenericImageMetadata; @@ -107,8 +107,8 @@ class RgbeInfo implements Closeable { final byte[] scanLineBytes = ByteConversions.toBytes((short) wd, ByteOrder.BIG_ENDIAN); - final byte[] rgbe = new byte[AllocationChecker.check(wd * 4)]; - final float[][] out = new float[3][AllocationChecker.check(wd * ht)]; + final byte[] rgbe = Allocator.byteArray(wd * 4); + final float[][] out = new float[3][Allocator.check(wd * ht)]; for (int i = 0; i < ht; i++) { BinaryFunctions.readAndVerifyBytes(in, TWO_TWO, "Scan line " + i + " expected to start with 0x2 0x2"); diff --git a/src/main/java/org/apache/commons/imaging/formats/tiff/TiffField.java b/src/main/java/org/apache/commons/imaging/formats/tiff/TiffField.java index f1994d48..5f6565cb 100644 --- a/src/main/java/org/apache/commons/imaging/formats/tiff/TiffField.java +++ b/src/main/java/org/apache/commons/imaging/formats/tiff/TiffField.java @@ -29,7 +29,7 @@ import java.util.logging.Level; import java.util.logging.Logger; import org.apache.commons.imaging.ImageReadException; -import org.apache.commons.imaging.common.AllocationChecker; +import org.apache.commons.imaging.common.Allocator; import org.apache.commons.imaging.common.BinaryFunctions; import org.apache.commons.imaging.formats.tiff.constants.TiffConstants; import org.apache.commons.imaging.formats.tiff.constants.TiffTagConstants; @@ -156,25 +156,25 @@ public class TiffField { } if (o instanceof Number[]) { final Number[] numbers = (Number[]) o; - final double[] result = new double[AllocationChecker.check(numbers.length)]; + final double[] result = Allocator.doubleArray(numbers.length); Arrays.setAll(result, i -> numbers[i].doubleValue()); return result; } if (o instanceof short[]) { final short[] numbers = (short[]) o; - final double[] result = new double[AllocationChecker.check(numbers.length)]; + final double[] result = Allocator.doubleArray(numbers.length); Arrays.setAll(result, i -> numbers[i]); return result; } if (o instanceof int[]) { final int[] numbers = (int[]) o; - final double[] result = new double[AllocationChecker.check(numbers.length)]; + final double[] result = Allocator.doubleArray(numbers.length); Arrays.setAll(result, i -> numbers[i]); return result; } if (o instanceof float[]) { final float[] numbers = (float[]) o; - final double[] result = new double[AllocationChecker.check(numbers.length)]; + final double[] result = Allocator.doubleArray(numbers.length); Arrays.setAll(result, i -> numbers[i]); return result; } @@ -220,13 +220,13 @@ public class TiffField { } if (o instanceof Number[]) { final Number[] numbers = (Number[]) o; - final int[] result = new int[AllocationChecker.check(numbers.length)]; + final int[] result = Allocator.intArray(numbers.length); Arrays.setAll(result, i -> numbers[i].intValue()); return result; } if (o instanceof short[]) { final short[] numbers = (short[]) o; - final int[] result = new int[AllocationChecker.check(numbers.length)]; + final int[] result = Allocator.intArray(numbers.length); Arrays.setAll(result, i -> 0xffff & numbers[i]); return result; } diff --git a/src/main/java/org/apache/commons/imaging/formats/tiff/TiffRasterDataFloat.java b/src/main/java/org/apache/commons/imaging/formats/tiff/TiffRasterDataFloat.java index 9b236a19..e512d5d4 100644 --- a/src/main/java/org/apache/commons/imaging/formats/tiff/TiffRasterDataFloat.java +++ b/src/main/java/org/apache/commons/imaging/formats/tiff/TiffRasterDataFloat.java @@ -18,7 +18,7 @@ package org.apache.commons.imaging.formats.tiff; import java.util.stream.IntStream; -import org.apache.commons.imaging.common.AllocationChecker; +import org.apache.commons.imaging.common.Allocator; /** * Provides a simple container for floating-point data. Some TIFF files are used @@ -55,7 +55,7 @@ public class TiffRasterDataFloat extends TiffRasterData { */ public TiffRasterDataFloat(final int width, final int height) { super(width, height, 1); - data = new float[AllocationChecker.check(nCells)]; + data = Allocator.floatArray(nCells); } /** @@ -84,7 +84,7 @@ public class TiffRasterDataFloat extends TiffRasterData { */ public TiffRasterDataFloat(final int width, final int height, final int samplesPerPixel) { super(width, height, samplesPerPixel); - data = new float[AllocationChecker.check(nCells)]; + data = Allocator.floatArray(nCells); } /** diff --git a/src/main/java/org/apache/commons/imaging/formats/tiff/TiffRasterDataInt.java b/src/main/java/org/apache/commons/imaging/formats/tiff/TiffRasterDataInt.java index 08a91704..b32539a5 100644 --- a/src/main/java/org/apache/commons/imaging/formats/tiff/TiffRasterDataInt.java +++ b/src/main/java/org/apache/commons/imaging/formats/tiff/TiffRasterDataInt.java @@ -16,7 +16,7 @@ */ package org.apache.commons.imaging.formats.tiff; -import org.apache.commons.imaging.common.AllocationChecker; +import org.apache.commons.imaging.common.Allocator; /** * Provides a simple container for floating-point data. Some TIFF files are used @@ -53,7 +53,7 @@ public class TiffRasterDataInt extends TiffRasterData { */ public TiffRasterDataInt(final int width, final int height) { super(width, height, 1); - data = new int[AllocationChecker.check(nCells)]; + data = Allocator.intArray(nCells); } /** @@ -65,7 +65,7 @@ public class TiffRasterDataInt extends TiffRasterData { */ public TiffRasterDataInt(final int width, final int height, final int samplesPerPixel) { super(width, height, samplesPerPixel); - data = new int[AllocationChecker.check(nCells)]; + data = Allocator.intArray(nCells); } /** @@ -113,7 +113,7 @@ public class TiffRasterDataInt extends TiffRasterData { */ @Override public float[] getData() { - final float[] result = new float[AllocationChecker.check(nCells)]; + final float[] result = Allocator.floatArray(nCells); for (int i = 0; i < nCells; i++) { result[i] = data[i]; } diff --git a/src/main/java/org/apache/commons/imaging/formats/tiff/datareaders/DataReaderStrips.java b/src/main/java/org/apache/commons/imaging/formats/tiff/datareaders/DataReaderStrips.java index e3f5a94d..0e4ec9d4 100644 --- a/src/main/java/org/apache/commons/imaging/formats/tiff/datareaders/DataReaderStrips.java +++ b/src/main/java/org/apache/commons/imaging/formats/tiff/datareaders/DataReaderStrips.java @@ -22,7 +22,7 @@ import java.io.IOException; import java.nio.ByteOrder; import org.apache.commons.imaging.ImageReadException; -import org.apache.commons.imaging.common.AllocationChecker; +import org.apache.commons.imaging.common.Allocator; import org.apache.commons.imaging.common.ImageBuilder; import org.apache.commons.imaging.formats.tiff.TiffDirectory; import org.apache.commons.imaging.formats.tiff.TiffImageData; @@ -219,7 +219,7 @@ public final class DataReaderStrips extends ImageDataReader { // special case handled above try (BitInputStream bis = new BitInputStream(new ByteArrayInputStream(bytes), byteOrder)) { - int[] samples = new int[AllocationChecker.check(bitsPerSampleLength)]; + int[] samples = Allocator.intArray(bitsPerSampleLength); resetPredictor(); for (int i = 0; i < pixelsPerStrip; i++) { getSamplesAsBytes(bis, samples); @@ -310,7 +310,7 @@ public final class DataReaderStrips extends ImageDataReader { final long bytesPerStrip = rowsInThisStrip * bytesPerRow; final long pixelsPerStrip = rowsInThisStrip * width; - final byte[] b = new byte[AllocationChecker.check((int) bytesPerStrip)]; + final byte[] b = Allocator.byteArray((int) bytesPerStrip); for (int iPlane = 0; iPlane < 3; iPlane++) { final int planeStrip = iPlane * nStripsInPlane + strip; final byte[] compressed = imageData.getImageData(planeStrip).getData(); @@ -373,7 +373,7 @@ public final class DataReaderStrips extends ImageDataReader { rasterHeight = height; } - final float[] rasterDataFloat = new float[AllocationChecker.check(rasterWidth * rasterHeight * samplesPerPixel)]; + final float[] rasterDataFloat = Allocator.floatArray(rasterWidth * rasterHeight * samplesPerPixel); // the legacy code is optimized to the reading of whole // strips (except for the last strip in the image, which can @@ -426,7 +426,7 @@ public final class DataReaderStrips extends ImageDataReader { rasterHeight = height; } - final int[] rasterDataInt = new int[AllocationChecker.check(rasterWidth * rasterHeight)]; + final int[] rasterDataInt = Allocator.intArray(rasterWidth * rasterHeight); // the legacy code is optimized to the reading of whole // strips (except for the last strip in the image, which can diff --git a/src/main/java/org/apache/commons/imaging/formats/tiff/datareaders/DataReaderTiled.java b/src/main/java/org/apache/commons/imaging/formats/tiff/datareaders/DataReaderTiled.java index 1bf65009..398fc3f6 100644 --- a/src/main/java/org/apache/commons/imaging/formats/tiff/datareaders/DataReaderTiled.java +++ b/src/main/java/org/apache/commons/imaging/formats/tiff/datareaders/DataReaderTiled.java @@ -28,7 +28,7 @@ import java.io.IOException; import java.nio.ByteOrder; import org.apache.commons.imaging.ImageReadException; -import org.apache.commons.imaging.common.AllocationChecker; +import org.apache.commons.imaging.common.Allocator; import org.apache.commons.imaging.common.ImageBuilder; import org.apache.commons.imaging.formats.tiff.TiffDirectory; import org.apache.commons.imaging.formats.tiff.TiffImageData; @@ -185,7 +185,7 @@ public final class DataReaderTiled extends ImageDataReader { int tileX = 0; int tileY = 0; - int[] samples = new int[AllocationChecker.check(bitsPerSampleLength)]; + int[] samples = Allocator.intArray(bitsPerSampleLength); resetPredictor(); for (int i = 0; i < pixelsPerTile; i++) { @@ -314,7 +314,7 @@ public final class DataReaderTiled extends ImageDataReader { rasterWidth = width; rasterHeight = height; } - final float[] rasterDataFloat = new float[AllocationChecker.check(rasterWidth * rasterHeight * samplesPerPixel)]; + final float[] rasterDataFloat = Allocator.floatArray(rasterWidth * rasterHeight * samplesPerPixel); // tileWidth is the width of the tile // tileLength is the height of the tile @@ -366,7 +366,7 @@ public final class DataReaderTiled extends ImageDataReader { rasterWidth = width; rasterHeight = height; } - final int[] rasterDataInt = new int[AllocationChecker.check(rasterWidth * rasterHeight)]; + final int[] rasterDataInt = Allocator.intArray(rasterWidth * rasterHeight); // tileWidth is the width of the tile // tileLength is the height of the tile diff --git a/src/main/java/org/apache/commons/imaging/formats/tiff/datareaders/ImageDataReader.java b/src/main/java/org/apache/commons/imaging/formats/tiff/datareaders/ImageDataReader.java index d0899056..4c835016 100644 --- a/src/main/java/org/apache/commons/imaging/formats/tiff/datareaders/ImageDataReader.java +++ b/src/main/java/org/apache/commons/imaging/formats/tiff/datareaders/ImageDataReader.java @@ -37,7 +37,7 @@ import java.nio.ByteOrder; import java.util.Arrays; import org.apache.commons.imaging.ImageReadException; -import org.apache.commons.imaging.common.AllocationChecker; +import org.apache.commons.imaging.common.Allocator; import org.apache.commons.imaging.common.ImageBuilder; import org.apache.commons.imaging.common.PackBits; import org.apache.commons.imaging.common.ZlibDeflate; @@ -200,7 +200,7 @@ public abstract class ImageDataReader { this.width = width; this.height = height; this.planarConfiguration = planarConfiguration; - last = new int[AllocationChecker.check(samplesPerPixel)]; + last = Allocator.intArray(samplesPerPixel); } @@ -641,7 +641,7 @@ public abstract class ImageDataReader { final int bytesPerScan = scanSize * samplesPerPixel * bytesPerSample; final int nBytes = bytesPerScan * height; final int length = bytes.length < nBytes ? nBytes / bytesPerScan : height; - final int[] samples = new int[AllocationChecker.check(scanSize * samplesPerPixel * height)]; + final int[] samples = Allocator.intArray(scanSize * samplesPerPixel * height); // floating-point differencing is indicated by a predictor value of 3. if (predictor == TiffTagConstants.PREDICTOR_VALUE_FLOATING_POINT_DIFFERENCING) { // at this time, this class supports the 32-bit format. The @@ -843,7 +843,7 @@ public abstract class ImageDataReader { final int nBytes = bytesPerSample * scanSize * height; final int length = bytes.length < nBytes ? nBytes / scanSize : height; - final int[] samples = new int[AllocationChecker.check(scanSize * height)]; + final int[] samples = Allocator.intArray(scanSize * height); // At this time, Commons Imaging only supports two-byte // two's complement short integers. It is assumed that // the calling module already checked the arguments for diff --git a/src/main/java/org/apache/commons/imaging/formats/tiff/fieldtypes/FieldTypeAscii.java b/src/main/java/org/apache/commons/imaging/formats/tiff/fieldtypes/FieldTypeAscii.java index 1dde1f10..5b354783 100644 --- a/src/main/java/org/apache/commons/imaging/formats/tiff/fieldtypes/FieldTypeAscii.java +++ b/src/main/java/org/apache/commons/imaging/formats/tiff/fieldtypes/FieldTypeAscii.java @@ -21,7 +21,7 @@ import java.nio.charset.StandardCharsets; import java.util.Arrays; import org.apache.commons.imaging.ImageWriteException; -import org.apache.commons.imaging.common.AllocationChecker; +import org.apache.commons.imaging.common.Allocator; import org.apache.commons.imaging.formats.tiff.TiffField; public class FieldTypeAscii extends FieldType { @@ -40,7 +40,7 @@ public class FieldTypeAscii extends FieldType { nullCount++; } } - final String[] strings = new String[AllocationChecker.check(nullCount)]; + final String[] strings = Allocator.array(nullCount, String[]::new); int stringsAdded = 0; strings[0] = ""; // if we have a 0 length string int nextStringPos = 0; @@ -90,7 +90,7 @@ public class FieldTypeAscii extends FieldType { final byte[] bytes = string.getBytes(StandardCharsets.UTF_8); totalLength += (bytes.length + 1); } - final byte[] result = new byte[AllocationChecker.check(totalLength)]; + final byte[] result = Allocator.byteArray(totalLength); int position = 0; for (final String string : strings) { final byte[] bytes = string.getBytes(StandardCharsets.UTF_8); diff --git a/src/main/java/org/apache/commons/imaging/formats/tiff/fieldtypes/FieldTypeDouble.java b/src/main/java/org/apache/commons/imaging/formats/tiff/fieldtypes/FieldTypeDouble.java index e6b3de18..65a143fa 100644 --- a/src/main/java/org/apache/commons/imaging/formats/tiff/fieldtypes/FieldTypeDouble.java +++ b/src/main/java/org/apache/commons/imaging/formats/tiff/fieldtypes/FieldTypeDouble.java @@ -20,7 +20,7 @@ import java.nio.ByteOrder; import java.util.Arrays; import org.apache.commons.imaging.ImageWriteException; -import org.apache.commons.imaging.common.AllocationChecker; +import org.apache.commons.imaging.common.Allocator; import org.apache.commons.imaging.common.ByteConversions; import org.apache.commons.imaging.formats.tiff.TiffField; @@ -49,7 +49,7 @@ public class FieldTypeDouble extends FieldType { if (!(o instanceof Double[])) { throw new ImageWriteException("Invalid data", o); } - final double[] values = new double[AllocationChecker.check(((Double[]) o).length)]; + final double[] values = new double[Allocator.check(((Double[]) o).length)]; Arrays.setAll(values, i -> ((Double[]) o)[i].doubleValue()); return ByteConversions.toBytes(values, byteOrder); } diff --git a/src/main/java/org/apache/commons/imaging/formats/tiff/fieldtypes/FieldTypeFloat.java b/src/main/java/org/apache/commons/imaging/formats/tiff/fieldtypes/FieldTypeFloat.java index f88e1707..ccff7b1f 100644 --- a/src/main/java/org/apache/commons/imaging/formats/tiff/fieldtypes/FieldTypeFloat.java +++ b/src/main/java/org/apache/commons/imaging/formats/tiff/fieldtypes/FieldTypeFloat.java @@ -19,7 +19,7 @@ package org.apache.commons.imaging.formats.tiff.fieldtypes; import java.nio.ByteOrder; import org.apache.commons.imaging.ImageWriteException; -import org.apache.commons.imaging.common.AllocationChecker; +import org.apache.commons.imaging.common.Allocator; import org.apache.commons.imaging.common.ByteConversions; import org.apache.commons.imaging.formats.tiff.TiffField; @@ -50,7 +50,7 @@ public class FieldTypeFloat extends FieldType { throw new ImageWriteException("Invalid data", o); } final Float[] numbers = (Float[]) o; - final float[] values = new float[AllocationChecker.check(numbers.length)]; + final float[] values = Allocator.floatArray(numbers.length); for (int i = 0; i < values.length; i++) { values[i] = numbers[i].floatValue(); } diff --git a/src/main/java/org/apache/commons/imaging/formats/tiff/fieldtypes/FieldTypeLong.java b/src/main/java/org/apache/commons/imaging/formats/tiff/fieldtypes/FieldTypeLong.java index 90bab592..11c7615c 100644 --- a/src/main/java/org/apache/commons/imaging/formats/tiff/fieldtypes/FieldTypeLong.java +++ b/src/main/java/org/apache/commons/imaging/formats/tiff/fieldtypes/FieldTypeLong.java @@ -19,7 +19,7 @@ package org.apache.commons.imaging.formats.tiff.fieldtypes; import java.nio.ByteOrder; import org.apache.commons.imaging.ImageWriteException; -import org.apache.commons.imaging.common.AllocationChecker; +import org.apache.commons.imaging.common.Allocator; import org.apache.commons.imaging.common.ByteConversions; import org.apache.commons.imaging.formats.tiff.TiffField; @@ -51,7 +51,7 @@ public class FieldTypeLong extends FieldType { throw new ImageWriteException("Invalid data", o); } final Integer[] numbers = (Integer[]) o; - final int[] values = new int[AllocationChecker.check(numbers.length)]; + final int[] values = Allocator.intArray(numbers.length); for (int i = 0; i < values.length; i++) { values[i] = numbers[i]; } diff --git a/src/main/java/org/apache/commons/imaging/formats/tiff/fieldtypes/FieldTypeRational.java b/src/main/java/org/apache/commons/imaging/formats/tiff/fieldtypes/FieldTypeRational.java index 7f3abad4..04572b81 100644 --- a/src/main/java/org/apache/commons/imaging/formats/tiff/fieldtypes/FieldTypeRational.java +++ b/src/main/java/org/apache/commons/imaging/formats/tiff/fieldtypes/FieldTypeRational.java @@ -20,7 +20,7 @@ import java.nio.ByteOrder; import java.util.Arrays; import org.apache.commons.imaging.ImageWriteException; -import org.apache.commons.imaging.common.AllocationChecker; +import org.apache.commons.imaging.common.Allocator; import org.apache.commons.imaging.common.ByteConversions; import org.apache.commons.imaging.common.RationalNumber; import org.apache.commons.imaging.formats.tiff.TiffField; @@ -57,7 +57,7 @@ public class FieldTypeRational extends FieldType { } if (o instanceof Number[]) { final Number[] numbers = (Number[]) o; - final RationalNumber[] rationalNumbers = new RationalNumber[AllocationChecker.check(numbers.length)]; + final RationalNumber[] rationalNumbers = Allocator.array(numbers.length, RationalNumber[]::new); Arrays.setAll(rationalNumbers, RationalNumber::valueOf); return ByteConversions.toBytes(rationalNumbers, byteOrder); } @@ -65,7 +65,7 @@ public class FieldTypeRational extends FieldType { throw new ImageWriteException("Invalid data", o); } final double[] numbers = (double[]) o; - final RationalNumber[] rationalNumbers = new RationalNumber[AllocationChecker.check(numbers.length)]; + final RationalNumber[] rationalNumbers = Allocator.array(numbers.length, RationalNumber[]::new); Arrays.setAll(rationalNumbers, RationalNumber::valueOf); return ByteConversions.toBytes(rationalNumbers, byteOrder); } diff --git a/src/main/java/org/apache/commons/imaging/formats/tiff/fieldtypes/FieldTypeShort.java b/src/main/java/org/apache/commons/imaging/formats/tiff/fieldtypes/FieldTypeShort.java index 4fee9326..ee661c2a 100644 --- a/src/main/java/org/apache/commons/imaging/formats/tiff/fieldtypes/FieldTypeShort.java +++ b/src/main/java/org/apache/commons/imaging/formats/tiff/fieldtypes/FieldTypeShort.java @@ -19,7 +19,7 @@ package org.apache.commons.imaging.formats.tiff.fieldtypes; import java.nio.ByteOrder; import org.apache.commons.imaging.ImageWriteException; -import org.apache.commons.imaging.common.AllocationChecker; +import org.apache.commons.imaging.common.Allocator; import org.apache.commons.imaging.common.ByteConversions; import org.apache.commons.imaging.formats.tiff.TiffField; @@ -50,7 +50,7 @@ public class FieldTypeShort extends FieldType { throw new ImageWriteException("Invalid data", o); } final Short[] numbers = (Short[]) o; - final short[] values = new short[AllocationChecker.check(numbers.length)]; + final short[] values = new short[Allocator.check(numbers.length)]; for (int i = 0; i < values.length; i++) { values[i] = numbers[i].shortValue(); } diff --git a/src/main/java/org/apache/commons/imaging/formats/tiff/photometricinterpreters/PhotometricInterpreterPalette.java b/src/main/java/org/apache/commons/imaging/formats/tiff/photometricinterpreters/PhotometricInterpreterPalette.java index 1a7940b0..42322791 100644 --- a/src/main/java/org/apache/commons/imaging/formats/tiff/photometricinterpreters/PhotometricInterpreterPalette.java +++ b/src/main/java/org/apache/commons/imaging/formats/tiff/photometricinterpreters/PhotometricInterpreterPalette.java @@ -20,7 +20,7 @@ import java.io.IOException; import java.util.Arrays; import org.apache.commons.imaging.ImageReadException; -import org.apache.commons.imaging.common.AllocationChecker; +import org.apache.commons.imaging.common.Allocator; import org.apache.commons.imaging.common.ImageBuilder; public class PhotometricInterpreterPalette extends PhotometricInterpreter { @@ -39,7 +39,7 @@ public class PhotometricInterpreterPalette extends PhotometricInterpreter { final int bitsPerPixel = getBitsPerSample(0); final int colormapScale = (1 << bitsPerPixel); - indexColorMap = new int[AllocationChecker.check(colormapScale)]; + indexColorMap = Allocator.intArray(colormapScale); Arrays.setAll(indexColorMap, i -> { final int red = (colorMap[i] >> 8) & 0xff; final int green = (colorMap[i + (colormapScale)] >> 8) & 0xff; diff --git a/src/main/java/org/apache/commons/imaging/formats/tiff/taginfos/TagInfoAscii.java b/src/main/java/org/apache/commons/imaging/formats/tiff/taginfos/TagInfoAscii.java index 57b823e4..2f14e99f 100644 --- a/src/main/java/org/apache/commons/imaging/formats/tiff/taginfos/TagInfoAscii.java +++ b/src/main/java/org/apache/commons/imaging/formats/tiff/taginfos/TagInfoAscii.java @@ -20,7 +20,7 @@ import java.nio.ByteOrder; import java.nio.charset.StandardCharsets; import org.apache.commons.imaging.ImageWriteException; -import org.apache.commons.imaging.common.AllocationChecker; +import org.apache.commons.imaging.common.Allocator; import org.apache.commons.imaging.formats.tiff.constants.TiffDirectoryType; import org.apache.commons.imaging.formats.tiff.fieldtypes.FieldType; @@ -42,7 +42,7 @@ public class TagInfoAscii extends TagInfo { nullCount++; } } - final String[] strings = new String[AllocationChecker.check(nullCount + 1)]; + final String[] strings = Allocator.array(nullCount + 1, String[]::new); int stringsAdded = 0; strings[0] = ""; // if we have a 0 length string int nextStringPos = 0; diff --git a/src/main/java/org/apache/commons/imaging/formats/tiff/taginfos/TagInfoGpsText.java b/src/main/java/org/apache/commons/imaging/formats/tiff/taginfos/TagInfoGpsText.java index df0041a2..e924aa82 100644 --- a/src/main/java/org/apache/commons/imaging/formats/tiff/taginfos/TagInfoGpsText.java +++ b/src/main/java/org/apache/commons/imaging/formats/tiff/taginfos/TagInfoGpsText.java @@ -22,7 +22,7 @@ import java.nio.charset.StandardCharsets; import org.apache.commons.imaging.ImageReadException; import org.apache.commons.imaging.ImageWriteException; -import org.apache.commons.imaging.common.AllocationChecker; +import org.apache.commons.imaging.common.Allocator; import org.apache.commons.imaging.common.BinaryFunctions; import org.apache.commons.imaging.formats.tiff.TiffField; import org.apache.commons.imaging.formats.tiff.constants.TiffDirectoryType; @@ -89,7 +89,7 @@ public final class TagInfoGpsText extends TagInfo { final String decodedAscii = new String(asciiBytes, TEXT_ENCODING_ASCII.encodingName); if (decodedAscii.equals(s)) { // no unicode/non-ascii values. - final byte[] result = new byte[AllocationChecker.check(asciiBytes.length + TEXT_ENCODING_ASCII.prefix.length)]; + final byte[] result = Allocator.byteArray(asciiBytes.length + TEXT_ENCODING_ASCII.prefix.length); System.arraycopy(TEXT_ENCODING_ASCII.prefix, 0, result, 0, TEXT_ENCODING_ASCII.prefix.length); System.arraycopy(asciiBytes, 0, result, TEXT_ENCODING_ASCII.prefix.length, asciiBytes.length); return result; @@ -102,7 +102,7 @@ public final class TagInfoGpsText extends TagInfo { encoding = TEXT_ENCODING_UNICODE_LE; } final byte[] unicodeBytes = s.getBytes(encoding.encodingName); - final byte[] result = new byte[AllocationChecker.check(unicodeBytes.length + encoding.prefix.length)]; + final byte[] result = Allocator.byteArray(unicodeBytes.length + encoding.prefix.length); System.arraycopy(encoding.prefix, 0, result, 0, encoding.prefix.length); System.arraycopy(unicodeBytes, 0, result, encoding.prefix.length, unicodeBytes.length); return result; diff --git a/src/main/java/org/apache/commons/imaging/formats/tiff/write/ImageDataOffsets.java b/src/main/java/org/apache/commons/imaging/formats/tiff/write/ImageDataOffsets.java index 7c2a0adc..8511009d 100644 --- a/src/main/java/org/apache/commons/imaging/formats/tiff/write/ImageDataOffsets.java +++ b/src/main/java/org/apache/commons/imaging/formats/tiff/write/ImageDataOffsets.java @@ -18,7 +18,7 @@ package org.apache.commons.imaging.formats.tiff.write; import java.util.Arrays; -import org.apache.commons.imaging.common.AllocationChecker; +import org.apache.commons.imaging.common.Allocator; import org.apache.commons.imaging.formats.tiff.TiffElement; class ImageDataOffsets { @@ -30,7 +30,7 @@ class ImageDataOffsets { this.imageDataOffsets = imageDataOffsets; this.imageDataOffsetsField = imageDataOffsetsField; - outputItems = new TiffOutputItem[AllocationChecker.check(imageData.length)]; + outputItems = Allocator.array(imageData.length, TiffOutputItem[]::new); Arrays.setAll(outputItems, i -> new TiffOutputItem.Value("TIFF image data", imageData[i].getData())); } diff --git a/src/main/java/org/apache/commons/imaging/formats/tiff/write/TiffImageWriterBase.java b/src/main/java/org/apache/commons/imaging/formats/tiff/write/TiffImageWriterBase.java index 1b708337..b135374b 100644 --- a/src/main/java/org/apache/commons/imaging/formats/tiff/write/TiffImageWriterBase.java +++ b/src/main/java/org/apache/commons/imaging/formats/tiff/write/TiffImageWriterBase.java @@ -43,7 +43,7 @@ import java.util.Map; import org.apache.commons.imaging.ImageWriteException; import org.apache.commons.imaging.PixelDensity; -import org.apache.commons.imaging.common.AllocationChecker; +import org.apache.commons.imaging.common.Allocator; import org.apache.commons.imaging.common.BinaryOutputStream; import org.apache.commons.imaging.common.PackBits; import org.apache.commons.imaging.common.RationalNumber; @@ -101,7 +101,7 @@ public abstract class TiffImageWriterBase { nRowsPerRead = 1; } final int nReads = (height + nRowsPerRead - 1) / nRowsPerRead; - final int[] argb = new int[AllocationChecker.check(nRowsPerRead * width)]; + final int[] argb = Allocator.intArray(nRowsPerRead * width); for (int iRead = 0; iRead < nReads; iRead++) { final int i0 = iRead * nRowsPerRead; final int i1 = i0 + nRowsPerRead > height ? height : i0 + nRowsPerRead; @@ -144,7 +144,7 @@ public abstract class TiffImageWriterBase { byte[][] result; { // Write Strips - result = new byte[AllocationChecker.check(stripCount)][]; + result = new byte[Allocator.check(stripCount)][]; int remainingRows = height; @@ -156,7 +156,7 @@ public abstract class TiffImageWriterBase { final int bytesPerRow = (bitsInRow + 7) / 8; final int bytesInStrip = rowsInStrip * bytesPerRow; - final byte[] uncompressed = new byte[AllocationChecker.check(bytesInStrip)]; + final byte[] uncompressed = Allocator.byteArray(bytesInStrip); int counter = 0; int y = i * rowsPerStrip; diff --git a/src/main/java/org/apache/commons/imaging/formats/tiff/write/TiffImageWriterLossless.java b/src/main/java/org/apache/commons/imaging/formats/tiff/write/TiffImageWriterLossless.java index 7c6f22e4..36951633 100644 --- a/src/main/java/org/apache/commons/imaging/formats/tiff/write/TiffImageWriterLossless.java +++ b/src/main/java/org/apache/commons/imaging/formats/tiff/write/TiffImageWriterLossless.java @@ -32,7 +32,7 @@ import java.util.Map; import org.apache.commons.imaging.FormatCompliance; import org.apache.commons.imaging.ImageReadException; import org.apache.commons.imaging.ImageWriteException; -import org.apache.commons.imaging.common.AllocationChecker; +import org.apache.commons.imaging.common.Allocator; import org.apache.commons.imaging.common.BinaryOutputStream; import org.apache.commons.imaging.common.bytesource.ByteSource; import org.apache.commons.imaging.common.bytesource.ByteSourceArray; @@ -290,7 +290,7 @@ public class TiffImageWriterLossless extends TiffImageWriterBase { final long outputLength) throws IOException, ImageWriteException { final TiffOutputDirectory rootDirectory = outputSet.getRootDirectory(); - final byte[] output = new byte[AllocationChecker.check(outputLength)]; + final byte[] output = Allocator.byteArray(outputLength); // copy old data (including maker notes, etc.) System.arraycopy(exifBytes, 0, output, 0, Math.min(exifBytes.length, output.length)); diff --git a/src/main/java/org/apache/commons/imaging/formats/tiff/write/TiffOutputDirectory.java b/src/main/java/org/apache/commons/imaging/formats/tiff/write/TiffOutputDirectory.java index 40d51008..4a8ad5f5 100644 --- a/src/main/java/org/apache/commons/imaging/formats/tiff/write/TiffOutputDirectory.java +++ b/src/main/java/org/apache/commons/imaging/formats/tiff/write/TiffOutputDirectory.java @@ -30,7 +30,7 @@ import java.util.Comparator; import java.util.List; import org.apache.commons.imaging.ImageWriteException; -import org.apache.commons.imaging.common.AllocationChecker; +import org.apache.commons.imaging.common.Allocator; import org.apache.commons.imaging.common.BinaryOutputStream; import org.apache.commons.imaging.common.RationalNumber; import org.apache.commons.imaging.formats.tiff.JpegImageData; @@ -634,8 +634,8 @@ public final class TiffOutputDirectory extends TiffOutputItem { // TiffOutputField imageDataOffsetsField = null; - final int[] imageDataOffsets = new int[AllocationChecker.check(imageData.length)]; - final int[] imageDataByteCounts = new int[AllocationChecker.check(imageData.length)]; + final int[] imageDataOffsets = Allocator.intArray(imageData.length); + final int[] imageDataByteCounts = Allocator.intArray(imageData.length); Arrays.setAll(imageDataByteCounts, i -> imageData[i].length); // Append imageData-related fields to first directory diff --git a/src/main/java/org/apache/commons/imaging/formats/xbm/XbmImageParser.java b/src/main/java/org/apache/commons/imaging/formats/xbm/XbmImageParser.java index 01d05a51..2f0a9203 100644 --- a/src/main/java/org/apache/commons/imaging/formats/xbm/XbmImageParser.java +++ b/src/main/java/org/apache/commons/imaging/formats/xbm/XbmImageParser.java @@ -42,7 +42,7 @@ import org.apache.commons.imaging.ImageInfo; import org.apache.commons.imaging.ImageParser; import org.apache.commons.imaging.ImageReadException; import org.apache.commons.imaging.ImageWriteException; -import org.apache.commons.imaging.common.AllocationChecker; +import org.apache.commons.imaging.common.Allocator; import org.apache.commons.imaging.common.BasicCParser; import org.apache.commons.imaging.common.ImageMetadata; import org.apache.commons.imaging.common.bytesource.ByteSource; @@ -297,7 +297,7 @@ public class XbmImageParser extends ImageParser<XbmImagingParameters> { } final int rowLength = (xbmHeader.width + 7) / 8; - final byte[] imageData = new byte[AllocationChecker.check(rowLength * xbmHeader.height)]; + final byte[] imageData = Allocator.byteArray(rowLength * xbmHeader.height); int i = 0; for (int y = 0; y < xbmHeader.height; y++) { for (int x = 0; x < xbmHeader.width; x += inputWidth) { diff --git a/src/main/java/org/apache/commons/imaging/formats/xpm/XpmImageParser.java b/src/main/java/org/apache/commons/imaging/formats/xpm/XpmImageParser.java index 299c70f8..ee302fbc 100644 --- a/src/main/java/org/apache/commons/imaging/formats/xpm/XpmImageParser.java +++ b/src/main/java/org/apache/commons/imaging/formats/xpm/XpmImageParser.java @@ -45,7 +45,7 @@ import org.apache.commons.imaging.ImageInfo; import org.apache.commons.imaging.ImageParser; import org.apache.commons.imaging.ImageReadException; import org.apache.commons.imaging.ImageWriteException; -import org.apache.commons.imaging.common.AllocationChecker; +import org.apache.commons.imaging.common.Allocator; import org.apache.commons.imaging.common.BasicCParser; import org.apache.commons.imaging.common.ImageMetadata; import org.apache.commons.imaging.common.bytesource.ByteSource; @@ -560,7 +560,7 @@ public class XpmImageParser extends ImageParser<XpmImagingParameters> { WritableRaster raster; int bpp; if (xpmHeader.palette.size() <= (1 << 8)) { - final int[] palette = new int[AllocationChecker.check(xpmHeader.palette.size())]; + final int[] palette = Allocator.intArray(xpmHeader.palette.size()); for (final Entry<Object, PaletteEntry> entry : xpmHeader.palette.entrySet()) { final PaletteEntry paletteEntry = entry.getValue(); palette[paletteEntry.index] = paletteEntry.getBestARGB(); @@ -572,7 +572,7 @@ public class XpmImageParser extends ImageParser<XpmImagingParameters> { null); bpp = 8; } else if (xpmHeader.palette.size() <= (1 << 16)) { - final int[] palette = new int[AllocationChecker.check(xpmHeader.palette.size())]; + final int[] palette = Allocator.intArray(xpmHeader.palette.size()); for (final Entry<Object, PaletteEntry> entry : xpmHeader.palette.entrySet()) { final PaletteEntry paletteEntry = entry.getValue(); palette[paletteEntry.index] = paletteEntry.getBestARGB(); @@ -638,7 +638,7 @@ public class XpmImageParser extends ImageParser<XpmImagingParameters> { private String toColor(final int color) { final String hex = Integer.toHexString(color); if (hex.length() < 6) { - final char[] zeroes = new char[AllocationChecker.check(6 - hex.length())]; + final char[] zeroes = new char[Allocator.check(6 - hex.length())]; Arrays.fill(zeroes, '0'); return "#" + new String(zeroes) + hex; } diff --git a/src/main/java/org/apache/commons/imaging/icc/IccProfileParser.java b/src/main/java/org/apache/commons/imaging/icc/IccProfileParser.java index 91401ac1..90b85c62 100644 --- a/src/main/java/org/apache/commons/imaging/icc/IccProfileParser.java +++ b/src/main/java/org/apache/commons/imaging/icc/IccProfileParser.java @@ -28,7 +28,7 @@ import java.nio.ByteOrder; import java.util.logging.Level; import java.util.logging.Logger; -import org.apache.commons.imaging.common.AllocationChecker; +import org.apache.commons.imaging.common.Allocator; import org.apache.commons.imaging.common.BinaryFileParser; import org.apache.commons.imaging.common.bytesource.ByteSource; import org.apache.commons.imaging.common.bytesource.ByteSourceArray; @@ -249,7 +249,7 @@ public class IccProfileParser extends BinaryFileParser { final int tagCount = read4Bytes("TagCount", is, "Not a Valid ICC Profile", getByteOrder()); // List tags = new ArrayList(); - final IccTag[] tags = new IccTag[AllocationChecker.check(tagCount)]; + final IccTag[] tags = Allocator.array(tagCount, IccTag[]::new); for (int i = 0; i < tagCount; i++) { final int tagSignature = read4Bytes("TagSignature[" + i + "]", is, "Not a Valid ICC Profile", getByteOrder()); diff --git a/src/main/java/org/apache/commons/imaging/palette/MedianCutQuantizer.java b/src/main/java/org/apache/commons/imaging/palette/MedianCutQuantizer.java index 2ad9539f..f261cf6a 100644 --- a/src/main/java/org/apache/commons/imaging/palette/MedianCutQuantizer.java +++ b/src/main/java/org/apache/commons/imaging/palette/MedianCutQuantizer.java @@ -23,7 +23,7 @@ import java.util.List; import java.util.Map; import org.apache.commons.imaging.ImageWriteException; -import org.apache.commons.imaging.common.AllocationChecker; +import org.apache.commons.imaging.common.Allocator; import org.apache.commons.imaging.internal.Debug; public class MedianCutQuantizer { @@ -57,7 +57,7 @@ public class MedianCutQuantizer { final int width = image.getWidth(); final int height = image.getHeight(); - final int[] row = new int[AllocationChecker.check(width)]; + final int[] row = Allocator.intArray(width); for (int y = 0; y < height; y++) { image.getRGB(0, y, width, 1, row, 0, width); for (int x = 0; x < width; x++) { @@ -92,7 +92,7 @@ public class MedianCutQuantizer { if (discreteColors <= maxColors) { Debug.debug("lossless palette: " + discreteColors); - final int[] palette = new int[AllocationChecker.check(discreteColors)]; + final int[] palette = Allocator.intArray(discreteColors); final List<ColorCount> colorCounts = new ArrayList<>( colorMap.values()); @@ -122,7 +122,7 @@ public class MedianCutQuantizer { final int paletteSize = colorGroups.size(); Debug.debug("palette size: " + paletteSize); - final int[] palette = new int[AllocationChecker.check(paletteSize)]; + final int[] palette = Allocator.intArray(paletteSize); for (int i = 0; i < colorGroups.size(); i++) { final ColorGroup colorGroup = colorGroups.get(i); diff --git a/src/main/java/org/apache/commons/imaging/palette/PaletteFactory.java b/src/main/java/org/apache/commons/imaging/palette/PaletteFactory.java index 74fa9a0f..1e247674 100644 --- a/src/main/java/org/apache/commons/imaging/palette/PaletteFactory.java +++ b/src/main/java/org/apache/commons/imaging/palette/PaletteFactory.java @@ -28,7 +28,7 @@ import java.util.logging.Level; import java.util.logging.Logger; import org.apache.commons.imaging.ImageWriteException; -import org.apache.commons.imaging.common.AllocationChecker; +import org.apache.commons.imaging.common.Allocator; /** * Factory for creating palettes. @@ -337,7 +337,7 @@ public class PaletteFactory { public Palette makeExactRgbPaletteFancy(final BufferedImage src) { // map what rgb values have been used - final byte[] rgbmap = new byte[AllocationChecker.check(256 * 256 * 32)]; + final byte[] rgbmap = Allocator.byteArray(256 * 256 * 32); final int width = src.getWidth(); final int height = src.getHeight(); @@ -362,7 +362,7 @@ public class PaletteFactory { LOGGER.finest("Used colors: " + count); } - final int[] colormap = new int[AllocationChecker.check(count)]; + final int[] colormap = Allocator.intArray(count); int mapsize = 0; for (int i = 0; i < rgbmap.length; i++) { final int eight = 0xff & rgbmap[i]; @@ -408,7 +408,7 @@ public class PaletteFactory { } } - final int[] result = new int[AllocationChecker.check(rgbs.size())]; + final int[] result = Allocator.intArray(rgbs.size()); int next = 0; for (final int rgb : rgbs) { result[next++] = rgb; @@ -448,7 +448,7 @@ public class PaletteFactory { final int tableScale = precision * COMPONENTS; final int tableSize = 1 << tableScale; - final int[] table = new int[AllocationChecker.check(tableSize)]; + final int[] table = Allocator.intArray(tableSize); final int width = src.getWidth(); final int height = src.getHeight(); diff --git a/src/main/java/org/apache/commons/imaging/palette/QuantizedPalette.java b/src/main/java/org/apache/commons/imaging/palette/QuantizedPalette.java index bcab31a3..4595bf29 100644 --- a/src/main/java/org/apache/commons/imaging/palette/QuantizedPalette.java +++ b/src/main/java/org/apache/commons/imaging/palette/QuantizedPalette.java @@ -20,7 +20,7 @@ import java.util.Collections; import java.util.List; import org.apache.commons.imaging.ImageWriteException; -import org.apache.commons.imaging.common.AllocationChecker; +import org.apache.commons.imaging.common.Allocator; public class QuantizedPalette implements Palette { @@ -32,7 +32,7 @@ public class QuantizedPalette implements Palette { this.subsets = subsets == null ? Collections.emptyList() : Collections.unmodifiableList(subsets); this.precision = precision; - straight = new ColorSpaceSubset[AllocationChecker.check(1 << (precision * 3))]; + straight = Allocator.array(1 << (precision * 3), ColorSpaceSubset[]::new); for (int i = 0; i < this.subsets.size(); i++) { final ColorSpaceSubset subset = subsets.get(i);