Skip to content

Commit

Permalink
Deprecate StreamUtils.emptyInput()
Browse files Browse the repository at this point in the history
Closes gh-29125
  • Loading branch information
bclozel committed Sep 12, 2022
1 parent d4a74c8 commit 0770d86
Show file tree
Hide file tree
Showing 14 changed files with 29 additions and 34 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,12 @@
package org.springframework.scripting.support;

import java.io.IOException;
import java.io.InputStream;

import org.junit.jupiter.api.Test;

import org.springframework.core.io.ByteArrayResource;
import org.springframework.core.io.Resource;
import org.springframework.util.StreamUtils;

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

ResourceScriptSource scriptSource = new ResourceScriptSource(resource);
assertThat(scriptSource.isModified()).as("ResourceScriptSource must start off in the 'isModified' state (it obviously isn't).").isTrue();
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2002-2020 the original author or authors.
* Copyright 2002-2022 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand Down Expand Up @@ -216,11 +216,14 @@ public static int drain(InputStream in) throws IOException {
Assert.notNull(in, "No InputStream specified");
return (int) in.transferTo(OutputStream.nullOutputStream());
}

/**
* Return an efficient empty {@link InputStream}.
* @return an InputStream which contains no bytes
* @since 4.2.2
* @deprecated as of 6.0 in favor of {@link InputStream#nullInputStream()}
*/
@Deprecated
public static InputStream emptyInput() {
return InputStream.nullInputStream();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@

package org.springframework.util.xml;

import java.io.InputStream;
import java.util.List;
import java.util.function.Supplier;

Expand All @@ -37,7 +38,6 @@
import org.xml.sax.XMLReader;

import org.springframework.lang.Nullable;
import org.springframework.util.StreamUtils;

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

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


/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@
import java.sql.SQLException;

import org.springframework.lang.Nullable;
import org.springframework.util.StreamUtils;

/**
* Simple JDBC {@link Blob} adapter that exposes a given byte array or binary stream.
Expand Down Expand Up @@ -65,7 +64,7 @@ public InputStream getBinaryStream() throws SQLException {
return new ByteArrayInputStream(this.content);
}
else {
return (this.binaryStream != null ? this.binaryStream : StreamUtils.emptyInput());
return (this.binaryStream != null ? this.binaryStream : InputStream.nullInputStream());
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,6 @@

import org.springframework.lang.Nullable;
import org.springframework.util.FileCopyUtils;
import org.springframework.util.StreamUtils;

/**
* Simple JDBC {@link Clob} adapter that exposes a given String or character stream.
Expand Down Expand Up @@ -84,7 +83,7 @@ else if (this.characterStream != null) {
}
else {
return new InputStreamReader(
(this.asciiStream != null ? this.asciiStream : StreamUtils.emptyInput()),
(this.asciiStream != null ? this.asciiStream : InputStream.nullInputStream()),
StandardCharsets.US_ASCII);
}
}
Expand All @@ -100,7 +99,7 @@ else if (this.characterStream != null) {
return new ByteArrayInputStream(tempContent.getBytes(StandardCharsets.US_ASCII));
}
else {
return (this.asciiStream != null ? this.asciiStream : StreamUtils.emptyInput());
return (this.asciiStream != null ? this.asciiStream : InputStream.nullInputStream());
}
}
catch (IOException ex) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,6 @@
import org.springframework.util.LinkedMultiValueMap;
import org.springframework.util.MultiValueMap;
import org.springframework.util.ObjectUtils;
import org.springframework.util.StreamUtils;
import org.springframework.util.StringUtils;
import org.springframework.web.util.UrlPathHelper;

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

private static final ServletInputStream EMPTY_SERVLET_INPUT_STREAM =
new DelegatingServletInputStream(StreamUtils.emptyInput());
new DelegatingServletInputStream(InputStream.nullInputStream());

private static final BufferedReader EMPTY_BUFFERED_READER =
new BufferedReader(new StringReader(""));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,6 @@
import org.springframework.util.LinkedMultiValueMap;
import org.springframework.util.MultiValueMap;
import org.springframework.util.ObjectUtils;
import org.springframework.util.StreamUtils;
import org.springframework.util.StringUtils;
import org.springframework.web.context.WebApplicationContext;
import org.springframework.web.context.support.WebApplicationContextUtils;
Expand Down Expand Up @@ -804,7 +803,7 @@ private MultiValueMap<String, String> parseFormData(MediaType mediaType) {
HttpInputMessage message = new HttpInputMessage() {
@Override
public InputStream getBody() {
return (content != null ? new ByteArrayInputStream(content) : StreamUtils.emptyInput());
return (content != null ? new ByteArrayInputStream(content) : InputStream.nullInputStream());
}
@Override
public HttpHeaders getHeaders() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,6 @@
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpStatusCode;
import org.springframework.lang.Nullable;
import org.springframework.util.StreamUtils;

/**
* {@link ClientHttpResponse} implementation based on
Expand Down Expand Up @@ -84,7 +83,7 @@ public HttpHeaders getHeaders() {
@Override
public InputStream getBody() throws IOException {
HttpEntity entity = this.httpResponse.getEntity();
return (entity != null ? entity.getContent() : StreamUtils.emptyInput());
return (entity != null ? entity.getContent() : InputStream.nullInputStream());
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,6 @@
import org.springframework.http.HttpStatusCode;
import org.springframework.lang.Nullable;
import org.springframework.util.Assert;
import org.springframework.util.StreamUtils;

/**
* {@link ClientHttpResponse} implementation based on OkHttp 3.x.
Expand Down Expand Up @@ -69,7 +68,7 @@ public String getStatusText() {
@Override
public InputStream getBody() throws IOException {
ResponseBody body = this.response.body();
return (body != null ? body.byteStream() : StreamUtils.emptyInput());
return (body != null ? body.byteStream() : InputStream.nullInputStream());
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ public class SourceHttpMessageConverter<T extends Source> extends AbstractHttpMe
(publicId, systemId) -> new InputSource(new StringReader(""));

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

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

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@

import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.URI;
import java.util.Arrays;
import java.util.Collections;
Expand Down Expand Up @@ -52,7 +53,6 @@
import org.springframework.http.converter.HttpMessageConverter;
import org.springframework.http.converter.json.KotlinSerializationJsonHttpMessageConverter;
import org.springframework.http.converter.json.MappingJackson2HttpMessageConverter;
import org.springframework.util.StreamUtils;
import org.springframework.web.util.DefaultUriBuilderFactory;

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

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

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

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

String result = template.patchForObject("https://example.com", null, String.class);
assertThat(result).as("Invalid POST result").isNull();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,14 +16,14 @@

package org.springframework.web.filter;

import java.io.InputStream;
import java.nio.charset.StandardCharsets;

import jakarta.servlet.FilterChain;
import jakarta.servlet.http.HttpServletResponse;
import org.junit.jupiter.api.Test;

import org.springframework.util.FileCopyUtils;
import org.springframework.util.StreamUtils;
import org.springframework.web.testfixture.servlet.MockHttpServletRequest;
import org.springframework.web.testfixture.servlet.MockHttpServletResponse;

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

assertThat(filter.isEligibleForEtag(request, response, 200, StreamUtils.emptyInput())).isTrue();
assertThat(filter.isEligibleForEtag(request, response, 300, StreamUtils.emptyInput())).isFalse();
assertThat(filter.isEligibleForEtag(request, response, 200, InputStream.nullInputStream())).isTrue();
assertThat(filter.isEligibleForEtag(request, response, 300, InputStream.nullInputStream())).isFalse();

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

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

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

@Test
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,6 @@
import org.springframework.util.LinkedMultiValueMap;
import org.springframework.util.MultiValueMap;
import org.springframework.util.ObjectUtils;
import org.springframework.util.StreamUtils;
import org.springframework.util.StringUtils;
import org.springframework.web.util.UrlPathHelper;

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

private static final ServletInputStream EMPTY_SERVLET_INPUT_STREAM =
new DelegatingServletInputStream(StreamUtils.emptyInput());
new DelegatingServletInputStream(InputStream.nullInputStream());

private static final BufferedReader EMPTY_BUFFERED_READER =
new BufferedReader(new StringReader(""));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,6 @@
import org.springframework.lang.Nullable;
import org.springframework.util.Assert;
import org.springframework.util.MimeTypeUtils;
import org.springframework.util.StreamUtils;
import org.springframework.validation.Errors;
import org.springframework.validation.annotation.ValidationAnnotationUtils;
import org.springframework.web.HttpMediaTypeNotSupportedException;
Expand Down Expand Up @@ -346,7 +345,7 @@ public HttpHeaders getHeaders() {

@Override
public InputStream getBody() {
return (this.body != null ? this.body : StreamUtils.emptyInput());
return (this.body != null ? this.body : InputStream.nullInputStream());
}

public boolean hasBody() {
Expand Down

0 comments on commit 0770d86

Please sign in to comment.