2 // ========================================================================
3 // Copyright (c) 1995-2014 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.
9 // The Eclipse Public License is available at
10 // http://www.eclipse.org/legal/epl-v10.html
12 // The Apache License v2.0 is available at
13 // http://www.opensource.org/licenses/apache2.0.php
15 // You may elect to redistribute this code under either of these licenses.
16 // ========================================================================
19 package org.eclipse.jetty.util;
21 import java.io.BufferedInputStream;
22 import java.io.IOException;
23 import java.io.InputStream;
24 import java.nio.charset.StandardCharsets;
29 * Read from an input stream, accepting CR/LF, LF or just CR.
31 public class ReadLineInputStream extends BufferedInputStream
36 public ReadLineInputStream(InputStream in)
41 public ReadLineInputStream(InputStream in, int size)
46 public String readLine() throws IOException
55 throw new IOException("Buffer size exceeded: no line terminator");
62 return new String(buf,m,pos-m, StandardCharsets.UTF_8);
71 // if we have seen CRLF before, hungrily consume LF
72 if (_seenCRLF && pos<count)
81 return new String(buf,m,p-m-1,StandardCharsets.UTF_8);
95 return new String(buf,m,pos-m-1,StandardCharsets.UTF_8);
101 public synchronized int read() throws IOException
103 int b = super.read();
107 if (_seenCRLF && b=='\n')
114 public synchronized int read(byte[] buf, int off, int len) throws IOException
116 if (_skipLF && len>0)
121 int b = super.read();
127 buf[off]=(byte)(0xff&b);
128 return 1+super.read(buf,off+1,len-1);
133 return super.read(buf,off,len);