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:44 UTC

[commons-imaging] 02/02: Better internal names

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 375b404ee13ded8df2c65499c2a46f334a2fdf3b
Author: Gary Gregory <ga...@gmail.com>
AuthorDate: Mon Jun 19 15:44:38 2023 -0400

    Better internal names
---
 .../commons/imaging/bytesource/ByteSource.java     |  2 +-
 ...InputStream.java => InputStreamByteSource.java} | 60 ++++++++++------------
 .../bytesource/ByteSourceInputStreamTest.java      |  2 +-
 .../examples/ApacheImagingSpeedAndMemoryTest.java  |  4 +-
 4 files changed, 32 insertions(+), 36 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 c1b3fa5a..3d2e5d55 100644
--- a/src/main/java/org/apache/commons/imaging/bytesource/ByteSource.java
+++ b/src/main/java/org/apache/commons/imaging/bytesource/ByteSource.java
@@ -41,7 +41,7 @@ public class ByteSource {
     }
 
     public static ByteSource inputStream(final InputStream is, final String name) {
-        return new ByteSourceInputStream(is, name);
+        return new InputStreamByteSource(is, name);
     }
 
     private final String fileName;
diff --git a/src/main/java/org/apache/commons/imaging/bytesource/ByteSourceInputStream.java b/src/main/java/org/apache/commons/imaging/bytesource/InputStreamByteSource.java
similarity index 78%
rename from src/main/java/org/apache/commons/imaging/bytesource/ByteSourceInputStream.java
rename to src/main/java/org/apache/commons/imaging/bytesource/InputStreamByteSource.java
index fe835921..f999e320 100644
--- a/src/main/java/org/apache/commons/imaging/bytesource/ByteSourceInputStream.java
+++ b/src/main/java/org/apache/commons/imaging/bytesource/InputStreamByteSource.java
@@ -9,7 +9,7 @@
  *      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,
