Coverage Report - us.daveread.utility.formatcheck.images.ImageUtility
 
Classes in this File Line Coverage Branch Coverage Complexity
ImageUtility
0%
0/34
0%
0/4
2.2
 
 1  
 package us.daveread.utility.formatcheck.images;
 2  
 
 3  
 import java.io.*;
 4  
 import java.net.*;
 5  
 import org.apache.log4j.Logger;
 6  
 
 7  
 /**
 8  
  * <p>Title: ImageUtility
 9  
  * <p>Description: General image access utility methods.  In all cases
 10  
  * the images are loaded from this class' package.
 11  
  * <p>Copyright: Copyright (c) 2005
 12  
  * <p>This program is free software; you can redistribute it and/or modify
 13  
  * it under the terms of the GNU General Public License as published by
 14  
  * the Free Software Foundation; either version 2 of the License, or
 15  
  * (at your option) any later version.
 16  
  * <p>This program is distributed in the hope that it will be useful,
 17  
  * but WITHOUT ANY WARRANTY; without even the implied warranty of
 18  
  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 19  
  * GNU General Public License for more details.
 20  
  * <p>You should have received a copy of the GNU General Public License
 21  
  * along with this program; if not, write to the Free Software
 22  
  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 23  
  * </p>
 24  
  *
 25  
  * @author David Read
 26  
  * @version $Id: ImageUtility.java,v 1.2 2006/06/14 23:34:45 daveread Exp $
 27  
  */
 28  
 public class ImageUtility {
 29  
 
 30  
     /** Instance handle for the singleton */
 31  
     private static ImageUtility objcSingleton;
 32  
 
 33  
     /** The number of bytes to load per read */
 34  0
     private static int icReadSize = 1000;
 35  
 
 36  
     /** Logger */
 37  0
     private static final Logger logger = Logger.getLogger(ImageUtility.class);
 38  
 
 39  
     /**
 40  
      * Constructor is private for singleton.
 41  
      */
 42  0
     private ImageUtility() {
 43  0
     }
 44  
 
 45  
     /**
 46  
      * Get the instance of this singleton, private as only the static utility
 47  
      * methods can have access to the class.
 48  
      *
 49  
      * @return The single instance of the class.
 50  
      */
 51  
     private static synchronized ImageUtility instance() {
 52  0
         if (objcSingleton == null) {
 53  0
             objcSingleton = new ImageUtility();
 54  
         }
 55  
 
 56  0
         return objcSingleton;
 57  
     }
 58  
 
 59  
     /**
 60  
      * Get the image as a stream.
 61  
      *
 62  
      * @param saImageName The file name of the image to be loaded.
 63  
      *
 64  
      * @return The InputStream opened for the image file.
 65  
      */
 66  
     public static InputStream getImageAsStream(String saImageName) {
 67  0
         return instance().getClass().getResourceAsStream(saImageName);
 68  
     }
 69  
 
 70  
     /**
 71  
      * Get the image as a byte array.
 72  
      *
 73  
      * @param saImageName The file name of the image to be loaded.
 74  
      *
 75  
      * @return The byte array containing the image data.
 76  
      */
 77  
     public static byte[] getImageAsByteArray(String saImageName) {
 78  
         byte bylImage[], bylTemp[];
 79  
         InputStream islImage;
 80  
         int ilSize, ilRead, ilPosit;
 81  
 
 82  0
         islImage = getImageAsStream(saImageName);
 83  0
         bylImage = null;
 84  0
         bylTemp = new byte[icReadSize];
 85  0
         ilSize = 0;
 86  
 
 87  0
         logger.info("Image [" + saImageName + "]");
 88  
 
 89  
         try {
 90  0
             while ((ilRead = islImage.read(bylTemp)) >= 0) {
 91  0
                 ilSize += ilRead;
 92  
             }
 93  0
         } catch (Exception excAny) {
 94  0
             logger.error("Error reading image file", excAny);
 95  0
         }
 96  
 
 97  0
         logger.info("Image Size [" + ilSize + "]");
 98  
 
 99  0
         if (ilSize > 0) {
 100  
             try {
 101  0
                 islImage = getImageAsStream(saImageName);
 102  0
                 bylImage = new byte[ilSize];
 103  0
                 ilPosit = 0;
 104  
                 while ((ilRead = islImage.read(bylImage, ilPosit,
 105  
                                                ilSize - ilPosit)) >
 106  0
                                  -1 && ilPosit < ilSize) {
 107  0
                     ilPosit += ilRead;
 108  0
                     logger.debug("Loading, Bytes[" + ilRead + "]  Total[" +
 109  
                                  ilPosit + "]  Expect[" + ilSize + "]");
 110  
                 }
 111  0
             } catch (Exception excAny) {
 112  0
                 logger.error("Error loading image", excAny);
 113  0
             }
 114  
         }
 115  
 
 116  0
         return bylImage;
 117  
     }
 118  
 
 119  
 
 120  
     /**
 121  
      * Get the image as a URL.
 122  
      *
 123  
      * @param saImageName The file name of the image to be loaded.
 124  
      *
 125  
      * @return The URL of the image.
 126  
      *  <strong>Under JDK1.3.1_01 on windows it appears that the underlying
 127  
      *      Class.getResource() call is not supported.</strong>
 128  
      */
 129  
     public static URL getImageAsURL(String saImageName) {
 130  0
         logger.info("Image [" + saImageName + "]");
 131  0
         logger.info("Class [" + instance().getClass() + "]");
 132  0
         logger.info("URL [" + instance().getClass().getResource(saImageName) +
 133  
                     "]");
 134  
 
 135  0
         return instance().getClass().getResource(saImageName);
 136  
     }
 137  
 }