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.write;
18  
19  import static org.apache.commons.imaging.formats.tiff.constants.TiffConstants.TIFF_ENTRY_MAX_VALUE_LENGTH;
20  
21  import java.io.IOException;
22  import java.nio.ByteOrder;
23  import java.util.Arrays;
24  
25  import org.apache.commons.imaging.ImagingException;
26  import org.apache.commons.imaging.common.BinaryOutputStream;
27  import org.apache.commons.imaging.formats.tiff.fieldtypes.AbstractFieldType;
28  import org.apache.commons.imaging.formats.tiff.taginfos.TagInfo;
29  
30  public class TiffOutputField {
31      private static final String NEWLINE = System.lineSeparator();
32  
33      protected static TiffOutputField createOffsetField(final TagInfo tagInfo, final ByteOrder byteOrder) throws ImagingException {
34          return new TiffOutputField(tagInfo, AbstractFieldType.LONG, 1, AbstractFieldType.LONG.writeData(0, byteOrder));
35      }
36  
37      public final int tag;
38      public final TagInfo tagInfo;
39      public final AbstractFieldType abstractFieldType;
40      public final int count;
41      private byte[] bytes;
42      private final AbstractTiffOutputItem.Value separateValueItem;
43  
44      private int sortHint = -1;
45  
46      public TiffOutputField(final int tag, final TagInfo tagInfo, final AbstractFieldType abstractFieldType, final int count, final byte[] bytes) {
47          this.tag = tag;
48          this.tagInfo = tagInfo;
49          this.abstractFieldType = abstractFieldType;
50          this.count = count;
51          this.bytes = bytes;
52  
53          if (isLocalValue()) {
54              separateValueItem = null;
55          } else {
56              final String name = "Field Separate value (" + tagInfo.getDescription() + ")";
57              separateValueItem = new AbstractTiffOutputItem.Value(name, bytes);
58          }
59      }
60  
61      public TiffOutputField(final TagInfo tagInfo, final AbstractFieldType abstractFieldType, final int count, final byte[] bytes) {
62          this(tagInfo.tag, tagInfo, abstractFieldType, count, bytes);
63      }
64  
65      /**
66       * Return a copy of the data in this TIFF output field.
67       * @return a copy of the data in this TIFF output field.
68       */
69      public byte[] getData() {
70          return Arrays.copyOf(this.bytes, this.bytes.length);
71      }
72  
73      protected AbstractTiffOutputItem getSeperateValue() {
74          return separateValueItem;
75      }
76  
77      public int getSortHint() {
78          return sortHint;
79      }
80  
81      protected final boolean isLocalValue() {
82          return bytes.length <= TIFF_ENTRY_MAX_VALUE_LENGTH;
83      }
84  
85      /**
86       * Set the data for this TIFF output field.
87       *
88       * @param bytes TIFF output field data.
89       * @throws ImagingException if the length of the bytes array do not match.
90       */
91      public void setData(final byte[] bytes) throws ImagingException {
92          // if (tagInfo.isUnknown())
93          // Debug.debug("unknown tag(0x" + Integer.toHexString(tag)
94          // + ") setData", bytes);
95  
96          if (this.bytes.length != bytes.length) {
97              throw new ImagingException("Cannot change size of value.");
98          }
99  
100         // boolean wasLocalValue = isLocalValue();
101         this.bytes = bytes;
102         if (separateValueItem != null) {
103             separateValueItem.updateValue(bytes);
104         }
105         // if (isLocalValue() != wasLocalValue)
106         // throw new Error("Bug. Locality disrupted! "
107         // + tagInfo.getDescription());
108     }
109 
110     public void setSortHint(final int sortHint) {
111         this.sortHint = sortHint;
112     }
113 
114     @Override
115     public String toString() {
116         return toString(null);
117     }
118 
119     public String toString(String prefix) {
120         if (prefix == null) {
121             prefix = "";
122         }
123         final StringBuilder result = new StringBuilder();
124 
125         result.append(prefix);
126         result.append(tagInfo);
127         result.append(NEWLINE);
128 
129         result.append(prefix);
130         result.append("count: ");
131         result.append(count);
132         result.append(NEWLINE);
133 
134         result.append(prefix);
135         result.append(abstractFieldType);
136         result.append(NEWLINE);
137 
138         return result.toString();
139     }
140 
141     protected void writeField(final BinaryOutputStream bos) throws IOException, ImagingException {
142         bos.write2Bytes(tag);
143         bos.write2Bytes(abstractFieldType.getType());
144         bos.write4Bytes(count);
145 
146         if (isLocalValue()) {
147             if (separateValueItem != null) {
148                 throw new ImagingException("Unexpected separate value item.");
149             }
150             if (bytes.length > 4) {
151                 throw new ImagingException("Local value has invalid length: " + bytes.length);
152             }
153 
154             bos.write(bytes);
155             final int remainder = TIFF_ENTRY_MAX_VALUE_LENGTH - bytes.length;
156             for (int i = 0; i < remainder; i++) {
157                 bos.write(0);
158             }
159         } else {
160             if (separateValueItem == null) {
161                 throw new ImagingException("Missing separate value item.");
162             }
163 
164             bos.write4Bytes((int) separateValueItem.getOffset());
165         }
166     }
167 }