001: /**
002: * ===========================================
003: * JFreeReport : a free Java reporting library
004: * ===========================================
005: *
006: * Project Info: http://reporting.pentaho.org/
007: *
008: * (C) Copyright 2001-2007, by Object Refinery Ltd, Pentaho Corporation and Contributors.
009: *
010: * This library is free software; you can redistribute it and/or modify it under the terms
011: * of the GNU Lesser General Public License as published by the Free Software Foundation;
012: * either version 2.1 of the License, or (at your option) any later version.
013: *
014: * This library is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
015: * without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
016: * See the GNU Lesser General Public License for more details.
017: *
018: * You should have received a copy of the GNU Lesser General Public License along with this
019: * library; if not, write to the Free Software Foundation, Inc., 59 Temple Place, Suite 330,
020: * Boston, MA 02111-1307, USA.
021: *
022: * [Java is a trademark or registered trademark of Sun Microsystems, Inc.
023: * in the United States and other countries.]
024: *
025: * ------------
026: * ImageComparator.java
027: * ------------
028: * (C) Copyright 2001-2007, by Object Refinery Ltd, Pentaho Corporation and Contributors.
029: */package org.jfree.report.util;
030:
031: import java.security.MessageDigest;
032: import java.security.NoSuchAlgorithmException;
033: import java.util.Arrays;
034:
035: import org.jfree.util.Log;
036:
037: /**
038: * The ImageComparator tries to compare a byte[] for equality by creating 2 hashes for the
039: * bytearray and comparing thoose hashes. If no digest algorithms are available, then the
040: * complete byte[] is used for comparison.
041: * <p>
042: * Using the digest method trades computing time for space.
043: *
044: * @author Thomas Morgner
045: * @deprecated This class is no longer used anywhere.
046: */
047: public class ImageComparator {
048: /**
049: * A ImageCompareData that uses the complete image data for comparison.
050: */
051: private static final class CompleteImageCompareData {
052: /**
053: * The image content.
054: */
055: private final byte[] image;
056:
057: /**
058: * Create a new CompleteImageCompareData instance.
059: *
060: * @param image the image data used for comparison.
061: */
062: private CompleteImageCompareData(final byte[] image) {
063: this .image = image;
064: }
065:
066: /**
067: * Checks whether the given Object equals this object.
068: *
069: * @param o the to be compared object
070: * @return true, if both objects are equal
071: */
072: public boolean equals(final Object o) {
073: if (this == o) {
074: return true;
075: }
076: if (!(o instanceof CompleteImageCompareData)) {
077: return false;
078: }
079:
080: final CompleteImageCompareData data = (CompleteImageCompareData) o;
081:
082: if (!Arrays.equals(image, data.image)) {
083: return false;
084: }
085:
086: return true;
087: }
088:
089: /**
090: * returns a hashcode for this class.
091: *
092: * @return always 0.
093: */
094: public int hashCode() {
095: return image.length;
096: }
097: }
098:
099: /**
100: * An ImageComparator which uses precomputed Message-Digests to compare the image.
101: */
102: private static final class DigestImageCompareData {
103: /**
104: * An MD5 digest.
105: */
106: private byte[] digestMD5Data;
107:
108: /**
109: * An SHA digest.
110: */
111: private byte[] digestSHAData;
112:
113: /**
114: * Creates a new DigestImageCompareData instance.
115: *
116: * @param digestMD5Data the MD5 digest data
117: * @param digestSHAData the SHA1 digest data
118: */
119: private DigestImageCompareData(final byte[] digestMD5Data,
120: final byte[] digestSHAData) {
121: if (digestMD5Data == null || digestSHAData == null) {
122: throw new NullPointerException();
123: }
124: this .digestMD5Data = digestMD5Data;
125: this .digestSHAData = digestSHAData;
126: }
127:
128: /**
129: * Checks whether the given Object equals this object.
130: *
131: * @param o the to be compared object
132: * @return true, if both objects are equal
133: */
134: public boolean equals(final Object o) {
135: if (this == o) {
136: return true;
137: }
138: if (!(o instanceof DigestImageCompareData)) {
139: return false;
140: }
141:
142: final DigestImageCompareData data = (DigestImageCompareData) o;
143:
144: if (!Arrays.equals(digestMD5Data, data.digestMD5Data)) {
145: return false;
146: }
147: if (!Arrays.equals(digestSHAData, data.digestSHAData)) {
148: return false;
149: }
150:
151: return true;
152: }
153:
154: /**
155: * returns a hashcode for this class.
156: *
157: * @return always 0.
158: */
159: public int hashCode() {
160: return 0;
161: }
162: }
163:
164: /**
165: * An MD5 message digest.
166: */
167: private MessageDigest digestMD5;
168:
169: /**
170: * An SHA message digest.
171: */
172: private MessageDigest digestSHA;
173:
174: /**
175: * Creates a new ImageComparator. The comparator checks whether the MD5 and the SHA
176: * message digest implementations are available. If they are not available, an
177: * alternative comparison method is used, which is more memory consuming.
178: */
179: public ImageComparator() {
180: try {
181: digestMD5 = MessageDigest.getInstance("MD5");
182: } catch (NoSuchAlgorithmException nse) {
183: Log.info("No MD5 algorithm available");
184: }
185: try {
186: digestSHA = MessageDigest.getInstance("SHA");
187: } catch (NoSuchAlgorithmException nse) {
188: Log.info("No SHA algorithm available");
189: }
190: }
191:
192: /**
193: * Creates 2 comparable objects. These objects can be compared for equality.
194: *
195: * @param image the image data which should be prepared for comparison
196: * @param fast whether to prefer the memory intensive faster compare method to the
197: * digest based comparation. This may result in outofmemory errors on huge
198: * reports or images.
199: * @return the prepared image data.
200: */
201: public Object createCompareData(final byte[] image,
202: final boolean fast) {
203: if (fast == false && (digestMD5 != null && digestSHA != null)) {
204: final byte[] dataMD5 = digestMD5.digest(image);
205: final byte[] dataSHA = digestSHA.digest(image);
206: if (dataSHA != null && dataMD5 != null) {
207: return new DigestImageCompareData(dataMD5, dataSHA);
208: }
209: }
210: return new CompleteImageCompareData(image);
211: }
212: }
|