|
1 | 1 | /*
|
2 |
| - * Copyright 2002-2016 the original author or authors. |
| 2 | + * Copyright 2002-2017 the original author or authors. |
3 | 3 | *
|
4 | 4 | * Licensed under the Apache License, Version 2.0 (the "License");
|
5 | 5 | * you may not use this file except in compliance with the License.
|
|
39 | 39 | */
|
40 | 40 | public class DefaultDataBuffer implements DataBuffer {
|
41 | 41 |
|
| 42 | + private static final int MAX_CAPACITY = Integer.MAX_VALUE; |
| 43 | + |
| 44 | + private static final int CAPACITY_THRESHOLD = 1024 * 1024 * 4; |
| 45 | + |
| 46 | + |
42 | 47 | private final DefaultDataBufferFactory dataBufferFactory;
|
43 | 48 |
|
44 | 49 | private ByteBuffer byteBuffer;
|
@@ -259,17 +264,45 @@ public OutputStream asOutputStream() {
|
259 | 264 | }
|
260 | 265 |
|
261 | 266 | private void ensureExtraCapacity(int extraCapacity) {
|
262 |
| - int neededCapacity = this.writePosition + extraCapacity; |
| 267 | + int neededCapacity = calculateCapacity(this.writePosition + extraCapacity); |
263 | 268 | if (neededCapacity > this.byteBuffer.capacity()) {
|
264 | 269 | grow(neededCapacity);
|
265 | 270 | }
|
266 | 271 | }
|
267 | 272 |
|
268 |
| - void grow(int minCapacity) { |
| 273 | + /** |
| 274 | + * @see io.netty.buffer.AbstractByteBufAllocator#calculateNewCapacity(int, int) |
| 275 | + */ |
| 276 | + private int calculateCapacity(int neededCapacity) { |
| 277 | + Assert.isTrue(neededCapacity >= 0, "'neededCapacity' must >= 0"); |
| 278 | + |
| 279 | + if (neededCapacity == CAPACITY_THRESHOLD) { |
| 280 | + return CAPACITY_THRESHOLD; |
| 281 | + } |
| 282 | + else if (neededCapacity > CAPACITY_THRESHOLD) { |
| 283 | + int newCapacity = neededCapacity / CAPACITY_THRESHOLD * CAPACITY_THRESHOLD; |
| 284 | + if (newCapacity > MAX_CAPACITY - CAPACITY_THRESHOLD) { |
| 285 | + newCapacity = MAX_CAPACITY; |
| 286 | + } |
| 287 | + else { |
| 288 | + newCapacity += CAPACITY_THRESHOLD; |
| 289 | + } |
| 290 | + return newCapacity; |
| 291 | + } |
| 292 | + else { |
| 293 | + int newCapacity = 64; |
| 294 | + while (newCapacity < neededCapacity) { |
| 295 | + newCapacity <<= 1; |
| 296 | + } |
| 297 | + return Math.min(newCapacity, MAX_CAPACITY); |
| 298 | + } |
| 299 | + } |
| 300 | + |
| 301 | + void grow(int capacity) { |
269 | 302 | ByteBuffer oldBuffer = this.byteBuffer;
|
270 | 303 | ByteBuffer newBuffer =
|
271 |
| - (oldBuffer.isDirect() ? ByteBuffer.allocateDirect(minCapacity) : |
272 |
| - ByteBuffer.allocate(minCapacity)); |
| 304 | + (oldBuffer.isDirect() ? ByteBuffer.allocateDirect(capacity) : |
| 305 | + ByteBuffer.allocate(capacity)); |
273 | 306 |
|
274 | 307 | // Explicit cast for compatibility with covariant return type on JDK 9's ByteBuffer
|
275 | 308 | final int remaining = readableByteCount();
|
@@ -362,7 +395,7 @@ private static class SlicedDefaultDataBuffer extends DefaultDataBuffer {
|
362 | 395 | }
|
363 | 396 |
|
364 | 397 | @Override
|
365 |
| - void grow(int minCapacity) { |
| 398 | + void grow(int capacity) { |
366 | 399 | throw new UnsupportedOperationException(
|
367 | 400 | "Growing the capacity of a sliced buffer is not supported");
|
368 | 401 | }
|
|
0 commit comments