changeset 1044:dd71a59fcf72

remove buffer marking
author Franklin Schmidt <fschmidt@gmail.com>
date Fri, 04 Nov 2016 02:26:54 -0600
parents ae1c92957739
children 48506d03e230
files src/org/eclipse/jetty/http/HttpParser.java src/org/eclipse/jetty/io/AbstractBuffer.java src/org/eclipse/jetty/io/Buffer.java src/org/eclipse/jetty/io/ByteArrayBuffer.java src/org/eclipse/jetty/io/View.java
diffstat 5 files changed, 71 insertions(+), 116 deletions(-) [+]
line wrap: on
line diff
--- a/src/org/eclipse/jetty/http/HttpParser.java	Fri Nov 04 01:10:12 2016 -0600
+++ b/src/org/eclipse/jetty/http/HttpParser.java	Fri Nov 04 02:26:54 2016 -0600
@@ -60,6 +60,7 @@
 	public final Buffer _header; // Buffer for header data (and small _content)
 	private final Buffer _body; // Buffer for large content
 	private Buffer _buffer; // The current buffer in use (either _header or _content)
+	private int _mark = -1;
 	private String _cached;
 	private String _tok0 = ""; // Saved token: header name, request method or response version
 	private String _tok1 = ""; // Saved token: header value, request URI or response code
@@ -85,6 +86,33 @@
 		_handler = handler;
 	}
 
