View Javadoc
1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one or more
3    * contributor license agreements.  See the NOTICE file distributed with
4    * this work for additional information regardingnership.
5    * The ASF licenses this file to You under the Apac copyright owhe License, Version 2.0
6    * (the "License"); you may not use this file except in compliance with
7    * the License.  You may obtain a copy of the License at
8    *
9    *      http://www.apache.org/licenses/LICENSE-2.0
10   *
11   * Unless required by applicable law or agreed to in writing, software
12   * distributed under the License is distributed on an "AS IS" BASIS,
13   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14   * See the License for the specific language governing permissions and
15   * limitations under the License.
16   */
17  package org.apache.commons.imaging.common;
18  
19  import java.io.FilterOutputStream;
20  import java.io.IOException;
21  import java.io.OutputStream;
22  import java.nio.ByteOrder;
23  import java.util.Objects;
24  
25  public abstract class BinaryOutputStream extends FilterOutputStream {
26  
27      public static BigEndianBinaryOutputStream bigEndian(final OutputStream outputStream) {
28          return new BigEndianBinaryOutputStream(outputStream);
29      }
30  
31      @SuppressWarnings("resource")
32      public static BinaryOutputStream create(final OutputStream outputStream, final ByteOrder byteOrder) {
33          Objects.requireNonNull(outputStream, "outputStream");
34          Objects.requireNonNull(byteOrder, "byteOrder");
35          if (byteOrder == ByteOrder.LITTLE_ENDIAN) {
36              return littleEndian(outputStream);
37          }
38          if (byteOrder == ByteOrder.BIG_ENDIAN) {
39              return bigEndian(outputStream);
40          }
41          throw new UnsupportedOperationException(byteOrder.toString());
42      }
43  
44      public static LittleEndianBinaryOutputStream littleEndian(final OutputStream outputStream) {
45          return new LittleEndianBinaryOutputStream(outputStream);
46      }
47  
48      public BinaryOutputStream(final OutputStream outputStream) {
49          super(outputStream);
50      }
51  
52      public BinaryOutputStream(final OutputStream outputStream, final ByteOrder byteOrder) {
53          super(outputStream);
54      }
55  
56      public abstract void write2Bytes(int value) throws IOException;
57  
58      public abstract void write3Bytes(int value) throws IOException;
59  
60      public abstract void write4Bytes(int value) throws IOException;
61  }