1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17 package org.apache.commons.imaging.formats.tiff.fieldtypes;
18
19 import java.nio.ByteOrder;
20
21 import org.apache.commons.imaging.ImagingException;
22 import org.apache.commons.imaging.common.Allocator;
23 import org.apache.commons.imaging.common.ByteConversions;
24 import org.apache.commons.imaging.formats.tiff.TiffField;
25
26 public class FieldTypeLong extends AbstractFieldType {
27 public FieldTypeLong(final int type, final String name) {
28 super(type, name, 4);
29 }
30
31 @Override
32 public Object getValue(final TiffField entry) {
33 final byte[] bytes = entry.getByteArrayValue();
34 if (entry.getCount() == 1) {
35 return ByteConversions.toInt(bytes, entry.getByteOrder());
36 }
37 return ByteConversions.toInts(bytes, entry.getByteOrder());
38 }
39
40 @Override
41 public byte[] writeData(final Object o, final ByteOrder byteOrder) throws ImagingException {
42 if (o instanceof Integer) {
43 return ByteConversions.toBytes((Integer) o, byteOrder);
44 }
45 if (o instanceof int[]) {
46 final int[] numbers = (int[]) o;
47 return ByteConversions.toBytes(numbers, byteOrder);
48 }
49 if (!(o instanceof Integer[])) {
50 throw new ImagingException("Invalid data", o);
51 }
52 final Integer[] numbers = (Integer[]) o;
53 final int[] values = Allocator.intArray(numbers.length);
54 System.arraycopy(numbers, 0, values, 0, values.length);
55 return ByteConversions.toBytes(values, byteOrder);
56 }
57
58 }