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