001: /*
002: * Copyright (C) 2004 TiongHiang Lee
003: *
004: * This library is free software; you can redistribute it and/or
005: * modify it under the terms of the GNU Lesser General Public
006: * License as published by the Free Software Foundation; either
007: * version 2.1 of the License, or (at your option) any later version.
008: *
009: * This library is distributed in the hope that it will be useful,
010: * but WITHOUT ANY WARRANTY; without even the implied warranty of
011: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
012: * Lesser General Public License for more details.
013: *
014: * You should have received a copy of the GNU Lesser General Public
015: * License along with this library; if not, write to the Free Software
016: * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
017: *
018: * Email: thlee@onemindsoft.org
019: */
020:
021: package org.onemind.swingweb.image;
022:
023: import java.awt.Graphics;
024: import java.awt.image.BufferedImage;
025: import java.io.ByteArrayInputStream;
026: import java.io.IOException;
027: import java.util.Iterator;
028: import java.util.logging.Logger;
029: import javax.imageio.ImageIO;
030: import javax.imageio.ImageReader;
031: import javax.imageio.stream.ImageInputStream;
032:
033: /**
034: * A image that wrap an byte array
035: * @author TiongHiang Lee (thlee@onemindsoft.org)
036: *
037: */
038: public class BufferedByteImage extends AbstractImage {
039:
040: /** the log4j **/
041: private static final Logger _logger = Logger
042: .getLogger(BufferedByteImage.class.getName());
043:
044: /** the buffer **/
045: private byte[] _buffer;
046:
047: /** the offset **/
048: private int _offset;
049:
050: /** the length **/
051: private int _length;
052:
053: private String _type = "image/gif";
054:
055: /**
056: * Constructor
057: * @param g the graphics
058: * @param buffer the buffer
059: * @param offset the offset
060: * @param length the length
061: */
062: public BufferedByteImage(Graphics g, byte[] buffer, int offset,
063: int length) {
064: super (g);
065: _buffer = buffer;
066: _offset = offset;
067: _length = length;
068: try {
069: ImageInputStream imgInput = ImageIO
070: .createImageInputStream(new ByteArrayInputStream(
071: buffer));
072: Iterator it = ImageIO.getImageReaders(imgInput);
073: while (it.hasNext()) {
074: ImageReader reader = (ImageReader) it.next();
075: reader.setInput(imgInput, true);
076: setWidth(reader.getWidth(0));
077: setHeight(reader.getHeight(0));
078: _type = "image/" + reader.getFormatName();
079: }
080: } catch (IOException e) {
081: try {
082: BufferedImage bImg = ImageIO
083: .read(new ByteArrayInputStream(buffer));
084: setWidth(bImg.getWidth());
085: setHeight(bImg.getHeight());
086: } catch (Exception ee) {
087: //do nothing
088: }
089: _logger.throwing(getClass().getName(), "constructor", e);
090: }
091: }
092:
093: /**
094: * Get the bytes;
095: * @return the bytes
096: */
097: public byte[] getBytes() {
098: return _buffer;
099: }
100:
101: /**
102: * Return the type
103: * @return the type;
104: */
105: public String getType() {
106: return _type;
107: }
108:
109: /**
110: * Return the length
111: * @return the length.
112: */
113: public final int getLength() {
114: return _length;
115: }
116:
117: /**
118: * Return the offset
119: * @return the offset.
120: */
121: public final int getOffset() {
122: return _offset;
123: }
124: }
|