+ * distributed under the License inputStream 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.
@@ -28,19 +28,19 @@ 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 {
+final class InputStreamByteSource extends ByteSource {
 
-    private class CacheBlock {
+    private final class Block {
 
         public final byte[] bytes;
-        private CacheBlock next;
+        private Block next;
         private boolean triedNext;
 
-        CacheBlock(final byte[] bytes) {
+        Block(final byte[] bytes) {
             this.bytes = bytes;
         }
 
-        public CacheBlock getNext() throws IOException {
+        public Block getNext() throws IOException {
             if (null != next) {
                 return next;
             }
@@ -54,8 +54,8 @@ class ByteSourceInputStream extends ByteSource {
 
     }
 
-    private class CachingInputStream extends InputStream {
-        private CacheBlock block;
+    private final class BlockInputStream extends InputStream {
+        private Block block;
         private boolean readFirst;
         private int blockIndex;
 
@@ -89,8 +89,7 @@ class ByteSourceInputStream extends ByteSource {
         public int read(final byte[] array, final int off, final int len) throws IOException {
             // first section copied verbatim from InputStream
             Objects.requireNonNull(array, "array");
-            if ((off < 0) || (off > array.length) || (len < 0)
-                    || ((off + len) > array.length) || ((off + len) < 0)) {
+            if ((off < 0) || (off > array.length) || (len < 0) || ((off + len) > array.length) || ((off + len) < 0)) {
                 throw new IndexOutOfBoundsException();
             }
             if (len == 0) {
@@ -171,29 +170,26 @@ class ByteSourceInputStream extends ByteSource {
     }
 
     private static final int BLOCK_SIZE = 1024;
-    private final InputStream is;
-    private CacheBlock cacheHead;
+    private final InputStream inputStream;
+    private Block headBlock;
     private byte[] readBuffer;
     private long streamLength = -1;
 
-    ByteSourceInputStream(final InputStream is, final String fileName) {
-        super(new InputStreamOrigin(is), fileName);
-        this.is = new BufferedInputStream(is);
+    InputStreamByteSource(final InputStream inputStream, final String fileName) {
+        super(new InputStreamOrigin(inputStream), fileName);
+        this.inputStream = new BufferedInputStream(inputStream);
     }
 
     @Override
-    public byte[] getByteArray(final long from, final int length) throws IOException {
+    public byte[] getByteArray(final long position, final int length) throws IOException {
         // We include a separate check for int overflow.
-        if ((from < 0) || (length < 0)
-                || (from + length < 0)
-                || (from + length > size())) {
-            throw new ImagingException("Could not read block (block start: "
-                    + from + ", block length: " + length
-                    + ", data length: " + streamLength + ").");
+        if ((position < 0) || (length < 0) || (position + length < 0) || (position + length > size())) {
+            throw new ImagingException(
+                    "Could not read block (block start: " + position + ", block length: " + length + ", data length: " + streamLength + ").");
         }
 
         final InputStream cis = getInputStream();
-        BinaryFunctions.skipBytes(cis, from);
+        BinaryFunctions.skipBytes(cis, position);
 
         final byte[] bytes = Allocator.byteArray(length);
         int total = 0;
@@ -209,35 +205,35 @@ class ByteSourceInputStream extends ByteSource {
         }
     }
 
-    private CacheBlock getFirstBlock() throws IOException {
-        if (null == cacheHead) {
-            cacheHead = readBlock();
+    private Block getFirstBlock() throws IOException {
+        if (null == headBlock) {
+            headBlock = readBlock();
         }
-        return cacheHead;
+        return headBlock;
     }
 
     @Override
     public InputStream getInputStream() throws IOException {
-        return new CachingInputStream();
+        return new BlockInputStream();
     }
 
-    private CacheBlock readBlock() throws IOException {
+    private Block readBlock() throws IOException {
         if (null == readBuffer) {
             readBuffer = new byte[BLOCK_SIZE];
         }
 
-        final int read = is.read(readBuffer);
+        final int read = inputStream.read(readBuffer);
         if (read < 1) {
             return null;
         }
         if (read < BLOCK_SIZE) {
             // return a copy.
-            return new CacheBlock(Arrays.copyOf(readBuffer, read));
+            return new Block(Arrays.copyOf(readBuffer, read));
         }
         // return current buffer.
         final byte[] result = readBuffer;
         readBuffer = null;
-        return new CacheBlock(result);
+        return new Block(result);
     }
 
     @Override
diff --git a/src/test/java/org/apache/commons/imaging/bytesource/ByteSourceInputStreamTest.java b/src/test/java/org/apache/commons/imaging/bytesource/ByteSourceInputStreamTest.java
index 7c124acf..71c2fd4f 100644
--- a/src/test/java/org/apache/commons/imaging/bytesource/ByteSourceInputStreamTest.java
+++ b/src/test/java/org/apache/commons/imaging/bytesource/ByteSourceInputStreamTest.java
@@ -42,7 +42,7 @@ class ByteSourceInputStreamTest {
         final String imagePath = FilenameUtils.separatorsToSystem(ICO_IMAGE_FILE);
         final File imageFile = new File(ImagingTestConstants.TEST_IMAGE_FOLDER, imagePath);
         try(BufferedInputStream imageStream = new BufferedInputStream(new FileInputStream(imageFile))) {
-            // ByteSourceInputStream is created inside of following method
+            // InputStreamByteSource is created inside of following method
             final BufferedImage bufferedImage = Imaging.getBufferedImage(imageStream, ICO_IMAGE_FILE);
 
             assertEquals(bufferedImage.getWidth(), ICO_IMAGE_WIDTH);
diff --git a/src/test/java/org/apache/commons/imaging/examples/ApacheImagingSpeedAndMemoryTest.java b/src/test/java/org/apache/commons/imaging/examples/ApacheImagingSpeedAndMemoryTest.java
index 350ca098..19a5688d 100644
--- a/src/test/java/org/apache/commons/imaging/examples/ApacheImagingSpeedAndMemoryTest.java
+++ b/src/test/java/org/apache/commons/imaging/examples/ApacheImagingSpeedAndMemoryTest.java
@@ -192,11 +192,11 @@ public class ApacheImagingSpeedAndMemoryTest {
                 ByteSource byteSource = ByteSource.file(target);
                 // This test code allows you to test cases where the
                 // input is processed using Apache Imaging's
-                // ByteSourceInputStream rather than the ByteSourceFile.
+                // InputStreamByteSource rather than the ByteSourceFile.
                 // You might also want to experiment with ByteSourceArray.
                 // FileInputStream fins = new FileInputStream(target);
                 // BufferedInputStream bins = new BufferedInputStream(fins);
-                // ByteSourceInputStream byteSource =
+                // InputStreamByteSource byteSource =
                 // ByteSource.inputStream(bins, target.getName());
                 // ready the parser (you may modify this code block
                 // to use your parser of choice)