changeset 1046:a8c92b0a08ed

add JBuffer
author Franklin Schmidt <fschmidt@gmail.com>
date Mon, 07 Nov 2016 22:39:39 -0700
parents 48506d03e230
children 1accf965d51a
files src/org/eclipse/jetty/http/AbstractGenerator.java 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/BufferUtil.java src/org/eclipse/jetty/io/JBuffer.java src/org/eclipse/jetty/io/nio/ChannelEndPoint.java src/org/eclipse/jetty/io/nio/SslConnection.java src/org/eclipse/jetty/server/AbstractHttpConnection.java src/org/eclipse/jetty/server/Connector.java src/org/eclipse/jetty/server/ssl/SslSelectChannelConnector.java
diffstat 11 files changed, 400 insertions(+), 58 deletions(-) [+]
line wrap: on
line diff
--- a/src/org/eclipse/jetty/http/AbstractGenerator.java	Sun Nov 06 14:54:43 2016 -0700
+++ b/src/org/eclipse/jetty/http/AbstractGenerator.java	Mon Nov 07 22:39:39 2016 -0700
@@ -22,7 +22,7 @@
 
 import org.eclipse.jetty.io.Buffer;
 import org.eclipse.jetty.io.Buffers;
-import org.eclipse.jetty.io.ByteArrayBuffer;
+import org.eclipse.jetty.io.BufferUtil;
 import org.eclipse.jetty.io.EndPoint;
 import org.eclipse.jetty.io.EofException;
 import org.slf4j.Logger;
@@ -214,7 +214,7 @@
 			// TODO don't hard code
 			if (len>1024)
 				len=1024;
