diff src/org/eclipse/jetty/http/HttpGenerator.java @ 1070:a44fc6b53757

fix use of HttpGenerator._buffer
author Franklin Schmidt <fschmidt@gmail.com>
date Thu, 10 Nov 2016 01:23:37 -0700
parents 7dd6ec499000
children b4ba8a4d5a16
line wrap: on
line diff
--- a/src/org/eclipse/jetty/http/HttpGenerator.java	Thu Nov 10 00:42:51 2016 -0700
+++ b/src/org/eclipse/jetty/http/HttpGenerator.java	Thu Nov 10 01:23:37 2016 -0700
@@ -178,7 +178,7 @@
 //			content.clear();
 			_content = null;
 		}
-		else if ((_buffer==null || _buffer.remaining()==0) && _content.hasRemaining() && (_last || isCommitted() && _content.remaining()>1024))
+		else if ((_buffer.position()==0) && _content.hasRemaining() && (_last || isCommitted() && _content.remaining()>1024))
 		{
 			_bypass = true;
 		}
@@ -186,8 +186,7 @@
 		{
 //System.out.println("qqqqqqqqqqqqqqqqqqq c");
 			// Copy _content to buffer;
-			int len = _buffer.put(_content);
-			_content.skip(len);
+			_buffer.putQ(_content);
 			if (!_content.hasRemaining())
 				_content = null;
 		}
@@ -217,7 +216,7 @@
 				throw new IllegalStateException("FULL");
 		}
 
-		_contentWritten -= _buffer.remaining();
+		_contentWritten -= _buffer.position();
 /*
 		// Handle the _content
 		if (_head)
@@ -230,7 +229,7 @@
 	public boolean isBufferFull()
 	{
 		// Should we flush the buffers?
-		return isBufferFull2() || _bufferChunked || _bypass /* || (_contentLength == HttpTokens.CHUNKED_CONTENT && _buffer != null && _buffer.space() < CHUNK_SPACE)*/;
+		return isBufferFull2() || _bufferChunked || _bypass /* || (_contentLength == HttpTokens.CHUNKED_CONTENT && _buffer.remaining() < CHUNK_SPACE)*/;
 	}
 
 	public void send1xx(int code) throws IOException
@@ -334,8 +333,7 @@
 				{
 					_noContent = true;
 					_content = null;
-					if (_buffer!=null)
-						_buffer.clearJ();
+					_buffer.clear();
 					// end the header.
 
 					if (_status!=101 )
@@ -349,8 +347,7 @@
 				{
 					_noContent = true;
 					_content = null;
-					if (_buffer!=null)
-						_buffer.clearJ();
+					_buffer.clear();
 				}
 			}
 
@@ -670,8 +667,10 @@
 						throw new IllegalStateException(); // should never happen!
 					case 6:
 						_header.flip();
+						_buffer.flip();
 						len = _endp.flush(_header, _buffer, null);
 						_header.compact();
+						_buffer.compact();
 						break;
 					case 5:
 						_header.flip();
@@ -684,35 +683,36 @@
 						_header.compact();
 						break;
 					case 3:
+						_buffer.flip();
 						len = _endp.flush(_buffer, _content, null);
+						_buffer.compact();
 						break;
 					case 2:
+						_buffer.flip();
 						len = _endp.flush(_buffer);
+						_buffer.compact();
 						break;
 					case 1:
 						len = _endp.flush(_content);
 						break;
 					case 0:
 					{
-						len=0;
+						len = 0;
 						// Nothing more we can write now.
 						_header.clear();
 
 						_bypass = false;
 						_bufferChunked = false;
 
-						if (_buffer != null)
+						_buffer.clear();  // ?
+						if (_contentLength == HttpTokens.CHUNKED_CONTENT)
 						{
-							_buffer.clearJ();
-							if (_contentLength == HttpTokens.CHUNKED_CONTENT)
+							// Special case handling for small left over buffer from
+							// an addContent that caused a buffer flush.
+							if (_content != null && _content.remaining() < _buffer.remaining() && _state != STATE_FLUSHING)
 							{
-								// Special case handling for small left over buffer from
-								// an addContent that caused a buffer flush.
-								if (_content != null && _content.remaining() < _buffer.space() && _state != STATE_FLUSHING)
-								{
-									_buffer.put(_content);
-									_content = null;
-								}
+								_buffer.putQ(_content);
+								_content = null;
 							}
 						}
 
@@ -745,14 +745,14 @@
 		catch (IOException e)
 		{
 			LOG.trace("",e);
-			throw (e instanceof EofException) ? e:new EofException(e);
+			throw (e instanceof EofException) ? e : new EofException(e);
 		}
 	}
 
 	private int flushMask()
 	{
 		return  ((_header.position() > 0)?4:0)
-		| ((_buffer != null && _buffer.remaining() > 0)?2:0)
+		| ((_buffer.position() > 0)?2:0)
 		| ((_bypass && _content != null && _content.hasRemaining())?1:0);
 	}
 
