1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17 package org.apache.commons.imaging.formats.jpeg.segments;
18
19 import static org.apache.commons.imaging.common.BinaryFunctions.readAndVerifyBytes;
20 import static org.apache.commons.imaging.common.BinaryFunctions.readByte;
21 import static org.apache.commons.imaging.common.BinaryFunctions.readBytes;
22
23 import java.io.ByteArrayInputStream;
24 import java.io.IOException;
25 import java.io.InputStream;
26
27 import org.apache.commons.imaging.ImagingException;
28 import org.apache.commons.imaging.formats.jpeg.JpegConstants;
29
30 public final class App2Segment extends AppnSegment implements Comparable<App2Segment> {
31 private final byte[] iccBytes;
32 public final int curMarker;
33 public final int numMarkers;
34
35 public App2Segment(final int marker, final byte[] segmentData) throws ImagingException, IOException {
36 this(marker, segmentData.length, new ByteArrayInputStream(segmentData));
37 }
38
39 public App2Segment(final int marker, int markerLength, final InputStream is2) throws ImagingException, IOException {
40 super(marker, markerLength, is2);
41
42 if (JpegConstants.ICC_PROFILE_LABEL.isStartOf(getSegmentData())) {
43 final InputStream is = new ByteArrayInputStream(getSegmentData());
44
45 readAndVerifyBytes(is, JpegConstants.ICC_PROFILE_LABEL, "Not a Valid App2 Segment: missing ICC Profile label");
46
47 curMarker = readByte("curMarker", is, "Not a valid App2 Marker");
48 numMarkers = readByte("numMarkers", is, "Not a valid App2 Marker");
49
50 markerLength -= JpegConstants.ICC_PROFILE_LABEL.size();
51 markerLength -= 1 + 1;
52
53 iccBytes = readBytes("App2 Data", is, markerLength, "Invalid App2 Segment: insufficient data");
54 } else {
55
56 curMarker = -1;
57 numMarkers = -1;
58 iccBytes = null;
59 }
60 }
61
62 @Override
63 public int compareTo(final App2Segment other) {
64 return curMarker - other.curMarker;
65 }
66
67 @Override
68 public boolean equals(final Object obj) {
69 if (obj instanceof App2Segment) {
70 final App2Segment other = (App2Segment) obj;
71 return curMarker == other.curMarker;
72 }
73 return false;
74 }
75
76
77
78
79 public byte[] getIccBytes() {
80 return iccBytes != null ? iccBytes.clone() : null;
81 }
82
83 @Override
84 public int hashCode() {
85 return curMarker;
86 }
87 }