/* * Copyright (c) 2005,2009 Declarative Engineering LLC. * All rights reserved. This program and the accompanying materials * are made available under the terms of the Declarative Engineering LLC * verson 1 which accompanies this distribution, and is available at * http://declarativeengineering.com/legal/DE_Developer_License_v1.txt */ package com.foundation.view; import java.io.Externalizable; import java.io.File; import java.io.FileInputStream; import java.io.FileNotFoundException; import java.io.IOException; import java.io.ObjectInput; import java.io.ObjectOutput; import com.common.debug.Debug; public class JefImage implements Externalizable, Cloneable { static final long serialVersionUID = -7739741206125442482L; public static final String[] SWT_SUPPORTED_IMAGE_EXTENSIONS = new String[] {"*.jpg;*.jpeg;*.gif;*.bmp;*.tiff;*.png;*.ico"}; /** The image data that can be used by the view system to initialize the view system specific image structure. */ private byte[] imageData = null; /** The hash code which is a simple hash of the image data. */ private int hashCode = 0; /** * JefImage default constructor. *

Note: The only reason this is public is to allow easy creation by the serialization code.

*/ public JefImage() { super(); }//JefImage()// /** * JefImage constructor. * @param fileName The path and file name to the image file. * @throws IOException Thrown if the file could not be found, opened, or read, or did not contain a valid image. */ public JefImage(String fileName) { super(); loadImageData(fileName); initialize(); }//JefImage()// /** * JefImage constructor. * @param imageData */ public JefImage(byte[] imageData) { super(); this.imageData = imageData; initialize(); }//JefImage()// /** * Gets the raw bytes for the image. * @return The image data. */ public byte[] getImageData() { return imageData; }//getImageData()// /** * Initializes the image data. * @param fileName The path and file name to the image file. */ private void loadImageData(String fileName) { File file = new File(fileName); boolean success = false; if(file.exists() && file.canRead() && file.isFile()) { FileInputStream in = null; try { int count = 0; in = new FileInputStream(file); imageData = new byte[in.available()]; while(count != imageData.length) { count += in.read((byte[]) imageData); }//while// success = true; }//try// catch(FileNotFoundException e) { Debug.log(e, "Unable to load the file: " + file); }//catch// catch(IOException e) { Debug.log(e, "Unable to load the file: " + file); }//catch// finally { try { in.close(); }//try// catch(Throwable e) { Debug.handle(e); }//catch// }//finally// }//if// if(!success) { Debug.log(new IOException("Failed to load the image: " + fileName)); }//if// }//loadImageData()// /** * Initializes the image data. * @param fileName The path and file name to the image file. */ private void initialize() { //Initialize the hash code.// if(imageData != null) { for(int index = 0; index < imageData.length; index++) { switch(index % 4) { case 0: hashCode ^= ((int) imageData[index]); break; case 1: hashCode ^= (((int) imageData[index]) << 8); break; case 2: hashCode ^= (((int) imageData[index]) << 16); break; case 3: hashCode ^= (((int) imageData[index]) << 24); break; default: break; }//switch// }//for// }//if// }//initialize()// /* (non-Javadoc) * @see java.io.Externalizable#readExternal(java.io.ObjectInput) */ public void readExternal(ObjectInput in) throws IOException, ClassNotFoundException { /*byte version = */in.readByte(); int imageLength = in.readInt(); int readCount = 0; imageData = new byte[imageLength]; while(readCount != imageData.length) { readCount += in.read(imageData, readCount, imageData.length - readCount); }//while// }//readExternal()// /* (non-Javadoc) * @see java.io.Externalizable#writeExternal(java.io.ObjectOutput) */ public void writeExternal(ObjectOutput out) throws IOException { out.writeByte(0); out.writeInt(imageData.length); out.write(imageData); }//writeExternal()// /* (non-Javadoc) * @see java.lang.Object#equals(java.lang.Object) */ public boolean equals(Object object) { boolean result = object instanceof JefImage && object.hashCode() == hashCode() && ((imageData == null && ((JefImage) object).getImageData() == null) || (((JefImage) object).getImageData() != null && imageData != null && ((JefImage) object).getImageData().length == imageData.length)); //Perform a full comparison on the image data if all else checks out.// if(result) { byte[] objectImageData = ((JefImage) object).getImageData(); for(int index = imageData.length - 1; (result) && (index >= 0); index--) { result = objectImageData[index] == imageData[index]; }//for// }//if// return result; }//equals()// /* (non-Javadoc) * @see java.lang.Object#hashCode() */ public int hashCode() { return hashCode; }//hashCode()// }//JefImage//