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/03/21 17:40:06 UTC

[commons-imaging] branch master updated: Refactor BinaryOutputStream

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 73e4b778 Refactor BinaryOutputStream
73e4b778 is described below

commit 73e4b77809d5d9736b0f4cab916594a00b9a8bf0
Author: Gary Gregory <ga...@gmail.com>
AuthorDate: Tue Mar 21 13:40:01 2023 -0400

    Refactor BinaryOutputStream
    
    Do not test byte order on every write[2|3|4]() call
---
 .../common/BigEndianBinaryOutputStream.java        |  50 ++++++++++
 .../commons/imaging/common/BinaryOutputStream.java | 101 ++++++++-------------
 .../common/LittleEndianBinaryOutputStream.java     |  50 ++++++++++
 .../imaging/formats/bmp/BmpImageParser.java        |   2 +-
 .../imaging/formats/dcx/DcxImageParser.java        |   3 +-
 .../imaging/formats/gif/GifImageParser.java        |   2 +-
 .../imaging/formats/icns/IcnsImageParser.java      |   2 +-
 .../imaging/formats/ico/IcoImageParser.java        |   4 +-
 .../imaging/formats/jpeg/iptc/IptcParser.java      |   4 +-
 .../commons/imaging/formats/pcx/PcxWriter.java     |   4 +-
 .../tiff/write/TiffImageWriterLossless.java        |   4 +-
 .../formats/tiff/write/TiffImageWriterLossy.java   |   2 +-
 .../imaging/formats/icns/IcnsRoundTripTest.java    |  17 ++--
 .../imaging/formats/ico/IcoRoundtripTest.java      |  37 +++-----
 14 files changed, 174 insertions(+), 108 deletions(-)

