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  
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              // debugByteArray("Unknown APP2 Segment Type", bytes);
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       * @return the iccBytes
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  }