001/* ----------------------------------------------------------------------------
002   The Kiwi Toolkit - A Java Class Library
003   Copyright (C) 1998-2004 Mark A. Lindner
004
005   This library is free software; you can redistribute it and/or
006   modify it under the terms of the GNU General Public License as
007   published by the Free Software Foundation; either version 2 of the
008   License, or (at your option) any later version.
009
010   This library is distributed in the hope that it will be useful,
011   but WITHOUT ANY WARRANTY; without even the implied warranty of
012   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
013   General Public License for more details.
014
015   You should have received a copy of the GNU General Public License
016   along with this library; if not, write to the Free Software
017   Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
018   02111-1307, USA.
019 
020   The author may be contacted at: mark_a_lindner@yahoo.com
021   ----------------------------------------------------------------------------
022   $Log: XDataInputStream.java,v $
023   Revision 1.5  2004/05/05 21:36:35  markl
024   comment block updates
025
026   Revision 1.4  2003/01/19 09:37:12  markl
027   Javadoc & comment header updates.
028
029   Revision 1.3  2001/03/12 01:58:43  markl
030   Source code cleanup.
031
032   Revision 1.2  1999/01/10 03:34:00  markl
033   added GPL header & RCS tag
034   ----------------------------------------------------------------------------
035*/
036
037package kiwi.io;
038
039import java.io.*;
040
041/** This class is a trivial extension of <code>DataInputStream</code>
042  * that re-implements the deprecated <code>readLine()</code> method
043  * as <code>getLine()</code>.
044  *
045  * @author Mark Lindner
046  *
047  * @see java.io.DataInputStream
048  * @see kiwi.io.XDataOutputStream
049  */
050
051public class XDataInputStream extends DataInputStream
052  {
053  private char lineBuffer[];
054
055  /** Construct a new <code>XDataInputStream</code>.
056    *
057    * @param in The <code>InputStream</code> to wrap.
058    */
059
060  public XDataInputStream(InputStream in)
061    {
062    super(in);
063    }
064
065  /** Read a CR+LF-terminated line from the stream. Any
066    * terminator characters are discarded.
067    */
068
069  public final String getLine() throws IOException
070    {
071    InputStream in = this.in;
072    char buf[] = lineBuffer;
073
074    if(buf == null)
075      buf = lineBuffer = new char[128];
076
077    int room = buf.length, offset = 0, c;
078
079    loop:
080    for(;;)
081      {
082      switch(c = in.read())
083        {
084        case -1: 
085        case '\n':
086          break loop;
087
088        case '\r':
089          int c2 = in.read();
090          if(c2 != '\n')
091            {
092            if(!(in instanceof PushbackInputStream))
093              {
094              in = this.in = new PushbackInputStream(in);
095              }
096            ((PushbackInputStream)in).unread(c2);
097            }
098          break loop;
099
100        default:
101          if(--room < 0)
102            {
103            buf = new char[offset + 128];
104            room = buf.length - offset - 1;
105            System.arraycopy(lineBuffer, 0, buf, 0, offset);
106            lineBuffer = buf;
107            }
108          buf[offset++] = (char)c;
109          break;
110        }
111      }
112
113    if((c == -1) && (offset == 0))
114      return(null);
115
116    return(String.copyValueOf(buf, 0, offset));
117    }
118  
119  }
120
121/* end of source file */