changeset 1054:87275900646e

remove JBuffer.toString()
author Franklin Schmidt <fschmidt@gmail.com>
date Tue, 08 Nov 2016 01:03:02 -0700
parents 7e4b41247544
children e6ca3cb52837
files src/org/eclipse/jetty/http/HttpParser.java src/org/eclipse/jetty/io/BufferUtil.java src/org/eclipse/jetty/io/JBuffer.java
diffstat 3 files changed, 42 insertions(+), 41 deletions(-) [+]
line wrap: on
line diff
--- a/src/org/eclipse/jetty/http/HttpParser.java	Tue Nov 08 00:32:02 2016 -0700
+++ b/src/org/eclipse/jetty/http/HttpParser.java	Tue Nov 08 01:03:02 2016 -0700
@@ -89,12 +89,12 @@
 		_mark = _buffer.getIndex() - 1;
 	}
 
-	private JBuffer sliceFromMark() {
+	private String sliceFromMark() {
 		JBuffer buf = _buffer.duplicate();
 		buf.position(_mark);
 		buf.limit(_buffer.position()-1);
 		_mark = -1;
-		return buf;
+		return BufferUtil.getString(buf);
 	}
 
 	private void clear() {
@@ -114,7 +114,7 @@
 		}
 	}
 
-	public JBuffer getBuffer(int length) {
+	private JBuffer getBuffer(int length) {
 		JBuffer dup = _buffer.duplicate();
 		int end = _buffer.position() + length;
 		dup.limit(end);
@@ -122,10 +122,17 @@
 		return dup;
 	}
 
-	public byte peek() {
+	private byte peek() {
 		return _buffer.get(_buffer.position());
 	}
 
+	private String bufferToString(int index, int length) {
+		JBuffer dup = _buffer.duplicate();
+		dup.limit(index+length);
+		dup.position(index);
+		return BufferUtil.getString(dup);
+	}
+
 
 	public long getContentLength()
 	{
@@ -328,7 +335,7 @@
 					case STATE_FIELD0:
 						if (ch == HttpTokens.SPACE)
 						{
-							_tok0 = _buffer.toString(_mark, _buffer.getIndex() - 1 - _mark);
+							_tok0 = bufferToString(_mark, _buffer.getIndex() - 1 - _mark);
 							_responseStatus = !HttpVersions.CACHE.contains(_tok0)?-1:0;
 							_state=STATE_SPACE1;
 							continue;
@@ -361,7 +368,7 @@
 						if (ch == HttpTokens.SPACE)
 						{
 //							_tok1.update(_mark, _buffer.getIndex() - 1);
-							_tok1 = _buffer.toString(_mark, _buffer.getIndex() - 1 - _mark);
+							_tok1 = bufferToString(_mark, _buffer.getIndex() - 1 - _mark);
 							_state = STATE_SPACE2;
 							continue;
 						}
@@ -388,14 +395,14 @@
 						if (ch == HttpTokens.SPACE)
 						{
 //							_tok1.update(_mark, _buffer.getIndex() - 1);
-							_tok1 = _buffer.toString(_mark, _buffer.getIndex() - 1 - _mark);
+							_tok1 = bufferToString(_mark, _buffer.getIndex() - 1 - _mark);
 							_state=STATE_SPACE2;
 							continue;
 						}
 						else if (ch < HttpTokens.SPACE && ch>=0)
 						{
 							// HTTP/0.9
-							_handler.startRequest(_tok0, sliceFromMark().toString(), null);
+							_handler.startRequest(_tok0, sliceFromMark(), null);
 							_persistent = false;
 							_state = STATE_SEEKING_EOF;
 							_handler.headerComplete();
@@ -441,7 +448,7 @@
 //								_handler.startResponse(version=HttpVersions.CACHE.lookup(_tok0), _responseStatus,sliceFromMark());
 								version = _tok0;
 							else
-								_handler.startRequest(_tok0, _tok1, version=sliceFromMark().toString());
+								_handler.startRequest(_tok0, _tok1, version=sliceFromMark());
 							_eol=ch;
 							_persistent = HttpVersions.CACHE.getOrdinal(version) >= HttpVersions.HTTP_1_1_ORDINAL;
 							_state=STATE_HEADER;
@@ -637,14 +644,14 @@
 							case HttpTokens.CARRIAGE_RETURN:
 							case HttpTokens.LINE_FEED:
 								if (_length > 0) {
-									_tok0 = _buffer.toString(_mark, _length);
+									_tok0 = bufferToString(_mark, _length);
 								}
 								_eol=ch;
 								_state=STATE_HEADER;
 								break;
 							case HttpTokens.COLON:
 								if (_length > 0 && _cached==null) {
-									_tok0 = _buffer.toString(_mark, _length);
+									_tok0 = bufferToString(_mark, _length);
 								}
 								_length=-1;
 								_state=STATE_HEADER_VALUE;
@@ -670,14 +677,14 @@
 							case HttpTokens.CARRIAGE_RETURN:
 							case HttpTokens.LINE_FEED:
 								if (_length > 0) {
-									_tok0 = _buffer.toString(_mark,_length);
+									_tok0 = bufferToString(_mark,_length);
 								}
 								_eol=ch;
 								_state=STATE_HEADER;
 								break;
 							case HttpTokens.COLON:
 								if (_length > 0 && _cached==null) {
-									_tok0 = _buffer.toString(_mark,_length);
+									_tok0 = bufferToString(_mark,_length);
 								}
 								_length=-1;
 								_state=STATE_HEADER_VALUE;
@@ -703,13 +710,13 @@
 								{
 									if (_tok1.length() == 0)
 //										_tok1.update(_mark, _mark + _length);
-										_tok1 = _buffer.toString(_mark, _length);
+										_tok1 = bufferToString(_mark, _length);
 									else
 									{
 										// Continuation line!
 										if (_multiLineValue == null) _multiLineValue = _tok1;
 //										_tok1.update(_mark, _mark + _length);
-										_tok1 = _buffer.toString(_mark, _length);
+										_tok1 = bufferToString(_mark, _length);
 										_multiLineValue += " " + _tok1;
 									}
 								}
@@ -738,13 +745,13 @@
 								{
 									if (_tok1.length() == 0)
 //										_tok1.update(_mark, _mark + _length);
-										_tok1 = _buffer.toString(_mark, _length);
+										_tok1 = bufferToString(_mark, _length);
 									else
 									{
 										// Continuation line!
 										if (_multiLineValue == null) _multiLineValue = _tok1;
 //										_tok1.update(_mark, _mark + _length);
-										_tok1 = _buffer.toString(_mark, _length);
+										_tok1 = bufferToString(_mark, _length);
 										_multiLineValue += " " + _tok1;
 									}
 								}
--- a/src/org/eclipse/jetty/io/BufferUtil.java	Tue Nov 08 00:32:02 2016 -0700
+++ b/src/org/eclipse/jetty/io/BufferUtil.java	Tue Nov 08 01:03:02 2016 -0700
@@ -19,6 +19,7 @@
 package org.eclipse.jetty.io;
 
 import java.nio.ByteBuffer;
+import java.nio.charset.Charset;
 import org.eclipse.jetty.util.StringUtil;
 
 
@@ -305,4 +306,13 @@
 		return new JBuffer(bb);
 	}
 
+
+
+	private static final Charset ISO_8859_1 = Charset.forName("ISO-8859-1");
+
+	public static String getString(JBuffer buffer) {
+		byte[] bytes = new byte[buffer.remaining()];
+		buffer.get(bytes);
+		return new String(bytes,ISO_8859_1);
+	}
 }
--- a/src/org/eclipse/jetty/io/JBuffer.java	Tue Nov 08 00:32:02 2016 -0700
+++ b/src/org/eclipse/jetty/io/JBuffer.java	Tue Nov 08 01:03:02 2016 -0700
@@ -61,6 +61,10 @@
 		return bb.get();
 	}
 
+	public void get(byte[] bytes) {
+		bb.get(bytes);
+	}
+
 	public void compact() {
 		int n = bb.remaining();
 		bb.compact();
@@ -173,30 +177,10 @@
 	}
 
 	@Override
-	public String toString()
-	{
-		return toString("ISO-8859-1");
-	}
-
-	public final String toString(int index, int length) {
-		ByteBuffer dup = bb.duplicate();
-		dup.limit(index+length);
-		dup.position(index);
-		return new JBuffer(dup).toString();
-	}
-
-	private final String toString(String charset)
-	{
-		byte[] bytes = asArray();
-		try
-		{
-			return new String(bytes,charset);
-		}
-		catch(Exception e)
-		{
-			LOG.warn("",e);
-			return new String(bytes);
-		}
+	public String toString() {
+//		return toString("ISO-8859-1");
+//		Thread.dumpStack();
+		throw new RuntimeException("toString");
 	}