Skip to content

Commit 7c82a62

Browse files
committed
Merge branch '6.2.x'
2 parents 941b791 + 182d654 commit 7c82a62

File tree

4 files changed

+88
-1
lines changed

4 files changed

+88
-1
lines changed

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

Lines changed: 37 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,8 @@
1818

1919
import java.io.IOException;
2020
import java.io.InputStream;
21+
import java.io.OutputStream;
22+
import java.util.Objects;
2123

2224
import org.springframework.util.Assert;
2325

@@ -103,10 +105,44 @@ public void close() {
103105
this.closed = true;
104106
}
105107

108+
@Override
109+
public byte[] readNBytes(int len) throws IOException {
110+
if (len < 0) {
111+
throw new IllegalArgumentException("len < 0");
112+
}
113+
checkClosed();
114+
int size = Math.min(available(), len);
115+
byte[] out = new byte[size];
116+
this.dataBuffer.read(out);
117+
return out;
118+
}
119+
120+
@Override
121+
public long skip(long n) throws IOException {
122+
checkClosed();
123+
if (n <= 0) {
124+
return 0L;
125+
}
126+
int skipped = Math.min(available(), n > Integer.MAX_VALUE ? Integer.MAX_VALUE : (int) n);
127+
this.dataBuffer.readPosition(Math.min(this.end, this.dataBuffer.readPosition() + skipped));
128+
return skipped;
129+
}
130+
131+
@Override
132+
public long transferTo(OutputStream out) throws IOException {
133+
Objects.requireNonNull(out, "out");
134+
checkClosed();
135+
if (available() == 0) {
136+
return 0L;
137+
}
138+
byte[] buf = readAllBytes();
139+
out.write(buf);
140+
return buf.length;
141+
}
142+
106143
private void checkClosed() throws IOException {
107144
if (this.closed) {
108145
throw new IOException("DataBufferInputStream is closed");
109146
}
110147
}
111-
112148
}

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

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616

1717
package org.springframework.core.io.buffer;
1818

19+
import java.io.ByteArrayOutputStream;
1920
import java.io.InputStream;
2021
import java.io.OutputStream;
2122
import java.nio.ByteBuffer;
@@ -341,6 +342,48 @@ void inputStream(DataBufferFactory bufferFactory) throws Exception {
341342
assertThat(len).isEqualTo(3);
342343
assertThat(bytes).containsExactly('c', 'd', 'e');
343344

345+
buffer.readPosition(0);
346+
inputStream = buffer.asInputStream();
347+
assertThat(inputStream.readAllBytes()).asString().isEqualTo("abcde");
348+
assertThat(inputStream.available()).isEqualTo(0);
349+
assertThat(inputStream.readAllBytes()).isEmpty();
350+
351+
buffer.readPosition(0);
352+
inputStream = buffer.asInputStream();
353+
inputStream.mark(5);
354+
assertThat(inputStream.readNBytes(0)).isEmpty();
355+
assertThat(inputStream.readNBytes(1000)).asString().isEqualTo("abcde");
356+
inputStream.reset();
357+
assertThat(inputStream.readNBytes(3)).asString().isEqualTo("abc");
358+
assertThat(inputStream.readNBytes(2)).asString().isEqualTo("de");
359+
assertThat(inputStream.readNBytes(10)).isEmpty();
360+
361+
buffer.readPosition(0);
362+
inputStream = buffer.asInputStream();
363+
inputStream.mark(5);
364+
assertThat(inputStream.skip(1)).isEqualTo(1);
365+
assertThat(inputStream.readAllBytes()).asString().isEqualTo("bcde");
366+
assertThat(inputStream.skip(10)).isEqualTo(0);
367+
assertThat(inputStream.available()).isEqualTo(0);
368+
inputStream.reset();
369+
assertThat(inputStream.skip(100)).isEqualTo(5);
370+
assertThat(inputStream.available()).isEqualTo(0);
371+
372+
buffer.readPosition(0);
373+
inputStream = buffer.asInputStream();
374+
inputStream.mark(5);
375+
ByteArrayOutputStream out = new ByteArrayOutputStream();
376+
assertThat(inputStream.transferTo(out)).isEqualTo(5);
377+
assertThat(out.toByteArray()).asString().isEqualTo("abcde");
378+
assertThat(inputStream.available()).isEqualTo(0);
379+
out.reset();
380+
inputStream.reset();
381+
assertThat(inputStream.read()).isEqualTo('a');
382+
assertThat(inputStream.transferTo(out)).isEqualTo(4);
383+
assertThat(out.toByteArray()).asString().isEqualTo("bcde");
384+
assertThat(inputStream.available()).isEqualTo(0);
385+
assertThat(inputStream.transferTo(OutputStream.nullOutputStream())).isEqualTo(0);
386+
344387
release(buffer);
345388
}
346389

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -124,6 +124,7 @@ protected final JAXBContext getJaxbContext(Class<?> clazz) {
124124
* Detect the charset from the given {@link HttpHeaders#getContentType()}.
125125
* @param httpHeaders the current HTTP headers
126126
* @return the charset defined in the content type header, or {@code null} if not found
127+
* @since 6.2.8
127128
*/
128129
protected @Nullable Charset detectCharset(HttpHeaders httpHeaders) {
129130
MediaType contentType = httpHeaders.getContentType();

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

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -160,6 +160,13 @@ protected Object readFromSource(Class<?> clazz, HttpHeaders headers, Source sour
160160
}
161161
}
162162

163+
/**
164+
* Process {@code source} with {@code charset}.
165+
* @param source source to process
166+
* @param charset charset to use
167+
* @return source
168+
* @since 6.2.8
169+
*/
163170
protected Source processSource(Source source, @Nullable Charset charset) {
164171
if (source instanceof StreamSource streamSource) {
165172
InputSource inputSource = new InputSource(streamSource.getInputStream());

0 commit comments

Comments
 (0)