StreamUtils.emptyInput() for consistent empty InputStream exposure

Issue: SPR-13563
This commit is contained in:
Juergen Hoeller
2015-10-12 22:25:40 +02:00
parent 6256586047
commit 66177dfd8c
9 changed files with 36 additions and 31 deletions

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2002-2013 the original author or authors.
* Copyright 2002-2015 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.
@@ -16,6 +16,7 @@
package org.springframework.util;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.FilterInputStream;
import java.io.FilterOutputStream;
@@ -43,6 +44,8 @@ public abstract class StreamUtils {
public static final int BUFFER_SIZE = 4096;
private static final byte[] EMPTY_CONTENT = new byte[0];
/**
* Copy the contents of the given InputStream into a new byte array.
@@ -129,7 +132,16 @@ public abstract class StreamUtils {
}
/**
* Returns a variant of the given {@link InputStream} where calling
* Return an efficient empty {@link InputStream}.
* @return a {@link ByteArrayInputStream} based on an empty byte array
* @since 4.2.2
*/
public static InputStream emptyInput() {
return new ByteArrayInputStream(EMPTY_CONTENT);
}
/**
* Return a variant of the given {@link InputStream} where calling
* {@link InputStream#close() close()} has no effect.
* @param in the InputStream to decorate
* @return a version of the InputStream that ignores calls to close
@@ -140,7 +152,7 @@ public abstract class StreamUtils {
}
/**
* Returns a variant of the given {@link OutputStream} where calling
* Return a variant of the given {@link OutputStream} where calling
* {@link OutputStream#close() close()} has no effect.
* @param out the OutputStream to decorate
* @return a version of the OutputStream that ignores calls to close