433692 improved buffer resizing
This commit is contained in:
parent
b4d1060e88
commit
2e261b75d6
|
@ -246,14 +246,12 @@ public class GZIPContentDecoder implements ContentDecoder
|
||||||
if (output == null)
|
if (output == null)
|
||||||
{
|
{
|
||||||
// Save the inflated bytes and loop to see if we have finished
|
// Save the inflated bytes and loop to see if we have finished
|
||||||
output = new byte[decoded];
|
output = Arrays.copyOf(bytes, decoded);
|
||||||
System.arraycopy(bytes, 0, output, 0, decoded);
|
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
// Accumulate inflated bytes and loop to see if we have finished
|
// Accumulate inflated bytes and loop to see if we have finished
|
||||||
byte[] newOutput = new byte[output.length + decoded];
|
byte[] newOutput = Arrays.copyOf(output, output.length+decoded);
|
||||||
System.arraycopy(output, 0, newOutput, 0, output.length);
|
|
||||||
System.arraycopy(bytes, 0, newOutput, output.length, decoded);
|
System.arraycopy(bytes, 0, newOutput, output.length, decoded);
|
||||||
output = newOutput;
|
output = newOutput;
|
||||||
}
|
}
|
||||||
|
|
|
@ -18,11 +18,14 @@
|
||||||
|
|
||||||
package org.eclipse.jetty.client.util;
|
package org.eclipse.jetty.client.util;
|
||||||
|
|
||||||
|
import java.io.ByteArrayInputStream;
|
||||||
|
import java.io.InputStream;
|
||||||
import java.io.UnsupportedEncodingException;
|
import java.io.UnsupportedEncodingException;
|
||||||
import java.nio.ByteBuffer;
|
import java.nio.ByteBuffer;
|
||||||
import java.nio.charset.Charset;
|
import java.nio.charset.Charset;
|
||||||
import java.nio.charset.StandardCharsets;
|
import java.nio.charset.StandardCharsets;
|
||||||
import java.nio.charset.UnsupportedCharsetException;
|
import java.nio.charset.UnsupportedCharsetException;
|
||||||
|
import java.util.Arrays;
|
||||||
import java.util.Locale;
|
import java.util.Locale;
|
||||||
|
|
||||||
import org.eclipse.jetty.client.api.Response;
|
import org.eclipse.jetty.client.api.Response;
|
||||||
|
@ -30,6 +33,7 @@ import org.eclipse.jetty.client.api.Response.Listener;
|
||||||
import org.eclipse.jetty.client.api.Result;
|
import org.eclipse.jetty.client.api.Result;
|
||||||
import org.eclipse.jetty.http.HttpFields;
|
import org.eclipse.jetty.http.HttpFields;
|
||||||
import org.eclipse.jetty.http.HttpHeader;
|
import org.eclipse.jetty.http.HttpHeader;
|
||||||
|
import org.eclipse.jetty.util.BufferUtil;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* <p>Implementation of {@link Listener} that buffers the content up to a maximum length
|
* <p>Implementation of {@link Listener} that buffers the content up to a maximum length
|
||||||
|
@ -40,7 +44,7 @@ import org.eclipse.jetty.http.HttpHeader;
|
||||||
public abstract class BufferingResponseListener extends Listener.Adapter
|
public abstract class BufferingResponseListener extends Listener.Adapter
|
||||||
{
|
{
|
||||||
private final int maxLength;
|
private final int maxLength;
|
||||||
private volatile byte[] buffer = new byte[0];
|
private volatile ByteBuffer buffer;
|
||||||
private volatile String encoding;
|
private volatile String encoding;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -58,53 +62,57 @@ public abstract class BufferingResponseListener extends Listener.Adapter
|
||||||
*/
|
*/
|
||||||
public BufferingResponseListener(int maxLength)
|
public BufferingResponseListener(int maxLength)
|
||||||
{
|
{
|
||||||
this.maxLength = maxLength;
|
this.maxLength=maxLength;
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void onHeaders(Response response)
|
public void onHeaders(Response response)
|
||||||
{
|
{
|
||||||
|
super.onHeaders(response);
|
||||||
|
|
||||||
HttpFields headers = response.getHeaders();
|
HttpFields headers = response.getHeaders();
|
||||||
long length = headers.getLongField(HttpHeader.CONTENT_LENGTH.asString());
|
long length = headers.getLongField(HttpHeader.CONTENT_LENGTH.asString());
|
||||||
if (length > maxLength)
|
if (length > maxLength)
|
||||||
{
|
{
|
||||||
response.abort(new IllegalArgumentException("Buffering capacity exceeded"));
|
response.abort(new IllegalArgumentException("Buffering capacity exceeded"));
|
||||||
|
return;
|
||||||
}
|
}
|
||||||
else
|
|
||||||
|
buffer=BufferUtil.allocate((length > 0)?(int)length:1024);
|
||||||
|
|
||||||
|
String contentType = headers.get(HttpHeader.CONTENT_TYPE);
|
||||||
|
if (contentType != null)
|
||||||
{
|
{
|
||||||
String contentType = headers.get(HttpHeader.CONTENT_TYPE);
|
String charset = "charset=";
|
||||||
if (contentType != null)
|
int index = contentType.toLowerCase(Locale.ENGLISH).indexOf(charset);
|
||||||
|
if (index > 0)
|
||||||
{
|
{
|
||||||
String charset = "charset=";
|
String encoding = contentType.substring(index + charset.length());
|
||||||
int index = contentType.toLowerCase(Locale.ENGLISH).indexOf(charset);
|
// Sometimes charsets arrive with an ending semicolon
|
||||||
|
index = encoding.indexOf(';');
|
||||||
if (index > 0)
|
if (index > 0)
|
||||||
{
|
encoding = encoding.substring(0, index);
|
||||||
String encoding = contentType.substring(index + charset.length());
|
this.encoding = encoding;
|
||||||
// Sometimes charsets arrive with an ending semicolon
|
|
||||||
index = encoding.indexOf(';');
|
|
||||||
if (index > 0)
|
|
||||||
encoding = encoding.substring(0, index);
|
|
||||||
this.encoding = encoding;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void onContent(Response response, ByteBuffer content)
|
public void onContent(Response response, ByteBuffer content)
|
||||||
{
|
{
|
||||||
long newLength = buffer.length + content.remaining();
|
int length = content.remaining();
|
||||||
if (newLength > maxLength)
|
if (length>BufferUtil.space(buffer))
|
||||||
{
|
{
|
||||||
response.abort(new IllegalArgumentException("Buffering capacity exceeded"));
|
int requiredCapacity = buffer.capacity()+length;
|
||||||
}
|
if (requiredCapacity>maxLength)
|
||||||
else
|
response.abort(new IllegalArgumentException("Buffering capacity exceeded"));
|
||||||
{
|
|
||||||
byte[] newBuffer = new byte[(int)newLength];
|
int newCapacity = Math.min(Integer.highestOneBit(requiredCapacity) << 1, maxLength);
|
||||||
System.arraycopy(buffer, 0, newBuffer, 0, buffer.length);
|
buffer = BufferUtil.ensureCapacity(buffer,newCapacity);
|
||||||
content.get(newBuffer, buffer.length, content.remaining());
|
|
||||||
buffer = newBuffer;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
BufferUtil.append(buffer, content);
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
|
@ -121,7 +129,9 @@ public abstract class BufferingResponseListener extends Listener.Adapter
|
||||||
*/
|
*/
|
||||||
public byte[] getContent()
|
public byte[] getContent()
|
||||||
{
|
{
|
||||||
return buffer;
|
if (buffer==null)
|
||||||
|
return new byte[0];
|
||||||
|
return BufferUtil.toArray(buffer);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -144,14 +154,9 @@ public abstract class BufferingResponseListener extends Listener.Adapter
|
||||||
*/
|
*/
|
||||||
public String getContentAsString(String encoding)
|
public String getContentAsString(String encoding)
|
||||||
{
|
{
|
||||||
try
|
if (buffer==null)
|
||||||
{
|
return null;
|
||||||
return new String(getContent(), encoding);
|
return BufferUtil.toString(buffer, Charset.forName(encoding));
|
||||||
}
|
|
||||||
catch (UnsupportedEncodingException x)
|
|
||||||
{
|
|
||||||
throw new UnsupportedCharsetException(encoding);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -161,6 +166,19 @@ public abstract class BufferingResponseListener extends Listener.Adapter
|
||||||
*/
|
*/
|
||||||
public String getContentAsString(Charset encoding)
|
public String getContentAsString(Charset encoding)
|
||||||
{
|
{
|
||||||
return new String(getContent(), encoding);
|
if (buffer==null)
|
||||||
|
return null;
|
||||||
|
return BufferUtil.toString(buffer, encoding);
|
||||||
|
}
|
||||||
|
|
||||||
|
/* ------------------------------------------------------------ */
|
||||||
|
/**
|
||||||
|
* @return Content as InputStream
|
||||||
|
*/
|
||||||
|
public InputStream getContentAsInputStream()
|
||||||
|
{
|
||||||
|
if (buffer==null)
|
||||||
|
return new ByteArrayInputStream(new byte[]{});
|
||||||
|
return new ByteArrayInputStream(buffer.array(), buffer.arrayOffset(), buffer.remaining());
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -37,12 +37,14 @@ import java.util.List;
|
||||||
import java.util.NoSuchElementException;
|
import java.util.NoSuchElementException;
|
||||||
import java.util.Random;
|
import java.util.Random;
|
||||||
import java.util.concurrent.CountDownLatch;
|
import java.util.concurrent.CountDownLatch;
|
||||||
|
import java.util.concurrent.Exchanger;
|
||||||
import java.util.concurrent.ExecutionException;
|
import java.util.concurrent.ExecutionException;
|
||||||
import java.util.concurrent.TimeUnit;
|
import java.util.concurrent.TimeUnit;
|
||||||
import java.util.concurrent.TimeoutException;
|
import java.util.concurrent.TimeoutException;
|
||||||
import java.util.concurrent.atomic.AtomicInteger;
|
import java.util.concurrent.atomic.AtomicInteger;
|
||||||
import java.util.concurrent.atomic.AtomicLong;
|
import java.util.concurrent.atomic.AtomicLong;
|
||||||
import java.util.zip.GZIPOutputStream;
|
import java.util.zip.GZIPOutputStream;
|
||||||
|
|
||||||
import javax.servlet.ServletException;
|
import javax.servlet.ServletException;
|
||||||
import javax.servlet.ServletOutputStream;
|
import javax.servlet.ServletOutputStream;
|
||||||
import javax.servlet.http.HttpServletRequest;
|
import javax.servlet.http.HttpServletRequest;
|
||||||
|
@ -1038,26 +1040,33 @@ public class HttpClientTest extends AbstractHttpClientServerTest
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
final AtomicInteger complete = new AtomicInteger();
|
final Exchanger<Response> ex = new Exchanger<Response>();
|
||||||
BufferingResponseListener listener = new BufferingResponseListener()
|
BufferingResponseListener listener = new BufferingResponseListener()
|
||||||
{
|
{
|
||||||
@Override
|
@Override
|
||||||
public void onComplete(Result result)
|
public void onComplete(Result result)
|
||||||
{
|
{
|
||||||
complete.incrementAndGet();
|
try
|
||||||
|
{
|
||||||
|
ex.exchange(result.getResponse());
|
||||||
|
}
|
||||||
|
catch (InterruptedException e)
|
||||||
|
{
|
||||||
|
e.printStackTrace();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
ContentResponse response = client.newRequest("localhost", connector.getLocalPort())
|
|
||||||
|
client.newRequest("localhost", connector.getLocalPort())
|
||||||
.scheme(scheme)
|
.scheme(scheme)
|
||||||
.onResponseContent(listener)
|
.send(listener);
|
||||||
.onComplete(listener)
|
|
||||||
.send();
|
Response response = ex.exchange(null);
|
||||||
|
|
||||||
Assert.assertEquals(200, response.getStatus());
|
Assert.assertEquals(200, response.getStatus());
|
||||||
Assert.assertEquals(1, complete.get());
|
|
||||||
Assert.assertArrayEquals(content, listener.getContent());
|
Assert.assertArrayEquals(content, listener.getContent());
|
||||||
Assert.assertArrayEquals(content, response.getContent());
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
|
|
|
@ -22,6 +22,7 @@ import java.io.IOException;
|
||||||
import java.nio.BufferOverflowException;
|
import java.nio.BufferOverflowException;
|
||||||
import java.nio.ByteBuffer;
|
import java.nio.ByteBuffer;
|
||||||
import java.nio.charset.StandardCharsets;
|
import java.nio.charset.StandardCharsets;
|
||||||
|
import java.util.Arrays;
|
||||||
|
|
||||||
import org.eclipse.jetty.http.HttpTokens.EndOfContent;
|
import org.eclipse.jetty.http.HttpTokens.EndOfContent;
|
||||||
import org.eclipse.jetty.util.BufferUtil;
|
import org.eclipse.jetty.util.BufferUtil;
|
||||||
|
@ -916,10 +917,8 @@ public class HttpGenerator
|
||||||
line[versionLength+6+reason.length()]=HttpTokens.LINE_FEED;
|
line[versionLength+6+reason.length()]=HttpTokens.LINE_FEED;
|
||||||
|
|
||||||
__preprepared[i] = new PreparedResponse();
|
__preprepared[i] = new PreparedResponse();
|
||||||
__preprepared[i]._reason=new byte[line.length-versionLength-7] ;
|
__preprepared[i]._schemeCode = Arrays.copyOfRange(line, 0,versionLength+5);
|
||||||
System.arraycopy(line,versionLength+5,__preprepared[i]._reason,0,line.length-versionLength-7);
|
__preprepared[i]._reason = Arrays.copyOfRange(line, versionLength+5, line.length-2);
|
||||||
__preprepared[i]._schemeCode=new byte[versionLength+5];
|
|
||||||
System.arraycopy(line,0,__preprepared[i]._schemeCode,0,versionLength+5);
|
|
||||||
__preprepared[i]._responseLine=line;
|
__preprepared[i]._responseLine=line;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -1091,8 +1090,7 @@ public class HttpGenerator
|
||||||
{
|
{
|
||||||
super(header,value);
|
super(header,value);
|
||||||
int cbl=header.getBytesColonSpace().length;
|
int cbl=header.getBytesColonSpace().length;
|
||||||
_bytes=new byte[cbl+value.length()+2];
|
_bytes=Arrays.copyOf(header.getBytesColonSpace(), cbl+value.length()+2);
|
||||||
System.arraycopy(header.getBytesColonSpace(),0,_bytes,0,cbl);
|
|
||||||
System.arraycopy(value.getBytes(StandardCharsets.ISO_8859_1),0,_bytes,cbl,value.length());
|
System.arraycopy(value.getBytes(StandardCharsets.ISO_8859_1),0,_bytes,cbl,value.length());
|
||||||
_bytes[_bytes.length-2]=(byte)'\r';
|
_bytes[_bytes.length-2]=(byte)'\r';
|
||||||
_bytes[_bytes.length-1]=(byte)'\n';
|
_bytes[_bytes.length-1]=(byte)'\n';
|
||||||
|
|
|
@ -22,6 +22,7 @@ import java.io.IOException;
|
||||||
import java.nio.ByteBuffer;
|
import java.nio.ByteBuffer;
|
||||||
import java.nio.channels.ClosedChannelException;
|
import java.nio.channels.ClosedChannelException;
|
||||||
import java.nio.channels.WritePendingException;
|
import java.nio.channels.WritePendingException;
|
||||||
|
import java.util.Arrays;
|
||||||
import java.util.EnumMap;
|
import java.util.EnumMap;
|
||||||
import java.util.EnumSet;
|
import java.util.EnumSet;
|
||||||
import java.util.Set;
|
import java.util.Set;
|
||||||
|
@ -301,10 +302,7 @@ abstract public class WriteFlusher
|
||||||
if (consumed == length)
|
if (consumed == length)
|
||||||
return EMPTY_BUFFERS;
|
return EMPTY_BUFFERS;
|
||||||
|
|
||||||
int newLength = length - consumed;
|
return Arrays.copyOfRange(buffers,consumed,length);
|
||||||
ByteBuffer[] result = new ByteBuffer[newLength];
|
|
||||||
System.arraycopy(buffers, consumed, result, 0, newLength);
|
|
||||||
return result;
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -1214,20 +1214,17 @@ public class ContextHandler extends ScopedHandler implements Attributes, Gracefu
|
||||||
_protectedTargets = null;
|
_protectedTargets = null;
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
_protectedTargets = new String[targets.length];
|
_protectedTargets = Arrays.copyOf(targets, targets.length);
|
||||||
System.arraycopy(targets, 0, _protectedTargets, 0, targets.length);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/* ------------------------------------------------------------ */
|
/* ------------------------------------------------------------ */
|
||||||
public String[] getProtectedTargets ()
|
public String[] getProtectedTargets()
|
||||||
{
|
{
|
||||||
if (_protectedTargets == null)
|
if (_protectedTargets == null)
|
||||||
return null;
|
return null;
|
||||||
|
|
||||||
String[] tmp = new String[_protectedTargets.length];
|
return Arrays.copyOf(_protectedTargets, _protectedTargets.length);
|
||||||
System.arraycopy(_protectedTargets, 0, tmp, 0, _protectedTargets.length);
|
|
||||||
return tmp;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
|
@ -22,6 +22,7 @@ import java.io.ByteArrayOutputStream;
|
||||||
import java.nio.ByteBuffer;
|
import java.nio.ByteBuffer;
|
||||||
import java.nio.charset.Charset;
|
import java.nio.charset.Charset;
|
||||||
import java.nio.charset.StandardCharsets;
|
import java.nio.charset.StandardCharsets;
|
||||||
|
import java.util.Arrays;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
import java.util.Locale;
|
import java.util.Locale;
|
||||||
|
|
||||||
|
@ -62,8 +63,7 @@ public class HeadersBlockGenerator
|
||||||
for (int i = 1; i < values.size(); ++i)
|
for (int i = 1; i < values.size(); ++i)
|
||||||
{
|
{
|
||||||
byte[] moreValueBytes = values.get(i).getBytes(iso1);
|
byte[] moreValueBytes = values.get(i).getBytes(iso1);
|
||||||
byte[] newValueBytes = new byte[valueBytes.length + 1 + moreValueBytes.length];
|
byte[] newValueBytes = Arrays.copyOf(valueBytes,valueBytes.length + 1 + moreValueBytes.length);
|
||||||
System.arraycopy(valueBytes, 0, newValueBytes, 0, valueBytes.length);
|
|
||||||
newValueBytes[valueBytes.length] = 0;
|
newValueBytes[valueBytes.length] = 0;
|
||||||
System.arraycopy(moreValueBytes, 0, newValueBytes, valueBytes.length + 1, moreValueBytes.length);
|
System.arraycopy(moreValueBytes, 0, newValueBytes, valueBytes.length + 1, moreValueBytes.length);
|
||||||
valueBytes = newValueBytes;
|
valueBytes = newValueBytes;
|
||||||
|
|
|
@ -21,6 +21,7 @@ package org.eclipse.jetty.spdy.parser;
|
||||||
import java.nio.ByteBuffer;
|
import java.nio.ByteBuffer;
|
||||||
import java.nio.charset.Charset;
|
import java.nio.charset.Charset;
|
||||||
import java.nio.charset.StandardCharsets;
|
import java.nio.charset.StandardCharsets;
|
||||||
|
import java.util.Arrays;
|
||||||
import java.util.zip.ZipException;
|
import java.util.zip.ZipException;
|
||||||
|
|
||||||
import org.eclipse.jetty.spdy.CompressionDictionary;
|
import org.eclipse.jetty.spdy.CompressionDictionary;
|
||||||
|
@ -114,16 +115,14 @@ public abstract class HeadersBlockParser
|
||||||
int needed = length - accumulated;
|
int needed = length - accumulated;
|
||||||
if (remaining < needed)
|
if (remaining < needed)
|
||||||
{
|
{
|
||||||
byte[] local = new byte[accumulated + remaining];
|
byte[] local = Arrays.copyOf(data,accumulated + remaining);
|
||||||
System.arraycopy(data, 0, local, 0, accumulated);
|
|
||||||
buffer.get(local, accumulated, remaining);
|
buffer.get(local, accumulated, remaining);
|
||||||
data = local;
|
data = local;
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
byte[] local = new byte[length];
|
byte[] local = Arrays.copyOf(data,length);
|
||||||
System.arraycopy(data, 0, local, 0, accumulated);
|
|
||||||
buffer.get(local, accumulated, needed);
|
buffer.get(local, accumulated, needed);
|
||||||
data = local;
|
data = local;
|
||||||
return true;
|
return true;
|
||||||
|
@ -199,8 +198,7 @@ public abstract class HeadersBlockParser
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
// Last pass needed to decompress, merge decompressed bytes
|
// Last pass needed to decompress, merge decompressed bytes
|
||||||
byte[] result = new byte[decompressed.length + count];
|
byte[] result = Arrays.copyOf(decompressed,decompressed.length+count);
|
||||||
System.arraycopy(decompressed, 0, result, 0, decompressed.length);
|
|
||||||
System.arraycopy(buffer, 0, result, decompressed.length, count);
|
System.arraycopy(buffer, 0, result, decompressed.length, count);
|
||||||
return ByteBuffer.wrap(result);
|
return ByteBuffer.wrap(result);
|
||||||
}
|
}
|
||||||
|
@ -214,8 +212,7 @@ public abstract class HeadersBlockParser
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
byte[] result = new byte[decompressed.length + buffer.length];
|
byte[] result = Arrays.copyOf(decompressed,decompressed.length+buffer.length);
|
||||||
System.arraycopy(decompressed, 0, result, 0, decompressed.length);
|
|
||||||
System.arraycopy(buffer, 0, result, decompressed.length, buffer.length);
|
System.arraycopy(buffer, 0, result, decompressed.length, buffer.length);
|
||||||
decompressed = result;
|
decompressed = result;
|
||||||
}
|
}
|
||||||
|
|
|
@ -19,6 +19,7 @@
|
||||||
package org.eclipse.jetty.util;
|
package org.eclipse.jetty.util;
|
||||||
|
|
||||||
import java.nio.ByteBuffer;
|
import java.nio.ByteBuffer;
|
||||||
|
import java.util.Arrays;
|
||||||
import java.util.HashSet;
|
import java.util.HashSet;
|
||||||
import java.util.Set;
|
import java.util.Set;
|
||||||
|
|
||||||
|
@ -107,11 +108,12 @@ public class ArrayTernaryTrie<V> extends AbstractTrie<V>
|
||||||
*/
|
*/
|
||||||
public ArrayTernaryTrie(ArrayTernaryTrie<V> trie, double factor)
|
public ArrayTernaryTrie(ArrayTernaryTrie<V> trie, double factor)
|
||||||
{
|
{
|
||||||
this(trie.isCaseInsensitive(),(int)(trie._value.length*factor));
|
super(trie.isCaseInsensitive());
|
||||||
|
int capacity=(int)(trie._value.length*factor);
|
||||||
_rows=trie._rows;
|
_rows=trie._rows;
|
||||||
System.arraycopy(trie._value,0,_value,0,trie._value.length);
|
_value=Arrays.copyOf(trie._value, capacity);
|
||||||
System.arraycopy(trie._tree,0,_tree,0,trie._tree.length);
|
_tree=Arrays.copyOf(trie._tree, capacity*ROW_SIZE);
|
||||||
System.arraycopy(trie._key,0,_key,0,trie._key.length);
|
_key=Arrays.copyOf(trie._key, capacity);
|
||||||
}
|
}
|
||||||
|
|
||||||
/* ------------------------------------------------------------ */
|
/* ------------------------------------------------------------ */
|
||||||
|
|
|
@ -30,6 +30,7 @@ import java.nio.channels.FileChannel;
|
||||||
import java.nio.channels.FileChannel.MapMode;
|
import java.nio.channels.FileChannel.MapMode;
|
||||||
import java.nio.charset.Charset;
|
import java.nio.charset.Charset;
|
||||||
import java.nio.charset.StandardCharsets;
|
import java.nio.charset.StandardCharsets;
|
||||||
|
import java.util.Arrays;
|
||||||
|
|
||||||
import org.eclipse.jetty.util.resource.Resource;
|
import org.eclipse.jetty.util.resource.Resource;
|
||||||
|
|
||||||
|
@ -215,15 +216,18 @@ public class BufferUtil
|
||||||
*/
|
*/
|
||||||
public static byte[] toArray(ByteBuffer buffer)
|
public static byte[] toArray(ByteBuffer buffer)
|
||||||
{
|
{
|
||||||
byte[] to = new byte[buffer.remaining()];
|
|
||||||
if (buffer.hasArray())
|
if (buffer.hasArray())
|
||||||
{
|
{
|
||||||
byte[] array = buffer.array();
|
byte[] array = buffer.array();
|
||||||
System.arraycopy(array, buffer.arrayOffset() + buffer.position(), to, 0, to.length);
|
int from=buffer.arrayOffset() + buffer.position();
|
||||||
|
return Arrays.copyOfRange(array,from,from+buffer.remaining());
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
|
{
|
||||||
|
byte[] to = new byte[buffer.remaining()];
|
||||||
buffer.slice().get(to);
|
buffer.slice().get(to);
|
||||||
return to;
|
return to;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/* ------------------------------------------------------------ */
|
/* ------------------------------------------------------------ */
|
||||||
|
@ -1022,6 +1026,20 @@ public class BufferUtil
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public static ByteBuffer ensureCapacity(ByteBuffer buffer, int capacity)
|
||||||
|
{
|
||||||
|
if (buffer==null)
|
||||||
|
return allocate(capacity);
|
||||||
|
|
||||||
|
if (buffer.capacity()>=capacity)
|
||||||
|
return buffer;
|
||||||
|
|
||||||
|
if (buffer.hasArray())
|
||||||
|
return ByteBuffer.wrap(Arrays.copyOfRange(buffer.array(), buffer.arrayOffset(), buffer.arrayOffset()+capacity),buffer.position(),buffer.remaining());
|
||||||
|
|
||||||
|
throw new UnsupportedOperationException();
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -22,6 +22,7 @@ import java.io.OutputStream;
|
||||||
import java.io.OutputStreamWriter;
|
import java.io.OutputStreamWriter;
|
||||||
import java.io.Writer;
|
import java.io.Writer;
|
||||||
import java.nio.charset.StandardCharsets;
|
import java.nio.charset.StandardCharsets;
|
||||||
|
import java.util.Arrays;
|
||||||
|
|
||||||
|
|
||||||
/* ------------------------------------------------------------ */
|
/* ------------------------------------------------------------ */
|
||||||
|
@ -253,19 +254,14 @@ public class ByteArrayISO8859Writer extends Writer
|
||||||
{
|
{
|
||||||
if (_fixed)
|
if (_fixed)
|
||||||
throw new IOException("Buffer overflow: "+_buf.length);
|
throw new IOException("Buffer overflow: "+_buf.length);
|
||||||
byte[] buf = new byte[(_buf.length+n)*4/3];
|
_buf=Arrays.copyOf(_buf,(_buf.length+n)*4/3);
|
||||||
System.arraycopy(_buf,0,buf,0,_size);
|
|
||||||
_buf=buf;
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
/* ------------------------------------------------------------ */
|
/* ------------------------------------------------------------ */
|
||||||
public byte[] getByteArray()
|
public byte[] getByteArray()
|
||||||
{
|
{
|
||||||
byte[] data=new byte[_size];
|
return Arrays.copyOf(_buf,_size);
|
||||||
System.arraycopy(_buf,0,data,0,_size);
|
|
||||||
return data;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -289,6 +289,41 @@ public class BufferUtilTest
|
||||||
int capacity = BufferUtil.TEMP_BUFFER_SIZE*2+1024;
|
int capacity = BufferUtil.TEMP_BUFFER_SIZE*2+1024;
|
||||||
testWriteToWithBufferThatDoesNotExposeArray(capacity);
|
testWriteToWithBufferThatDoesNotExposeArray(capacity);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void testEnsureCapacity() throws Exception
|
||||||
|
{
|
||||||
|
ByteBuffer b = BufferUtil.toBuffer("Goodbye Cruel World");
|
||||||
|
assertTrue(b==BufferUtil.ensureCapacity(b, 0));
|
||||||
|
assertTrue(b==BufferUtil.ensureCapacity(b, 10));
|
||||||
|
assertTrue(b==BufferUtil.ensureCapacity(b, b.capacity()));
|
||||||
|
|
||||||
|
|
||||||
|
ByteBuffer b1 = BufferUtil.ensureCapacity(b, 64);
|
||||||
|
assertTrue(b!=b1);
|
||||||
|
assertEquals(64, b1.capacity());
|
||||||
|
assertEquals("Goodbye Cruel World", BufferUtil.toString(b1));
|
||||||
|
|
||||||
|
b1.position(8);
|
||||||
|
b1.limit(13);
|
||||||
|
assertEquals("Cruel", BufferUtil.toString(b1));
|
||||||
|
ByteBuffer b2 = b1.slice();
|
||||||
|
assertEquals("Cruel", BufferUtil.toString(b2));
|
||||||
|
System.err.println(BufferUtil.toDetailString(b2));
|
||||||
|
assertEquals(8, b2.arrayOffset());
|
||||||
|
assertEquals(5, b2.capacity());
|
||||||
|
|
||||||
|
assertTrue(b2==BufferUtil.ensureCapacity(b2, 5));
|
||||||
|
|
||||||
|
ByteBuffer b3 = BufferUtil.ensureCapacity(b2, 64);
|
||||||
|
assertTrue(b2!=b3);
|
||||||
|
assertEquals(64, b3.capacity());
|
||||||
|
assertEquals("Cruel", BufferUtil.toString(b3));
|
||||||
|
assertEquals(0, b3.arrayOffset());
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
private void testWriteToWithBufferThatDoesNotExposeArray(int capacity) throws IOException
|
private void testWriteToWithBufferThatDoesNotExposeArray(int capacity) throws IOException
|
||||||
{
|
{
|
||||||
|
|
|
@ -18,6 +18,8 @@
|
||||||
|
|
||||||
package org.eclipse.jetty.websocket.client.masks;
|
package org.eclipse.jetty.websocket.client.masks;
|
||||||
|
|
||||||
|
import java.util.Arrays;
|
||||||
|
|
||||||
import org.eclipse.jetty.websocket.common.WebSocketFrame;
|
import org.eclipse.jetty.websocket.common.WebSocketFrame;
|
||||||
|
|
||||||
public class FixedMasker implements Masker
|
public class FixedMasker implements Masker
|
||||||
|
@ -32,10 +34,9 @@ public class FixedMasker implements Masker
|
||||||
|
|
||||||
public FixedMasker(byte[] mask)
|
public FixedMasker(byte[] mask)
|
||||||
{
|
{
|
||||||
this.mask = new byte[4];
|
|
||||||
// Copy to avoid that external code keeps a reference
|
// Copy to avoid that external code keeps a reference
|
||||||
// to the array parameter to modify masking on-the-fly
|
// to the array parameter to modify masking on-the-fly
|
||||||
System.arraycopy(mask,0,mask,0,4);
|
this.mask=Arrays.copyOf(mask, 4);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
|
|
|
@ -154,10 +154,7 @@ public abstract class WebSocketFrame implements Frame
|
||||||
masked = copy.masked;
|
masked = copy.masked;
|
||||||
mask = null;
|
mask = null;
|
||||||
if (copy.mask != null)
|
if (copy.mask != null)
|
||||||
{
|
mask = Arrays.copyOf(copy.mask, copy.mask.length);
|
||||||
mask = new byte[copy.mask.length];
|
|
||||||
System.arraycopy(copy.mask,0,mask,0,mask.length);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
|
|
|
@ -20,6 +20,7 @@ package org.eclipse.jetty.websocket.server.ab;
|
||||||
|
|
||||||
import java.net.URI;
|
import java.net.URI;
|
||||||
import java.nio.ByteBuffer;
|
import java.nio.ByteBuffer;
|
||||||
|
import java.util.Arrays;
|
||||||
|
|
||||||
import org.eclipse.jetty.io.MappedByteBufferPool;
|
import org.eclipse.jetty.io.MappedByteBufferPool;
|
||||||
import org.eclipse.jetty.util.BufferUtil;
|
import org.eclipse.jetty.util.BufferUtil;
|
||||||
|
@ -113,9 +114,7 @@ public abstract class AbstractABCase implements Fuzzed
|
||||||
*/
|
*/
|
||||||
protected ByteBuffer copyOf(byte[] payload)
|
protected ByteBuffer copyOf(byte[] payload)
|
||||||
{
|
{
|
||||||
byte copy[] = new byte[payload.length];
|
return ByteBuffer.wrap(Arrays.copyOf(payload,payload.length));
|
||||||
System.arraycopy(payload,0,copy,0,payload.length);
|
|
||||||
return ByteBuffer.wrap(copy);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|
Loading…
Reference in New Issue