-			_reason = new ByteArrayBuffer(len);
+			_reason = BufferUtil.newBuffer(len);
 			for (int i=0;i<len;i++)
 			{
 				char ch = reason.charAt(i);
@@ -334,12 +334,12 @@
 			if (content != null)
 			{
 				completeHeader(null, false);
-				addContent(new ByteArrayBuffer(content), LAST);
+				addContent(BufferUtil.wrap(content), LAST);
 			}
 			else if (code>=400)
 			{
 				completeHeader(null, false);
-				addContent(new ByteArrayBuffer("Error: "+(reason==null?(""+code):reason)), LAST);
+				addContent(BufferUtil.wrap("Error: "+(reason==null?(""+code):reason)), LAST);
 			}
 			else
 			{
--- a/src/org/eclipse/jetty/http/HttpParser.java	Sun Nov 06 14:54:43 2016 -0700
+++ b/src/org/eclipse/jetty/http/HttpParser.java	Mon Nov 07 22:39:39 2016 -0700
@@ -21,7 +21,6 @@
 import java.io.IOException;
 
 import org.eclipse.jetty.io.Buffer;
-import org.eclipse.jetty.io.ByteArrayBuffer;
 import org.eclipse.jetty.io.BufferUtil;
 import org.eclipse.jetty.io.EndPoint;
 import org.eclipse.jetty.io.EofException;
@@ -68,7 +67,7 @@
 	private int _responseStatus; // If >0 then we are parsing a response
 	private boolean _persistent;
 
-	private Buffer _contentView = new ByteArrayBuffer(0); // View of the content in the buffer for {@link Input}
+	private Buffer _contentView = BufferUtil.EMPTY_BUFFER; // View of the content in the buffer for {@link Input}
 	private int _state = STATE_START;
 	private byte _eol;
 	private int _length;
@@ -189,10 +188,11 @@
 	{
 		try
 		{
-			int progress=0;
+			int progress = 0;
 
-			if (_state == STATE_END)
+			if (_state == STATE_END) {
 				return 0;
+			}
 
 			if (_buffer==null)
 				_buffer = _header;
@@ -200,7 +200,7 @@
 
 			if (_state == STATE_CONTENT && _contentPosition == _contentLength)
 			{
-				_state=STATE_END;
+				_state = STATE_END;
 				_handler.messageComplete(_contentPosition);
 				return 1;
 			}
@@ -210,11 +210,11 @@
 			// Fill buffer if we can
 			if (length == 0)
 			{
-				int filled=-1;
-				IOException ex=null;
+				int filled = -1;
+				IOException ex = null;
 				try
 				{
-					filled=fill();
+					filled = fill();
 					LOG.debug("filled {}/{}",filled,_buffer.remaining());
 				}
 				catch(IOException e)
@@ -246,16 +246,16 @@
 					{
 						case STATE_END:
 						case STATE_SEEKING_EOF:
-							_state=STATE_END;
+							_state = STATE_END;
 							break;
 
 						case STATE_EOF_CONTENT:
-							_state=STATE_END;
+							_state = STATE_END;
 							_handler.messageComplete(_contentPosition);
 							break;
 
 						default:
-							_state=STATE_END;
+							_state = STATE_END;
 							if (!_headResponse)
 								_handler.earlyEOF();
 							_handler.messageComplete(_contentPosition);
@@ -269,23 +269,23 @@
 
 					return -1;
 				}
-				length=_buffer.remaining();
+				length = _buffer.remaining();
 			}
 
 
 			// Handle header states
 			byte ch;
-			byte[] array=_buffer.array();
-			int last=_state;
+			byte[] array = _buffer.array();
+			int last = _state;
 			while (_state<STATE_END && length-->0)
 			{
 				if (last!=_state)
 				{
 					progress++;
-					last=_state;
+					last = _state;
 				}
 
-				ch=_buffer.get();
+				ch = _buffer.get();
 
 				if (_eol == HttpTokens.CARRIAGE_RETURN)
 				{
@@ -471,7 +471,7 @@
 												{
 													try
 													{
-														_contentLength=BufferUtil.toLong(value);
+														_contentLength = BufferUtil.toLong(value);
 													}
 													catch(NumberFormatException e)
 													{
@@ -791,7 +791,7 @@
 
 					case STATE_CONTENT:
 					{
-						long remaining=_contentLength - _contentPosition;
+						long remaining = _contentLength - _contentPosition;
 						if (remaining == 0)
 						{
 							_state = _persistent?STATE_END:STATE_SEEKING_EOF;
@@ -931,7 +931,7 @@
 					}
 				}
 
-				length=_buffer.remaining();
+				length = _buffer.remaining();
 			}
 
 			return progress;
--- a/src/org/eclipse/jetty/io/AbstractBuffer.java	Sun Nov 06 14:54:43 2016 -0700
+++ b/src/org/eclipse/jetty/io/AbstractBuffer.java	Mon Nov 07 22:39:39 2016 -0700
@@ -57,7 +57,7 @@
 		_access = access;
 	}
 
-	private byte[] asArray()
+	public final byte[] asArray()
 	{
 		byte[] bytes = new byte[remaining()];
 		byte[] array = array();
@@ -429,23 +429,6 @@
 		}
 	}
 
-	@Override
-	public final String toString(Charset charset)
-	{
-		try
-		{
-			byte[] bytes=array();
-			if (bytes!=null)
-				return new String(bytes,getIndex(),remaining(),charset);
-			return new String(asArray(), 0, remaining(),charset);
-		}
-		catch(Exception e)
-		{
-			LOG.warn("",e);
-			return new String(asArray(), 0, remaining());
-		}
-	}
-
 	/* ------------------------------------------------------------ */
 	public String toDebugString()
 	{
--- a/src/org/eclipse/jetty/io/Buffer.java	Sun Nov 06 14:54:43 2016 -0700
+++ b/src/org/eclipse/jetty/io/Buffer.java	Mon Nov 07 22:39:39 2016 -0700
@@ -256,10 +256,10 @@
 	
 
 	String toString(String charset);
-	
-	String toString(Charset charset);
 
 
 	// like ByteBuffer
 	Buffer duplicate();
+
+	public byte[] asArray();
 }
--- a/src/org/eclipse/jetty/io/BufferUtil.java	Sun Nov 06 14:54:43 2016 -0700
+++ b/src/org/eclipse/jetty/io/BufferUtil.java	Mon Nov 07 22:39:39 2016 -0700
@@ -18,6 +18,10 @@
 
 package org.eclipse.jetty.io;
 
+import java.nio.ByteBuffer;
+import org.eclipse.jetty.io.nio.NIOBuffer;
+import org.eclipse.jetty.util.StringUtil;
+
 
 /* ------------------------------------------------------------------------------- */
 /** Buffer utility methods.
@@ -273,4 +277,33 @@
 		buffer.put((byte)10);
 	}
 
+
+	public static final NIOBuffer EMPTY_BUFFER = new JBuffer(ByteBuffer.allocate(0));
+
+	public static NIOBuffer wrap(byte[] array,int offset,int length) {
+		return new JBuffer(ByteBuffer.wrap(array,offset,length));
+	}
+
+	public static NIOBuffer wrap(byte[] array) {
+		return new JBuffer(ByteBuffer.wrap(array));
+	}
+
+	public static NIOBuffer wrap(String s) {
+		byte[] bytes = StringUtil.getBytes(s);
+		ByteBuffer bb = ByteBuffer.wrap(bytes).asReadOnlyBuffer();
+		return new JBuffer(bb);
+	}
+
+	public static NIOBuffer newBuffer(int size) {
+		ByteBuffer bb = ByteBuffer.allocate(size);
+		bb.limit(0);
+		return new JBuffer(bb);
+	}
+
+	public static NIOBuffer newDirectBuffer(int size) {
+		ByteBuffer bb = ByteBuffer.allocateDirect(size);
+		bb.limit(0);
+		return new JBuffer(bb);
+	}
+
 }
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/src/org/eclipse/jetty/io/JBuffer.java	Mon Nov 07 22:39:39 2016 -0700
@@ -0,0 +1,322 @@
+// tmp class to implement Buffer until I can get rid of it
+
+package org.eclipse.jetty.io;
+
+import java.io.InputStream;
+import java.io.IOException;
+import java.nio.ByteBuffer;
+import java.nio.channels.Channels;
+import java.nio.channels.ReadableByteChannel;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+import org.eclipse.jetty.io.nio.NIOBuffer;
+import org.eclipse.jetty.util.TypeUtil;
+
+
+public final class JBuffer implements NIOBuffer {
+	private static final Logger LOG = LoggerFactory.getLogger(JBuffer.class);
+
+	private final ByteBuffer bb;
+
+	public JBuffer(ByteBuffer bb) {
+		this.bb = bb;
+	}
+
+	@Override
+	public byte[] array() {
+		return bb.hasArray() ? bb.array() : null;
+	}
+
+	@Override
+	public Buffer duplicate() {
+		return new JBuffer(bb.duplicate());
+	}
+
+	@Override
+	public int remaining() {
+		return bb.remaining();
+	}
+
+	@Override
+	public boolean isReadOnly() {
+		return bb.isReadOnly();
+	}
+
+	@Override
+	public boolean hasRemaining() {
+		return bb.hasRemaining();
+	}
+
+	@Override
+	public byte get() {
+		return bb.get();
+	}
+
+	@Override
+	public void compact() {
+		int n = bb.remaining();
+		bb.compact();
+		bb.position(0);
+		bb.limit(n);
+	}
+
+	@Override
+	public int capacity() {
+		return bb.capacity();
+	}
+
+	@Override
+	public boolean isDirect() {
+		return bb.isDirect();
+	}
+
+
+
+	@Override
+	public ByteBuffer getByteBuffer() {
+		ByteBuffer dup = bb.duplicate();
+		dup.limit(dup.capacity());
+		return dup;
+	}
+
+	@Override
+	public int getIndex() {
+		return bb.position();
+	}
+
+	@Override
+	public void clear() {
+		bb.position(0);
+		bb.limit(0);
+	}
+
+	@Override
+	public int space() {
+		return bb.capacity() - bb.limit();
+	}
+
+	@Override
+	public Buffer buffer() {
+		return this;
+	}
+
+
+	@Override
+	public Buffer get(int length) {
+		ByteBuffer dup = bb.duplicate();
+		int end = bb.position()+length;
+		dup.limit(end);
+		bb.position(end);
+		return new JBuffer(dup);
+	}
+
+	@Override
+	public int get(byte[] b, int offset, int length) {
+		int remaining = bb.remaining();
+		if( remaining == 0 )
+			return -1;
+		if( length > remaining )
+			length = remaining;
+		bb.get(b,offset,length);
+		return length;
+	}
+
+
+	@Override
+	public int put(Buffer src) {
+		return put(src.asArray());
+	}
+
+	@Override
+	public void put(byte b)
+	{
+		ByteBuffer dup = bb.duplicate();
+		dup.position(bb.limit());
+		dup.limit(bb.capacity());
+		dup.put(b);
+		bb.limit(bb.limit()+1);
+	}
+
+	@Override
+	public int put(byte[] b, int offset, int length) {
+		ByteBuffer dup = bb.duplicate();
+		int put = bb.limit();
+		int capacity = bb.capacity();
+		dup.position(put);
+		dup.limit(capacity);
+		if( length > capacity - put )
+			length = capacity - put;
+		dup.put(b,offset,length);
+		bb.limit(put+length);
+		return length;
+	}
+
+	@Override
+	public int put(byte[] b) {
+		return put(b,0,b.length);
+	}
+
+	@Override
+	public final int putIndex() {
+		return bb.limit();
+	}
+
+	@Override
+	public void setGetIndex(int getIndex) {
+		bb.position(getIndex);
+	}
+
+	@Override
+	public void setPutIndex(int putIndex) {
+		bb.limit(putIndex);
+	}
+
+	@Override
+	public int skip(int n) {
+		if (remaining() < n) n = remaining();
+		bb.position(bb.position() + n);
+		return n;
+	}
+
+	@Override
+	public Buffer slice() {
+		return duplicate();
+	}
+
+	@Override
+	public final Buffer sliceFrom(int index) {
+		ByteBuffer dup = bb.duplicate();
+		dup.position(index);
+		dup.limit(bb.position()-1);
+		return new JBuffer(dup);
+	}
+
+	@Override
+	public int readFrom(InputStream in,int max) throws IOException {
+		ByteBuffer dup = bb.duplicate();
+		int put = bb.limit();
+		dup.limit( Math.min(put+max,bb.capacity()) );
+		dup.position(put);
+
+		ReadableByteChannel chan = Channels.newChannel(in);
+		int n = chan.read(dup);
+
+		if( n > 0 )
+			bb.limit(put+n);
+		return n;
+	}
+
+	public final byte[] asArray() {
+		byte[] bytes = new byte[remaining()];
+		bb.duplicate().get(bytes);
+		return bytes;
+	}
+
+	@Override
+	public String toString()
+	{
+		return toString("ISO-8859-1");
+	}
+
+	@Override
+	public final String toString(int index, int length) {
+		ByteBuffer dup = bb.duplicate();
+		dup.limit(index+length);
+		dup.position(index);
+		return new JBuffer(dup).toString();
+	}
+
+	@Override
+	public final String toString(String charset)
+	{
+		byte[] bytes = asArray();
+		try
+		{
+			return new String(bytes,charset);
+		}
+		catch(Exception e)
+		{
+			LOG.warn("",e);
+			return new String(bytes);
+		}
+	}
+
+	@Override
+	public String toDetailString()
+	{
+		StringBuilder buf = new StringBuilder();
+		buf.append("[");
+		buf.append(super.hashCode());
+		buf.append(",");
+		buf.append(this.buffer().hashCode());
+		buf.append(",g=");
+		buf.append(getIndex());
+		buf.append(",p=");
+		buf.append(putIndex());
+		buf.append(",c=");
+		buf.append(capacity());
+		buf.append("]={");
+		int count = 0;
+		for (int i = getIndex(); i < putIndex(); i++)
+		{
+			byte b =  peek(i);
+			TypeUtil.toHex(b,buf);
+			if (count++ == 50)
+			{
+				if (putIndex() - i > 20)
+				{
+					buf.append(" ... ");
+					i = putIndex() - 20;
+				}
+			}
+		}
+		buf.append('}');
+		return buf.toString();
+	}
+
+
+
+	private Buffer pokeBuffer(int index) {
+		Buffer dup = duplicate();
+		dup.setPutIndex(index);
+		return dup;
+	}
+
+	@Override
+	public int poke(int index, byte b[], int offset, int length) {
+		return pokeBuffer(index).put(b,offset,length);
+	}
+
+	@Override
+	public void poke(int index, byte b) {
+		pokeBuffer(index).put(b);
+	}
+
+	@Override
+	public int poke(int index, Buffer src) {
+		return pokeBuffer(index).put(src);
+	}
+
+	private Buffer peekBuffer(int index) {
+		Buffer dup = duplicate();
+		dup.setGetIndex(index);
+		dup.setPutIndex(dup.capacity());
+		return dup;
+	}
+
+	@Override
+	public int peek(int index, byte[] b, int offset, int length) {
+		return peekBuffer(index).get(b,offset,length);
+	}
+
+	@Override
+	public byte peek(int index) {
+		return bb.get(index);
+	}
+
+	@Override
+	public byte peek() {
+		return peek(bb.position());
+	}
+
+}
--- a/src/org/eclipse/jetty/io/nio/ChannelEndPoint.java	Sun Nov 06 14:54:43 2016 -0700
+++ b/src/org/eclipse/jetty/io/nio/ChannelEndPoint.java	Mon Nov 07 22:39:39 2016 -0700
@@ -183,7 +183,7 @@
 					try
 					{
 						bbuf.position(buffer.putIndex());
-						len=_channel.read(bbuf);
+						len = _channel.read(bbuf);
 					}
 					finally
 					{
@@ -215,7 +215,7 @@
 
 				if (len>0)
 					throw x;
-				len=-1;
+				len = -1;
 			}
 		}
 		else
--- a/src/org/eclipse/jetty/io/nio/SslConnection.java	Sun Nov 06 14:54:43 2016 -0700
+++ b/src/org/eclipse/jetty/io/nio/SslConnection.java	Mon Nov 07 22:39:39 2016 -0700
@@ -31,6 +31,7 @@
 import org.eclipse.jetty.io.AbstractConnection;
 import org.eclipse.jetty.io.AsyncEndPoint;
 import org.eclipse.jetty.io.Buffer;
+import org.eclipse.jetty.io.BufferUtil;
 import org.eclipse.jetty.io.EndPoint;
 import org.eclipse.jetty.server.AsyncHttpConnection;
 import org.slf4j.Logger;
@@ -50,7 +51,7 @@
 {
 	private final Logger _logger = LoggerFactory.getLogger("org.eclipse.jetty.io.nio.ssl");
 
-	private static final NIOBuffer __ZERO_BUFFER = new IndirectNIOBuffer(0);
+	private static final NIOBuffer __ZERO_BUFFER = BufferUtil.EMPTY_BUFFER;
 
 	private static final ThreadLocal<SslBuffers> __buffers = new ThreadLocal<SslBuffers>();
 	private final SSLEngine _engine;
@@ -80,9 +81,9 @@
 
 		SslBuffers(int packetSize, int appSize)
 		{
-			_in=new IndirectNIOBuffer(packetSize);
-			_out=new IndirectNIOBuffer(packetSize);
-			_unwrap=new IndirectNIOBuffer(appSize);
+			_in = BufferUtil.newBuffer(packetSize);
+			_out = BufferUtil.newBuffer(packetSize);
+			_unwrap = BufferUtil.newBuffer(appSize);
 		}
 	}
 
--- a/src/org/eclipse/jetty/server/AbstractHttpConnection.java	Sun Nov 06 14:54:43 2016 -0700
+++ b/src/org/eclipse/jetty/server/AbstractHttpConnection.java	Mon Nov 07 22:39:39 2016 -0700
@@ -42,7 +42,7 @@
 import org.eclipse.jetty.io.AbstractConnection;
 import org.eclipse.jetty.io.Buffer;
 import org.eclipse.jetty.io.Buffers;
-import org.eclipse.jetty.io.ByteArrayBuffer;
+import org.eclipse.jetty.io.BufferUtil;
 import org.eclipse.jetty.io.EndPoint;
 import org.eclipse.jetty.io.EofException;
 import org.eclipse.jetty.io.RuntimeIOException;
@@ -704,7 +704,7 @@
 	public final class Output extends ServletOutputStream
 	{
 		private boolean _closed;
-		private ByteArrayBuffer _onebyte;
+		private Buffer _onebyte;
 		
 		// These are held here for reuse by Writer
 		String _characterEncoding;
@@ -720,20 +720,20 @@
 		@Override
 		public final void write(byte[] b, int off, int len) throws IOException
 		{
-			write(new ByteArrayBuffer(b,off,len));
+			write(BufferUtil.wrap(b,off,len));
 		}
 	
 		@Override
 		public final void write(byte[] b) throws IOException
 		{
-			write(new ByteArrayBuffer(b));
+			write(BufferUtil.wrap(b));
 		}
 	
 		@Override
 		public final void write(int b) throws IOException
 		{
 			if (_onebyte==null)
-				_onebyte = new ByteArrayBuffer(1);
+				_onebyte = BufferUtil.newBuffer(1);
 			else
 				_onebyte.clear();
 			_onebyte.put((byte)b);
--- a/src/org/eclipse/jetty/server/Connector.java	Sun Nov 06 14:54:43 2016 -0700
+++ b/src/org/eclipse/jetty/server/Connector.java	Mon Nov 07 22:39:39 2016 -0700
@@ -30,6 +30,7 @@
 
 import org.eclipse.jetty.io.Buffer;
 import org.eclipse.jetty.io.Buffers;
+import org.eclipse.jetty.io.BufferUtil;
 import org.eclipse.jetty.io.EndPoint;
 import org.eclipse.jetty.io.EofException;
 import org.eclipse.jetty.io.nio.DirectNIOBuffer;
@@ -253,7 +254,8 @@
 	// my own buffers
 
 	protected Buffer newBuffer(int size) {
-		return new DirectNIOBuffer(size);
+//		return new DirectNIOBuffer(size);
+		return BufferUtil.newDirectBuffer(size);
 	}
 
 	private class MyBuffers implements Buffers {
@@ -267,7 +269,8 @@
 
 		@Override
 		public Buffer getHeader() {
-			return new IndirectNIOBuffer(headerSize);
+//			return new IndirectNIOBuffer(headerSize);
+			return BufferUtil.newBuffer(headerSize);
 		}
 
 		@Override
--- a/src/org/eclipse/jetty/server/ssl/SslSelectChannelConnector.java	Sun Nov 06 14:54:43 2016 -0700
+++ b/src/org/eclipse/jetty/server/ssl/SslSelectChannelConnector.java	Mon Nov 07 22:39:39 2016 -0700
@@ -29,12 +29,12 @@
 import org.eclipse.jetty.io.AsyncEndPoint;
 import org.eclipse.jetty.io.Buffer;
 import org.eclipse.jetty.io.Buffers;
+import org.eclipse.jetty.io.BufferUtil;
 import org.eclipse.jetty.io.EndPoint;
 import org.eclipse.jetty.io.RuntimeIOException;
 import org.eclipse.jetty.io.nio.AsyncConnection;
 import org.eclipse.jetty.io.nio.SslConnection;
 import org.eclipse.jetty.io.nio.SelectChannelEndPoint;
-import org.eclipse.jetty.io.nio.IndirectNIOBuffer;
 import org.eclipse.jetty.server.Request;
 import org.eclipse.jetty.server.Server;
 import org.eclipse.jetty.server.AbstractHttpConnection;
@@ -76,7 +76,7 @@
 
 	@Override
 	protected Buffer newBuffer(int size) {
-		return new IndirectNIOBuffer(size);
+		return BufferUtil.newBuffer(size);
 	}