Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
  //
  //  ========================================================================
  //  Copyright (c) 1995-2013 Mort Bay Consulting Pty. Ltd.
  //  ------------------------------------------------------------------------
  //  All rights reserved. This program and the accompanying materials
  //  are made available under the terms of the Eclipse Public License v1.0
  //  and Apache License v2.0 which accompanies this distribution.
  //
  //      The Eclipse Public License is available at
 //      http://www.eclipse.org/legal/epl-v10.html
 //
 //      The Apache License v2.0 is available at
 //      http://www.opensource.org/licenses/apache2.0.php
 //
 //  You may elect to redistribute this code under either of these licenses.
 //  ========================================================================
 //
 
 package org.eclipse.jetty.util;
 
ReadLineInputStream Read from an input stream, accepting CR/LF, LF or just CR.
 
 public class ReadLineInputStream extends BufferedInputStream
 {
     boolean _seenCRLF;
     boolean _skipLF;
     
     public ReadLineInputStream(InputStream in)
     {
         super(in);
     }
 
     public ReadLineInputStream(InputStream inint size)
     {
         super(in,size);
     }
     
     public String readLine() throws IOException
     {
         mark(.);
                 
         while (true)
         {
             int b=super.read();
             if (b==-1)
             {
                 int m=;
                 =-1;
                 if (>m)
                     return new String(,m,-m,.);
 
                 return null;
             }
             
             if (b=='\r')
             {
                 int p=;
                 
                 // if we have seen CRLF before, hungrily consume LF
                 if ( && <)
                 {
                     if ([]=='\n')
                         +=1;
                 }
                 else
                     =true;
                 int m=;
                 =-1;
                 return new String(,m,p-m-1,.);
             }
             
             if (b=='\n')
             {
                 if ()
                 {
                     =false;
                     =true;
                     ++;
                     continue;
                 }
                 int m=;
                 =-1;
                 return new String(,m,-m-1,.);
             }
         }
     }
 
     @Override
     public synchronized int read() throws IOException
     {
         int b = super.read();
         if ()
        {
            =false;
            if ( && b=='\n')
                b=super.read();
        }
        return b;
    }
    @Override
    public synchronized int read(byte[] bufint offint lenthrows IOException
    {
        if ( && len>0)
        {
            =false;
            if ()
            {
                int b = super.read();
                if (b==-1)
                    return -1;
                
                if (b!='\n')
                {
                    buf[off]=(byte)(0xff&b);
                    return 1+super.read(buf,off+1,len-1);
                }
            }
        }
        
        return super.read(buf,off,len);
    }
    
    
New to GrepCode? Check out our FAQ X