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 java.io.IOException;
20  
21  import org.apache.commons.imaging.ImagingException;
22  import org.apache.commons.imaging.common.BinaryOutputStream;
23  
24  abstract class AbstractTiffOutputItem {
25  
26      public static class Value extends AbstractTiffOutputItem {
27  
28          static final int SHALLOW_SIZE = 32;
29          private final byte[] bytes;
30          private final String name;
31  
32          Value(final String name, final byte[] bytes) {
33              this.name = name;
34              this.bytes = bytes;
35          }
36  
37          @Override
38          public String getItemDescription() {
39              return name;
40          }
41  
42          @Override
43          public int getItemLength() {
44              return bytes.length;
45          }
46  
47          public void updateValue(final byte[] bytes) throws ImagingException {
48              if (this.bytes.length != bytes.length) {
49                  throw new ImagingException("Updated data size mismatch: " + this.bytes.length + " vs. " + bytes.length);
50              }
51              System.arraycopy(bytes, 0, this.bytes, 0, bytes.length);
52          }
53  
54          @Override
55          public void writeItem(final BinaryOutputStream bos) throws IOException, ImagingException {
56              bos.write(bytes);
57          }
58      }
59  
60      public static final long UNDEFINED_VALUE = -1;
61  
62      private long offset = UNDEFINED_VALUE;
63  
64      public abstract String getItemDescription();
65  
66      public abstract int getItemLength();
67  
68      protected long getOffset() {
69          return offset;
70      }
71  
72      protected void setOffset(final long offset) {
73          this.offset = offset;
74      }
75  
76      public abstract void writeItem(BinaryOutputStream bos) throws IOException, ImagingException;
77  }