Source for gnu.java.net.LineInputStream

   1: /* LineInputStream.java --
   2:    Copyright (C) 2002, 2003, 2004, 2005  Free Software Foundation, Inc.
   3: 
   4: This file is part of GNU Classpath.
   5: 
   6: GNU Classpath is free software; you can redistribute it and/or modify
   7: it under the terms of the GNU General Public License as published by
   8: the Free Software Foundation; either version 2, or (at your option)
   9: any later version.
  10:  
  11: GNU Classpath is distributed in the hope that it will be useful, but
  12: WITHOUT ANY WARRANTY; without even the implied warranty of
  13: MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
  14: General Public License for more details.
  15: 
  16: You should have received a copy of the GNU General Public License
  17: along with GNU Classpath; see the file COPYING.  If not, write to the
  18: Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
  19: 02110-1301 USA.
  20: 
  21: Linking this library statically or dynamically with other modules is
  22: making a combined work based on this library.  Thus, the terms and
  23: conditions of the GNU General Public License cover the whole
  24: combination.
  25: 
  26: As a special exception, the copyright holders of this library give you
  27: permission to link this library with independent modules to produce an
  28: executable, regardless of the license terms of these independent
  29: modules, and to copy and distribute the resulting executable under
  30: terms of your choice, provided that you also meet, for each linked
  31: independent module, the terms and conditions of the license of that
  32: module.  An independent module is a module which is not derived from
  33: or based on this library.  If you modify this library, you may extend
  34: this exception to your version of the library, but you are not
  35: obligated to do so.  If you do not wish to do so, delete this
  36: exception statement from your version. */
  37: 
  38: 
  39: package gnu.java.net;
  40: 
  41: import java.io.BufferedInputStream;
  42: import java.io.ByteArrayOutputStream;
  43: import java.io.FilterInputStream;
  44: import java.io.IOException;
  45: import java.io.InputStream;
  46: 
  47: /**
  48:  * An input stream that can read lines of input.
  49:  *
  50:  * @author Chris Burdess (dog@gnu.org)
  51:  */
  52: public class LineInputStream
  53:   extends FilterInputStream
  54: {
  55:   /*
  56:    * Line buffer.
  57:    */
  58:   private ByteArrayOutputStream buf;
  59: 
  60:   /*
  61:    * Encoding to use when translating bytes to characters.
  62:    */
  63:   private String encoding;
  64: 
  65:   /*
  66:    * End-of-stream flag.
  67:    */
  68:   private boolean eof;
  69: 
  70:   /**
  71:    * Whether we can use block reads.
  72:    */
  73:   private final boolean blockReads;
  74: 
  75:   /**
  76:    * Constructor using the US-ASCII character encoding.
  77:    * @param in the underlying input stream
  78:    */
  79:   public LineInputStream(InputStream in)
  80:   {
  81:     this(in, "US-ASCII");
  82:   }
  83: 
  84:   /**
  85:    * Constructor.
  86:    * @param in the underlying input stream
  87:    * @param encoding the character encoding to use
  88:    */
  89:   public LineInputStream(InputStream in, String encoding)
  90:   {
  91:     super(in);
  92:     buf = new ByteArrayOutputStream();
  93:     this.encoding = encoding;
  94:     eof = false;
  95:     // If it is already buffered, additional buffering gains nothing.
  96:     blockReads = !(in instanceof BufferedInputStream) && in.markSupported();
  97:   }
  98: 
  99:   /**
 100:    * Read a line of input.
 101:    */
 102:   public String readLine()
 103:     throws IOException
 104:   {
 105:     if (eof)
 106:       {
 107:         return null;
 108:       }
 109:     do
 110:       {
 111:         if (blockReads)
 112:           {
 113:             // Use mark and reset to read chunks of bytes
 114:             final int MAX_LENGTH = 1024;
 115:             int len, pos;
 116: 
 117:             len = in.available();
 118:             if (len == 0 || len > MAX_LENGTH)
 119:               len = MAX_LENGTH;
 120:             byte[] b = new byte[len];
 121:             in.mark(len);
 122:             // Read into buffer b
 123:             len = in.read(b, 0, len);
 124:             // Handle EOF
 125:             if (len == -1)
 126:               {
 127:                 eof = true;
 128:                 if (buf.size() == 0)
 129:                   {
 130:                     return null;
 131:                   }
 132:                 else
 133:                   {
 134:                     // We don't care about resetting buf
 135:                     return buf.toString(encoding);
 136:                   }
 137:               }
 138:             // Get index of LF in b
 139:             pos = indexOf(b, len, (byte) 0x0a);
 140:             if (pos != -1)
 141:               {
 142:                 // Write pos bytes to buf
 143:                 buf.write(b, 0, pos);
 144:                 // Reset stream, and read pos + 1 bytes
 145:                 in.reset();
 146:                 pos += 1;
 147:                 while (pos > 0)
 148:                   {
 149:                     len = in.read(b, 0, pos);
 150:                     pos = (len == -1) ? -1 : pos - len;
 151:                   }
 152:                 // Return line
 153:                 String ret = buf.toString(encoding);
 154:                 buf.reset();
 155:                 return ret;
 156:               }
 157:             else
 158:               {
 159:                 // Append everything to buf and fall through to re-read.
 160:                 buf.write(b, 0, len);
 161:               }
 162:           }
 163:         else
 164:           {
 165:             // We must use character reads in order not to read too much
 166:             // from the underlying stream.
 167:             int c = in.read();
 168:             switch (c)
 169:               {
 170:               case -1:
 171:                 eof = true;
 172:                 if (buf.size() == 0)
 173:                   {
 174:                     return null;
 175:                   }
 176:                 // Fall through and return contents of buffer.
 177:               case 0x0a:                // LF
 178:                 String ret = buf.toString(encoding);
 179:                 buf.reset();
 180:                 return ret;
 181:               default:
 182:                 buf.write(c);
 183:               }
 184:           }
 185:       }
 186:     while (true);
 187:   }
 188: 
 189:   private int indexOf(byte[] b, int len, byte c)
 190:   {
 191:     for (int pos = 0; pos < len; pos++)
 192:       {
 193:         if (b[pos] == c)
 194:           {
 195:             return pos;
 196:           }
 197:       }
 198:     return -1;
 199:   }
 200: }