Skip to content

Commit 0770d86

Browse files
committed
Deprecate StreamUtils.emptyInput()
Closes gh-29125
1 parent d4a74c8 commit 0770d86

File tree

14 files changed

+29
-34
lines changed

14 files changed

+29
-34
lines changed

spring-context/src/test/java/org/springframework/scripting/support/ResourceScriptSourceTests.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,12 +17,12 @@
1717
package org.springframework.scripting.support;
1818

1919
import java.io.IOException;
20+
import java.io.InputStream;
2021

2122
import org.junit.jupiter.api.Test;
2223

2324
import org.springframework.core.io.ByteArrayResource;
2425
import org.springframework.core.io.Resource;
25-
import org.springframework.util.StreamUtils;
2626

2727
import static org.assertj.core.api.Assertions.assertThat;
2828
import static org.mockito.BDDMockito.given;
@@ -60,7 +60,7 @@ public void lastModifiedWorksWithResourceThatDoesNotSupportFileBasedReading() th
6060
// does not support File-based reading; delegates to InputStream-style reading...
6161
//resource.getFile();
6262
//mock.setThrowable(new FileNotFoundException());
63-
given(resource.getInputStream()).willReturn(StreamUtils.emptyInput());
63+
given(resource.getInputStream()).willReturn(InputStream.nullInputStream());
6464

6565
ResourceScriptSource scriptSource = new ResourceScriptSource(resource);
6666
assertThat(scriptSource.isModified()).as("ResourceScriptSource must start off in the 'isModified' state (it obviously isn't).").isTrue();

spring-core/src/main/java/org/springframework/util/StreamUtils.java

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2020 the original author or authors.
2+
* Copyright 2002-2022 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -216,11 +216,14 @@ public static int drain(InputStream in) throws IOException {
216216
Assert.notNull(in, "No InputStream specified");
217217
return (int) in.transferTo(OutputStream.nullOutputStream());
218218
}
219+
219220
/**
220221
* Return an efficient empty {@link InputStream}.
221222
* @return an InputStream which contains no bytes
222223
* @since 4.2.2
224+
* @deprecated as of 6.0 in favor of {@link InputStream#nullInputStream()}
223225
*/
226+
@Deprecated
224227
public static InputStream emptyInput() {
225228
return InputStream.nullInputStream();
226229
}

spring-core/src/main/java/org/springframework/util/xml/StaxUtils.java

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

1717
package org.springframework.util.xml;
1818

19+
import java.io.InputStream;
1920
import java.util.List;
2021
import java.util.function.Supplier;
2122

@@ -37,7 +38,6 @@
3738
import org.xml.sax.XMLReader;
3839

3940
import org.springframework.lang.Nullable;
40-
import org.springframework.util.StreamUtils;
4141