+	private void mark() {
+		_mark = _buffer.getIndex() - 1;
+	}
+
+	private Buffer sliceFromMark() {
+		Buffer buf = _buffer.sliceFrom(_mark);
+		_mark = -1;
+		return buf;
+	}
+
+	private void clear() {
+		_buffer.clear();
+		_mark = -1;
+	}
+
+	private void compact() {
+		if( _mark == -1 ) {
+			_buffer.compact();
+		} else if( _mark > 0 ) {
+			int old = _buffer.getIndex();
+			_buffer.setGetIndex(_mark);
+			_buffer.compact();
+			_buffer.setGetIndex( old - _mark );
+			_mark = 0;
+		}
+	}
+
 	public long getContentLength()
 	{
 		return _contentLength;
@@ -277,15 +305,15 @@
 						_cached = null;
 						if (ch > HttpTokens.SPACE || ch<0)
 						{
-							_buffer.mark();
-							_state=STATE_FIELD0;
+							mark();
+							_state = STATE_FIELD0;
 						}
 						break;
 
 					case STATE_FIELD0:
 						if (ch == HttpTokens.SPACE)
 						{
-							_tok0 = _buffer.toString(_buffer.markIndex(), _buffer.getIndex() - 1 - _buffer.markIndex());
+							_tok0 = _buffer.toString(_mark, _buffer.getIndex() - 1 - _mark);
 							_responseStatus = !HttpVersions.CACHE.contains(_tok0)?-1:0;
 							_state=STATE_SPACE1;
 							continue;
@@ -299,7 +327,7 @@
 					case STATE_SPACE1:
 						if (ch > HttpTokens.SPACE || ch<0)
 						{
-							_buffer.mark();
+							mark();
 							if (_responseStatus>=0)
 							{
 								_state = STATE_STATUS;
@@ -317,8 +345,8 @@
 					case STATE_STATUS:
 						if (ch == HttpTokens.SPACE)
 						{
-//							_tok1.update(_buffer.markIndex(), _buffer.getIndex() - 1);
-							_tok1 = _buffer.toString(_buffer.markIndex(), _buffer.getIndex() - 1 - _buffer.markIndex());
+//							_tok1.update(_mark, _buffer.getIndex() - 1);
+							_tok1 = _buffer.toString(_mark, _buffer.getIndex() - 1 - _mark);
 							_state = STATE_SPACE2;
 							continue;
 						}
@@ -344,15 +372,15 @@
 					case STATE_URI:
 						if (ch == HttpTokens.SPACE)
 						{
-//							_tok1.update(_buffer.markIndex(), _buffer.getIndex() - 1);
-							_tok1 = _buffer.toString(_buffer.markIndex(), _buffer.getIndex() - 1 - _buffer.markIndex());
+//							_tok1.update(_mark, _buffer.getIndex() - 1);
+							_tok1 = _buffer.toString(_mark, _buffer.getIndex() - 1 - _mark);
 							_state=STATE_SPACE2;
 							continue;
 						}
 						else if (ch < HttpTokens.SPACE && ch>=0)
 						{
 							// HTTP/0.9
-							_handler.startRequest(_tok0, _buffer.sliceFromMark().toString(), null);
+							_handler.startRequest(_tok0, sliceFromMark().toString(), null);
 							_persistent = false;
 							_state = STATE_SEEKING_EOF;
 							_handler.headerComplete();
@@ -364,7 +392,7 @@
 					case STATE_SPACE2:
 						if (ch > HttpTokens.SPACE || ch<0)
 						{
-							_buffer.mark();
+							mark();
 							_state=STATE_FIELD2;
 						}
 						else if (ch < HttpTokens.SPACE)
@@ -395,10 +423,10 @@
 						{
 							String version;
 							if (_responseStatus > 0)
-//								_handler.startResponse(version=HttpVersions.CACHE.lookup(_tok0), _responseStatus,_buffer.sliceFromMark());
+//								_handler.startResponse(version=HttpVersions.CACHE.lookup(_tok0), _responseStatus,sliceFromMark());
 								version = _tok0;
 							else
-								_handler.startRequest(_tok0, _tok1, version=_buffer.sliceFromMark().toString());
+								_handler.startRequest(_tok0, _tok1, version=sliceFromMark().toString());
 							_eol=ch;
 							_persistent = HttpVersions.CACHE.getOrdinal(version) >= HttpVersions.HTTP_1_1_ORDINAL;
 							_state=STATE_HEADER;
@@ -507,7 +535,7 @@
 									_tok1 = "";
 									_multiLineValue=null;
 								}
-								_buffer.setMarkIndex(-1);
+								_mark = -1;
 
 								// now handle ch
 								if (ch == HttpTokens.CARRIAGE_RETURN || ch == HttpTokens.LINE_FEED)
@@ -566,19 +594,19 @@
 								{
 									// New header
 									_length = 1;
-									_buffer.mark();
+									mark();
 									_state = STATE_HEADER_NAME;
 
 									// try cached name!
 									if (array!=null)
 									{
-										String s = new String(array, _buffer.markIndex(), length+1);
+										String s = new String(array, _mark, length+1);
 										_cached = HttpHeaders.CACHE.getBest(s);
 
 										if (_cached!=null)
 										{
 											_length = _cached.length();
-											_buffer.setGetIndex(_buffer.markIndex()+_length);
+											_buffer.setGetIndex(_mark+_length);
 											length = _buffer.remaining();
 										}
 									}
@@ -594,14 +622,14 @@
 							case HttpTokens.CARRIAGE_RETURN:
 							case HttpTokens.LINE_FEED:
 								if (_length > 0) {
-									_tok0 = _buffer.toString(_buffer.markIndex(), _length);
+									_tok0 = _buffer.toString(_mark, _length);
 								}
 								_eol=ch;
 								_state=STATE_HEADER;
 								break;
 							case HttpTokens.COLON:
 								if (_length > 0 && _cached==null) {
-									_tok0 = _buffer.toString(_buffer.markIndex(), _length);
+									_tok0 = _buffer.toString(_mark, _length);
 								}
 								_length=-1;
 								_state=STATE_HEADER_VALUE;
@@ -613,8 +641,8 @@
 							{
 								_cached = null;
 								if (_length == -1)
-									_buffer.mark();
-								_length=_buffer.getIndex() - _buffer.markIndex();
+									mark();
+								_length=_buffer.getIndex() - _mark;
 								_state = STATE_HEADER_IN_NAME;
 							}
 						}
@@ -627,14 +655,14 @@
 							case HttpTokens.CARRIAGE_RETURN:
 							case HttpTokens.LINE_FEED:
 								if (_length > 0) {
-									_tok0 = _buffer.toString(_buffer.markIndex(),_length);
+									_tok0 = _buffer.toString(_mark,_length);
 								}
 								_eol=ch;
 								_state=STATE_HEADER;
 								break;
 							case HttpTokens.COLON:
 								if (_length > 0 && _cached==null) {
-									_tok0 = _buffer.toString(_buffer.markIndex(),_length);
+									_tok0 = _buffer.toString(_mark,_length);
 								}
 								_length=-1;
 								_state=STATE_HEADER_VALUE;
@@ -659,14 +687,14 @@
 								if (_length > 0)
 								{
 									if (_tok1.length() == 0)
-//										_tok1.update(_buffer.markIndex(), _buffer.markIndex() + _length);
-										_tok1 = _buffer.toString(_buffer.markIndex(), _length);
+//										_tok1.update(_mark, _mark + _length);
+										_tok1 = _buffer.toString(_mark, _length);
 									else
 									{
 										// Continuation line!
 										if (_multiLineValue == null) _multiLineValue = _tok1;
-//										_tok1.update(_buffer.markIndex(), _buffer.markIndex() + _length);
-										_tok1 = _buffer.toString(_buffer.markIndex(), _length);
+//										_tok1.update(_mark, _mark + _length);
+										_tok1 = _buffer.toString(_mark, _length);
 										_multiLineValue += " " + _tok1;
 									}
 								}
@@ -679,8 +707,8 @@
 							default:
 							{
 								if (_length == -1)
-									_buffer.mark();
-								_length=_buffer.getIndex() - _buffer.markIndex();
+									mark();
+								_length=_buffer.getIndex() - _mark;
 								_state = STATE_HEADER_IN_VALUE;
 							}
 						}
@@ -694,14 +722,14 @@
 								if (_length > 0)
 								{
 									if (_tok1.length() == 0)
-//										_tok1.update(_buffer.markIndex(), _buffer.markIndex() + _length);
-										_tok1 = _buffer.toString(_buffer.markIndex(), _length);
+//										_tok1.update(_mark, _mark + _length);
+										_tok1 = _buffer.toString(_mark, _length);
 									else
 									{
 										// Continuation line!
 										if (_multiLineValue == null) _multiLineValue = _tok1;
-//										_tok1.update(_buffer.markIndex(), _buffer.markIndex() + _length);
-										_tok1 = _buffer.toString(_buffer.markIndex(), _length);
+//										_tok1.update(_mark, _mark + _length);
+										_tok1 = _buffer.toString(_mark, _length);
 										_multiLineValue += " " + _tok1;
 									}
 								}
@@ -894,11 +922,11 @@
 								{
 									_state = STATE_END;
 									_endp.close();
-									_buffer.clear();
+									clear();
 								}
 						}
 						
-						_buffer.clear();
+						clear();
 						break;
 					}
 				}
@@ -942,14 +970,14 @@
 		// Shall we compact the body?
 		if (_buffer==_body || _state>STATE_END)
 		{
-			_buffer.compact();
+			compact();
 		}
 
 		// Are we full?
 		if (_buffer.space() == 0)
 		{
 			LOG.warn("HttpParser Full for {} ",_endp);
-			_buffer.clear();
+			clear();
 			throw new HttpException(HttpStatus.REQUEST_ENTITY_TOO_LARGE_413, "Request Entity Too Large: "+(_buffer==_body?"body":"head"));
 		}
 /* why?
--- a/src/org/eclipse/jetty/io/AbstractBuffer.java	Fri Nov 04 01:10:12 2016 -0600
+++ b/src/org/eclipse/jetty/io/AbstractBuffer.java	Fri Nov 04 02:26:54 2016 -0600
@@ -46,7 +46,6 @@
 	protected int _hash;
 	protected int _hashGet;
 	protected int _hashPut;
-	protected int _mark;
 
 	/**
 	 * Constructor for BufferView
@@ -55,7 +54,6 @@
 	 */
 	protected AbstractBuffer(int access)
 	{
-		setMarkIndex(-1);
 		_access = access;
 	}
 
@@ -77,7 +75,6 @@
 
 	public void clear()
 	{
-		setMarkIndex(-1);
 		setGetIndex(0);
 		setPutIndex(0);
 	}
@@ -85,7 +82,7 @@
 	public void compact()
 	{
 		if (isReadOnly()) throw new IllegalStateException(__READONLY);
-		int s = markIndex() >= 0 ? markIndex() : getIndex();
+		int s = getIndex();
 		if (s > 0)
 		{
 			byte array[] = array();
@@ -97,7 +94,6 @@
 				else
 					poke(0, peek(s, length));
 			}
-			if (markIndex() > 0) setMarkIndex(markIndex() - s);
 			setGetIndex(getIndex() - s);
 			setPutIndex(putIndex() - s);
 		}
@@ -220,16 +216,6 @@
 		return _put - _get;
 	}
 