@@ -762,10 +762,9 @@
 		if (!_bufferChunked)
 		{
 			// Refill buffer if possible
-			if (!_bypass && _content != null && _content.hasRemaining() && _buffer != null && _buffer.space() > 0)
+			if (!_bypass && _content != null && _content.hasRemaining() && _buffer.hasRemaining())
 			{
-				int len = _buffer.put(_content);
-				_content.skip(len);
+				_buffer.putQ(_content);
 				if (!_content.hasRemaining())
 					_content = null;
 			}
@@ -773,7 +772,7 @@
 			// Chunk buffer if need be
 			if (_contentLength == HttpTokens.CHUNKED_CONTENT)
 			{
-				if (_bypass && (_buffer==null||_buffer.remaining()==0) && _content!=null)
+				if (_bypass && _buffer.position()==0 && _content!=null)
 				{
 					// this is a bypass write
 					int size = _content.remaining();
@@ -793,9 +792,9 @@
 					// Need a CRLF after the content
 					_needCRLF = true;
 				}
-				else if (_buffer!=null)
+				else
 				{
-					int size = _buffer.remaining();
+					int size = _buffer.position();
 					if (size > 0)
 					{
 						// Prepare a chunk!
@@ -811,8 +810,8 @@
 						_header.putQ(HttpTokens.CRLF);
 
 						// Add end chunk trailer.
-						if (_buffer.space() >= 2)
-							_buffer.put(HttpTokens.CRLF);
+						if (_buffer.remaining() >= 2)
+							_buffer.putQ(HttpTokens.CRLF);
 						else
 							_needCRLF = true;
 					}
@@ -821,40 +820,18 @@
 				// If we need EOC and everything written
 				if (_needEOC && (_content == null || !_content.hasRemaining()))
 				{
-					if (_needCRLF)
-					{
-						if (_buffer == null && _header.remaining() >= HttpTokens.CRLF.length)
-						{
-							_header.putQ(HttpTokens.CRLF);
-							_needCRLF = false;
-						}
-						else if (_buffer!=null && _buffer.space() >= HttpTokens.CRLF.length)
-						{
-							_buffer.put(HttpTokens.CRLF);
-							_needCRLF = false;
-						}
+					if (_needCRLF && _buffer.remaining() >= HttpTokens.CRLF.length) {
+						_buffer.putQ(HttpTokens.CRLF);
+						_needCRLF = false;
 					}
 
-					if (!_needCRLF && _needEOC)
-					{
-						if (_buffer == null && _header.remaining() >= LAST_CHUNK.length)
+					if (!_needCRLF && _needEOC && _buffer.remaining() >= LAST_CHUNK.length) {
+						if (!_head)
 						{
-							if (!_head)
-							{
-								_header.putQ(LAST_CHUNK);
-								_bufferChunked=true;
-							}
-							_needEOC = false;
+							_buffer.putQ(LAST_CHUNK);
+							_bufferChunked = true;
 						}
-						else if (_buffer!=null && _buffer.space() >= LAST_CHUNK.length)
-						{
-							if (!_head)
-							{
-								_buffer.put(LAST_CHUNK);
-								_bufferChunked=true;
-							}
-							_needEOC = false;
-						}
+						_needEOC = false;
 					}
 				}
 			}
@@ -869,13 +846,13 @@
 	public String toString()
 	{
 		JBuffer header = _header;
-		JBuffer buffer=_buffer;
+		JBuffer buffer = _buffer;
 		JBuffer content = _content;
 		return String.format("%s{s=%d,h=%d,b=%d,c=%d}",
 				getClass().getSimpleName(),
 				_state,
 				header == null ? -1 : header.position(),
-				buffer == null ? -1 : buffer.remaining(),
+				buffer == null ? -1 : buffer.position(),
 				content == null ? -1 : content.remaining());
 	}
 
@@ -930,7 +907,6 @@
 		this._endp = io;
 		_header = _buffers.getHeader();
 		_buffer = _buffers.getBuffer();
-		_buffer.limit(0);
 	}
 
 	public final boolean isOpen()
@@ -948,8 +924,7 @@
 		_contentWritten = 0;
 		_contentLength = HttpTokens.UNKNOWN_CONTENT;
 		_content = null;
-		if (_buffer!=null)
-			_buffer.clearJ();
+		_buffer.clear();
 	}
 
 	/* ------------------------------------------------------------ */
@@ -1052,21 +1027,14 @@
 
 	public final void completeUncheckedAddContent()
 	{
-		_contentWritten += _buffer.remaining();
+		_contentWritten += _buffer.position();
 		if (_head)
-			_buffer.clearJ();
+			_buffer.clear();
 	}
 
 	private boolean isBufferFull2()
 	{
-		if (_buffer != null && _buffer.space()==0)
-		{
-			if (_buffer.remaining()==0)
-				_buffer.limit(0);
-			return _buffer.space()==0;
-		}
-
-		return _content!=null && _content.remaining()>0;
+		return !_buffer.hasRemaining() || _content!=null && _content.remaining()>0;
 	}
 
 	public final boolean isWritten()
@@ -1099,18 +1067,18 @@
 	public final void flush(long maxIdleTime) throws IOException
 	{
 		// block until everything is flushed
-		long now=System.currentTimeMillis();
-		long end=now+maxIdleTime;
+		long now = System.currentTimeMillis();
+		long end = now+maxIdleTime;
 		JBuffer content = _content;
 		JBuffer buffer = _buffer;
-		if (content!=null && content.remaining()>0 || buffer!=null && buffer.remaining()>0 || isBufferFull())
+		if (content!=null && content.remaining()>0 || buffer.position()>0 || isBufferFull())
 		{
 			flushBuffer();
 
-			while (now<end && (content!=null && content.remaining()>0 ||buffer!=null && buffer.remaining()>0) && _endp.isOpen()&& !_endp.isOutputShutdown())
+			while (now<end && (content!=null && content.remaining()>0 || buffer.position()>0) && _endp.isOpen()&& !_endp.isOutputShutdown())
 			{
 				blockForOutput(end-now);
-				now=System.currentTimeMillis();
+				now = System.currentTimeMillis();
 			}
 		}
 	}