Browse Source

Merge branch '6.2.x'

pull/33430/head
Brian Clozel 7 months ago
parent
commit
7c82a62bfb
  1. 38
      spring-core/src/main/java/org/springframework/core/io/buffer/DataBufferInputStream.java
  2. 43
      spring-core/src/test/java/org/springframework/core/io/buffer/DataBufferTests.java
  3. 1
      spring-web/src/main/java/org/springframework/http/converter/xml/AbstractJaxb2HttpMessageConverter.java
  4. 7
      spring-web/src/main/java/org/springframework/http/converter/xml/Jaxb2RootElementHttpMessageConverter.java

38
spring-core/src/main/java/org/springframework/core/io/buffer/DataBufferInputStream.java

@ -18,6 +18,8 @@ package org.springframework.core.io.buffer; @@ -18,6 +18,8 @@ package org.springframework.core.io.buffer;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.Objects;
import org.springframework.util.Assert;
@ -103,10 +105,44 @@ final class DataBufferInputStream extends InputStream { @@ -103,10 +105,44 @@ final class DataBufferInputStream extends InputStream {
this.closed = true;
}
@Override
public byte[] readNBytes(int len) throws IOException {
if (len < 0) {
throw new IllegalArgumentException("len < 0");
}
checkClosed();
int size = Math.min(available(), len);
byte[] out = new byte[size];
this.dataBuffer.read(out);
return out;
}
@Override
public long skip(long n) throws IOException {
checkClosed();
if (n <= 0) {
return 0L;
}
int skipped = Math.min(available(), n > Integer.MAX_VALUE ? Integer.MAX_VALUE : (int) n);
this.dataBuffer.readPosition(Math.min(this.end, this.dataBuffer.readPosition() + skipped));
return skipped;
}
@Override
public long transferTo(OutputStream out) throws IOException {
Objects.requireNonNull(out, "out");
checkClosed();
if (available() == 0) {
return 0L;
}
byte[] buf = readAllBytes();
out.write(buf);
return buf.length;
}
private void checkClosed() throws IOException {
if (this.closed) {
throw new IOException("DataBufferInputStream is closed");
}
}
}

43
spring-core/src/test/java/org/springframework/core/io/buffer/DataBufferTests.java

@ -16,6 +16,7 @@ @@ -16,6 +16,7 @@
package org.springframework.core.io.buffer;
import java.io.ByteArrayOutputStream;
import java.io.InputStream;
import java.io.OutputStream;
import java.nio.ByteBuffer;
@ -341,6 +342,48 @@ class DataBufferTests extends AbstractDataBufferAllocatingTests { @@ -341,6 +342,48 @@ class DataBufferTests extends AbstractDataBufferAllocatingTests {
assertThat(len).isEqualTo(3);
assertThat(bytes).containsExactly('c', 'd', 'e');
buffer.readPosition(0);
inputStream = buffer.asInputStream();
assertThat(inputStream.readAllBytes()).asString().isEqualTo("abcde");
assertThat(inputStream.available()).isEqualTo(0);
assertThat(inputStream.readAllBytes()).isEmpty();
buffer.readPosition(0);
inputStream = buffer.asInputStream();
inputStream.mark(5);
assertThat(inputStream.readNBytes(0)).isEmpty();
assertThat(inputStream.readNBytes(1000)).asString().isEqualTo("abcde");
inputStream.reset();
assertThat(inputStream.readNBytes(3)).asString().isEqualTo("abc");
assertThat(inputStream.readNBytes(2)).asString().isEqualTo("de");
assertThat(inputStream.readNBytes(10)).isEmpty();
buffer.readPosition(0);
inputStream = buffer.asInputStream();
inputStream.mark(5);
assertThat(inputStream.skip(1)).isEqualTo(1);
assertThat(inputStream.readAllBytes()).asString().isEqualTo("bcde");
assertThat(inputStream.skip(10)).isEqualTo(0);
assertThat(inputStream.available()).isEqualTo(0);
inputStream.reset();
assertThat(inputStream.skip(100)).isEqualTo(5);
assertThat(inputStream.available()).isEqualTo(0);
buffer.readPosition(0);
inputStream = buffer.asInputStream();
inputStream.mark(5);
ByteArrayOutputStream out = new ByteArrayOutputStream();
assertThat(inputStream.transferTo(out)).isEqualTo(5);
assertThat(out.toByteArray()).asString().isEqualTo("abcde");
assertThat(inputStream.available()).isEqualTo(0);
out.reset();
inputStream.reset();
assertThat(inputStream.read()).isEqualTo('a');
assertThat(inputStream.transferTo(out)).isEqualTo(4);
assertThat(out.toByteArray()).asString().isEqualTo("bcde");
assertThat(inputStream.available()).isEqualTo(0);
assertThat(inputStream.transferTo(OutputStream.nullOutputStream())).isEqualTo(0);
release(buffer);
}

1
spring-web/src/main/java/org/springframework/http/converter/xml/AbstractJaxb2HttpMessageConverter.java

@ -124,6 +124,7 @@ public abstract class AbstractJaxb2HttpMessageConverter<T> extends AbstractXmlHt @@ -124,6 +124,7 @@ public abstract class AbstractJaxb2HttpMessageConverter<T> extends AbstractXmlHt
* Detect the charset from the given {@link HttpHeaders#getContentType()}.
* @param httpHeaders the current HTTP headers
* @return the charset defined in the content type header, or {@code null} if not found
* @since 6.2.8
*/
protected @Nullable Charset detectCharset(HttpHeaders httpHeaders) {
MediaType contentType = httpHeaders.getContentType();

7
spring-web/src/main/java/org/springframework/http/converter/xml/Jaxb2RootElementHttpMessageConverter.java

@ -160,6 +160,13 @@ public class Jaxb2RootElementHttpMessageConverter extends AbstractJaxb2HttpMessa @@ -160,6 +160,13 @@ public class Jaxb2RootElementHttpMessageConverter extends AbstractJaxb2HttpMessa
}
}
/**
* Process {@code source} with {@code charset}.
* @param source source to process
* @param charset charset to use
* @return source
* @since 6.2.8
*/
protected Source processSource(Source source, @Nullable Charset charset) {
if (source instanceof StreamSource streamSource) {
InputSource inputSource = new InputSource(streamSource.getInputStream());

Loading…
Cancel
Save