From 4ac511ba13cdd38d81a63b8c30e87928ce413cf4 Mon Sep 17 00:00:00 2001 From: Greg Wilkins Date: Wed, 22 May 2019 13:00:48 +0200 Subject: [PATCH] Issue #3681 minor cleanups Signed-off-by: Greg Wilkins --- .../org/eclipse/jetty/http/HttpField.java | 5 +- .../org/eclipse/jetty/http/HttpFields.java | 83 ++++++++++++++----- .../jetty/http2/hpack/MetaDataBuilder.java | 4 +- 3 files changed, 64 insertions(+), 28 deletions(-) diff --git a/jetty-http/src/main/java/org/eclipse/jetty/http/HttpField.java b/jetty-http/src/main/java/org/eclipse/jetty/http/HttpField.java index 0202e9310cd..dbbc048d839 100644 --- a/jetty-http/src/main/java/org/eclipse/jetty/http/HttpField.java +++ b/jetty-http/src/main/java/org/eclipse/jetty/http/HttpField.java @@ -276,12 +276,9 @@ public class HttpField public boolean isSameName(HttpField field) { - @SuppressWarnings("ReferenceEquality") - boolean sameObject = (field==this); - if (field==null) return false; - if (sameObject) + if (field==this) return true; if (_header!=null && _header==field.getHeader()) return true; diff --git a/jetty-http/src/main/java/org/eclipse/jetty/http/HttpFields.java b/jetty-http/src/main/java/org/eclipse/jetty/http/HttpFields.java index 6515faea23f..36a7c4b0423 100644 --- a/jetty-http/src/main/java/org/eclipse/jetty/http/HttpFields.java +++ b/jetty-http/src/main/java/org/eclipse/jetty/http/HttpFields.java @@ -31,9 +31,9 @@ import java.util.Map; import java.util.NoSuchElementException; import java.util.Set; import java.util.StringTokenizer; +import java.util.function.Consumer; import java.util.function.ToIntFunction; import java.util.stream.Stream; -import java.util.stream.StreamSupport; import org.eclipse.jetty.util.ArrayTernaryTrie; import org.eclipse.jetty.util.QuotedStringTokenizer; @@ -66,7 +66,7 @@ public class HttpFields implements Iterable */ public HttpFields() { - this(16 ); // TODO tune default + this(16 ); // TODO tune default. Currently based on small same of Chrome requests. } /** @@ -94,6 +94,13 @@ public class HttpFields implements Iterable { return _size; } + + @Override + public void forEach(Consumer action) + { + for (HttpField f : this) + action.accept(f); + } @Override public Iterator iterator() @@ -103,13 +110,13 @@ public class HttpFields implements Iterable public ListIterator listIterator() { - return new Itr(); + return new ListItr(); } public Stream stream() { - return StreamSupport.stream(Arrays.spliterator(_fields,0,_size),false); + return Arrays.stream(_fields); } /** @@ -120,10 +127,7 @@ public class HttpFields implements Iterable { final Set set = new HashSet<>(_size); for (HttpField f : this) - { - if (f!=null) - set.add(f.getName()); - } + set.add(f.getName()); return set; } @@ -269,9 +273,12 @@ public class HttpFields implements Iterable public List getValuesList(HttpHeader header) { final List list = new ArrayList<>(); - for (HttpField f : this) - if (f.getHeader()==header) + for (int i=0;i<_size;i++) + { + HttpField f = _fields[i]; + if (f.getHeader() == header) list.add(f.getValue()); + } return list; } @@ -284,9 +291,12 @@ public class HttpFields implements Iterable public List getValuesList(String name) { final List list = new ArrayList<>(); - for (HttpField f : this) + for (int i=0;i<_size;i++) + { + HttpField f = _fields[i]; if (f.getName().equalsIgnoreCase(name)) list.add(f.getValue()); + } return list; } @@ -301,8 +311,9 @@ public class HttpFields implements Iterable public boolean addCSV(HttpHeader header,String... values) { QuotedCSV existing = null; - for (HttpField f : this) + for (int i=0;i<_size;i++) { + HttpField f = _fields[i]; if (f.getHeader()==header) { if (existing==null) @@ -330,8 +341,9 @@ public class HttpFields implements Iterable public boolean addCSV(String name,String... values) { QuotedCSV existing = null; - for (HttpField f : this) + for (int i=0;i<_size;i++) { + HttpField f = _fields[i]; if (f.getName().equalsIgnoreCase(name)) { if (existing==null) @@ -348,7 +360,7 @@ public class HttpFields implements Iterable return false; } - protected String addCSV(QuotedCSV existing,String... values) + protected String addCSV(QuotedCSV existing, String... values) { // remove any existing values from the new values boolean add = true; @@ -1084,7 +1096,36 @@ public class HttpFields implements Iterable } - private class Itr implements ListIterator + private class Itr implements Iterator + { + int _cursor; // index of next element to return + + @Override + public boolean hasNext() + { + return _cursor != _size; + } + + @Override + public HttpField next() + { + if (_cursor == _size) + throw new NoSuchElementException(); + return _fields[_cursor++]; + } + + @Override + public void remove() + { + if (_cursor==0) + throw new IllegalStateException(); + System.arraycopy(_fields, _cursor, _fields,_cursor-1,_size - _cursor); + _size--; + _cursor--; + } + } + + private class ListItr implements ListIterator { int _cursor; // index of next element to return int _last=-1; @@ -1098,11 +1139,10 @@ public class HttpFields implements Iterable @Override public HttpField next() { - int i = _cursor; - if (i >= _size) + if (_cursor == _size) throw new NoSuchElementException(); - _cursor = i + 1; - return _fields[_last=i]; + _last = _cursor++; + return _fields[_last]; } @Override @@ -1110,7 +1150,6 @@ public class HttpFields implements Iterable { if (_last<0) throw new IllegalStateException(); - System.arraycopy(_fields,_last+1,_fields,_last,--_size-_last); _cursor=_last; _last=-1; @@ -1127,7 +1166,8 @@ public class HttpFields implements Iterable { if (_cursor == 0) throw new NoSuchElementException(); - return _fields[_last=--_cursor]; + _last = --_cursor; + return _fields[_last]; } @Override @@ -1159,5 +1199,4 @@ public class HttpFields implements Iterable _last=-1; } } - } diff --git a/jetty-http2/http2-hpack/src/main/java/org/eclipse/jetty/http2/hpack/MetaDataBuilder.java b/jetty-http2/http2-hpack/src/main/java/org/eclipse/jetty/http2/hpack/MetaDataBuilder.java index 2421477247d..601fcceab37 100644 --- a/jetty-http2/http2-hpack/src/main/java/org/eclipse/jetty/http2/hpack/MetaDataBuilder.java +++ b/jetty-http2/http2-hpack/src/main/java/org/eclipse/jetty/http2/hpack/MetaDataBuilder.java @@ -39,7 +39,7 @@ public class MetaDataBuilder private HostPortHttpField _authority; private String _path; private long _contentLength=Long.MIN_VALUE; - private HttpFields _fields = new HttpFields(16); // TODO make this tunable + private HttpFields _fields = new HttpFields(); private HpackException.StreamException _streamException; private boolean _request; private boolean _response; @@ -255,7 +255,7 @@ public class MetaDataBuilder } finally { - _fields = new HttpFields(Math.max(10, fields.size() + 5)); + _fields = new HttpFields(Math.max(16, fields.size() + 5)); _request = false; _response = false; _status = null;