/*****************************************************************************
 *                                                                           *
 *         Licensed Materials - Property of IBM                              *
 *                                                                           *
 *         "Restricted Materials of IBM"                                     *
 *                                                                           *
 *         5799-QRY                                                          *
 *                                                                           *
 *         (C) Copyright IBM Corp. 1998                                      *
 *                                                                           *
 *****************************************************************************/
package com.ibm.devtools.SIPNoTE;

import java.io.*;

/**
 * This class is an extension of DataInputStream, because the readline
 * method is deprecated within DataInputStream, but our usage will not
 * experience any of the cases for which the original was deprecated.
 * <p>
 * This does not check for a CRLF, but instead only a LF.
 */
public class ModDataInputStream extends DataInputStream
{
  /**
   * Creates a new ModDataInputStream.
   *
   * @param in The InputStream from which to read data.
   */
  public ModDataInputStream(InputStream in)
  {
    super(in);
  }
  
  /**
   * Read a line from the stream, where the end of the line is delimited
   * by a linefeed (\n).
   *
   * @return The line which was read.
   */
  public String readLinelf()
       throws IOException
  {
    byte buf[] = new byte[256];
    int cnt = 0;
    int c;
  loop:
    while(true)
      {
	switch (c = in.read())
	  {
	  case -1: 
	  case '\n':
	    break loop;
	  default:
	    if (cnt == buf.length)
	      {
		byte temp[] = new byte[buf.length+128];
		System.arraycopy(temp, 0, buf, 0, cnt);
		buf = temp;
	      }
	    buf[cnt] = (byte)c;
	    cnt++;
	  }
      }
    
    if (c == -1)
      return null;
    return new String(buf, 0, cnt);
  }
}
