Package kameleon.gui.util

Source Code of kameleon.gui.util.ImageUtility

/*
* Copyright (c) 2012, Fromentin Xavier, Schnell Michaël, Dervin Cyrielle, Brabant Quentin
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
*      * Redistributions of source code must retain the above copyright
*       notice, this list of conditions and the following disclaimer.
*      * Redistributions in binary form must reproduce the above copyright
*       notice, this list of conditions and the following disclaimer in the
*       documentation and/or other materials provided with the distribution.
*      * The names of its contributors may not be used to endorse or promote products
*       derived from this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
* WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
* DISCLAIMED. IN NO EVENT SHALL Fromentin Xavier, Schnell Michaël, Dervin Cyrielle OR Brabant Quentin
* BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
* (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
* LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
* ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
package kameleon.gui.util;

import java.io.BufferedInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;

import kameleon.exception.FileReadingException;

/**
* Utility class for retrieving bytes from a given picture file.
*
* @author    Schnell Michaël
* @version    1.0
*/
public class ImageUtility {

  /**
   * Returns an array containing all the bytes read from a given
   * {@code InputStream}.
   *
   * @param   is
   *       source stream for the read bytes
   *
   * @return  Array containing all the bytes read from the given
   *       stream
   *
   * @throws   FileReadingException
   *       if an error occurred while reading the stream
   */
  public static byte[] getImageBytes(InputStream is)
      throws FileReadingException {
    // Read all the bytes into a buffer
    ArrayList<Integer> buffer = new ArrayList<Integer>() ;
    try {
      int next = is.read() ;
      while (next != -1) {
        buffer.add(new Integer(next)) ;
        next = is.read() ;
      }// while
    } catch (IOException e) {
      throw new FileReadingException() ;
    }// try
   
    // Copy the content of the buffer into an array
    byte[] imageBytes = new byte[buffer.size()] ;
    int index = 0 ;
    for(Integer b : buffer) {
      imageBytes[index] = b.byteValue() ;
      index++ ;
    }// for
   
    return imageBytes ;
  }// getImagesBytes(InputStream)

  /**
   * Returns an array containing all the bytes read from the file
   * with the given path.
   *
   * @param   source
   *       absolute path of the source file
   *
   * @return  Array containing all the bytes read from the given
   *       file
   *
   * @throws   FileReadingException
   *       if an error occurred while reading the stream
   */
  public static byte[] getImageBytes(String source)
      throws FileReadingException {
    try {
      InputStream is = new BufferedInputStream(
          new FileInputStream(source));
      byte[] result = getImageBytes(is) ;
      is.close() ;
      return result ;
    } catch (FileNotFoundException e) {
      throw new FileReadingException(new File(source)) ;
    } catch (IOException e) {
      throw new FileReadingException(new File(source)) ;
    }// try
  }// getImageBytes(String)

}// class ImageUtility
TOP

Related Classes of kameleon.gui.util.ImageUtility

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.