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 FieldTypeShort extends AbstractFieldType {
27 public FieldTypeShort(final int type, final String name) {
28 super(type, name, 2);
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.toShort(bytes, entry.getByteOrder());
36 }
37 return ByteConversions.toShorts(bytes, entry.getByteOrder());
38 }
39
40 @Override
41 public byte[] writeData(final Object o, final ByteOrder byteOrder) throws ImagingException {
42 if (o instanceof Short) {
43 return ByteConversions.toBytes(((Short) o).shortValue(), byteOrder);
44 }
45 if (o instanceof short[]) {
46 final short[] numbers = (short[]) o;
47 return ByteConversions.toBytes(numbers, byteOrder);
48 }
49 if (!(o instanceof Short[])) {
50 throw new ImagingException("Invalid data", o);
51 }
52 final Short[] numbers = (Short[]) o;
53 final short[] values = Allocator.shortArray(numbers.length);
54 for (int i = 0; i < values.length; i++) {
55 values[i] = numbers[i].shortValue();
56 }
57 return ByteConversions.toBytes(values, byteOrder);
58 }
59
60 }