View Javadoc
1   /*
2    *   Licensed to the Apache Software Foundation (ASF) under one
3    *   or more contributor license agreements.  See the NOTICE file
4    *   distributed with this work for additional information
5    *   regarding copyright ownership.  The ASF licenses this file
6    *   to you under the Apache License, Version 2.0 (the
7    *   "License"); you may not use this file except in compliance
8    *   with the License.  You may obtain a copy of the License at
9    *
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.core.buffer.IoBuffer;
25  import org.apache.mina.core.session.IoSession;
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 <a href="http://mina.apache.org">Apache MINA Project</a>
37   */
38  
39  public class ImageResponseDecoder extends CumulativeProtocolDecoder {
40  
41      private static final String DECODER_STATE_KEY = ImageResponseDecoder.class.getName() + ".STATE";
42  
43      public static final int MAX_IMAGE_SIZE = 5 * 1024 * 1024;
44  
45      private static class DecoderState {
46          private BufferedImage image1;
47      }
48  
49      protected boolean doDecode(IoSession session, IoBuffer in, ProtocolDecoderOutput out) throws Exception {
50          DecoderState decoderState = (DecoderState) session.getAttribute(DECODER_STATE_KEY);
51          if (decoderState == null) {
52              decoderState = new DecoderState();
53              session.setAttribute(DECODER_STATE_KEY, decoderState);
54          }
55          if (decoderState.image1 == null) {
56              // try to read first image
57              if (in.prefixedDataAvailable(4, MAX_IMAGE_SIZE)) {
58                  decoderState.image1 = readImage(in);
59              } else {
60                  // not enough data available to read first image
61                  return false;
62              }
63          }
64          if (decoderState.image1 != null) {
65              // try to read second image
66              if (in.prefixedDataAvailable(4, MAX_IMAGE_SIZE)) {
67                  BufferedImage image2 = readImage(in);
68                  ImageResponse imageResponse = new ImageResponse(decoderState.image1, image2);
69                  out.write(imageResponse);
70                  decoderState.image1 = null;
71                  return true;
72              } else {
73                  // not enough data available to read second image
74                  return false;
75              }
76          }
77          return false;
78      }
79  
80      private BufferedImage readImage(IoBuffer in) throws IOException {
81          int length = in.getInt();
82          byte[] bytes = new byte[length];
83          in.get(bytes);
84          ByteArrayInputStream bais = new ByteArrayInputStream(bytes);
85          return ImageIO.read(bais);
86      }
87      
88  }