diff --git a/spring-core/src/main/java/org/springframework/core/convert/TypeDescriptor.java b/spring-core/src/main/java/org/springframework/core/convert/TypeDescriptor.java index 8e2ca54b02b..7aa600dd788 100644 --- a/spring-core/src/main/java/org/springframework/core/convert/TypeDescriptor.java +++ b/spring-core/src/main/java/org/springframework/core/convert/TypeDescriptor.java @@ -110,9 +110,10 @@ public class TypeDescriptor implements Serializable { } /** - * Create a new type descriptor from a {@link ResolvableType}. This protected - * constructor is used internally and may also be used by subclasses that support - * non-Java languages with extended type systems. + * Create a new type descriptor from a {@link ResolvableType}. + *

This constructor is used internally and may also be used by subclasses + * that support non-Java languages with extended type systems. It is public + * as of 5.1.4 whereas it was protected before. * @param resolvableType the resolvable type * @param type the backing type (or {@code null} if it should get resolved) * @param annotations the type annotations diff --git a/spring-core/src/main/java/org/springframework/core/io/buffer/DataBuffer.java b/spring-core/src/main/java/org/springframework/core/io/buffer/DataBuffer.java index 9041099449e..c4efe9ef54a 100644 --- a/spring-core/src/main/java/org/springframework/core/io/buffer/DataBuffer.java +++ b/spring-core/src/main/java/org/springframework/core/io/buffer/DataBuffer.java @@ -1,5 +1,5 @@ /* - * Copyright 2002-2018 the original author or authors. + * Copyright 2002-2019 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -122,7 +122,9 @@ public interface DataBuffer { * @return this buffer * @since 5.1.4 */ - DataBuffer ensureCapacity(int capacity); + default DataBuffer ensureCapacity(int capacity) { + return this; + } /** * Return the position from which this buffer will read. @@ -242,8 +244,8 @@ public interface DataBuffer { * @since 5.1.4 */ default DataBuffer write(CharSequence charSequence, Charset charset) { - Assert.notNull(charSequence, "'charSequence' must not be null"); - Assert.notNull(charset, "'charset' must not be null"); + Assert.notNull(charSequence, "CharSequence must not be null"); + Assert.notNull(charset, "Charset must not be null"); CharsetEncoder charsetEncoder = charset.newEncoder() .onMalformedInput(CodingErrorAction.REPLACE) .onUnmappableCharacter(CodingErrorAction.REPLACE); @@ -251,9 +253,9 @@ public interface DataBuffer { int estimatedSize = (int) (inBuffer.remaining() * charsetEncoder.averageBytesPerChar()); ByteBuffer outBuffer = ensureCapacity(estimatedSize) .asByteBuffer(writePosition(), writableByteCount()); - for (; ; ) { - CoderResult cr = inBuffer.hasRemaining() ? - charsetEncoder.encode(inBuffer, outBuffer, true) : CoderResult.UNDERFLOW; + while (true) { + CoderResult cr = (inBuffer.hasRemaining() ? + charsetEncoder.encode(inBuffer, outBuffer, true) : CoderResult.UNDERFLOW); if (cr.isUnderflow()) { cr = charsetEncoder.flush(outBuffer); } diff --git a/spring-core/src/main/java/org/springframework/core/io/buffer/DefaultDataBuffer.java b/spring-core/src/main/java/org/springframework/core/io/buffer/DefaultDataBuffer.java index ced1890961a..8cc662e2266 100644 --- a/spring-core/src/main/java/org/springframework/core/io/buffer/DefaultDataBuffer.java +++ b/spring-core/src/main/java/org/springframework/core/io/buffer/DefaultDataBuffer.java @@ -1,5 +1,5 @@ /* - * Copyright 2002-2018 the original author or authors. + * Copyright 2002-2019 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -37,6 +37,7 @@ import org.springframework.util.ObjectUtils; * * @author Arjen Poutsma * @author Juergen Hoeller + * @author Brian Clozel * @since 5.0 * @see DefaultDataBufferFactory */ @@ -99,8 +100,7 @@ public class DefaultDataBuffer implements DataBuffer { @Override public int indexOf(IntPredicate predicate, int fromIndex) { - Assert.notNull(predicate, "'predicate' must not be null"); - + Assert.notNull(predicate, "IntPredicate must not be null"); if (fromIndex < 0) { fromIndex = 0; } @@ -118,7 +118,7 @@ public class DefaultDataBuffer implements DataBuffer { @Override public int lastIndexOf(IntPredicate predicate, int fromIndex) { - Assert.notNull(predicate, "'predicate' must not be null"); + Assert.notNull(predicate, "IntPredicate must not be null"); int i = Math.min(fromIndex, this.writePosition - 1); for (; i >= 0; i--) { byte b = this.byteBuffer.get(i); @@ -149,7 +149,6 @@ public class DefaultDataBuffer implements DataBuffer { assertIndex(readPosition >= 0, "'readPosition' %d must be >= 0", readPosition); assertIndex(readPosition <= this.writePosition, "'readPosition' %d must be <= %d", readPosition, this.writePosition); - this.readPosition = readPosition; return this; } @@ -165,7 +164,6 @@ public class DefaultDataBuffer implements DataBuffer { writePosition, this.readPosition); assertIndex(writePosition <= this.capacity, "'writePosition' %d must be <= %d", writePosition, this.capacity); - this.writePosition = writePosition; return this; } @@ -177,9 +175,9 @@ public class DefaultDataBuffer implements DataBuffer { @Override public DefaultDataBuffer capacity(int newCapacity) { - Assert.isTrue(newCapacity > 0, - String.format("'newCapacity' %d must be higher than 0", newCapacity)); - + if (newCapacity <= 0) { + throw new IllegalArgumentException(String.format("'newCapacity' %d must be higher than 0", newCapacity)); + } int readPosition = readPosition(); int writePosition = writePosition(); int oldCapacity = capacity(); @@ -225,15 +223,13 @@ public class DefaultDataBuffer implements DataBuffer { } private static ByteBuffer allocate(int capacity, boolean direct) { - return direct ? ByteBuffer.allocateDirect(capacity) : ByteBuffer.allocate(capacity); + return (direct ? ByteBuffer.allocateDirect(capacity) : ByteBuffer.allocate(capacity)); } @Override public byte getByte(int index) { assertIndex(index >= 0, "index %d must be >= 0", index); - assertIndex(index <= this.writePosition - 1, "index %d must be <= %d", - index, this.writePosition - 1); - + assertIndex(index <= this.writePosition - 1, "index %d must be <= %d", index, this.writePosition - 1); return this.byteBuffer.get(index); } @@ -249,14 +245,14 @@ public class DefaultDataBuffer implements DataBuffer { @Override public DefaultDataBuffer read(byte[] destination) { - Assert.notNull(destination, "'destination' must not be null"); + Assert.notNull(destination, "Byte array must not be null"); read(destination, 0, destination.length); return this; } @Override public DefaultDataBuffer read(byte[] destination, int offset, int length) { - Assert.notNull(destination, "'destination' must not be null"); + Assert.notNull(destination, "Byte array must not be null"); assertIndex(this.readPosition <= this.writePosition - length, "readPosition %d and length %d should be smaller than writePosition %d", this.readPosition, length, this.writePosition); @@ -281,14 +277,14 @@ public class DefaultDataBuffer implements DataBuffer { @Override public DefaultDataBuffer write(byte[] source) { - Assert.notNull(source, "'source' must not be null"); + Assert.notNull(source, "Byte array must not be null"); write(source, 0, source.length); return this; } @Override public DefaultDataBuffer write(byte[] source, int offset, int length) { - Assert.notNull(source, "'source' must not be null"); + Assert.notNull(source, "Byte array must not be null"); ensureCapacity(length); ByteBuffer tmp = this.byteBuffer.duplicate(); @@ -309,11 +305,12 @@ public class DefaultDataBuffer implements DataBuffer { } @Override - public DefaultDataBuffer write(ByteBuffer... byteBuffers) { - Assert.notEmpty(byteBuffers, "'byteBuffers' must not be empty"); - int capacity = Arrays.stream(byteBuffers).mapToInt(ByteBuffer::remaining).sum(); - ensureCapacity(capacity); - Arrays.stream(byteBuffers).forEach(this::write); + public DefaultDataBuffer write(ByteBuffer... buffers) { + if (!ObjectUtils.isEmpty(buffers)) { + int capacity = Arrays.stream(buffers).mapToInt(ByteBuffer::remaining).sum(); + ensureCapacity(capacity); + Arrays.stream(buffers).forEach(this::write); + } return this; } @@ -442,7 +439,7 @@ public class DefaultDataBuffer implements DataBuffer { assertIndex(length <= this.capacity, "length %d must be <= %d", index, this.capacity); } - private static void assertIndex(boolean expression, String format, Object... args) { + private void assertIndex(boolean expression, String format, Object... args) { if (!expression) { String message = String.format(format, args); throw new IndexOutOfBoundsException(message); diff --git a/spring-core/src/main/java/org/springframework/core/io/buffer/NettyDataBuffer.java b/spring-core/src/main/java/org/springframework/core/io/buffer/NettyDataBuffer.java index 455d9d3afc4..9f84cd98df1 100644 --- a/spring-core/src/main/java/org/springframework/core/io/buffer/NettyDataBuffer.java +++ b/spring-core/src/main/java/org/springframework/core/io/buffer/NettyDataBuffer.java @@ -1,5 +1,5 @@ /* - * Copyright 2002-2018 the original author or authors. + * Copyright 2002-2019 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -36,6 +36,7 @@ import org.springframework.util.ObjectUtils; * {@link ByteBuf}. Typically constructed with {@link NettyDataBufferFactory}. * * @author Arjen Poutsma + * @author Brian Clozel * @since 5.0 */ public class NettyDataBuffer implements PooledDataBuffer { @@ -240,8 +241,8 @@ public class NettyDataBuffer implements PooledDataBuffer { @Override public DataBuffer write(CharSequence charSequence, Charset charset) { - Assert.notNull(charSequence, "'charSequence' must not be null"); - Assert.notNull(charset, "'charset' must not be null"); + Assert.notNull(charSequence, "CharSequence must not be null"); + Assert.notNull(charset, "Charset must not be null"); if (StandardCharsets.UTF_8.equals(charset)) { ByteBufUtil.writeUtf8(this.byteBuf, charSequence); } diff --git a/spring-core/src/test/java/org/springframework/core/io/buffer/LeakAwareDataBuffer.java b/spring-core/src/test/java/org/springframework/core/io/buffer/LeakAwareDataBuffer.java index ee8fdd2a62b..b07a742fda9 100644 --- a/spring-core/src/test/java/org/springframework/core/io/buffer/LeakAwareDataBuffer.java +++ b/spring-core/src/test/java/org/springframework/core/io/buffer/LeakAwareDataBuffer.java @@ -1,5 +1,5 @@ /* - * Copyright 2002-2018 the original author or authors. + * Copyright 2002-2019 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -186,8 +186,8 @@ class LeakAwareDataBuffer implements PooledDataBuffer { } @Override - public DataBuffer write(ByteBuffer... byteBuffers) { - return this.delegate.write(byteBuffers); + public DataBuffer write(ByteBuffer... buffers) { + return this.delegate.write(buffers); } @Override diff --git a/spring-web/src/main/java/org/springframework/http/server/reactive/UndertowServerHttpRequest.java b/spring-web/src/main/java/org/springframework/http/server/reactive/UndertowServerHttpRequest.java index 49fbedf0a4d..86836930711 100644 --- a/spring-web/src/main/java/org/springframework/http/server/reactive/UndertowServerHttpRequest.java +++ b/spring-web/src/main/java/org/springframework/http/server/reactive/UndertowServerHttpRequest.java @@ -1,5 +1,5 @@ /* - * Copyright 2002-2018 the original author or authors. + * Copyright 2002-2019 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -140,7 +140,6 @@ class UndertowServerHttpRequest extends AbstractServerHttpRequest { private final ByteBufferPool byteBufferPool; - public RequestBodyPublisher(HttpServerExchange exchange, DataBufferFactory bufferFactory) { super(UndertowServerHttpRequest.this.getLogPrefix()); this.channel = exchange.getRequestChannel(); @@ -207,6 +206,7 @@ class UndertowServerHttpRequest extends AbstractServerHttpRequest { } } + private static class UndertowDataBuffer implements PooledDataBuffer { private final DataBuffer dataBuffer; @@ -316,8 +316,7 @@ class UndertowServerHttpRequest extends AbstractServerHttpRequest { } @Override - public DataBuffer read(byte[] destination, int offset, - int length) { + public DataBuffer read(byte[] destination, int offset, int length) { return this.dataBuffer.read(destination, offset, length); } @@ -332,20 +331,17 @@ class UndertowServerHttpRequest extends AbstractServerHttpRequest { } @Override - public DataBuffer write(byte[] source, int offset, - int length) { + public DataBuffer write(byte[] source, int offset, int length) { return this.dataBuffer.write(source, offset, length); } @Override - public DataBuffer write( - DataBuffer... buffers) { + public DataBuffer write(DataBuffer... buffers) { return this.dataBuffer.write(buffers); } @Override - public DataBuffer write( - ByteBuffer... byteBuffers) { + public DataBuffer write(ByteBuffer... byteBuffers) { return this.dataBuffer.write(byteBuffers); } @@ -384,4 +380,5 @@ class UndertowServerHttpRequest extends AbstractServerHttpRequest { return this.dataBuffer.asOutputStream(); } } + }