diff --git a/src/main/java/org/apache/commons/imaging/common/BigEndianBinaryOutputStream.java b/src/main/java/org/apache/commons/imaging/common/BigEndianBinaryOutputStream.java
new file mode 100644
index 00000000..983c6a14
--- /dev/null
+++ b/src/main/java/org/apache/commons/imaging/common/BigEndianBinaryOutputStream.java
@@ -0,0 +1,50 @@
+package org.apache.commons.imaging.common;
+
+import java.io.IOException;
+import java.io.OutputStream;
+
+/*
+ * 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 regardingnership.
+ * The ASF licenses this file to You under the Apac copyright owhe 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.
+ */
+
+final class BigEndianBinaryOutputStream extends BinaryOutputStream {
+
+    BigEndianBinaryOutputStream(OutputStream os) {
+        super(os);
+    }
+
+    @Override
+    public void write2Bytes(final int value) throws IOException {
+        write(0xff & (value >> 8));
+        write(0xff & value);
+    }
+
+    @Override
+    public void write3Bytes(final int value) throws IOException {
+        write(0xff & (value >> 16));
+        write(0xff & (value >> 8));
+        write(0xff & value);
+    }
+
+    @Override
+    public void write4Bytes(final int value) throws IOException {
+        write(0xff & (value >> 24));
+        write(0xff & (value >> 16));
+        write(0xff & (value >> 8));
+        write(0xff & value);
+    }
+
+}
diff --git a/src/main/java/org/apache/commons/imaging/common/BinaryOutputStream.java b/src/main/java/org/apache/commons/imaging/common/BinaryOutputStream.java
index 631ddcf5..2a3d10c6 100644
--- a/src/main/java/org/apache/commons/imaging/common/BinaryOutputStream.java
+++ b/src/main/java/org/apache/commons/imaging/common/BinaryOutputStream.java
@@ -1,8 +1,8 @@
 /*
  * 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
+ * this work for additional information regardingnership.
+ * The ASF licenses this file to You under the Apac copyright owhe 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
  *
@@ -20,77 +20,56 @@ import java.io.FilterOutputStream;
 import java.io.IOException;
 import java.io.OutputStream;
 import java.nio.ByteOrder;
+import java.util.Objects;
 
-public class BinaryOutputStream extends FilterOutputStream {
+public abstract class BinaryOutputStream extends FilterOutputStream {
 
-    /** Default byte order for Java, many file formats. */
-    private ByteOrder byteOrder = ByteOrder.BIG_ENDIAN;
-
-    public BinaryOutputStream(final OutputStream os, final ByteOrder byteOrder) {
-        super(os);
-        this.byteOrder = byteOrder;
-    }
-
-    public BinaryOutputStream(final OutputStream os) {
-        super(os);
+    public static BigEndianBinaryOutputStream bigEndian(final OutputStream outputStream) {
+        return new BigEndianBinaryOutputStream(outputStream);
     }
 
-    protected void setByteOrder(final ByteOrder byteOrder) {
-        this.byteOrder = byteOrder;
-    }
-
-    public ByteOrder getByteOrder() {
-        return byteOrder;
+    @SuppressWarnings("resource")
+    public static BinaryOutputStream create(final OutputStream outputStream, final ByteOrder byteOrder) {
+        Objects.requireNonNull(outputStream, "outputStream");
+        Objects.requireNonNull(byteOrder, "byteOrder");
+        if (byteOrder == ByteOrder.LITTLE_ENDIAN) {
+            return littleEndian(outputStream);
+        } else if (byteOrder == ByteOrder.BIG_ENDIAN) {
+            return bigEndian(outputStream);
+        }
+        throw new UnsupportedOperationException(byteOrder.toString());
     }
 
-    @Override
-    public void write(final int i) throws IOException {
-        super.write(i);
+    public static LittleEndianBinaryOutputStream littleEndian(final OutputStream outputStream) {
+        return new LittleEndianBinaryOutputStream(outputStream);
     }
 
-    @Override
-    public final void write(final byte[] bytes) throws IOException {
-        super.write(bytes, 0, bytes.length);
+    public BinaryOutputStream(final OutputStream outputStream) {
+        super(outputStream);
     }
 
-    @Override
-    public final void write(final byte[] bytes, final int offset, final int length) throws IOException {
-        super.write(bytes, offset, length);
+    public BinaryOutputStream(final OutputStream outputStream, final ByteOrder byteOrder) {
+        super(outputStream);
     }
 
-    public final void write4Bytes(final int value) throws IOException {
-        if (byteOrder == ByteOrder.BIG_ENDIAN) {
-            write(0xff & (value >> 24));
-            write(0xff & (value >> 16));
-            write(0xff & (value >> 8));
-            write(0xff & value);
-        } else {
-            write(0xff & value);
-            write(0xff & (value >> 8));
-            write(0xff & (value >> 16));
-            write(0xff & (value >> 24));
-        }
-    }
+//    @Override
+//    public final void write(final byte[] bytes) throws IOException {
+//        super.write(bytes, 0, bytes.length);
+//    }
+//
+//    @Override
+//    public final void write(final byte[] bytes, final int offset, final int length) throws IOException {
+//        super.write(bytes, offset, length);
+//    }
+//
+//    @Override
+//    public void write(final int i) throws IOException {
+//        super.write(i);
+//    }
+//
+    public abstract void write2Bytes(int value) throws IOException;
 
-    public final void write3Bytes(final int value) throws IOException {
-        if (byteOrder == ByteOrder.BIG_ENDIAN) {
-            write(0xff & (value >> 16));
-            write(0xff & (value >> 8));
-            write(0xff & value);
-        } else {
-            write(0xff & value);
-            write(0xff & (value >> 8));
-            write(0xff & (value >> 16));
-        }
-    }
+    public abstract void write3Bytes(int value) throws IOException;
 
-    public final void write2Bytes(final int value) throws IOException {
-        if (byteOrder == ByteOrder.BIG_ENDIAN) {
-            write(0xff & (value >> 8));
-            write(0xff & value);
-        } else {
-            write(0xff & value);
-            write(0xff & (value >> 8));
-        }
-    }
+    public abstract void write4Bytes(int value) throws IOException;
 }
diff --git a/src/main/java/org/apache/commons/imaging/common/LittleEndianBinaryOutputStream.java b/src/main/java/org/apache/commons/imaging/common/LittleEndianBinaryOutputStream.java
new file mode 100644
index 00000000..b2ad7aa4
--- /dev/null
+++ b/src/main/java/org/apache/commons/imaging/common/LittleEndianBinaryOutputStream.java
@@ -0,0 +1,50 @@
+/*
+ * 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 regardingnership.
+ * The ASF licenses this file to You under the Apac copyright owhe 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.io.IOException;
+import java.io.OutputStream;
+
+final class LittleEndianBinaryOutputStream extends BinaryOutputStream {
+
+    LittleEndianBinaryOutputStream(OutputStream os) {
+        super(os);
+    }
+
+    @Override
+    public void write2Bytes(final int value) throws IOException {
+        write(0xff & value);
+        write(0xff & (value >> 8));
+    }
+
+    @Override
+    public void write3Bytes(final int value) throws IOException {
+        write(0xff & value);
+        write(0xff & (value >> 8));
+        write(0xff & (value >> 16));
+    }
+
+    @Override
+    public void write4Bytes(final int value) throws IOException {
+        write(0xff & value);
+        write(0xff & (value >> 8));
+        write(0xff & (value >> 16));
+        write(0xff & (value >> 24));
+    }
+
+}
diff --git a/src/main/java/org/apache/commons/imaging/formats/bmp/BmpImageParser.java b/src/main/java/org/apache/commons/imaging/formats/bmp/BmpImageParser.java
index 53916e4c..7bc2a1ad 100644
--- a/src/main/java/org/apache/commons/imaging/formats/bmp/BmpImageParser.java
+++ b/src/main/java/org/apache/commons/imaging/formats/bmp/BmpImageParser.java
@@ -667,7 +667,7 @@ public class BmpImageParser extends ImageParser<BmpImagingParameters> {
         }
 
         final byte[] imageData = writer.getImageData(src);
-        final BinaryOutputStream bos = new BinaryOutputStream(os, ByteOrder.LITTLE_ENDIAN);
+        final BinaryOutputStream bos = BinaryOutputStream.littleEndian(os);
 
         // write BitmapFileHeader
         os.write(0x42); // B, Windows 3.1x, 95, NT, Bitmap
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 b5447775..96dce6fd 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
@@ -184,8 +184,7 @@ public class DcxImageParser extends ImageParser<PcxImagingParameters> {
             throws ImageWriteException, IOException {
         final int headerSize = 4 + 1024 * 4;
 
-        final BinaryOutputStream bos = new BinaryOutputStream(os,
-                ByteOrder.LITTLE_ENDIAN);
+        final BinaryOutputStream bos = BinaryOutputStream.littleEndian(os);
         bos.write4Bytes(DcxHeader.DCX_ID);
         // Some apps may need a full 1024 entry table
         bos.write4Bytes(headerSize);
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 36a6678e..acef3872 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
@@ -879,7 +879,7 @@ public class GifImageParser extends ImageParser<GifImagingParameters> implements
         }
         final int paletteSize = palette2.length() + (hasAlpha ? 1 : 0);
 
-        final BinaryOutputStream bos = new BinaryOutputStream(os, ByteOrder.LITTLE_ENDIAN);
+        final BinaryOutputStream bos = BinaryOutputStream.littleEndian(os);
 
         // write Header
         os.write(0x47); // G magic numbers
diff --git a/src/main/java/org/apache/commons/imaging/formats/icns/IcnsImageParser.java b/src/main/java/org/apache/commons/imaging/formats/icns/IcnsImageParser.java
index 14556ce9..58b0629c 100644
--- a/src/main/java/org/apache/commons/imaging/formats/icns/IcnsImageParser.java
+++ b/src/main/java/org/apache/commons/imaging/formats/icns/IcnsImageParser.java
@@ -259,7 +259,7 @@ public class IcnsImageParser extends ImageParser<IcnsImagingParameters> {
                     + src.getWidth() + " and height " + src.getHeight());
         }
 
-        try (BinaryOutputStream bos = new BinaryOutputStream(os, ByteOrder.BIG_ENDIAN)) {
+        try (BinaryOutputStream bos = BinaryOutputStream.bigEndian(os)) {
             bos.write4Bytes(ICNS_MAGIC);
             bos.write4Bytes(4 + 4 + 4 + 4 + 4 * imageType.getWidth()
             * imageType.getHeight() + 4 + 4 + imageType.getWidth()
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 982536ad..13e941d5 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
@@ -432,7 +432,7 @@ public class IcoImageParser extends ImageParser<IcoImagingParameters> {
         final int bitmapSize = 14 + 56 + restOfFile.length;
 
         final ByteArrayOutputStream baos = new ByteArrayOutputStream(bitmapSize);
-        try (BinaryOutputStream bos = new BinaryOutputStream(baos, ByteOrder.LITTLE_ENDIAN)) {
+        try (BinaryOutputStream bos = BinaryOutputStream.littleEndian(baos)) {
             bos.write('B');
             bos.write('M');
             bos.write4Bytes(bitmapSize);
@@ -647,7 +647,7 @@ public class IcoImageParser extends ImageParser<IcoImagingParameters> {
             bitCount = 8;
         }
 
-        final BinaryOutputStream bos = new BinaryOutputStream(os, ByteOrder.LITTLE_ENDIAN);
+        final BinaryOutputStream bos = BinaryOutputStream.littleEndian(os);
 
         int scanline_size = (bitCount * src.getWidth() + 7) / 8;
         if ((scanline_size % 4) != 0) {
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 7029d309..0502f1a4 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
@@ -376,7 +376,7 @@ public class IptcParser extends BinaryFileParser {
     public byte[] writePhotoshopApp13Segment(final PhotoshopApp13Data data)
             throws IOException, ImageWriteException {
         final ByteArrayOutputStream os = new ByteArrayOutputStream();
-        final BinaryOutputStream bos = new BinaryOutputStream(os);
+        final BinaryOutputStream bos = BinaryOutputStream.bigEndian(os);
 
         JpegConstants.PHOTOSHOP_IDENTIFICATION_STRING.writeTo(bos);
 
@@ -426,7 +426,7 @@ public class IptcParser extends BinaryFileParser {
         }
         byte[] blockData;
         final ByteArrayOutputStream baos = new ByteArrayOutputStream();
-        try (BinaryOutputStream bos = new BinaryOutputStream(baos, getByteOrder())) {
+        try (BinaryOutputStream bos = BinaryOutputStream.create(baos, getByteOrder())) {
             if (!charset.equals(DEFAULT_CHARSET)) {
                 bos.write(IptcConstants.IPTC_RECORD_TAG_MARKER);
                 bos.write(IptcConstants.IPTC_ENVELOPE_RECORD_NUMBER);
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 6f2e4dcd..ed53d86d 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
@@ -18,7 +18,6 @@ package org.apache.commons.imaging.formats.pcx;
 import java.awt.image.BufferedImage;
 import java.io.IOException;
 import java.io.OutputStream;
-import java.nio.ByteOrder;
 import java.util.Arrays;
 
 import org.apache.commons.imaging.PixelDensity;
@@ -63,8 +62,7 @@ class PcxWriter {
             throws IOException {
         final PaletteFactory paletteFactory = new PaletteFactory();
         final SimplePalette palette = paletteFactory.makeExactRgbPaletteSimple(src, 256);
-        final BinaryOutputStream bos = new BinaryOutputStream(os,
-                ByteOrder.LITTLE_ENDIAN);
+        final BinaryOutputStream bos = BinaryOutputStream.littleEndian(os);
         final int bitDepth;
         final int planes;
         if (palette == null || bitDepthWanted == 24 || bitDepthWanted == 32) {
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 b305ce04..79248cf9 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
@@ -295,7 +295,7 @@ public class TiffImageWriterLossless extends TiffImageWriterBase {
         System.arraycopy(exifBytes, 0, output, 0, Math.min(exifBytes.length, output.length));
 
         final BufferOutputStream headerStream = new BufferOutputStream(output, 0);
-        final BinaryOutputStream headerBinaryStream = new BinaryOutputStream(headerStream, byteOrder);
+        final BinaryOutputStream headerBinaryStream = BinaryOutputStream.create(headerStream, byteOrder);
         writeImageFileHeader(headerBinaryStream, rootDirectory.getOffset());
 
         // zero out the parsed pieces of old exif segment, in case we don't
@@ -307,7 +307,7 @@ public class TiffImageWriterLossless extends TiffImageWriterBase {
 
         // write in the new items
         for (final TiffOutputItem outputItem : outputItems) {
-            try (BinaryOutputStream bos = new BinaryOutputStream(
+            try (BinaryOutputStream bos = BinaryOutputStream.create(
                     new BufferOutputStream(output, (int) outputItem.getOffset()), byteOrder)) {
                 outputItem.writeItem(bos);
             }
diff --git a/src/main/java/org/apache/commons/imaging/formats/tiff/write/TiffImageWriterLossy.java b/src/main/java/org/apache/commons/imaging/formats/tiff/write/TiffImageWriterLossy.java
index ccd8eda9..95148ddc 100644
--- a/src/main/java/org/apache/commons/imaging/formats/tiff/write/TiffImageWriterLossy.java
+++ b/src/main/java/org/apache/commons/imaging/formats/tiff/write/TiffImageWriterLossy.java
@@ -47,7 +47,7 @@ public class TiffImageWriterLossy extends TiffImageWriterBase {
 
         outputSummary.updateOffsets(byteOrder);
 
-        final BinaryOutputStream bos = new BinaryOutputStream(os, byteOrder);
+        final BinaryOutputStream bos = BinaryOutputStream.create(os, byteOrder);
 
         // NB: resource is intentionally left open
         writeStep(bos, outputItems);
diff --git a/src/test/java/org/apache/commons/imaging/formats/icns/IcnsRoundTripTest.java b/src/test/java/org/apache/commons/imaging/formats/icns/IcnsRoundTripTest.java
index 7d3b7a92..901918fb 100644
--- a/src/test/java/org/apache/commons/imaging/formats/icns/IcnsRoundTripTest.java
+++ b/src/test/java/org/apache/commons/imaging/formats/icns/IcnsRoundTripTest.java
@@ -25,7 +25,6 @@ import java.awt.image.BufferedImage;
 import java.io.ByteArrayOutputStream;
 import java.io.File;
 import java.io.IOException;
-import java.nio.ByteOrder;
 import java.nio.file.Files;
 
 import org.apache.commons.imaging.ImageReadException;
@@ -61,7 +60,7 @@ public class IcnsRoundTripTest extends IcnsBaseTest {
         final int foreground = 0xff000000;
         final int background = 0xff000000;
         try (final ByteArrayOutputStream baos = new ByteArrayOutputStream();
-                final BinaryOutputStream bos = new BinaryOutputStream(baos, ByteOrder.BIG_ENDIAN)) {
+                final BinaryOutputStream bos = BinaryOutputStream.bigEndian(baos)) {
             bos.write4Bytes(IcnsImageParser.ICNS_MAGIC);
             bos.write4Bytes(4 + 4 + 4 + 4 + 2 * 16 * 16 / 8 + 4 + 4 + 16 * 16);
             bos.write4Bytes(IcnsType.ICNS_16x16_1BIT_IMAGE_AND_MASK.getType());
@@ -98,7 +97,7 @@ public class IcnsRoundTripTest extends IcnsBaseTest {
         final int foreground = 0xff000000;
         final int background = 0x00cccccc;
         try (final ByteArrayOutputStream baos = new ByteArrayOutputStream();
-                final BinaryOutputStream bos = new BinaryOutputStream(baos, ByteOrder.BIG_ENDIAN)) {
+                final BinaryOutputStream bos = BinaryOutputStream.bigEndian(baos)) {
             bos.write4Bytes(IcnsImageParser.ICNS_MAGIC);
             bos.write4Bytes(4 + 4 + 4 + 4 + 16 * 16 + 4 + 4 + 16 * 16);
             bos.write4Bytes(IcnsType.ICNS_16x16_8BIT_IMAGE.getType());
@@ -135,7 +134,7 @@ public class IcnsRoundTripTest extends IcnsBaseTest {
         final int foreground = 0xff000000;
         final int background = 0x00cccccc;
         try (final ByteArrayOutputStream baos = new ByteArrayOutputStream();
-                final BinaryOutputStream bos = new BinaryOutputStream(baos, ByteOrder.BIG_ENDIAN)) {
+                final BinaryOutputStream bos = BinaryOutputStream.bigEndian(baos)) {
             bos.write4Bytes(IcnsImageParser.ICNS_MAGIC);
             bos.write4Bytes(4 + 4 + 4 + 4 + 16 * 16 + 4 + 4 + 16 * 16 + 4 + 4 + 2 * 16 * 16 / 8);
             bos.write4Bytes(IcnsType.ICNS_16x16_8BIT_IMAGE.getType());
@@ -192,7 +191,7 @@ public class IcnsRoundTripTest extends IcnsBaseTest {
         final int foreground = 0xff000000;
         final int background = 0x00cccccc;
         try (final ByteArrayOutputStream baos = new ByteArrayOutputStream();
-                final BinaryOutputStream bos = new BinaryOutputStream(baos, ByteOrder.BIG_ENDIAN)) {
+                final BinaryOutputStream bos = BinaryOutputStream.bigEndian(baos)) {
             bos.write4Bytes(IcnsImageParser.ICNS_MAGIC);
             bos.write4Bytes(4 + 4 + 4 + 4 + 16 * 16 + 4 + 4 + 16 * 16 + 4 + 4 + 2 * 16 * 16 / 8);
             bos.write4Bytes(IcnsType.ICNS_16x16_8BIT_IMAGE.getType());
@@ -249,7 +248,7 @@ public class IcnsRoundTripTest extends IcnsBaseTest {
         final int foreground = 0xff000000;
         final int background = 0xffcccccc;
         try (final ByteArrayOutputStream baos = new ByteArrayOutputStream();
-                final BinaryOutputStream bos = new BinaryOutputStream(baos, ByteOrder.BIG_ENDIAN)) {
+                final BinaryOutputStream bos = BinaryOutputStream.bigEndian(baos)) {
             bos.write4Bytes(IcnsImageParser.ICNS_MAGIC);
             bos.write4Bytes(4 + 4 + 4 + 4 + 16 * 16);
             bos.write4Bytes(IcnsType.ICNS_16x16_8BIT_IMAGE.getType());
@@ -273,7 +272,7 @@ public class IcnsRoundTripTest extends IcnsBaseTest {
         final int foreground = 0xff000000;
         final int background = 0x000000ff;
         try (final ByteArrayOutputStream baos = new ByteArrayOutputStream();
-                final BinaryOutputStream bos = new BinaryOutputStream(baos, ByteOrder.BIG_ENDIAN)) {
+                final BinaryOutputStream bos = BinaryOutputStream.bigEndian(baos)) {
             bos.write4Bytes(IcnsImageParser.ICNS_MAGIC);
             bos.write4Bytes(4 + 4 + 4 + 4 + 4 * 16 * 16 + 4 + 4 + 2 * 16 * 16 / 8);
             bos.write4Bytes(IcnsType.ICNS_16x16_32BIT_IMAGE.getType());
@@ -329,7 +328,7 @@ public class IcnsRoundTripTest extends IcnsBaseTest {
         final int foreground = 0xff000000;
         final int background = 0xff0000ff;
         try (final ByteArrayOutputStream baos = new ByteArrayOutputStream();
-                final BinaryOutputStream bos = new BinaryOutputStream(baos, ByteOrder.BIG_ENDIAN)) {
+                final BinaryOutputStream bos = BinaryOutputStream.bigEndian(baos)) {
             bos.write4Bytes(IcnsImageParser.ICNS_MAGIC);
             bos.write4Bytes(4 + 4 + 4 + 4 + 4 * 16 * 16 + 4 + 4 + 16 * 16 / 8);
             bos.write4Bytes(IcnsType.ICNS_16x16_32BIT_IMAGE.getType());
@@ -381,7 +380,7 @@ public class IcnsRoundTripTest extends IcnsBaseTest {
         final int foreground = 0xff000000;
         final int background = 0xff0000ff;
         try (final ByteArrayOutputStream baos = new ByteArrayOutputStream();
-                final BinaryOutputStream bos = new BinaryOutputStream(baos, ByteOrder.BIG_ENDIAN)) {
+                final BinaryOutputStream bos = BinaryOutputStream.bigEndian(baos)) {
             bos.write4Bytes(IcnsImageParser.ICNS_MAGIC);
             bos.write4Bytes(4 + 4 + 4 + 4 + 4 * 16 * 16);
             bos.write4Bytes(IcnsType.ICNS_16x16_32BIT_IMAGE.getType());
diff --git a/src/test/java/org/apache/commons/imaging/formats/ico/IcoRoundtripTest.java b/src/test/java/org/apache/commons/imaging/formats/ico/IcoRoundtripTest.java
index 5c25d519..788ff255 100644
--- a/src/test/java/org/apache/commons/imaging/formats/ico/IcoRoundtripTest.java
+++ b/src/test/java/org/apache/commons/imaging/formats/ico/IcoRoundtripTest.java
@@ -25,7 +25,6 @@ import java.awt.image.BufferedImage;
 import java.io.ByteArrayOutputStream;
 import java.io.File;
 import java.io.IOException;
-import java.nio.ByteOrder;
 import java.nio.file.Files;
 import java.util.HashMap;
 import java.util.Map;
@@ -81,7 +80,7 @@ public class IcoRoundtripTest extends IcoBaseTest {
  final int paletteSize)
                 throws IOException, ImageWriteException {
             try (final ByteArrayOutputStream byteArrayStream = new ByteArrayOutputStream();
-                    final BinaryOutputStream bos = new BinaryOutputStream(byteArrayStream, ByteOrder.LITTLE_ENDIAN)) {
+                    final BinaryOutputStream bos = BinaryOutputStream.littleEndian(byteArrayStream)) {
                 // Palette
                 bos.write3Bytes(background);
                 bos.write(0);
@@ -121,7 +120,7 @@ public class IcoRoundtripTest extends IcoBaseTest {
         public byte[] generateBitmap(final int foreground, final int background, final int paletteSize)
                 throws IOException, ImageWriteException {
             try (final ByteArrayOutputStream byteArrayStream = new ByteArrayOutputStream();
-                    final BinaryOutputStream bos = new BinaryOutputStream(byteArrayStream, ByteOrder.LITTLE_ENDIAN)) {
+                    final BinaryOutputStream bos = BinaryOutputStream.littleEndian(byteArrayStream)) {
                 // Palette
                 bos.write3Bytes(background);
                 bos.write(0);
@@ -155,7 +154,7 @@ public class IcoRoundtripTest extends IcoBaseTest {
         public byte[] generateBitmap(final int foreground, final int background, final int paletteSize)
                 throws IOException, ImageWriteException {
             try (final ByteArrayOutputStream byteArrayStream = new ByteArrayOutputStream();
-                    final BinaryOutputStream bos = new BinaryOutputStream(byteArrayStream, ByteOrder.LITTLE_ENDIAN)) {
+                    final BinaryOutputStream bos = BinaryOutputStream.littleEndian(byteArrayStream)) {
                 // Palette
                 bos.write3Bytes(background);
                 bos.write(0);
@@ -190,7 +189,7 @@ public class IcoRoundtripTest extends IcoBaseTest {
  final int paletteSize)
                 throws IOException, ImageWriteException {
             try (final ByteArrayOutputStream byteArrayStream = new ByteArrayOutputStream();
-                    final BinaryOutputStream bos = new BinaryOutputStream(byteArrayStream, ByteOrder.LITTLE_ENDIAN)) {
+                    final BinaryOutputStream bos = BinaryOutputStream.littleEndian(byteArrayStream)) {
                 // Palette
                 for (int i = 0; i < paletteSize; i++) {
                     bos.write4Bytes(0);
@@ -226,7 +225,7 @@ public class IcoRoundtripTest extends IcoBaseTest {
         public byte[] generateBitmap(final int foreground, final int background, final int paletteSize)
                 throws IOException, ImageWriteException {
             try (final ByteArrayOutputStream byteArrayStream = new ByteArrayOutputStream();
-                    final BinaryOutputStream bos = new BinaryOutputStream(byteArrayStream, ByteOrder.LITTLE_ENDIAN)) {
+                    final BinaryOutputStream bos = BinaryOutputStream.littleEndian(byteArrayStream)) {
                 // Palette
                 for (int i = 0; i < paletteSize; i++) {
                     bos.write4Bytes(0);
@@ -266,7 +265,7 @@ public class IcoRoundtripTest extends IcoBaseTest {
         public byte[] generate32bitRGBABitmap(final int foreground, final int background, final int paletteSize,
                 final boolean writeMask) throws IOException {
             try (final ByteArrayOutputStream byteArrayStream = new ByteArrayOutputStream();
-                    final BinaryOutputStream bos = new BinaryOutputStream(byteArrayStream, ByteOrder.LITTLE_ENDIAN)) {
+                    final BinaryOutputStream bos = BinaryOutputStream.littleEndian(byteArrayStream)) {
                 // Palette
                 for (int i = 0; i < paletteSize; i++) {
                     bos.write4Bytes(0);
@@ -343,8 +342,7 @@ public class IcoRoundtripTest extends IcoBaseTest {
             final BitmapGenerator bitmapGenerator = entry.getValue();
 
             final ByteArrayOutputStream baos = new ByteArrayOutputStream();
-            final BinaryOutputStream bos = new BinaryOutputStream(baos,
-                    ByteOrder.LITTLE_ENDIAN);
+            final BinaryOutputStream bos = BinaryOutputStream.littleEndian(baos);
             final byte[] bitmap = bitmapGenerator.generateBitmap(foreground,
                     background, (bitDepth <= 8) ? (1 << bitDepth) : 0);
             writeICONDIR(bos, 0, 1, 1);
@@ -368,8 +366,7 @@ public class IcoRoundtripTest extends IcoBaseTest {
             final BitmapGenerator bitmapGenerator = entry.getValue();
 
             final ByteArrayOutputStream baos = new ByteArrayOutputStream();
-            final BinaryOutputStream bos = new BinaryOutputStream(baos,
-                    ByteOrder.LITTLE_ENDIAN);
+            final BinaryOutputStream bos = BinaryOutputStream.littleEndian(baos);
             final byte[] bitmap = bitmapGenerator.generateBitmap(foreground,
                     background, (bitDepth <= 8) ? (1 << bitDepth) : 0);
             writeICONDIR(bos, 0, 1, 1);
@@ -411,8 +408,7 @@ public class IcoRoundtripTest extends IcoBaseTest {
             final BitmapGenerator bitmapGenerator = entry.getValue();
 
             final ByteArrayOutputStream baos = new ByteArrayOutputStream();
-            final BinaryOutputStream bos = new BinaryOutputStream(baos,
-                    ByteOrder.LITTLE_ENDIAN);
+            final BinaryOutputStream bos = BinaryOutputStream.littleEndian(baos);
             final byte[] bitmap = bitmapGenerator.generateBitmap(foreground,
                     background, 2);
             writeICONDIR(bos, 0, 1, 1);
@@ -434,8 +430,7 @@ public class IcoRoundtripTest extends IcoBaseTest {
             final BitmapGenerator bitmapGenerator = entry.getValue();
 
             final ByteArrayOutputStream baos = new ByteArrayOutputStream();
-            final BinaryOutputStream bos = new BinaryOutputStream(baos,
-                    ByteOrder.LITTLE_ENDIAN);
+            final BinaryOutputStream bos = BinaryOutputStream.littleEndian(baos);
             final byte[] bitmap = bitmapGenerator.generateBitmap(foreground,
                     background, (bitDepth <= 8) ? (1 << bitDepth) : 0);
             writeICONDIR(bos, 0, 1, 1);
@@ -461,8 +456,7 @@ public class IcoRoundtripTest extends IcoBaseTest {
     @Test
     public void testBitfieldCompression() throws Exception {
         final ByteArrayOutputStream baos = new ByteArrayOutputStream();
-        final BinaryOutputStream bos = new BinaryOutputStream(baos,
-                ByteOrder.LITTLE_ENDIAN);
+        final BinaryOutputStream bos = BinaryOutputStream.littleEndian(baos);
         final byte[] bitmap = new GeneratorFor32BitBitmaps().generate32bitRGBABitmap(
                 0xFFFF0000, 0xFFFFFFFF, 0, true);
         writeICONDIR(bos, 0, 1, 1);
@@ -483,8 +477,7 @@ public class IcoRoundtripTest extends IcoBaseTest {
         final int foreground = 0xFFF000E0;
         final int background = 0xFF102030;
         final ByteArrayOutputStream baos = new ByteArrayOutputStream();
-        final BinaryOutputStream bos = new BinaryOutputStream(baos,
-                ByteOrder.LITTLE_ENDIAN);
+        final BinaryOutputStream bos = BinaryOutputStream.littleEndian(baos);
         // For 32 bit RGBA, the AND mask can be missing:
         final byte[] bitmap = new GeneratorFor32BitBitmaps().generate32bitRGBABitmap(
                 foreground, background, 0, false);
@@ -500,8 +493,7 @@ public class IcoRoundtripTest extends IcoBaseTest {
     @Test
     public void testAlphaVersusANDMask() throws Exception {
         final ByteArrayOutputStream baos = new ByteArrayOutputStream();
-        final BinaryOutputStream bos = new BinaryOutputStream(baos,
-                ByteOrder.LITTLE_ENDIAN);
+        final BinaryOutputStream bos = BinaryOutputStream.littleEndian(baos);
         final byte[] bitmap = new GeneratorFor32BitBitmaps().generate32bitRGBABitmap(
                 0xFF000000, 0x00000000, 0, true);
         writeICONDIR(bos, 0, 1, 1);
@@ -518,8 +510,7 @@ public class IcoRoundtripTest extends IcoBaseTest {
     @Test
     public void testFullyTransparent32bitRGBA() throws Exception {
         final ByteArrayOutputStream baos = new ByteArrayOutputStream();
-        final BinaryOutputStream bos = new BinaryOutputStream(baos,
-                ByteOrder.LITTLE_ENDIAN);
+        final BinaryOutputStream bos = BinaryOutputStream.littleEndian(baos);
         final byte[] bitmap = new GeneratorFor32BitBitmaps().generate32bitRGBABitmap(
                 0x00000000, 0x00FFFFFF, 0, true);
         writeICONDIR(bos, 0, 1, 1);