Polishing (forward-ported from 4.1.x)

This commit is contained in:
Juergen Hoeller 2015-02-10 23:04:12 +01:00
parent 098e60e125
commit f58abd3d9a
4 changed files with 41 additions and 43 deletions

View File

@ -22,11 +22,10 @@ import java.util.Random;
import java.util.UUID; import java.util.UUID;
/** /**
* An {@link org.springframework.util.IdGenerator IdGenerator} that uses * An {@link IdGenerator} that uses {@link SecureRandom} for the initial seed and
* {@link SecureRandom} for the initial seed and {@link Random} thereafter * {@link Random} thereafter, instead of calling {@link UUID#randomUUID()} every
* instead of calling {@link UUID#randomUUID()} every time as * time as {@link org.springframework.util.JdkIdGenerator JdkIdGenerator} does.
* {@link org.springframework.util.JdkIdGenerator JdkIdGenerator} does. * This provides a better balance between securely random ids and performance.
* This provides a better balance between securely random id's and performance.
* *
* @author Rossen Stoyanchev * @author Rossen Stoyanchev
* @author Rob Winch * @author Rob Winch

View File

@ -19,11 +19,10 @@ package org.springframework.util;
import java.util.UUID; import java.util.UUID;
/** /**
* An {@link org.springframework.util.IdGenerator IdGenerator} that calls * An {@link IdGenerator} that calls {@link java.util.UUID#randomUUID()}.
* {@link java.util.UUID#randomUUID()}.
* *
* @author Rossen Stoyanchev * @author Rossen Stoyanchev
* @since 4.2 * @since 4.1.5
*/ */
public class JdkIdGenerator implements IdGenerator { public class JdkIdGenerator implements IdGenerator {

View File

@ -20,11 +20,10 @@ import java.util.UUID;
import java.util.concurrent.atomic.AtomicLong; import java.util.concurrent.atomic.AtomicLong;
/** /**
* A simple {@link org.springframework.util.IdGenerator IdGenerator} that * A simple {@link IdGenerator} that starts at 1 and increments by 1 with each call.
* starts at 1 and increments by 1 with each call.
* *
* @author Rossen Stoyanchev * @author Rossen Stoyanchev
* @since 4.2 * @since 4.1.5
*/ */
public class SimpleIdGenerator implements IdGenerator { public class SimpleIdGenerator implements IdGenerator {

View File

@ -25,8 +25,9 @@ import org.springframework.http.HttpStatus;
import org.springframework.http.client.ClientHttpResponse; import org.springframework.http.client.ClientHttpResponse;
/** /**
* Implementation of {@link ClientHttpResponse} that can not only check if the response * Implementation of {@link ClientHttpResponse} that can not only check if
* has a message body, but also if its length is 0 (i.e. empty) by actually reading the input stream. * the response has a message body, but also if its length is 0 (i.e. empty)
* by actually reading the input stream.
* *
* @author Brian Clozel * @author Brian Clozel
* @since 4.1.5 * @since 4.1.5
@ -34,23 +35,23 @@ import org.springframework.http.client.ClientHttpResponse;
*/ */
class MessageBodyClientHttpResponseWrapper implements ClientHttpResponse { class MessageBodyClientHttpResponseWrapper implements ClientHttpResponse {
private final ClientHttpResponse response;
private PushbackInputStream pushbackInputStream; private PushbackInputStream pushbackInputStream;
private final ClientHttpResponse response;
public MessageBodyClientHttpResponseWrapper(ClientHttpResponse response) throws IOException { public MessageBodyClientHttpResponseWrapper(ClientHttpResponse response) throws IOException {
this.response = response; this.response = response;
} }
/** /**
* Indicates whether the response has a message body. * Indicates whether the response has a message body.
*
* <p>Implementation returns {@code false} for: * <p>Implementation returns {@code false} for:
* <ul> * <ul>
* <li>a response status of {@code 1XX}, {@code 204} or {@code 304}</li> * <li>a response status of {@code 1XX}, {@code 204} or {@code 304}</li>
* <li>a {@code Content-Length} header of {@code 0}</li> * <li>a {@code Content-Length} header of {@code 0}</li>
* </ul> * </ul>
*
* @return {@code true} if the response has a message body, {@code false} otherwise * @return {@code true} if the response has a message body, {@code false} otherwise
* @throws IOException in case of I/O errors * @throws IOException in case of I/O errors
*/ */
@ -68,13 +69,11 @@ class MessageBodyClientHttpResponseWrapper implements ClientHttpResponse {
/** /**
* Indicates whether the response has an empty message body. * Indicates whether the response has an empty message body.
*
* <p>Implementation tries to read the first bytes of the response stream: * <p>Implementation tries to read the first bytes of the response stream:
* <ul> * <ul>
* <li>if no bytes are available, the message body is empty</li> * <li>if no bytes are available, the message body is empty</li>
* <li>otherwise it is not empty and the stream is reset to its start for further reading</li> * <li>otherwise it is not empty and the stream is reset to its start for further reading</li>
* </ul> * </ul>
*
* @return {@code true} if the response has a zero-length message body, {@code false} otherwise * @return {@code true} if the response has a zero-length message body, {@code false} otherwise
* @throws IOException in case of I/O errors * @throws IOException in case of I/O errors
*/ */
@ -95,44 +94,46 @@ class MessageBodyClientHttpResponseWrapper implements ClientHttpResponse {
} }
else { else {
this.pushbackInputStream = new PushbackInputStream(body); this.pushbackInputStream = new PushbackInputStream(body);
int b = pushbackInputStream.read(); int b = this.pushbackInputStream.read();
if (b == -1) { if (b == -1) {
return true; return true;
} }
else { else {
pushbackInputStream.unread(b); this.pushbackInputStream.unread(b);
return false; return false;
} }
} }
} }
@Override
public HttpStatus getStatusCode() throws IOException {
return response.getStatusCode();
}
@Override @Override
public int getRawStatusCode() throws IOException { public HttpHeaders getHeaders() {
return response.getRawStatusCode(); return this.response.getHeaders();
}
@Override
public String getStatusText() throws IOException {
return response.getStatusText();
}
@Override
public void close() {
response.close();
} }
@Override @Override
public InputStream getBody() throws IOException { public InputStream getBody() throws IOException {
return this.pushbackInputStream != null ? this.pushbackInputStream : response.getBody(); return (this.pushbackInputStream != null ? this.pushbackInputStream : this.response.getBody());
} }
@Override @Override
public HttpHeaders getHeaders() { public HttpStatus getStatusCode() throws IOException {
return response.getHeaders(); return this.response.getStatusCode();
} }
@Override
public int getRawStatusCode() throws IOException {
return this.response.getRawStatusCode();
}
@Override
public String getStatusText() throws IOException {
return this.response.getStatusText();
}
@Override
public void close() {
this.response.close();
}
} }