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.filter.codec.demux;
21  
22  import org.apache.mina.core.buffer.IoBuffer;
23  import org.apache.mina.core.session.IoSession;
24  import org.apache.mina.filter.codec.ProtocolDecoderOutput;
25  
26  /**
27   * Decodes a certain type of messages.
28   * <p>
29   * We didn't provide any <tt>dispose</tt> method for {@link MessageDecoder}
30   * because it can give you  performance penalty in case you have a lot of
31   * message types to handle.
32   *
33   * @author <a href="http://mina.apache.org">Apache MINA Project</a>
34   *
35   * @see DemuxingProtocolDecoder
36   * @see MessageDecoderFactory
37   */
38  public interface MessageDecoder {
39      /**
40       * Represents a result from {@link #decodable(IoSession, IoBuffer)} and
41       * {@link #decode(IoSession, IoBuffer, ProtocolDecoderOutput)}.  Please
42       * refer to each method's documentation for detailed explanation.
43       */
44      MessageDecoderResult OK = MessageDecoderResult.OK;
45  
46      /**
47       * Represents a result from {@link #decodable(IoSession, IoBuffer)} and
48       * {@link #decode(IoSession, IoBuffer, ProtocolDecoderOutput)}.  Please
49       * refer to each method's documentation for detailed explanation.
50       */
51      MessageDecoderResult NEED_DATA = MessageDecoderResult.NEED_DATA;
52  
53      /**
54       * Represents a result from {@link #decodable(IoSession, IoBuffer)} and
55       * {@link #decode(IoSession, IoBuffer, ProtocolDecoderOutput)}.  Please
56       * refer to each method's documentation for detailed explanation.
57       */
58      MessageDecoderResult NOT_OK = MessageDecoderResult.NOT_OK;
59  
60      /**
61       * Checks the specified buffer is decodable by this decoder.
62       *
63       * @param session The current session
64       * @param in The buffer containing the data to decode
65       * @return {@link #OK} if this decoder can decode the specified buffer.
66       *         {@link #NOT_OK} if this decoder cannot decode the specified buffer.
67       *         {@link #NEED_DATA} if more data is required to determine if the
68       *         specified buffer is decodable ({@link #OK}) or not decodable
69       *         {@link #NOT_OK}.
70       */
71      MessageDecoderResult decodable(IoSession session, IoBuffer in);
72  
73      /**
74       * Decodes binary or protocol-specific content into higher-level message objects.
75       * MINA invokes {@link #decode(IoSession, IoBuffer, ProtocolDecoderOutput)}
76       * method with read data, and then the decoder implementation puts decoded
77       * messages into {@link ProtocolDecoderOutput}.
78       *
79       * @param session The current session
80       * @param in The buffer containing the data to decode
81       * @param out The instance of {@link ProtocolDecoderOutput} that will receive the decoded messages
82       * @return {@link #OK} if you finished decoding messages successfully.
83       *         {@link #NEED_DATA} if you need more data to finish decoding current message.
84       *         {@link #NOT_OK} if you cannot decode current message due to protocol specification violation.
85       * @throws Exception if the read data violated protocol specification
86       */
87      MessageDecoderResult decode(IoSession session, IoBuffer in, ProtocolDecoderOutput out) throws Exception;
88  
89      /**
90       * Invoked when the specified <tt>session</tt> is closed while this decoder was
91       * parsing the data.  This method is useful when you deal with the protocol which doesn't
92       * specify the length of a message such as HTTP response without <tt>content-length</tt>
93       * header. Implement this method to process the remaining data that
94       * {@link #decode(IoSession, IoBuffer, ProtocolDecoderOutput)} method didn't process
95       * completely.
96       *
97       * @param session The current session
98       * @param out The instance of {@link ProtocolDecoderOutput} that contains the decoded messages
99       * @throws Exception if the read data violated protocol specification
100      */
101     void finishDecode(IoSession session, ProtocolDecoderOutput out) throws Exception;
102 }