001/* 002 * Licensed to the Apache Software Foundation (ASF) under one or more 003 * contributor license agreements. See the NOTICE file distributed with 004 * this work for additional information regarding copyright ownership. 005 * The ASF licenses this file to You under the Apache License, Version 2.0 006 * (the "License"); you may not use this file except in compliance with 007 * the License. You may obtain a copy of the License at 008 * 009 * http://www.apache.org/licenses/LICENSE-2.0 010 * 011 * Unless required by applicable law or agreed to in writing, software 012 * distributed under the License is distributed on an "AS IS" BASIS, 013 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 014 * See the License for the specific language governing permissions and 015 * limitations under the License. 016 */ 017package org.apache.commons.imaging.formats.jpeg.segments; 018 019import static org.apache.commons.imaging.common.BinaryFunctions.readAndVerifyBytes; 020import static org.apache.commons.imaging.common.BinaryFunctions.readByte; 021import static org.apache.commons.imaging.common.BinaryFunctions.readBytes; 022 023import java.io.ByteArrayInputStream; 024import java.io.IOException; 025import java.io.InputStream; 026 027import org.apache.commons.imaging.ImagingException; 028import org.apache.commons.imaging.formats.jpeg.JpegConstants; 029 030public final class App2Segment extends AppnSegment implements Comparable<App2Segment> { 031 private final byte[] iccBytes; 032 public final int curMarker; 033 public final int numMarkers; 034 035 public App2Segment(final int marker, final byte[] segmentData) throws ImagingException, IOException { 036 this(marker, segmentData.length, new ByteArrayInputStream(segmentData)); 037 } 038 039 public App2Segment(final int marker, int markerLength, final InputStream is2) throws ImagingException, IOException { 040 super(marker, markerLength, is2); 041 042 if (JpegConstants.ICC_PROFILE_LABEL.isStartOf(getSegmentData())) { 043 final InputStream is = new ByteArrayInputStream(getSegmentData()); 044 045 readAndVerifyBytes(is, JpegConstants.ICC_PROFILE_LABEL, "Not a Valid App2 Segment: missing ICC Profile label"); 046 047 curMarker = readByte("curMarker", is, "Not a valid App2 Marker"); 048 numMarkers = readByte("numMarkers", is, "Not a valid App2 Marker"); 049 050 markerLength -= JpegConstants.ICC_PROFILE_LABEL.size(); 051 markerLength -= 1 + 1; 052 053 iccBytes = readBytes("App2 Data", is, markerLength, "Invalid App2 Segment: insufficient data"); 054 } else { 055 // debugByteArray("Unknown APP2 Segment Type", bytes); 056 curMarker = -1; 057 numMarkers = -1; 058 iccBytes = null; 059 } 060 } 061 062 @Override 063 public int compareTo(final App2Segment other) { 064 return curMarker - other.curMarker; 065 } 066 067 @Override 068 public boolean equals(final Object obj) { 069 if (obj instanceof App2Segment) { 070 final App2Segment other = (App2Segment) obj; 071 return curMarker == other.curMarker; 072 } 073 return false; 074 } 075 076 /** 077 * @return the iccBytes 078 */ 079 public byte[] getIccBytes() { 080 return iccBytes != null ? iccBytes.clone() : null; 081 } 082 083 @Override 084 public int hashCode() { 085 return curMarker; 086 } 087}