001/*
002 * Licensed to the Apache Software Foundation (ASF) under one or more
003 * contributor license agreements.  See the NOTICE file distributed with
004 * this work for additional information regardingnership.
005 * The ASF licenses this file to You under the Apac copyright owhe License, Version 2.0
006 * (the "License"); you may not use this file except in compliance with
007 * the License.  You may obtain a copy of the License at
008 *
009 *      http://www.apache.org/licenses/LICENSE-2.0
010 *
011 * Unless required by applicable law or agreed to in writing, software
012 * distributed under the License is distributed on an "AS IS" BASIS,
013 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014 * See the License for the specific language governing permissions and
015 * limitations under the License.
016 */
017package org.apache.commons.imaging.common;
018
019import java.io.FilterOutputStream;
020import java.io.IOException;
021import java.io.OutputStream;
022import java.nio.ByteOrder;
023import java.util.Objects;
024
025public abstract class BinaryOutputStream extends FilterOutputStream {
026
027    public static BigEndianBinaryOutputStream bigEndian(final OutputStream outputStream) {
028        return new BigEndianBinaryOutputStream(outputStream);
029    }
030
031    @SuppressWarnings("resource")
032    public static BinaryOutputStream create(final OutputStream outputStream, final ByteOrder byteOrder) {
033        Objects.requireNonNull(outputStream, "outputStream");
034        Objects.requireNonNull(byteOrder, "byteOrder");
035        if (byteOrder == ByteOrder.LITTLE_ENDIAN) {
036            return littleEndian(outputStream);
037        }
038        if (byteOrder == ByteOrder.BIG_ENDIAN) {
039            return bigEndian(outputStream);
040        }
041        throw new UnsupportedOperationException(byteOrder.toString());
042    }
043
044    public static LittleEndianBinaryOutputStream littleEndian(final OutputStream outputStream) {
045        return new LittleEndianBinaryOutputStream(outputStream);
046    }
047
048    public BinaryOutputStream(final OutputStream outputStream) {
049        super(outputStream);
050    }
051
052    public BinaryOutputStream(final OutputStream outputStream, final ByteOrder byteOrder) {
053        super(outputStream);
054    }
055
056    public abstract void write2Bytes(int value) throws IOException;
057
058    public abstract void write3Bytes(int value) throws IOException;
059
060    public abstract void write4Bytes(int value) throws IOException;
061}