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