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.filter.codec;
021
022import org.apache.mina.core.buffer.IoBuffer;
023import org.apache.mina.core.session.IoSession;
024
025/**
026 * Decodes binary or protocol-specific data into higher-level message objects.
027 * MINA invokes {@link #decode(IoSession, IoBuffer, ProtocolDecoderOutput)}
028 * method with read data, and then the decoder implementation puts decoded
029 * messages into {@link ProtocolDecoderOutput} by calling
030 * {@link ProtocolDecoderOutput#write(Object)}.
031 * <p>
032 * Please refer to
033 * <a href="../../../../../xref-examples/org/apache/mina/examples/reverser/TextLineDecoder.html"><code>TextLineDecoder</code></a>
034 * example.
035 *
036 * @author <a href="http://mina.apache.org">Apache MINA Project</a>
037 * 
038 * @see ProtocolDecoderException
039 */
040public interface ProtocolDecoder {
041    /**
042     * Decodes binary or protocol-specific content into higher-level message objects.
043     * MINA invokes {@link #decode(IoSession, IoBuffer, ProtocolDecoderOutput)}
044     * method with read data, and then the decoder implementation puts decoded
045     * messages into {@link ProtocolDecoderOutput}.
046     *
047     * @param session The current Session
048     * @param in the buffer to decode
049     * @param out The {@link ProtocolDecoderOutput} that will receive the decoded message
050     * @throws Exception if the read data violated protocol specification
051     */
052    void decode(IoSession session, IoBuffer in, ProtocolDecoderOutput out) throws Exception;
053
054    /**
055     * Invoked when the specified <tt>session</tt> is closed.  This method is useful
056     * when you deal with the protocol which doesn't specify the length of a message
057     * such as HTTP response without <tt>content-length</tt> header. Implement this
058     * method to process the remaining data that {@link #decode(IoSession, IoBuffer, ProtocolDecoderOutput)}
059     * method didn't process completely.
060     *
061     * @param session The current Session
062     * @param out The {@link ProtocolDecoderOutput} that contains the decoded message
063     * @throws Exception if the read data violated protocol specification
064     */
065    void finishDecode(IoSession session, ProtocolDecoderOutput out) throws Exception;
066
067    /**
068     * Releases all resources related with this decoder.
069     *
070     * @param session The current Session
071     * @throws Exception if failed to dispose all resources
072     */
073    void dispose(IoSession session) throws Exception;
074}