View Javadoc

1   //
2   //  ========================================================================
3   //  Copyright (c) 1995-2013 Mort Bay Consulting Pty. Ltd.
4   //  ------------------------------------------------------------------------
5   //  All rights reserved. This program and the accompanying materials
6   //  are made available under the terms of the Eclipse Public License v1.0
7   //  and Apache License v2.0 which accompanies this distribution.
8   //
9   //      The Eclipse Public License is available at
10  //      http://www.eclipse.org/legal/epl-v10.html
11  //
12  //      The Apache License v2.0 is available at
13  //      http://www.opensource.org/licenses/apache2.0.php
14  //
15  //  You may elect to redistribute this code under either of these licenses.
16  //  ========================================================================
17  //
18  
19  package org.eclipse.jetty.util;
20  
21  import java.io.BufferedInputStream;
22  import java.io.IOException;
23  import java.io.InputStream;
24  
25  /**
26   * ReadLineInputStream
27   *
28   * Read from an input stream, accepting CR/LF, LF or just CR.
29   */
30  public class ReadLineInputStream extends BufferedInputStream
31  {
32      boolean _seenCRLF;
33      boolean _skipLF;
34      
35      public ReadLineInputStream(InputStream in)
36      {
37          super(in);
38      }
39  
40      public ReadLineInputStream(InputStream in, int size)
41      {
42          super(in,size);
43      }
44      
45      public String readLine() throws IOException
46      {
47          mark(buf.length);
48                  
49          while (true)
50          {
51              int b=super.read();
52              if (b==-1)
53              {
54                  int m=markpos;
55                  markpos=-1;
56                  if (pos>m)
57                      return new String(buf,m,pos-m,StringUtil.__UTF8_CHARSET);
58  
59                  return null;
60              }
61              
62              if (b=='\r')
63              {
64                  int p=pos;
65                  
66                  // if we have seen CRLF before, hungrily consume LF
67                  if (_seenCRLF && pos<count)
68                  {
69                      if (buf[pos]=='\n')
70                          pos+=1;
71                  }
72                  else
73                      _skipLF=true;
74                  int m=markpos;
75                  markpos=-1;
76                  return new String(buf,m,p-m-1,StringUtil.__UTF8_CHARSET);
77              }
78              
79              if (b=='\n')
80              {
81                  if (_skipLF)
82                  {
83                      _skipLF=false;
84                      _seenCRLF=true;
85                      markpos++;
86                      continue;
87                  }
88                  int m=markpos;
89                  markpos=-1;
90                  return new String(buf,m,pos-m-1,StringUtil.__UTF8_CHARSET);
91              }
92          }
93      }
94  
95      @Override
96      public synchronized int read() throws IOException
97      {
98          int b = super.read();
99          if (_skipLF)
100         {
101             _skipLF=false;
102             if (_seenCRLF && b=='\n')
103                 b=super.read();
104         }
105         return b;
106     }
107 
108     @Override
109     public synchronized int read(byte[] buf, int off, int len) throws IOException
110     {
111         if (_skipLF && len>0)
112         {
113             _skipLF=false;
114             if (_seenCRLF)
115             {
116                 int b = super.read();
117                 if (b==-1)
118                     return -1;
119                 
120                 if (b!='\n')
121                 {
122                     buf[off]=(byte)(0xff&b);
123                     return 1+super.read(buf,off+1,len-1);
124                 }
125             }
126         }
127         
128         return super.read(buf,off,len);
129     }
130     
131     
132 }