001/*
002 * This file is part of the JDrupes non-blocking HTTP Codec
003 * Copyright (C) 2016, 2017  Michael N. Lipp
004 *
005 * This program is free software; you can redistribute it and/or modify it 
006 * under the terms of the GNU Lesser General Public License as published
007 * by the Free Software Foundation; either version 3 of the License, or 
008 * (at your option) any later version.
009 *
010 * This program is distributed in the hope that it will be useful, but 
011 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
012 * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public 
013 * License for more details.
014 *
015 * You should have received a copy of the GNU Lesser General Public License along 
016 * with this program; if not, see <http://www.gnu.org/licenses/>.
017 */
018
019package org.jdrupes.httpcodec;
020
021import java.nio.Buffer;
022import java.nio.ByteBuffer;
023import java.util.Optional;
024
025/**
026 * The general interface of an encoder.
027 * 
028 * @param <T> the type of the message header to be encoded
029 * @param <D> the type of the message header decoded by the peer decoder
030 */
031public interface Encoder<T extends MessageHeader, D extends MessageHeader>
032        extends Codec {
033
034        /**
035         * Sets the peer decoder. Some decoder implementations need to know
036         * the state of the decoder or the last decoded message.
037         *
038         * @param decoder the decoder
039         * @return the encoder
040         */
041        Encoder<T, D> setPeerDecoder(Decoder<D, T> decoder);
042        
043        /**
044         * Returns the type of the messages encoded by this encoder.
045         * 
046         * @return the value
047         */
048        Class<T> encoding();
049        
050        /**
051         * Set the header of the message that is to be encoded. Must be invoked
052         * before the first invocation to any {@code encode} method for a given
053         * message.
054         * 
055         * @param messageHeader
056         *            the message header
057         */
058        public void encode(T messageHeader);
059        
060        /**
061         * Encodes a message. First encodes the message header set by
062         * {@link #encode(MessageHeader)} and then (optionally) adds payload 
063         * data from {@code in}.
064         * 
065         * @param in
066         *            the body data
067         * @param out
068         *            the buffer to which data is written
069         * @param endOfInput
070         *            {@code true} if there is no input left beyond the data
071         *            currently in the {@code in} buffer (indicates end of body or
072         *            no body at all)
073         * @return the result
074         */
075        public Result encode(Buffer in, ByteBuffer out,
076                boolean endOfInput)     ;
077
078        /**
079         * Convenience method for invoking
080         * {@link #encode(Buffer, ByteBuffer, boolean)} with an empty {@code in}
081         * buffer and {@code true}. Can be used to get the result of encoding a 
082         * message without body.
083         * 
084         * @param out
085         *            the buffer to which data is written
086         * @return the result
087         */
088        public default Result encode(ByteBuffer out) {
089                return encode(EMPTY_IN, out, true);
090        }
091
092        /**
093         * Returns the last message (header) encoded. 
094         * 
095         * @return the result
096         */
097        public Optional<T> header();
098        
099}