-	public void mark()
-	{
-		setMarkIndex(_get - 1);
-	}
-
-	public int markIndex()
-	{
-		return _mark;
-	}
-
 	public byte peek()
 	{
 		return peek(_get);
@@ -344,10 +330,9 @@
 		return _put;
 	}
 
-	public void rewind()
+	public final void rewind()
 	{
 		setGetIndex(0);
-		setMarkIndex(-1);
 	}
 
 	public void setGetIndex(int getIndex)
@@ -356,11 +341,6 @@
 		_hash=0;
 	}
 
-	public void setMarkIndex(int index)
-	{
-		_mark = index;
-	}
-
 	public void setPutIndex(int putIndex)
 	{
 		_put = putIndex;
@@ -379,17 +359,8 @@
 		return peek(getIndex(), remaining());
 	}
 
-	public Buffer sliceFromMark()
-	{
-		return sliceFromMark(getIndex() - markIndex() - 1);
-	}
-
-	public Buffer sliceFromMark(int length)
-	{
-		if (markIndex() < 0) return null;
-		Buffer view = peek(markIndex(), length);
-		setMarkIndex(-1);
-		return view;
+	public final Buffer sliceFrom(int index) {
+		return peek(index, getIndex()-index-1);
 	}
 
 	public int space()
@@ -404,8 +375,6 @@
 		buf.append(super.hashCode());
 		buf.append(",");
 		buf.append(this.buffer().hashCode());
-		buf.append(",m=");
-		buf.append(markIndex());
 		buf.append(",g=");
 		buf.append(getIndex());
 		buf.append(",p=");
@@ -413,15 +382,6 @@
 		buf.append(",c=");
 		buf.append(capacity());
 		buf.append("]={");
-		if (markIndex() >= 0)
-		{
-			for (int i = markIndex(); i < getIndex(); i++)
-			{
-				byte b =  peek(i);
-				TypeUtil.toHex(b,buf);
-			}
-			buf.append("}{");
-		}
 		int count = 0;
 		for (int i = getIndex(); i < putIndex(); i++)
 		{
--- a/src/org/eclipse/jetty/io/Buffer.java	Fri Nov 04 01:10:12 2016 -0600
+++ b/src/org/eclipse/jetty/io/Buffer.java	Fri Nov 04 02:26:54 2016 -0600
@@ -132,17 +132,6 @@
 	int remaining();
 	
 	/**
-	 * Set the mark to the current getIndex.
-	 */
-	void mark();
-	
-	/**
-	 * The current index of the mark.
-	 * @return an <code>int</code> index in the buffer or -1 if the mark is not set.
-	 */
-	int markIndex();
-
-	/**
 	 * Get the byte at the current getIndex without incrementing the getIndex.
 	 * @return The <code>byte</code> value from the current getIndex.
 	 */
@@ -231,12 +220,6 @@
 	void setGetIndex(int newStart);
 	
 	/**
-	 * Set a specific value for the mark.
-	 * @param newMark an <code>int</code> value
-	 */
-	void setMarkIndex(int newMark);
-	
-	/**
 	 * 
 	 * @param newLimit an <code>int</code> value
 	 */
@@ -255,21 +238,8 @@
 	 */
 	Buffer slice();
 	
-	/**
-	 * 
-	 *
-	 * @return a volitile <code>Buffer</code> value from the mark to the putIndex
-	 */
-	Buffer sliceFromMark();
-	
-	/**
-	 * 
-	 *
-	 * @param length an <code>int</code> value
-	 * @return a valitile <code>Buffer</code> value from the mark of the length requested.
-	 */
-	Buffer sliceFromMark(int length);
-	
+	public Buffer sliceFrom(int index);
+
 	/**
 	 * 
 	 * @return a <code>String</code> value describing the state and contents of the buffer.
--- a/src/org/eclipse/jetty/io/ByteArrayBuffer.java	Fri Nov 04 01:10:12 2016 -0600
+++ b/src/org/eclipse/jetty/io/ByteArrayBuffer.java	Fri Nov 04 02:26:54 2016 -0600
@@ -87,7 +87,7 @@
 	{
 		if (isReadOnly()) 
 			throw new IllegalStateException(__READONLY);
-		int s = markIndex() >= 0 ? markIndex() : getIndex();
+		int s = getIndex();
 		if (s > 0)
 		{
 			int length = putIndex() - s;
@@ -95,7 +95,6 @@
 			{
 				System.arraycopy(_bytes, s,_bytes, 0, length);
 			}
-			if (markIndex() > 0) setMarkIndex(markIndex() - s);
 			setGetIndex(getIndex() - s);
 			setPutIndex(putIndex() - s);
 		}
--- a/src/org/eclipse/jetty/io/View.java	Fri Nov 04 01:10:12 2016 -0600
+++ b/src/org/eclipse/jetty/io/View.java	Fri Nov 04 02:26:54 2016 -0600
@@ -36,7 +36,6 @@
 		setGetIndex(0);
 		setPutIndex(buffer.putIndex());
 		setGetIndex(buffer.getIndex());
-		setMarkIndex(buffer.markIndex());
 	}
 
 
@@ -61,7 +60,6 @@
 	@Override
 	public void clear()
 	{
-		setMarkIndex(-1);
 		setGetIndex(0);
 		setPutIndex(_buffer.getIndex());
 		setGetIndex(_buffer.getIndex());