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 b15e99bc Reuse Allocator
b15e99bc is described below
commit b15e99bcb7fee887555586bee36fba1e030df52e
Author: Gary Gregory <[email protected]>
AuthorDate: Sat May 13 15:54:45 2023 -0400
Reuse Allocator
---
.../apache/commons/imaging/common/Allocator.java | 32 +++++++++++++++++++++-
.../apache/commons/imaging/common/ZlibDeflate.java | 2 +-
.../imaging/common/mylzw/MyLzwCompressor.java | 4 ++-
.../imaging/formats/dcx/DcxImageParser.java | 3 +-
.../imaging/formats/gif/GifImageParser.java | 6 ++--
.../imaging/formats/ico/IcoImageParser.java | 5 ++--
.../imaging/formats/jpeg/JpegImageParser.java | 2 +-
.../imaging/formats/png/PngImageParser.java | 7 +++--
.../imaging/formats/tiff/TiffDirectory.java | 3 +-
.../imaging/formats/tiff/TiffImageParser.java | 3 +-
10 files changed, 51 insertions(+), 16 deletions(-)
diff --git a/src/main/java/org/apache/commons/imaging/common/Allocator.java
b/src/main/java/org/apache/commons/imaging/common/Allocator.java
index 26a50038..873250e9 100644
--- a/src/main/java/org/apache/commons/imaging/common/Allocator.java
+++ b/src/main/java/org/apache/commons/imaging/common/Allocator.java
@@ -17,6 +17,7 @@
package org.apache.commons.imaging.common;
+import java.util.ArrayList;
import java.util.function.IntFunction;
/**
@@ -35,7 +36,21 @@ public class Allocator {
}
/**
- * Allocates an Object array of type T of the requested size.
+ * Allocates an Object 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 apply(final int request, final IntFunction<T> factory)
{
+ return factory.apply(check(request));
+ }
+
+ /**
+ * Allocates an array of type T of the requested size.
*
* @param <T> The return array type
* @param request The requested size.
@@ -45,9 +60,24 @@ public class Allocator {
* @see #check(int)
*/
public static <T> T[] array(final int request, final IntFunction<T[]>
factory) {
+ // We could pass in a shallow object byte size to multiply with the
request.
+ // The shallow byte size would need to be computed and hard-coded in
statics.
return factory.apply(check(request));
}
+ /**
+ * Allocates an Object array of type T of the requested size.
+ *
+ * @param <T> The return array type
+ * @param request The requested size.
+ * @return a new byte array.
+ * @throws AllocationRequestException Thrown when the request exceeds the
limit.
+ * @see #check(int)
+ */
+ public static <T> ArrayList<T> arrayList(final int request) {
+ return apply(request, ArrayList::new);
+ }
+
/**
* Allocates a byte array of the requested size.
*
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 f2180fe7..68b72457 100644
--- a/src/main/java/org/apache/commons/imaging/common/ZlibDeflate.java
+++ b/src/main/java/org/apache/commons/imaging/common/ZlibDeflate.java
@@ -48,7 +48,7 @@ public class ZlibDeflate {
* @see DeflaterOutputStream
*/
public static byte[] compress(final byte[] bytes) throws
ImageWriteException {
- final ByteArrayOutputStream out = new
ByteArrayOutputStream(bytes.length / 2);
+ final ByteArrayOutputStream out = new
ByteArrayOutputStream(Allocator.check(bytes.length / 2));
try (DeflaterOutputStream compressOut = new DeflaterOutputStream(out))
{
compressOut.write(bytes);
} catch (final IOException e) {
diff --git
a/src/main/java/org/apache/commons/imaging/common/mylzw/MyLzwCompressor.java
b/src/main/java/org/apache/commons/imaging/common/mylzw/MyLzwCompressor.java
index c0edebfa..91b2aa42 100644
--- a/src/main/java/org/apache/commons/imaging/common/mylzw/MyLzwCompressor.java
+++ b/src/main/java/org/apache/commons/imaging/common/mylzw/MyLzwCompressor.java
@@ -22,6 +22,8 @@ import java.nio.ByteOrder;
import java.util.HashMap;
import java.util.Map;
+import org.apache.commons.imaging.common.Allocator;
+
public class MyLzwCompressor {
private static final class ByteArray {
private final byte[] bytes;
@@ -173,7 +175,7 @@ public class MyLzwCompressor {
}
public byte[] compress(final byte[] bytes) throws IOException {
- final ByteArrayOutputStream baos = new
ByteArrayOutputStream(bytes.length);
+ final ByteArrayOutputStream baos = new
ByteArrayOutputStream(Allocator.check(bytes.length));
final MyBitOutputStream bos = new MyBitOutputStream(baos, byteOrder);
initializeStringTable();
diff --git
a/src/main/java/org/apache/commons/imaging/formats/dcx/DcxImageParser.java
b/src/main/java/org/apache/commons/imaging/formats/dcx/DcxImageParser.java
index 630b6dac..d83a8599 100644
--- a/src/main/java/org/apache/commons/imaging/formats/dcx/DcxImageParser.java
+++ b/src/main/java/org/apache/commons/imaging/formats/dcx/DcxImageParser.java
@@ -34,6 +34,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.Allocator;
import org.apache.commons.imaging.common.BinaryOutputStream;
import org.apache.commons.imaging.common.ImageMetadata;
import org.apache.commons.imaging.common.bytesource.ByteSource;
@@ -158,7 +159,7 @@ public class DcxImageParser extends
ImageParser<PcxImagingParameters> {
throws ImageReadException, IOException {
try (InputStream is = byteSource.getInputStream()) {
final int id = read4Bytes("Id", is, "Not a Valid DCX File",
getByteOrder());
- final List<Long> pageTable = new ArrayList<>(1024);
+ final List<Long> pageTable = Allocator.arrayList(1024);
for (int i = 0; i < 1024; i++) {
final long pageOffset = 0xFFFFffffL & read4Bytes("PageTable",
is, "Not a Valid DCX File", getByteOrder());
if (pageOffset == 0) {
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 4359f5cf..cfdf5546 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
@@ -177,7 +177,7 @@ public class GifImageParser extends
ImageParser<GifImagingParameters> implements
throw new ImageReadException("GIF: Invalid amount of Graphic
Control Extensions");
}
- final List<GifImageData> imageData = new
ArrayList<>(descriptors.size());
+ final List<GifImageData> imageData =
Allocator.arrayList(descriptors.size());
for(int i = 0; i < descriptors.size(); i++) {
final ImageDescriptor descriptor = descriptors.get(i);
if (descriptor == null) {
@@ -237,7 +237,7 @@ public class GifImageParser extends
ImageParser<GifImagingParameters> implements
}
final List<GifImageData> imageData = findAllImageData(imageContents);
- final List<BufferedImage> result = new ArrayList<>(imageData.size());
+ final List<BufferedImage> result =
Allocator.arrayList(imageData.size());
for(final GifImageData id : imageData) {
result.add(getBufferedImage(ghi, id,
imageContents.globalColorTable));
}
@@ -492,7 +492,7 @@ public class GifImageParser extends
ImageParser<GifImagingParameters> implements
}
final List<GifImageData> imageData = findAllImageData(imageContents);
- final List<GifImageMetadataItem> metadataItems = new
ArrayList<>(imageData.size());
+ final List<GifImageMetadataItem> metadataItems =
Allocator.arrayList(imageData.size());
for(final GifImageData id : imageData) {
final DisposalMethod disposalMethod =
createDisposalMethodFromIntValue(id.gce.dispose);
metadataItems.add(new GifImageMetadataItem(id.gce.delay,
id.descriptor.imageLeftPosition, id.descriptor.imageTopPosition,
disposalMethod));
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 00df52b6..f650ce80 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
@@ -30,7 +30,6 @@ import java.io.InputStream;
import java.io.OutputStream;
import java.io.PrintWriter;
import java.nio.ByteOrder;
-import java.util.ArrayList;
import java.util.List;
import org.apache.commons.imaging.ImageFormat;
@@ -265,7 +264,7 @@ public class IcoImageParser extends
ImageParser<IcoImagingParameters> {
final ImageContents contents = readImage(byteSource);
final FileHeader fileHeader = contents.fileHeader;
- final List<BufferedImage> result = new
ArrayList<>(fileHeader.iconCount);
+ final List<BufferedImage> result =
Allocator.arrayList(fileHeader.iconCount);
for (int i = 0; i < fileHeader.iconCount; i++) {
result.add(contents.iconDatas[i].readBufferedImage());
}
@@ -437,7 +436,7 @@ public class IcoImageParser extends
ImageParser<IcoImagingParameters> {
: colorsUsed);
final int bitmapSize = 14 + 56 + restOfFile.length;
- final ByteArrayOutputStream baos = new
ByteArrayOutputStream(bitmapSize);
+ final ByteArrayOutputStream baos = new
ByteArrayOutputStream(Allocator.check(bitmapSize));
try (BinaryOutputStream bos = BinaryOutputStream.littleEndian(baos)) {
bos.write('B');
bos.write('M');
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 8ca31d3e..4dad4ba4 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
@@ -464,7 +464,7 @@ public class JpegImageParser extends
ImageParser<JpegImagingParameters> implemen
final List<Segment> commentSegments = readSegments(byteSource,
new int[] { JpegConstants.COM_MARKER}, false);
- final List<String> comments = new ArrayList<>(commentSegments.size());
+ final List<String> comments =
Allocator.arrayList(commentSegments.size());
for (final Segment commentSegment : commentSegments) {
final ComSegment comSegment = (ComSegment) commentSegment;
comments.add(new String(comSegment.getComment(),
StandardCharsets.UTF_8));
diff --git
a/src/main/java/org/apache/commons/imaging/formats/png/PngImageParser.java
b/src/main/java/org/apache/commons/imaging/formats/png/PngImageParser.java
index 0ea28510..39a60f4d 100644
--- a/src/main/java/org/apache/commons/imaging/formats/png/PngImageParser.java
+++ b/src/main/java/org/apache/commons/imaging/formats/png/PngImageParser.java
@@ -47,6 +47,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.Allocator;
import org.apache.commons.imaging.common.GenericImageMetadata;
import org.apache.commons.imaging.common.ImageMetadata;
import org.apache.commons.imaging.common.XmpEmbeddable;
@@ -335,7 +336,7 @@ public class PngImageParser extends
ImageParser<PngImagingParameters> implement
public List<String> getChunkTypes(final InputStream is)
throws ImageReadException, IOException {
final List<PngChunk> chunks = readChunks(is, null, false);
- final List<String> chunkTypes = new ArrayList<>(chunks.size());
+ final List<String> chunkTypes = Allocator.arrayList(chunks.size());
for (final PngChunk chunk : chunks) {
chunkTypes.add(getChunkTypeName(chunk.chunkType));
}
@@ -442,8 +443,8 @@ public class PngImageParser extends
ImageParser<PngImagingParameters> implement
final List<PngChunk> iTXts = filterChunks(chunks, ChunkType.iTXt);
final int chunkCount = tEXts.size() + zTXts.size() + iTXts.size();
- final List<String> comments = new ArrayList<>(chunkCount);
- final List<PngText> textChunks = new ArrayList<>(chunkCount);
+ final List<String> comments = Allocator.arrayList(chunkCount);
+ final List<PngText> textChunks = Allocator.arrayList(chunkCount);
for (final PngChunk tEXt : tEXts) {
final PngChunkText pngChunktEXt = (PngChunkText) tEXt;
diff --git
a/src/main/java/org/apache/commons/imaging/formats/tiff/TiffDirectory.java
b/src/main/java/org/apache/commons/imaging/formats/tiff/TiffDirectory.java
index 6ade7bef..8374ee24 100644
--- a/src/main/java/org/apache/commons/imaging/formats/tiff/TiffDirectory.java
+++ b/src/main/java/org/apache/commons/imaging/formats/tiff/TiffDirectory.java
@@ -24,6 +24,7 @@ import java.util.Collections;
import java.util.List;
import org.apache.commons.imaging.ImageReadException;
+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.constants.TiffConstants;
@@ -727,7 +728,7 @@ public class TiffDirectory extends TiffElement {
+ ") != byteCounts.length(" + byteCounts.length + ")");
}
- final List<ImageDataElement> result = new ArrayList<>(offsets.length);
+ final List<ImageDataElement> result =
Allocator.arrayList(offsets.length);
for (int i = 0; i < offsets.length; i++) {
result.add(new ImageDataElement(offsets[i], byteCounts[i]));
}
diff --git
a/src/main/java/org/apache/commons/imaging/formats/tiff/TiffImageParser.java
b/src/main/java/org/apache/commons/imaging/formats/tiff/TiffImageParser.java
index cfd7aa79..a1e83a6b 100644
--- a/src/main/java/org/apache/commons/imaging/formats/tiff/TiffImageParser.java
+++ b/src/main/java/org/apache/commons/imaging/formats/tiff/TiffImageParser.java
@@ -43,6 +43,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.Allocator;
import org.apache.commons.imaging.common.ImageBuilder;
import org.apache.commons.imaging.common.ImageMetadata;
import org.apache.commons.imaging.common.XmpEmbeddable;
@@ -520,7 +521,7 @@ public class TiffImageParser extends
ImageParser<TiffImagingParameters> implemen
// dunno if this handles colormapped images correctly.
final List<TiffField> entries = directory.entries;
- final List<String> comments = new ArrayList<>(entries.size());
+ final List<String> comments = Allocator.arrayList(entries.size());
for (final TiffField field : entries) {
final String comment = field.toString();
comments.add(comment);