org/sonews/daemon/LineEncoder.java
author cli
Thu, 06 Aug 2009 18:41:34 +0200
changeset 6 dcc7e491fbc9
parent 1 6fceb66e1ad7
permissions -rw-r--r--
Removing doc subdir which resides now in sonews-doc repository.
     1 /*
     2  *   SONEWS News Server
     3  *   see AUTHORS for the list of contributors
     4  *
     5  *   This program is free software: you can redistribute it and/or modify
     6  *   it under the terms of the GNU General Public License as published by
     7  *   the Free Software Foundation, either version 3 of the License, or
     8  *   (at your option) any later version.
     9  *
    10  *   This program is distributed in the hope that it will be useful,
    11  *   but WITHOUT ANY WARRANTY; without even the implied warranty of
    12  *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    13  *   GNU General Public License for more details.
    14  *
    15  *   You should have received a copy of the GNU General Public License
    16  *   along with this program.  If not, see <http://www.gnu.org/licenses/>.
    17  */
    18 
    19 package org.sonews.daemon;
    20 
    21 import java.nio.ByteBuffer;
    22 import java.nio.CharBuffer;
    23 import java.nio.channels.ClosedChannelException;
    24 import java.nio.charset.Charset;
    25 import java.nio.charset.CharsetEncoder;
    26 import java.nio.charset.CoderResult;
    27 
    28 /**
    29  * Encodes a line to buffers using the correct charset.
    30  * @author Christian Lins
    31  * @since sonews/0.5.0
    32  */
    33 class LineEncoder
    34 {
    35 
    36   private CharBuffer    characters;
    37   private Charset       charset;
    38   
    39   /**
    40    * Constructs new LineEncoder.
    41    * @param characters
    42    * @param charset
    43    */
    44   public LineEncoder(CharBuffer characters, Charset charset)
    45   {
    46     this.characters = characters;
    47     this.charset    = charset;
    48   }
    49   
    50   /**
    51    * Encodes the characters of this instance to the given ChannelLineBuffers
    52    * using the Charset of this instance.
    53    * @param buffer
    54    * @throws java.nio.channels.ClosedChannelException
    55    */
    56   public void encode(ChannelLineBuffers buffer)
    57     throws ClosedChannelException
    58   {
    59     CharsetEncoder encoder = charset.newEncoder();
    60     while (characters.hasRemaining())
    61     {
    62       ByteBuffer buf = ChannelLineBuffers.newLineBuffer();
    63       assert buf.position() == 0;
    64       assert buf.capacity() >= 512;
    65 
    66       CoderResult res = encoder.encode(characters, buf, true);
    67 
    68       // Set limit to current position and current position to 0;
    69       // means make ready for read from buffer
    70       buf.flip();
    71       buffer.addOutputBuffer(buf);
    72 
    73       if (res.isUnderflow()) // All input processed
    74       {
    75         break;
    76       }
    77     }
    78   }
    79   
    80 }