linereader.java

来自「一款Java实现的HTTP代理服务器」· Java 代码 · 共 45 行

JAVA
45
字号
package rabbit.proxy;import java.io.IOException;import java.nio.ByteBuffer;/** A class that reads lines from a ByteBuffer.  * * @author <a href="mailto:robo@khelekore.org">Robert Olofsson</a> */public class LineReader {    private boolean strictHttp;    public LineReader (boolean strictHttp) {	this.strictHttp = strictHttp;    }    /** Try to read a line.      *  If a line ending is found the buffers mark is set to the next position.     * @param buffer the ByteBuffer to read bytes from.     * @param listener the line listener.     */    public void readLine (ByteBuffer buffer, LineListener listener) 	throws IOException {	StringBuilder sb = new StringBuilder (200);	int l = -1;	boolean lineEnding = false;	while (buffer.hasRemaining ()) {	    byte c = buffer.get ();	    if (c == '\n') {		if (l == '\r') {		    lineEnding = true;		    sb.setLength (sb.length () - 1);		}		if (lineEnding || !strictHttp) {		    buffer.mark ();		    listener.lineRead (sb.toString ());		    return;		}	    }	    sb.append ((char)c);	    l = c;	}    }}

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?