org/sonews/util/io/VarCharsetReader.java
changeset 1 6fceb66e1ad7
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/org/sonews/util/io/VarCharsetReader.java	Fri Jun 26 16:48:50 2009 +0200
     1.3 @@ -0,0 +1,90 @@
     1.4 +/*
     1.5 + *   SONEWS News Server
     1.6 + *   see AUTHORS for the list of contributors
     1.7 + *
     1.8 + *   This program is free software: you can redistribute it and/or modify
     1.9 + *   it under the terms of the GNU General Public License as published by
    1.10 + *   the Free Software Foundation, either version 3 of the License, or
    1.11 + *   (at your option) any later version.
    1.12 + *
    1.13 + *   This program is distributed in the hope that it will be useful,
    1.14 + *   but WITHOUT ANY WARRANTY; without even the implied warranty of
    1.15 + *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    1.16 + *   GNU General Public License for more details.
    1.17 + *
    1.18 + *   You should have received a copy of the GNU General Public License
    1.19 + *   along with this program.  If not, see <http://www.gnu.org/licenses/>.
    1.20 + */
    1.21 +
    1.22 +package org.sonews.util.io;
    1.23 +
    1.24 +import java.io.IOException;
    1.25 +import java.io.InputStream;
    1.26 +import java.nio.ByteBuffer;
    1.27 +import java.nio.charset.Charset;
    1.28 +
    1.29 +/**
    1.30 + * InputStream that can change its decoding charset while reading from the
    1.31 + * underlying byte based stream.
    1.32 + * @author Christian Lins
    1.33 + * @since sonews/0.5.0
    1.34 + */
    1.35 +public class VarCharsetReader
    1.36 +{
    1.37 +
    1.38 +  private final ByteBuffer buf = ByteBuffer.allocate(4096);
    1.39 +  private InputStream in;
    1.40 +  
    1.41 +  public VarCharsetReader(final InputStream in)
    1.42 +  {
    1.43 +    if(in == null)
    1.44 +    {
    1.45 +      throw new IllegalArgumentException("null InputStream");
    1.46 +    }
    1.47 +    this.in = in;
    1.48 +  }
    1.49 +  
    1.50 +  /**
    1.51 +   * Reads up to the next newline character and returns the line as String.
    1.52 +   * The String is decoded using the given charset.
    1.53 +   */
    1.54 +  public String readLine(Charset charset)
    1.55 +    throws IOException
    1.56 +  {    
    1.57 +    byte[] byteBuf = new byte[1];
    1.58 +    String bufStr;
    1.59 +    
    1.60 +    for(;;)
    1.61 +    {
    1.62 +      int read = this.in.read(byteBuf);
    1.63 +      if(read == 0)
    1.64 +      {
    1.65 +        continue;
    1.66 +      }
    1.67 +      else if(read == -1)
    1.68 +      {
    1.69 +        this.in = null;
    1.70 +        bufStr  = new String(this.buf.array(), 0, this.buf.position(), charset);
    1.71 +        break;
    1.72 +      }
    1.73 +      else if(byteBuf[0] == 10) // Is this safe? \n
    1.74 +      {
    1.75 +        bufStr  = new String(this.buf.array(), 0, this.buf.position(), charset);
    1.76 +        break;
    1.77 +      }
    1.78 +      else if(byteBuf[0] == 13) // \r
    1.79 +      { // Skip
    1.80 +        continue;
    1.81 +      }
    1.82 +      else
    1.83 +      {
    1.84 +        this.buf.put(byteBuf[0]);
    1.85 +      }
    1.86 +    }
    1.87 +    
    1.88 +    this.buf.clear();
    1.89 +    
    1.90 +    return bufStr;
    1.91 +  }
    1.92 +  
    1.93 +}