/*
* Copyright (C) 2004 TiongHiang Lee
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*
* Email: thlee@onemindsoft.org
*/
package org.onemind.swingweb.image;
import java.awt.Graphics;
import java.awt.image.BufferedImage;
import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.util.Iterator;
import java.util.logging.Logger;
import javax.imageio.ImageIO;
import javax.imageio.ImageReader;
import javax.imageio.stream.ImageInputStream;
/**
* A image that wrap an byte array
* @author TiongHiang Lee (thlee@onemindsoft.org)
*
*/
public class BufferedByteImage extends AbstractImage
{
/** the log4j **/
private static final Logger _logger = Logger.getLogger(BufferedByteImage.class.getName());
/** the buffer **/
private byte[] _buffer;
/** the offset **/
private int _offset;
/** the length **/
private int _length;
private String _type = "image/gif";
/**
* Constructor
* @param g the graphics
* @param buffer the buffer
* @param offset the offset
* @param length the length
*/
public BufferedByteImage(Graphics g, byte[] buffer, int offset, int length)
{
super(g);
_buffer = buffer;
_offset = offset;
_length = length;
try
{
ImageInputStream imgInput = ImageIO.createImageInputStream(new ByteArrayInputStream(buffer));
Iterator it = ImageIO.getImageReaders(imgInput);
while (it.hasNext())
{
ImageReader reader = (ImageReader) it.next();
reader.setInput(imgInput, true);
setWidth(reader.getWidth(0));
setHeight(reader.getHeight(0));
_type = "image/" + reader.getFormatName();
}
} catch (IOException e)
{
try
{
BufferedImage bImg = ImageIO.read(new ByteArrayInputStream(buffer));
setWidth(bImg.getWidth());
setHeight(bImg.getHeight());
} catch (Exception ee)
{
//do nothing
}
_logger.throwing(getClass().getName(), "constructor", e);
}
}
/**
* Get the bytes;
* @return the bytes
*/
public byte[] getBytes()
{
return _buffer;
}
/**
* Return the type
* @return the type;
*/
public String getType()
{
return _type;
}
/**
* Return the length
* @return the length.
*/
public final int getLength()
{
return _length;
}
/**
* Return the offset
* @return the offset.
*/
public final int getOffset()
{
return _offset;
}
}