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 regarding copyright ownership.
005 * The ASF licenses this file to You under the Apache 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.IOException;
020import java.io.OutputStream;
021import java.nio.ByteOrder;
022
023public class BinaryOutputStream extends OutputStream {
024    private final OutputStream os;
025    // default byte order for Java, many file formats.
026    private ByteOrder byteOrder = ByteOrder.BIG_ENDIAN;
027    private int count;
028
029    public BinaryOutputStream(final OutputStream os, final ByteOrder byteOrder) {
030        this.byteOrder = byteOrder;
031        this.os = os;
032    }
033
034    public BinaryOutputStream(final OutputStream os) {
035        this.os = os;
036    }
037
038    protected void setByteOrder(final ByteOrder byteOrder) {
039        this.byteOrder = byteOrder;
040    }
041
042    public ByteOrder getByteOrder() {
043        return byteOrder;
044    }
045
046    @Override
047    public void write(final int i) throws IOException {
048        os.write(i);
049        count++;
050    }
051
052    @Override
053    public final void write(final byte[] bytes) throws IOException {
054        os.write(bytes, 0, bytes.length);
055        count += bytes.length;
056    }
057
058    @Override
059    public final void write(final byte[] bytes, final int offset, final int length) throws IOException {
060        os.write(bytes, offset, length);
061        count += length;
062    }
063
064    @Override
065    public void flush() throws IOException {
066        os.flush();
067    }
068
069    @Override
070    public void close() throws IOException {
071        os.close();
072    }
073
074    public int getByteCount() {
075        return count;
076    }
077
078    public final void write4Bytes(final int value) throws IOException {
079        if (byteOrder == ByteOrder.BIG_ENDIAN) {
080            write(0xff & (value >> 24));
081            write(0xff & (value >> 16));
082            write(0xff & (value >> 8));
083            write(0xff & value);
084        } else {
085            write(0xff & value);
086            write(0xff & (value >> 8));
087            write(0xff & (value >> 16));
088            write(0xff & (value >> 24));
089        }
090    }
091
092    public final void write3Bytes(final int value) throws IOException {
093        if (byteOrder == ByteOrder.BIG_ENDIAN) {
094            write(0xff & (value >> 16));
095            write(0xff & (value >> 8));
096            write(0xff & value);
097        } else {
098            write(0xff & value);
099            write(0xff & (value >> 8));
100            write(0xff & (value >> 16));
101        }
102    }
103
104    public final void write2Bytes(final int value) throws IOException {
105        if (byteOrder == ByteOrder.BIG_ENDIAN) {
106            write(0xff & (value >> 8));
107            write(0xff & value);
108        } else {
109            write(0xff & value);
110            write(0xff & (value >> 8));
111        }
112    }
113}