You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@commons.apache.org by gg...@apache.org on 2023/06/19 19:44:43 UTC

[commons-imaging] 01/02: Reuse Commons IO

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

commit a11d26b850c22e7516f3dd0916eac7989085951e
Author: Gary Gregory <ga...@gmail.com>
AuthorDate: Mon Jun 19 15:39:28 2023 -0400

    Reuse Commons IO
---
 .../commons/imaging/bytesource/ByteSource.java     | 37 +++++++++----
 .../imaging/bytesource/ByteSourceArray.java        | 63 ----------------------
 .../commons/imaging/bytesource/ByteSourceFile.java | 61 ---------------------
 .../imaging/bytesource/ByteSourceInputStream.java  |  3 +-
 .../commons/imaging/bytesource/ByteSourceTest.java |  3 +-
 5 files changed, 30 insertions(+), 137 deletions(-)

diff --git a/src/main/java/org/apache/commons/imaging/bytesource/ByteSource.java b/src/main/java/org/apache/commons/imaging/bytesource/ByteSource.java
index 1ac1fc82..c1b3fa5a 100644
--- a/src/main/java/org/apache/commons/imaging/bytesource/ByteSource.java
+++ b/src/main/java/org/apache/commons/imaging/bytesource/ByteSource.java
@@ -19,21 +19,25 @@ package org.apache.commons.imaging.bytesource;
 import java.io.File;
 import java.io.IOException;
 import java.io.InputStream;
+import java.util.Arrays;
 
 import org.apache.commons.imaging.common.BinaryFunctions;
+import org.apache.commons.io.build.AbstractOrigin;
+import org.apache.commons.io.build.AbstractOrigin.ByteArrayOrigin;
+import org.apache.commons.io.build.AbstractOrigin.FileOrigin;
 
