Class BufferedWriteFilter

  • All Implemented Interfaces:
    IoFilter

    public final class BufferedWriteFilter
    extends IoFilterAdapter
    An IoFilter implementation used to buffer outgoing WriteRequest almost like what BufferedOutputStream does. Using this filter allows to be less dependent from network latency. It is also useful when a session is generating very small messages too frequently and consequently generating unnecessary traffic overhead. Please note that it should always be placed before the ProtocolCodecFilter as it only handles WriteRequest's carrying IoBuffer objects.
    Since:
    MINA 2.0.0-M2
    • Field Detail

      • LOGGER

        private static final org.slf4j.Logger LOGGER
      • DEFAULT_BUFFER_SIZE

        public static final int DEFAULT_BUFFER_SIZE
        Default buffer size value in bytes.
        See Also:
        Constant Field Values
      • bufferSize

        private int bufferSize
        The buffer size allocated for each new session's buffer.
    • Constructor Detail

      • BufferedWriteFilter

        public BufferedWriteFilter()
        Default constructor. Sets buffer size to DEFAULT_BUFFER_SIZE bytes. Uses a default instance of ConcurrentHashMap.
      • BufferedWriteFilter

        public BufferedWriteFilter​(int bufferSize)
        Constructor which sets buffer size to bufferSize.Uses a default instance of ConcurrentHashMap.
        Parameters:
        bufferSize - the new buffer size
      • BufferedWriteFilter

        public BufferedWriteFilter​(int bufferSize,
                                   LazyInitializedCacheMap<IoSession,​IoBuffer> buffersMap)
        Constructor which sets buffer size to bufferSize. If buffersMap is null then a default instance of ConcurrentHashMap is created else the provided instance is used.
        Parameters:
        bufferSize - the new buffer size
        buffersMap - the map to use for storing each session buffer
    • Method Detail

      • getBufferSize

        public int getBufferSize()
        Returns:
        The buffer size.
      • setBufferSize

        public void setBufferSize​(int bufferSize)
        Sets the buffer size but only for the newly created buffers.
        Parameters:
        bufferSize - the new buffer size
      • write

        private void write​(IoSession session,
                           IoBuffer data)
        Writes an IoBuffer to the session's buffer.
        Parameters:
        session - the session to which a write is requested
        data - the data to buffer
      • write

        private void write​(IoSession session,
                           IoBuffer data,
                           IoBuffer buf)
        Writes data IoBuffer to the buf IoBuffer which buffers write requests for the session IoSession until buffer is full or manually flushed.
        Parameters:
        session - the session where buffer will be written
        data - the data to buffer
        buf - the buffer where data will be temporarily written
      • internalFlush

        private void internalFlush​(IoFilter.NextFilter nextFilter,
                                   IoSession session,
                                   IoBuffer buf)
                            throws java.lang.Exception
        Internal method that actually flushes the buffered data.
        Parameters:
        nextFilter - the IoFilter.NextFilter of this filter
        session - the session where buffer will be written
        buf - the data to write
        Throws:
        java.lang.Exception - if a write operation fails
      • flush

        public void flush​(IoSession session)
        Flushes the buffered data.
        Parameters:
        session - the session where buffer will be written
      • free

        private void free​(IoSession session)
        Internal method that actually frees the IoBuffer that contains the buffered data that has not been flushed.
        Parameters:
        session - the session we operate on