org/sonews/util/io/VarCharsetReader.java
author chris <chris@marvin>
Wed, 01 Jul 2009 10:48:22 +0200
changeset 2 1090e2141798
parent 1 6fceb66e1ad7
permissions -rw-r--r--
sonews/0.5.1 fixes merged
     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.util.io;
    20 
    21 import java.io.IOException;
    22 import java.io.InputStream;
    23 import java.nio.ByteBuffer;
    24 import java.nio.charset.Charset;
    25 
    26 /**
    27  * InputStream that can change its decoding charset while reading from the
    28  * underlying byte based stream.
    29  * @author Christian Lins
    30  * @since sonews/0.5.0
    31  */
    32 public class VarCharsetReader
    33 {
    34 
    35   private final ByteBuffer buf = ByteBuffer.allocate(4096);
    36   private InputStream in;
    37   
    38   public VarCharsetReader(final InputStream in)
    39   {
    40     if(in == null)
    41     {
    42       throw new IllegalArgumentException("null InputStream");
    43     }
    44     this.in = in;
    45   }
    46   
    47   /**
    48    * Reads up to the next newline character and returns the line as String.
    49    * The String is decoded using the given charset.
    50    */
    51   public String readLine(Charset charset)
    52     throws IOException
    53   {    
    54     byte[] byteBuf = new byte[1];
    55     String bufStr;
    56     
    57     for(;;)
    58     {
    59       int read = this.in.read(byteBuf);
    60       if(read == 0)
    61       {
    62         continue;
    63       }
    64       else if(read == -1)
    65       {
    66         this.in = null;
    67         bufStr  = new String(this.buf.array(), 0, this.buf.position(), charset);
    68         break;
    69       }
    70       else if(byteBuf[0] == 10) // Is this safe? \n
    71       {
    72         bufStr  = new String(this.buf.array(), 0, this.buf.position(), charset);
    73         break;
    74       }
    75       else if(byteBuf[0] == 13) // \r
    76       { // Skip
    77         continue;
    78       }
    79       else
    80       {
    81         this.buf.put(byteBuf[0]);
    82       }
    83     }
    84     
    85     this.buf.clear();
    86     
    87     return bufStr;
    88   }
    89   
    90 }