Browse Source

Polishing

pull/18152/head
Juergen Hoeller 7 years ago
parent
commit
72dddfbc7b
  1. 7
      spring-core/src/main/java/org/springframework/core/convert/TypeDescriptor.java
  2. 16
      spring-core/src/main/java/org/springframework/core/io/buffer/DataBuffer.java
  3. 43
      spring-core/src/main/java/org/springframework/core/io/buffer/DefaultDataBuffer.java
  4. 7
      spring-core/src/main/java/org/springframework/core/io/buffer/NettyDataBuffer.java
  5. 6
      spring-core/src/test/java/org/springframework/core/io/buffer/LeakAwareDataBuffer.java
  6. 17
      spring-web/src/main/java/org/springframework/http/server/reactive/UndertowServerHttpRequest.java

7
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 * Create a new type descriptor from a {@link ResolvableType}.
* constructor is used internally and may also be used by subclasses that support * <p>This constructor is used internally and may also be used by subclasses
* non-Java languages with extended type systems. * 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 resolvableType the resolvable type
* @param type the backing type (or {@code null} if it should get resolved) * @param type the backing type (or {@code null} if it should get resolved)
* @param annotations the type annotations * @param annotations the type annotations

16
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"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. * you may not use this file except in compliance with the License.
@ -122,7 +122,9 @@ public interface DataBuffer {
* @return this buffer * @return this buffer
* @since 5.1.4 * @since 5.1.4
*/ */
DataBuffer ensureCapacity(int capacity); default DataBuffer ensureCapacity(int capacity) {
return this;
}
/** /**
* Return the position from which this buffer will read. * Return the position from which this buffer will read.
@ -242,8 +244,8 @@ public interface DataBuffer {
* @since 5.1.4 * @since 5.1.4
*/ */
default DataBuffer write(CharSequence charSequence, Charset charset) { default DataBuffer write(CharSequence charSequence, Charset charset) {
Assert.notNull(charSequence, "'charSequence' must not be null"); Assert.notNull(charSequence, "CharSequence must not be null");
Assert.notNull(charset, "'charset' must not be null"); Assert.notNull(charset, "Charset must not be null");
CharsetEncoder charsetEncoder = charset.newEncoder() CharsetEncoder charsetEncoder = charset.newEncoder()
.onMalformedInput(CodingErrorAction.REPLACE) .onMalformedInput(CodingErrorAction.REPLACE)
.onUnmappableCharacter(CodingErrorAction.REPLACE); .onUnmappableCharacter(CodingErrorAction.REPLACE);
@ -251,9 +253,9 @@ public interface DataBuffer {
int estimatedSize = (int) (inBuffer.remaining() * charsetEncoder.averageBytesPerChar()); int estimatedSize = (int) (inBuffer.remaining() * charsetEncoder.averageBytesPerChar());
ByteBuffer outBuffer = ensureCapacity(estimatedSize) ByteBuffer outBuffer = ensureCapacity(estimatedSize)
.asByteBuffer(writePosition(), writableByteCount()); .asByteBuffer(writePosition(), writableByteCount());
for (; ; ) { while (true) {
CoderResult cr = inBuffer.hasRemaining() ? CoderResult cr = (inBuffer.hasRemaining() ?
charsetEncoder.encode(inBuffer, outBuffer, true) : CoderResult.UNDERFLOW; charsetEncoder.encode(inBuffer, outBuffer, true) : CoderResult.UNDERFLOW);
if (cr.isUnderflow()) { if (cr.isUnderflow()) {
cr = charsetEncoder.flush(outBuffer); cr = charsetEncoder.flush(outBuffer);
} }

43
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"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with 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 Arjen Poutsma
* @author Juergen Hoeller * @author Juergen Hoeller
* @author Brian Clozel
* @since 5.0 * @since 5.0
* @see DefaultDataBufferFactory * @see DefaultDataBufferFactory
*/ */
@ -99,8 +100,7 @@ public class DefaultDataBuffer implements DataBuffer {
@Override @Override
public int indexOf(IntPredicate predicate, int fromIndex) { 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) { if (fromIndex < 0) {
fromIndex = 0; fromIndex = 0;
} }
@ -118,7 +118,7 @@ public class DefaultDataBuffer implements DataBuffer {
@Override @Override
public int lastIndexOf(IntPredicate predicate, int fromIndex) { 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); int i = Math.min(fromIndex, this.writePosition - 1);
for (; i >= 0; i--) { for (; i >= 0; i--) {
byte b = this.byteBuffer.get(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 >= 0, "'readPosition' %d must be >= 0", readPosition);
assertIndex(readPosition <= this.writePosition, "'readPosition' %d must be <= %d", assertIndex(readPosition <= this.writePosition, "'readPosition' %d must be <= %d",
readPosition, this.writePosition); readPosition, this.writePosition);
this.readPosition = readPosition; this.readPosition = readPosition;
return this; return this;
} }
@ -165,7 +164,6 @@ public class DefaultDataBuffer implements DataBuffer {
writePosition, this.readPosition); writePosition, this.readPosition);
assertIndex(writePosition <= this.capacity, "'writePosition' %d must be <= %d", assertIndex(writePosition <= this.capacity, "'writePosition' %d must be <= %d",
writePosition, this.capacity); writePosition, this.capacity);
this.writePosition = writePosition; this.writePosition = writePosition;
return this; return this;
} }
@ -177,9 +175,9 @@ public class DefaultDataBuffer implements DataBuffer {
@Override @Override
public DefaultDataBuffer capacity(int newCapacity) { public DefaultDataBuffer capacity(int newCapacity) {
Assert.isTrue(newCapacity > 0, if (newCapacity <= 0) {
String.format("'newCapacity' %d must be higher than 0", newCapacity)); throw new IllegalArgumentException(String.format("'newCapacity' %d must be higher than 0", newCapacity));
}
int readPosition = readPosition(); int readPosition = readPosition();
int writePosition = writePosition(); int writePosition = writePosition();
int oldCapacity = capacity(); int oldCapacity = capacity();
@ -225,15 +223,13 @@ public class DefaultDataBuffer implements DataBuffer {
} }
private static ByteBuffer allocate(int capacity, boolean direct) { 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 @Override
public byte getByte(int index) { public byte getByte(int index) {
assertIndex(index >= 0, "index %d must be >= 0", index); assertIndex(index >= 0, "index %d must be >= 0", index);
assertIndex(index <= this.writePosition - 1, "index %d must be <= %d", assertIndex(index <= this.writePosition - 1, "index %d must be <= %d", index, this.writePosition - 1);
index, this.writePosition - 1);
return this.byteBuffer.get(index); return this.byteBuffer.get(index);
} }
@ -249,14 +245,14 @@ public class DefaultDataBuffer implements DataBuffer {
@Override @Override
public DefaultDataBuffer read(byte[] destination) { 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); read(destination, 0, destination.length);
return this; return this;
} }
@Override @Override
public DefaultDataBuffer read(byte[] destination, int offset, int length) { 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, assertIndex(this.readPosition <= this.writePosition - length,
"readPosition %d and length %d should be smaller than writePosition %d", "readPosition %d and length %d should be smaller than writePosition %d",
this.readPosition, length, this.writePosition); this.readPosition, length, this.writePosition);
@ -281,14 +277,14 @@ public class DefaultDataBuffer implements DataBuffer {
@Override @Override
public DefaultDataBuffer write(byte[] source) { 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); write(source, 0, source.length);
return this; return this;
} }
@Override @Override
public DefaultDataBuffer write(byte[] source, int offset, int length) { 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); ensureCapacity(length);
ByteBuffer tmp = this.byteBuffer.duplicate(); ByteBuffer tmp = this.byteBuffer.duplicate();
@ -309,11 +305,12 @@ public class DefaultDataBuffer implements DataBuffer {
} }
@Override @Override
public DefaultDataBuffer write(ByteBuffer... byteBuffers) { public DefaultDataBuffer write(ByteBuffer... buffers) {
Assert.notEmpty(byteBuffers, "'byteBuffers' must not be empty"); if (!ObjectUtils.isEmpty(buffers)) {
int capacity = Arrays.stream(byteBuffers).mapToInt(ByteBuffer::remaining).sum(); int capacity = Arrays.stream(buffers).mapToInt(ByteBuffer::remaining).sum();
ensureCapacity(capacity); ensureCapacity(capacity);
Arrays.stream(byteBuffers).forEach(this::write); Arrays.stream(buffers).forEach(this::write);
}
return this; return this;
} }
@ -442,7 +439,7 @@ public class DefaultDataBuffer implements DataBuffer {
assertIndex(length <= this.capacity, "length %d must be <= %d", index, this.capacity); 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) { if (!expression) {
String message = String.format(format, args); String message = String.format(format, args);
throw new IndexOutOfBoundsException(message); throw new IndexOutOfBoundsException(message);

7
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"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with 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}. * {@link ByteBuf}. Typically constructed with {@link NettyDataBufferFactory}.
* *
* @author Arjen Poutsma * @author Arjen Poutsma
* @author Brian Clozel
* @since 5.0 * @since 5.0
*/ */
public class NettyDataBuffer implements PooledDataBuffer { public class NettyDataBuffer implements PooledDataBuffer {
@ -240,8 +241,8 @@ public class NettyDataBuffer implements PooledDataBuffer {
@Override @Override
public DataBuffer write(CharSequence charSequence, Charset charset) { public DataBuffer write(CharSequence charSequence, Charset charset) {
Assert.notNull(charSequence, "'charSequence' must not be null"); Assert.notNull(charSequence, "CharSequence must not be null");
Assert.notNull(charset, "'charset' must not be null"); Assert.notNull(charset, "Charset must not be null");
if (StandardCharsets.UTF_8.equals(charset)) { if (StandardCharsets.UTF_8.equals(charset)) {
ByteBufUtil.writeUtf8(this.byteBuf, charSequence); ByteBufUtil.writeUtf8(this.byteBuf, charSequence);
} }

6
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"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. * you may not use this file except in compliance with the License.
@ -186,8 +186,8 @@ class LeakAwareDataBuffer implements PooledDataBuffer {
} }
@Override @Override
public DataBuffer write(ByteBuffer... byteBuffers) { public DataBuffer write(ByteBuffer... buffers) {
return this.delegate.write(byteBuffers); return this.delegate.write(buffers);
} }
@Override @Override

17
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"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with 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; private final ByteBufferPool byteBufferPool;
public RequestBodyPublisher(HttpServerExchange exchange, DataBufferFactory bufferFactory) { public RequestBodyPublisher(HttpServerExchange exchange, DataBufferFactory bufferFactory) {
super(UndertowServerHttpRequest.this.getLogPrefix()); super(UndertowServerHttpRequest.this.getLogPrefix());
this.channel = exchange.getRequestChannel(); this.channel = exchange.getRequestChannel();
@ -207,6 +206,7 @@ class UndertowServerHttpRequest extends AbstractServerHttpRequest {
} }
} }
private static class UndertowDataBuffer implements PooledDataBuffer { private static class UndertowDataBuffer implements PooledDataBuffer {
private final DataBuffer dataBuffer; private final DataBuffer dataBuffer;
@ -316,8 +316,7 @@ class UndertowServerHttpRequest extends AbstractServerHttpRequest {
} }
@Override @Override
public DataBuffer read(byte[] destination, int offset, public DataBuffer read(byte[] destination, int offset, int length) {
int length) {
return this.dataBuffer.read(destination, offset, length); return this.dataBuffer.read(destination, offset, length);
} }
@ -332,20 +331,17 @@ class UndertowServerHttpRequest extends AbstractServerHttpRequest {
} }
@Override @Override
public DataBuffer write(byte[] source, int offset, public DataBuffer write(byte[] source, int offset, int length) {
int length) {
return this.dataBuffer.write(source, offset, length); return this.dataBuffer.write(source, offset, length);
} }
@Override @Override
public DataBuffer write( public DataBuffer write(DataBuffer... buffers) {
DataBuffer... buffers) {
return this.dataBuffer.write(buffers); return this.dataBuffer.write(buffers);
} }
@Override @Override
public DataBuffer write( public DataBuffer write(ByteBuffer... byteBuffers) {
ByteBuffer... byteBuffers) {
return this.dataBuffer.write(byteBuffers); return this.dataBuffer.write(byteBuffers);
} }
@ -384,4 +380,5 @@ class UndertowServerHttpRequest extends AbstractServerHttpRequest {
return this.dataBuffer.asOutputStream(); return this.dataBuffer.asOutputStream();
} }
} }
} }

Loading…
Cancel
Save