01: /*
02: * Licensed to the Apache Software Foundation (ASF) under one
03: * or more contributor license agreements. See the NOTICE file
04: * distributed with this work for additional information
05: * regarding copyright ownership. The ASF licenses this file
06: * to you under the Apache License, Version 2.0 (the
07: * "License"); you may not use this file except in compliance
08: * with the License. You may obtain a copy of the License at
09: *
10: * http://www.apache.org/licenses/LICENSE-2.0
11: *
12: * Unless required by applicable law or agreed to in writing,
13: * software distributed under the License is distributed on an
14: * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15: * KIND, either express or implied. See the License for the
16: * specific language governing permissions and limitations
17: * under the License.
18: *
19: */
20: package org.apache.mina.example.imagine.step1.codec;
21:
22: import org.apache.mina.filter.codec.ProtocolDecoderOutput;
23: import org.apache.mina.filter.codec.CumulativeProtocolDecoder;
24: import org.apache.mina.common.IoSession;
25: import org.apache.mina.common.IoBuffer;
26: import org.apache.mina.example.imagine.step1.ImageResponse;
27:
28: import javax.imageio.ImageIO;
29: import java.awt.image.BufferedImage;
30: import java.io.ByteArrayInputStream;
31: import java.io.IOException;
32:
33: /**
34: * a decoder for {@link ImageResponse} objects
35: *
36: * @author The Apache MINA Project (dev@mina.apache.org)
37: * @version $Rev$, $Date$
38: */
39:
40: public class ImageResponseDecoder extends CumulativeProtocolDecoder {
41:
42: private static final String DECODER_STATE_KEY = ImageResponseDecoder.class
43: .getName()
44: + ".STATE";
45:
46: public static final int MAX_IMAGE_SIZE = 5 * 1024 * 1024;
47:
48: private static class DecoderState {
49: BufferedImage image1;
50: }
51:
52: protected boolean doDecode(IoSession session, IoBuffer in,
53: ProtocolDecoderOutput out) throws Exception {
54: DecoderState decoderState = (DecoderState) session
55: .getAttribute(DECODER_STATE_KEY);
56: if (decoderState == null) {
57: decoderState = new DecoderState();
58: session.setAttribute(DECODER_STATE_KEY, decoderState);
59: }
60: if (decoderState.image1 == null) {
61: // try to read first image
62: if (in.prefixedDataAvailable(4, MAX_IMAGE_SIZE)) {
63: decoderState.image1 = readImage(in);
64: } else {
65: // not enough data available to read first image
66: return false;
67: }
68: }
69: if (decoderState.image1 != null) {
70: // try to read second image
71: if (in.prefixedDataAvailable(4, MAX_IMAGE_SIZE)) {
72: BufferedImage image2 = readImage(in);
73: ImageResponse imageResponse = new ImageResponse(
74: decoderState.image1, image2);
75: out.write(imageResponse);
76: decoderState.image1 = null;
77: return true;
78: } else {
79: // not enough data available to read second image
80: return false;
81: }
82: }
83: return false;
84: }
85:
86: private BufferedImage readImage(IoBuffer in) throws IOException {
87: int length = in.getInt();
88: byte[] bytes = new byte[length];
89: in.get(bytes);
90: ByteArrayInputStream bais = new ByteArrayInputStream(bytes);
91: return ImageIO.read(bais);
92: }
93:
94: }
|