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 regarding copyright ownership.
5    * The ASF licenses this file to You under the Apache 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.IOException;
20  import java.io.PrintWriter;
21  import java.io.StringWriter;
22  import java.nio.ByteOrder;
23  import java.util.logging.Level;
24  import java.util.logging.Logger;
25  
26  public class BinaryFileParser {
27  
28      private static final Logger LOGGER = Logger.getLogger(BinaryFileParser.class.getName());
29  
30      /**
31       * The default {@link ByteOrder} for parsers is {@link ByteOrder#BIG_ENDIAN}.
32       */
33      private ByteOrder byteOrder = ByteOrder.BIG_ENDIAN;
34  
35      /**
36       * Constructs a BinaryFileParser with the default, big-endian, byte order.
37       */
38      public BinaryFileParser() {
39          // as above
40      }
41  
42      public BinaryFileParser(final ByteOrder byteOrder) {
43          this.byteOrder = byteOrder;
44      }
45  
46      protected final void debugNumber(final PrintWriter pw, final String msg, final int data, final int bytes) {
47          pw.print(msg + ": " + data + " (");
48          int byteData = data;
49          for (int i = 0; i < bytes; i++) {
50              if (i > 0) {
51                  pw.print(",");
52              }
53              final int singleByte = 0xff & byteData;
54              pw.print((char) singleByte + " [" + singleByte + "]");
55              byteData >>= 8;
56          }
57          pw.println(") [0x" + Integer.toHexString(data) + ", " + Integer.toBinaryString(data) + "]");
58          pw.flush();
59      }
60  
61      protected final void debugNumber(final String msg, final int data, final int bytes) {
62          try (StringWriter sw = new StringWriter();
63                  PrintWriter pw = new PrintWriter(sw)) {
64              debugNumber(pw, msg, data, bytes);
65              pw.flush();
66              sw.flush();
67              LOGGER.fine(sw.toString());
68          } catch (final IOException e) {
69              LOGGER.log(Level.SEVERE, e.getMessage(), e);
70          }
71      }
72  
73      public ByteOrder getByteOrder() {
74          return byteOrder;
75      }
76  
77      protected void setByteOrder(final ByteOrder byteOrder) {
78          this.byteOrder = byteOrder;
79      }
80  }