-public abstract class ByteSource {
+public class ByteSource {
 
     public static ByteSource array(final byte[] array) {
-        return new ByteSourceArray(array, null);
+        return new ByteSource(new ByteArrayOrigin(array), null);
     }
 
     public static ByteSource array(final byte[] array, final String name) {
-        return new ByteSourceArray(array, name);
+        return new ByteSource(new ByteArrayOrigin(array), name);
     }
 
     public static ByteSource file(final File file) {
-        return new ByteSourceFile(file);
+        return new ByteSource(new FileOrigin(file), file.getName());
     }
 
     public static ByteSource inputStream(final InputStream is, final String name) {
@@ -41,18 +45,30 @@ public abstract class ByteSource {
     }
 
     private final String fileName;
+    private final AbstractOrigin<?, ?> origin;
 
-    public ByteSource(final String fileName) {
+    public ByteSource(final AbstractOrigin<?, ?> origin, final String fileName) {
         this.fileName = fileName;
+        this.origin = origin;
     }
 
-    public abstract byte[] getByteArray(long from, int length) throws IOException;
+    public byte[] getByteArray(final long position, final int length) throws IOException {
+        final byte[] bytes = origin.getByteArray();
+        // Checks for int overflow.
+        final int start = Math.toIntExact(position);
+        if (start < 0 || length < 0 || start + length < 0 || start + length > bytes.length) {
+            throw new IllegalArgumentException("Couldn't read array (start: " + start + ", length: " + length + ", data length: " + bytes.length + ").");
+        }
+        return Arrays.copyOfRange(bytes, start, start + length);
+    }
 
     public final String getFileName() {
         return fileName;
     }
 
-    public abstract InputStream getInputStream() throws IOException;
+    public InputStream getInputStream() throws IOException {
+        return origin.getInputStream();
+    }
 
     public final InputStream getInputStream(final long start) throws IOException {
         InputStream is = null;
@@ -70,13 +86,14 @@ public abstract class ByteSource {
     }
 
     /**
-     * This operation can be VERY expensive; for InputStream byte sources, the
-     * entire stream must be drained to determine its length.
+     * This operation can be VERY expensive; for InputStream byte sources, the entire stream must be drained to determine its length.
      *
      * @return the byte source length
      * @throws IOException if it fails to read the byte source data
      */
-    public abstract long size() throws IOException;
+    public long size() throws IOException {
+        return origin.getByteArray().length;
+    }
 
     @Override
     public String toString() {
diff --git a/src/main/java/org/apache/commons/imaging/bytesource/ByteSourceArray.java b/src/main/java/org/apache/commons/imaging/bytesource/ByteSourceArray.java
deleted file mode 100644
index 346dee4f..00000000
--- a/src/main/java/org/apache/commons/imaging/bytesource/ByteSourceArray.java
+++ /dev/null
@@ -1,63 +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.bytesource;
-
-import java.io.ByteArrayInputStream;
-import java.io.InputStream;
-import java.util.Arrays;
-
-import org.apache.commons.imaging.ImagingException;
-
-class ByteSourceArray extends ByteSource {
-
-    private final byte[] bytes;
-
-    ByteSourceArray(final byte[] bytes, final String fileName) {
-        super(fileName);
-        this.bytes = bytes;
-    }
-
-    @Override
-    public byte[] getByteArray(final long from, final int length) throws ImagingException {
-        int start;
-        try {
-            start = Math.toIntExact(from);
-        } catch (final ArithmeticException e) {
-            throw new ImagingException(e);
-        }
-        // We include a separate check for int overflow.
-        if ((start < 0) || (length < 0) || (start + length < 0)
-                || (start + length > bytes.length)) {
-            throw new ImagingException("Could not read block (block start: " + start
-                    + ", block length: " + length + ", data length: "
-                    + bytes.length + ").");
-        }
-
-        return Arrays.copyOfRange(bytes, start, start + length);
-    }
-
-    @Override
-    public InputStream getInputStream() {
-        return new ByteArrayInputStream(bytes);
-    }
-
-    @Override
-    public long size() {
-        return bytes.length;
-    }
-
-}
diff --git a/src/main/java/org/apache/commons/imaging/bytesource/ByteSourceFile.java b/src/main/java/org/apache/commons/imaging/bytesource/ByteSourceFile.java
deleted file mode 100644
index 6bde65c8..00000000
--- a/src/main/java/org/apache/commons/imaging/bytesource/ByteSourceFile.java
+++ /dev/null
@@ -1,61 +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.bytesource;
-
-import java.io.BufferedInputStream;
-import java.io.File;
-import java.io.IOException;
-import java.io.InputStream;
-import java.io.RandomAccessFile;
-import java.nio.file.Files;
-
-import org.apache.commons.imaging.ImagingException;
-import org.apache.commons.imaging.common.BinaryFunctions;
-import org.apache.commons.io.RandomAccessFileMode;
-
-class ByteSourceFile extends ByteSource {
-    private final File file;
-
-    ByteSourceFile(final File file) {
-        super(file.getName());
-        this.file = file;
-    }
-
-    @Override
-    public byte[] getByteArray(final long from, final int length) throws IOException {
-        try (RandomAccessFile raf = RandomAccessFileMode.READ_ONLY.create(file)) {
-            // We include a separate check for int overflow.
-            if ((from < 0) || (length < 0) || (from + length < 0) || (from + length > raf.length())) {
-                throw new ImagingException(
-                        "Could not read block (block start: " + from + ", block length: " + length + ", data length: " + raf.length() + ").");
-            }
-
-            return BinaryFunctions.getRAFBytes(raf, from, length, "Could not read value from file");
-        }
-    }
-
-    @Override
-    public InputStream getInputStream() throws IOException {
-        return new BufferedInputStream(Files.newInputStream(file.toPath()));
-    }
-
-    @Override
-    public long size() {
-        return file.length();
-    }
-
-}
diff --git a/src/main/java/org/apache/commons/imaging/bytesource/ByteSourceInputStream.java b/src/main/java/org/apache/commons/imaging/bytesource/ByteSourceInputStream.java
index 7441fd21..fe835921 100644
--- a/src/main/java/org/apache/commons/imaging/bytesource/ByteSourceInputStream.java
+++ b/src/main/java/org/apache/commons/imaging/bytesource/ByteSourceInputStream.java
@@ -26,6 +26,7 @@ import org.apache.commons.imaging.ImagingException;
 import org.apache.commons.imaging.common.Allocator;
 import org.apache.commons.imaging.common.BinaryFunctions;
 import org.apache.commons.io.IOUtils;
+import org.apache.commons.io.build.AbstractOrigin.InputStreamOrigin;
 
 class ByteSourceInputStream extends ByteSource {
 
@@ -176,7 +177,7 @@ class ByteSourceInputStream extends ByteSource {
     private long streamLength = -1;
 
     ByteSourceInputStream(final InputStream is, final String fileName) {
-        super(fileName);
+        super(new InputStreamOrigin(is), fileName);
         this.is = new BufferedInputStream(is);
     }
 
diff --git a/src/test/java/org/apache/commons/imaging/bytesource/ByteSourceTest.java b/src/test/java/org/apache/commons/imaging/bytesource/ByteSourceTest.java
index 0bf443f1..87546829 100644
--- a/src/test/java/org/apache/commons/imaging/bytesource/ByteSourceTest.java
+++ b/src/test/java/org/apache/commons/imaging/bytesource/ByteSourceTest.java
@@ -71,8 +71,7 @@ public abstract class ByteSourceTest extends ImagingTest {
 
     @Test
     public void testGetInputStreamThrowsNullPointerException() {
-        final ByteSource byteSourceArray = ByteSource.array(null);
-        Assertions.assertThrows(NullPointerException.class, () -> byteSourceArray.getInputStream(0L));
+        Assertions.assertThrows(NullPointerException.class, () -> ByteSource.array(null));
     }
 
 }