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.formats.tiff.fieldtypes;
18  
19  import java.io.UnsupportedEncodingException;
20  import java.nio.ByteOrder;
21  
22  import org.apache.commons.imaging.ImageWriteException;
23  import org.apache.commons.imaging.formats.tiff.TiffField;
24  
25  public class FieldTypeAscii extends FieldType {
26      public FieldTypeAscii(final int type, final String name) {
27          super(type, name, 1);
28      }
29  
30      @Override
31      public Object getValue(final TiffField entry) {
32          // According to EXIF specification
33          // "2 = ASCII An 8-bit byte containing one 7-bit ASCII code. The final byte is terminated with NULL."
34          final byte[] bytes = entry.getByteArrayValue();
35          int nullCount = 1;
36          for (int i = 0; i < bytes.length - 1; i++) {
37              if (bytes[i] == 0) {
38                  nullCount++;
39              }
40          }
41          final String[] strings = new String[nullCount];
42          int stringsAdded = 0;
43          strings[0] = ""; // if we have a 0 length string
44          int nextStringPos = 0;
45          // According to the Exiftool FAQ, http://www.metadataworkinggroup.org
46          // specifies that the TIFF ASCII fields are actually UTF-8.
47          // Exiftool however allows you to configure the charset used.
48          for (int i = 0; i < bytes.length; i++) {
49              if (bytes[i] == 0) {
50                  try {
51                      final String string = new String(bytes, nextStringPos, i
52                              - nextStringPos, "UTF-8");
53                      strings[stringsAdded++] = string;
54                  } catch (final UnsupportedEncodingException unsupportedEncoding) { // NOPMD
55                  }
56                  nextStringPos = i + 1;
57              }
58          }
59          if (nextStringPos < bytes.length) {
60              // Buggy file, string wasn't null terminated
61              try {
62                  final String string = new String(bytes, nextStringPos, bytes.length
63                          - nextStringPos, "UTF-8");
64                  strings[stringsAdded++] = string;
65              } catch (final UnsupportedEncodingException unsupportedEncoding) { // NOPMD
66              }
67          }
68          if (strings.length == 1) {
69              return strings[0];
70          }
71          return strings;
72      }
73  
74      @Override
75      public byte[] writeData(final Object o, final ByteOrder byteOrder) throws ImageWriteException {
76          if (o instanceof byte[]) {
77              final byte[] bytes = (byte[]) o;
78              final byte[] result = new byte[bytes.length + 1];
79              System.arraycopy(bytes, 0, result, 0, bytes.length);
80              result[result.length - 1] = 0;
81              return result;
82          } else if (o instanceof String) {
83              byte[] bytes;
84              try {
85                  bytes = ((String) o).getBytes("UTF-8");
86              } catch (final UnsupportedEncodingException cannotHappen) {
87                  throw new IllegalArgumentException("Your Java doesn't support UTF-8", cannotHappen);
88              }
89              final byte[] result = new byte[bytes.length + 1];
90              System.arraycopy(bytes, 0, result, 0, bytes.length);
91              result[result.length - 1] = 0;
92              return result;
93          } else if (o instanceof String[]) {
94              final String[] strings = (String[]) o;
95              int totalLength = 0;
96              for (final String string : strings) {
97                  byte[] bytes;
98                  try {
99                      bytes = string.getBytes("UTF-8");
100                 } catch (final UnsupportedEncodingException cannotHappen) {
101                     throw new IllegalArgumentException("Your Java doesn't support UTF-8", cannotHappen);
102                 }
103                 totalLength += (bytes.length + 1);
104             }
105             final byte[] result = new byte[totalLength];
106             int position = 0;
107             for (final String string : strings) {
108                 byte[] bytes;
109                 try {
110                     bytes = string.getBytes("UTF-8");
111                 } catch (final UnsupportedEncodingException cannotHappen) {
112                     throw new IllegalArgumentException("Your Java doesn't support UTF-8", cannotHappen);
113                 }
114                 System.arraycopy(bytes, 0, result, position, bytes.length);
115                 position += (bytes.length + 1);
116             }
117             return result;
118         } else {
119             throw new ImageWriteException("Unknown data type: " + o);
120         }
121     }
122 
123 }