X-Git-Url: https://code.wpia.club/?p=gigi.git;a=blobdiff_plain;f=lib%2Fjetty%2Forg%2Feclipse%2Fjetty%2Futil%2FReadLineInputStream.java;fp=lib%2Fjetty%2Forg%2Feclipse%2Fjetty%2Futil%2FReadLineInputStream.java;h=067b2c14897ed7b435bacbb77143b1c20859ff6d;hp=0000000000000000000000000000000000000000;hb=73ef54a38e3930a1a789cdc6b5fa23cdd4c9d086;hpb=515007c7c1351045420669d65b59c08fa46850f2 diff --git a/lib/jetty/org/eclipse/jetty/util/ReadLineInputStream.java b/lib/jetty/org/eclipse/jetty/util/ReadLineInputStream.java new file mode 100644 index 00000000..067b2c14 --- /dev/null +++ b/lib/jetty/org/eclipse/jetty/util/ReadLineInputStream.java @@ -0,0 +1,137 @@ +// +// ======================================================================== +// Copyright (c) 1995-2014 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; + +import java.io.BufferedInputStream; +import java.io.IOException; +import java.io.InputStream; +import java.nio.charset.StandardCharsets; + +/** + * 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 in, int size) + { + super(in,size); + } + + public String readLine() throws IOException + { + mark(buf.length); + + while (true) + { + int b=super.read(); + + if (markpos < 0) + throw new IOException("Buffer size exceeded: no line terminator"); + + if (b==-1) + { + int m=markpos; + markpos=-1; + if (pos>m) + return new String(buf,m,pos-m, StandardCharsets.UTF_8); + + return null; + } + + if (b=='\r') + { + int p=pos; + + // if we have seen CRLF before, hungrily consume LF + if (_seenCRLF && pos0) + { + _skipLF=false; + if (_seenCRLF) + { + 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); + } + + +}