4242
/**
4343
* Convenience methods for working with the StAX API. Partly historic due to JAXP 1.3
@@ -54,7 +54,7 @@
5454
public abstract class StaxUtils {
5555

5656
private static final XMLResolver NO_OP_XML_RESOLVER =
57-
(publicID, systemID, base, ns) -> StreamUtils.emptyInput();
57+
(publicID, systemID, base, ns) -> InputStream.nullInputStream();
5858

5959

6060
/**

spring-jdbc/src/main/java/org/springframework/jdbc/support/lob/PassThroughBlob.java

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,6 @@
2323
import java.sql.SQLException;
2424

2525
import org.springframework.lang.Nullable;
26-
import org.springframework.util.StreamUtils;
2726

2827
/**
2928
* Simple JDBC {@link Blob} adapter that exposes a given byte array or binary stream.
@@ -65,7 +64,7 @@ public InputStream getBinaryStream() throws SQLException {
6564
return new ByteArrayInputStream(this.content);
6665
}
6766
else {
68-
return (this.binaryStream != null ? this.binaryStream : StreamUtils.emptyInput());
67+
return (this.binaryStream != null ? this.binaryStream : InputStream.nullInputStream());
6968
}
7069
}
7170

spring-jdbc/src/main/java/org/springframework/jdbc/support/lob/PassThroughClob.java

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,6 @@
3030

3131
import org.springframework.lang.Nullable;
3232
import org.springframework.util.FileCopyUtils;
33-
import org.springframework.util.StreamUtils;
3433

3534
/**
3635
* Simple JDBC {@link Clob} adapter that exposes a given String or character stream.
@@ -84,7 +83,7 @@ else if (this.characterStream != null) {
8483
}
8584
else {
8685
return new InputStreamReader(
87-
(this.asciiStream != null ? this.asciiStream : StreamUtils.emptyInput()),
86+
(this.asciiStream != null ? this.asciiStream : InputStream.nullInputStream()),
8887
StandardCharsets.US_ASCII);
8988
}
9089
}
@@ -100,7 +99,7 @@ else if (this.characterStream != null) {
10099
return new ByteArrayInputStream(tempContent.getBytes(StandardCharsets.US_ASCII));
101100
}
102101
else {
103-
return (this.asciiStream != null ? this.asciiStream : StreamUtils.emptyInput());
102+
return (this.asciiStream != null ? this.asciiStream : InputStream.nullInputStream());
104103
}
105104
}
106105
catch (IOException ex) {

spring-test/src/main/java/org/springframework/mock/web/MockHttpServletRequest.java

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,6 @@
6969
import org.springframework.util.LinkedMultiValueMap;
7070
import org.springframework.util.MultiValueMap;
7171
import org.springframework.util.ObjectUtils;
72-
import org.springframework.util.StreamUtils;
7372
import org.springframework.util.StringUtils;
7473
import org.springframework.web.util.UrlPathHelper;
7574

@@ -102,7 +101,7 @@ public class MockHttpServletRequest implements HttpServletRequest {
102101
private static final TimeZone GMT = TimeZone.getTimeZone("GMT");
103102

104103
private static final ServletInputStream EMPTY_SERVLET_INPUT_STREAM =
105-
new DelegatingServletInputStream(StreamUtils.emptyInput());
104+
new DelegatingServletInputStream(InputStream.nullInputStream());
106105

107106
private static final BufferedReader EMPTY_BUFFERED_READER =
108107
new BufferedReader(new StringReader(""));

spring-test/src/main/java/org/springframework/test/web/servlet/request/MockHttpServletRequestBuilder.java

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,6 @@
5151
import org.springframework.util.LinkedMultiValueMap;
5252
import org.springframework.util.MultiValueMap;
5353
import org.springframework.util.ObjectUtils;
54-
import org.springframework.util.StreamUtils;
5554
import org.springframework.util.StringUtils;
5655
import org.springframework.web.context.WebApplicationContext;
5756
import org.springframework.web.context.support.WebApplicationContextUtils;
@@ -804,7 +803,7 @@ private MultiValueMap<String, String> parseFormData(MediaType mediaType) {
804803
HttpInputMessage message = new HttpInputMessage() {
805804
@Override
806805
public InputStream getBody() {
807-
return (content != null ? new ByteArrayInputStream(content) : StreamUtils.emptyInput());
806+
return (content != null ? new ByteArrayInputStream(content) : InputStream.nullInputStream());
808807
}
809808
@Override
810809
public HttpHeaders getHeaders() {

spring-web/src/main/java/org/springframework/http/client/HttpComponentsClientHttpResponse.java

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,6 @@
2828
import org.springframework.http.HttpHeaders;
2929
import org.springframework.http.HttpStatusCode;
3030
import org.springframework.lang.Nullable;
31-
import org.springframework.util.StreamUtils;
3231

3332
/**
3433
* {@link ClientHttpResponse} implementation based on
@@ -84,7 +83,7 @@ public HttpHeaders getHeaders() {
8483
@Override
8584
public InputStream getBody() throws IOException {
8685
HttpEntity entity = this.httpResponse.getEntity();
87-
return (entity != null ? entity.getContent() : StreamUtils.emptyInput());
86+
return (entity != null ? entity.getContent() : InputStream.nullInputStream());
8887
}
8988

9089
@Override

spring-web/src/main/java/org/springframework/http/client/OkHttp3ClientHttpResponse.java

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,6 @@
2626
import org.springframework.http.HttpStatusCode;
2727
import org.springframework.lang.Nullable;
2828
import org.springframework.util.Assert;
29-
import org.springframework.util.StreamUtils;
3029

3130
/**
3231
* {@link ClientHttpResponse} implementation based on OkHttp 3.x.
@@ -69,7 +68,7 @@ public String getStatusText() {
6968
@Override
7069
public InputStream getBody() throws IOException {
7170
ResponseBody body = this.response.body();
72-
return (body != null ? body.byteStream() : StreamUtils.emptyInput());
71+
return (body != null ? body.byteStream() : InputStream.nullInputStream());
7372
}
7473

7574
@Override

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,7 @@ public class SourceHttpMessageConverter<T extends Source> extends AbstractHttpMe
7373
(publicId, systemId) -> new InputSource(new StringReader(""));
7474

7575
private static final XMLResolver NO_OP_XML_RESOLVER =
76-
(publicID, systemID, base, ns) -> StreamUtils.emptyInput();
76+
(publicID, systemID, base, ns) -> InputStream.nullInputStream();
7777

7878
private static final Set<Class<?>> SUPPORTED_CLASSES = new HashSet<>(8);
7979

spring-web/src/test/java/org/springframework/web/client/RestTemplateTests.java

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818

1919
import java.io.ByteArrayInputStream;
2020
import java.io.IOException;
21+
import java.io.InputStream;
2122
import java.net.URI;
2223
import java.util.Arrays;
2324
import java.util.Collections;
@@ -52,7 +53,6 @@
5253
import org.springframework.http.converter.HttpMessageConverter;
5354
import org.springframework.http.converter.json.KotlinSerializationJsonHttpMessageConverter;
5455
import org.springframework.http.converter.json.MappingJackson2HttpMessageConverter;
55-
import org.springframework.util.StreamUtils;
5656
import org.springframework.web.util.DefaultUriBuilderFactory;
5757

5858
import static org.assertj.core.api.Assertions.assertThat;
@@ -284,7 +284,7 @@ void getForObjectWithCustomUriTemplateHandler() throws Exception {
284284
mockSentRequest(GET, "https://example.com/hotels/1/pic/pics%2Flogo.png/size/150x150");
285285
mockResponseStatus(HttpStatus.OK);
286286
given(response.getHeaders()).willReturn(new HttpHeaders());
287-
given(response.getBody()).willReturn(StreamUtils.emptyInput());
287+
given(response.getBody()).willReturn(InputStream.nullInputStream());
288288

289289
Map<String, String> uriVariables = new HashMap<>(2);
290290
uriVariables.put("hotel", "1");
@@ -440,7 +440,7 @@ void postForObjectNull() throws Exception {
440440
responseHeaders.setContentType(MediaType.TEXT_PLAIN);
441441
responseHeaders.setContentLength(10);
442442
given(response.getHeaders()).willReturn(responseHeaders);
443-
given(response.getBody()).willReturn(StreamUtils.emptyInput());
443+
given(response.getBody()).willReturn(InputStream.nullInputStream());
444444
given(converter.read(String.class, response)).willReturn(null);
445445

446446
String result = template.postForObject("https://example.com", null, String.class);
@@ -460,7 +460,7 @@ void postForEntityNull() throws Exception {
460460
responseHeaders.setContentType(MediaType.TEXT_PLAIN);
461461
responseHeaders.setContentLength(10);
462462
given(response.getHeaders()).willReturn(responseHeaders);
463-
given(response.getBody()).willReturn(StreamUtils.emptyInput());
463+
given(response.getBody()).willReturn(InputStream.nullInputStream());
464464
given(converter.read(String.class, response)).willReturn(null);
465465

466466
ResponseEntity<String> result = template.postForEntity("https://example.com", null, String.class);
@@ -556,7 +556,7 @@ void patchForObjectNull() throws Exception {
556556
responseHeaders.setContentType(MediaType.TEXT_PLAIN);
557557
responseHeaders.setContentLength(10);
558558
given(response.getHeaders()).willReturn(responseHeaders);
559-
given(response.getBody()).willReturn(StreamUtils.emptyInput());
559+
given(response.getBody()).willReturn(InputStream.nullInputStream());
560560

561561
String result = template.patchForObject("https://example.com", null, String.class);
562562
assertThat(result).as("Invalid POST result").isNull();

spring-web/src/test/java/org/springframework/web/filter/ShallowEtagHeaderFilterTests.java

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

1717
package org.springframework.web.filter;
1818

19+
import java.io.InputStream;
1920
import java.nio.charset.StandardCharsets;
2021

2122
import jakarta.servlet.FilterChain;
2223
import jakarta.servlet.http.HttpServletResponse;
2324
import org.junit.jupiter.api.Test;
2425

2526
import org.springframework.util.FileCopyUtils;
26-
import org.springframework.util.StreamUtils;
2727
import org.springframework.web.testfixture.servlet.MockHttpServletRequest;
2828
import org.springframework.web.testfixture.servlet.MockHttpServletResponse;
2929

@@ -45,18 +45,18 @@ public void isEligibleForEtag() {
4545
MockHttpServletRequest request = new MockHttpServletRequest("GET", "/hotels");
4646
MockHttpServletResponse response = new MockHttpServletResponse();
4747

48-
assertThat(filter.isEligibleForEtag(request, response, 200, StreamUtils.emptyInput())).isTrue();
49-
assertThat(filter.isEligibleForEtag(request, response, 300, StreamUtils.emptyInput())).isFalse();
48+
assertThat(filter.isEligibleForEtag(request, response, 200, InputStream.nullInputStream())).isTrue();
49+
assertThat(filter.isEligibleForEtag(request, response, 300, InputStream.nullInputStream())).isFalse();
5050

5151
request = new MockHttpServletRequest("HEAD", "/hotels");
52-
assertThat(filter.isEligibleForEtag(request, response, 200, StreamUtils.emptyInput())).isFalse();
52+
assertThat(filter.isEligibleForEtag(request, response, 200, InputStream.nullInputStream())).isFalse();
5353

5454
request = new MockHttpServletRequest("POST", "/hotels");
55-
assertThat(filter.isEligibleForEtag(request, response, 200, StreamUtils.emptyInput())).isFalse();
55+
assertThat(filter.isEligibleForEtag(request, response, 200, InputStream.nullInputStream())).isFalse();
5656

5757
request = new MockHttpServletRequest("POST", "/hotels");
5858
request.addHeader("Cache-Control","must-revalidate, no-store");
59-
assertThat(filter.isEligibleForEtag(request, response, 200, StreamUtils.emptyInput())).isFalse();
59+
assertThat(filter.isEligibleForEtag(request, response, 200, InputStream.nullInputStream())).isFalse();
6060
}
6161

6262
@Test

spring-web/src/testFixtures/java/org/springframework/web/testfixture/servlet/MockHttpServletRequest.java

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,6 @@
6969
import org.springframework.util.LinkedMultiValueMap;
7070
import org.springframework.util.MultiValueMap;
7171
import org.springframework.util.ObjectUtils;
72-
import org.springframework.util.StreamUtils;
7372
import org.springframework.util.StringUtils;
7473
import org.springframework.web.util.UrlPathHelper;
7574

@@ -102,7 +101,7 @@ public class MockHttpServletRequest implements HttpServletRequest {
102101
private static final TimeZone GMT = TimeZone.getTimeZone("GMT");
103102

104103
private static final ServletInputStream EMPTY_SERVLET_INPUT_STREAM =
105-
new DelegatingServletInputStream(StreamUtils.emptyInput());
104+
new DelegatingServletInputStream(InputStream.nullInputStream());
106105

107106
private static final BufferedReader EMPTY_BUFFERED_READER =
108107
new BufferedReader(new StringReader(""));

spring-webmvc/src/main/java/org/springframework/web/servlet/mvc/method/annotation/AbstractMessageConverterMethodArgumentResolver.java

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,6 @@
4848
import org.springframework.lang.Nullable;
4949
import org.springframework.util.Assert;
5050
import org.springframework.util.MimeTypeUtils;
51-
import org.springframework.util.StreamUtils;
5251
import org.springframework.validation.Errors;
5352
import org.springframework.validation.annotation.ValidationAnnotationUtils;
5453
import org.springframework.web.HttpMediaTypeNotSupportedException;
@@ -346,7 +345,7 @@ public HttpHeaders getHeaders() {
346345

347346
@Override
348347
public InputStream getBody() {
349-
return (this.body != null ? this.body : StreamUtils.emptyInput());
348+
return (this.body != null ? this.body : InputStream.nullInputStream());
350349
}
351350

352351
public boolean hasBody() {

0 commit comments

Comments
 (0)