001/*
002 *   Licensed to the Apache Software Foundation (ASF) under one
003 *   or more contributor license agreements.  See the NOTICE file
004 *   distributed with this work for additional information
005 *   regarding copyright ownership.  The ASF licenses this file
006 *   to you under the Apache License, Version 2.0 (the
007 *   "License"); you may not use this file except in compliance
008 *   with the License.  You may obtain a copy of the License at
009 *
010 *     http://www.apache.org/licenses/LICENSE-2.0
011 *
012 *   Unless required by applicable law or agreed to in writing,
013 *   software distributed under the License is distributed on an
014 *   "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
015 *   KIND, either express or implied.  See the License for the
016 *   specific language governing permissions and limitations
017 *   under the License.
018 *
019 */
020package org.apache.mina.example.imagine.step1.codec;
021
022import org.apache.mina.filter.codec.ProtocolDecoderOutput;
023import org.apache.mina.filter.codec.CumulativeProtocolDecoder;
024import org.apache.mina.core.buffer.IoBuffer;
025import org.apache.mina.core.session.IoSession;
026import org.apache.mina.example.imagine.step1.ImageResponse;
027
028import javax.imageio.ImageIO;
029import java.awt.image.BufferedImage;
030import java.io.ByteArrayInputStream;
031import java.io.IOException;
032
033/**
034 * a decoder for {@link ImageResponse} objects 
035 *
036 * @author <a href="http://mina.apache.org">Apache MINA Project</a>
037 */
038
039public class ImageResponseDecoder extends CumulativeProtocolDecoder {
040
041    private static final String DECODER_STATE_KEY = ImageResponseDecoder.class.getName() + ".STATE";
042
043    public static final int MAX_IMAGE_SIZE = 5 * 1024 * 1024;
044
045    private static class DecoderState {
046        private BufferedImage image1;
047    }
048
049    protected boolean doDecode(IoSession session, IoBuffer in, ProtocolDecoderOutput out) throws Exception {
050        DecoderState decoderState = (DecoderState) session.getAttribute(DECODER_STATE_KEY);
051        if (decoderState == null) {
052            decoderState = new DecoderState();
053            session.setAttribute(DECODER_STATE_KEY, decoderState);
054        }
055        if (decoderState.image1 == null) {
056            // try to read first image
057            if (in.prefixedDataAvailable(4, MAX_IMAGE_SIZE)) {
058                decoderState.image1 = readImage(in);
059            } else {
060                // not enough data available to read first image
061                return false;
062            }
063        }
064        if (decoderState.image1 != null) {
065            // try to read second image
066            if (in.prefixedDataAvailable(4, MAX_IMAGE_SIZE)) {
067                BufferedImage image2 = readImage(in);
068                ImageResponse imageResponse = new ImageResponse(decoderState.image1, image2);
069                out.write(imageResponse);
070                decoderState.image1 = null;
071                return true;
072            } else {
073                // not enough data available to read second image
074                return false;
075            }
076        }
077        return false;
078    }
079
080    private BufferedImage readImage(IoBuffer in) throws IOException {
081        int length = in.getInt();
082        byte[] bytes = new byte[length];
083        in.get(bytes);
084        ByteArrayInputStream bais = new ByteArrayInputStream(bytes);
085        return ImageIO.read(bais);
086    }
087    
088}