1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
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
67
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
87
88
89
90
91 public void setData(final byte[] bytes) throws ImagingException {
92
93
94
95
96 if (this.bytes.length != bytes.length) {
97 throw new ImagingException("Cannot change size of value.");
98 }
99
100
101 this.bytes = bytes;
102 if (separateValueItem != null) {
103 separateValueItem.updateValue(bytes);
104 }
105
106
107
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 }