|
| 1 | +/* |
| 2 | + * Copyright 2002-2013 the original author or authors. |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | + |
| 17 | +package org.springframework.util; |
| 18 | + |
| 19 | +import java.io.ByteArrayOutputStream; |
| 20 | +import java.io.FilterInputStream; |
| 21 | +import java.io.FilterOutputStream; |
| 22 | +import java.io.IOException; |
| 23 | +import java.io.InputStream; |
| 24 | +import java.io.InputStreamReader; |
| 25 | +import java.io.OutputStream; |
| 26 | +import java.io.OutputStreamWriter; |
| 27 | +import java.io.Writer; |
| 28 | +import java.nio.charset.Charset; |
| 29 | + |
| 30 | + |
| 31 | +/** |
| 32 | + * Simple utility methods for dealing with streams. The copy methods of this class are |
| 33 | + * similar to those defined in {@link FileCopyUtils} except that all affected streams are |
| 34 | + * left open when done. All copy methods use a block size of 4096 bytes. |
| 35 | + * |
| 36 | + * <p>Mainly for use within the framework, but also useful for application code. |
| 37 | + * |
| 38 | + * @author Juergen Hoeller |
| 39 | + * @author Phillip Webb |
| 40 | + * @since 3.2 |
| 41 | + * @see FileCopyUtils |
| 42 | + */ |
| 43 | +public abstract class StreamUtils { |
| 44 | + |
| 45 | + public static final int BUFFER_SIZE = 4096; |
| 46 | + |
| 47 | + |
| 48 | + /** |
| 49 | + * Copy the contents of the given InputStream into a new byte array. |
| 50 | + * Leaves the stream open when done. |
| 51 | + * @param in the stream to copy from |
| 52 | + * @return the new byte array that has been copied to |
| 53 | + * @throws IOException in case of I/O errors |
| 54 | + */ |
| 55 | + public static byte[] copyToByteArray(InputStream in) throws IOException { |
| 56 | + ByteArrayOutputStream out = new ByteArrayOutputStream(BUFFER_SIZE); |
| 57 | + copy(in, out); |
| 58 | + return out.toByteArray(); |
| 59 | + } |
| 60 | + |
| 61 | + /** |
| 62 | + * Copy the contents of the given InputStream into a String. |
| 63 | + * Leaves the stream open when done. |
| 64 | + * @param in the InputStream to copy from |
| 65 | + * @return the String that has been copied to |
| 66 | + * @throws IOException in case of I/O errors |
| 67 | + */ |
| 68 | + public static String copyToString(InputStream in, Charset charset) throws IOException { |
| 69 | + Assert.notNull(in, "No InputStream specified"); |
| 70 | + StringBuilder out = new StringBuilder(); |
| 71 | + InputStreamReader reader = new InputStreamReader(in, charset); |
| 72 | + char[] buffer = new char[BUFFER_SIZE]; |
| 73 | + int bytesRead = -1; |
| 74 | + while ((bytesRead = reader.read(buffer)) != -1) { |
| 75 | + out.append(buffer, 0, bytesRead); |
| 76 | + } |
| 77 | + return out.toString(); |
| 78 | + } |
| 79 | + |
| 80 | + /** |
| 81 | + * Copy the contents of the given byte array to the given OutputStream. |
| 82 | + * Leaves the stream open when done. |
| 83 | + * @param in the byte array to copy from |
| 84 | + * @param out the OutputStream to copy to |
| 85 | + * @throws IOException in case of I/O errors |
| 86 | + */ |
| 87 | + public static void copy(byte[] in, OutputStream out) throws IOException { |
| 88 | + Assert.notNull(in, "No input byte array specified"); |
| 89 | + Assert.notNull(out, "No OutputStream specified"); |
| 90 | + out.write(in); |
| 91 | + } |
| 92 | + |
| 93 | + /** |
| 94 | + * Copy the contents of the given String to the given output OutputStream. |
| 95 | + * Leaves the stream open when done. |
| 96 | + * @param in the String to copy from |
| 97 | + * @param charset the Charset |
| 98 | + * @param out the OutputStream to copy to |
| 99 | + * @throws IOException in case of I/O errors |
| 100 | + */ |
| 101 | + public static void copy(String in, Charset charset, OutputStream out) throws IOException { |
| 102 | + Assert.notNull(in, "No input String specified"); |
| 103 | + Assert.notNull(charset, "No charset specified"); |
| 104 | + Assert.notNull(out, "No OutputStream specified"); |
| 105 | + Writer writer = new OutputStreamWriter(out, charset); |
| 106 | + writer.write(in); |
| 107 | + writer.flush(); |
| 108 | + } |
| 109 | + |
| 110 | + /** |
| 111 | + * Copy the contents of the given InputStream to the given OutputStream. |
| 112 | + * Leaves both streams open when done. |
| 113 | + * @param in the InputStream to copy from |
| 114 | + * @param out the OutputStream to copy to |
| 115 | + * @return the number of bytes copied |
| 116 | + * @throws IOException in case of I/O errors |
| 117 | + */ |
| 118 | + public static int copy(InputStream in, OutputStream out) throws IOException { |
| 119 | + Assert.notNull(in, "No InputStream specified"); |
| 120 | + Assert.notNull(out, "No OutputStream specified"); |
| 121 | + int byteCount = 0; |
| 122 | + byte[] buffer = new byte[BUFFER_SIZE]; |
| 123 | + int bytesRead = -1; |
| 124 | + while ((bytesRead = in.read(buffer)) != -1) { |
| 125 | + out.write(buffer, 0, bytesRead); |
| 126 | + byteCount += bytesRead; |
| 127 | + } |
| 128 | + out.flush(); |
| 129 | + return byteCount; |
| 130 | + } |
| 131 | + |
| 132 | + /** |
| 133 | + * Returns a variant of the given {@link InputStream} where calling |
| 134 | + * {@link InputStream#close() close()} has no effect. |
| 135 | + * @param in the InputStream to decorate |
| 136 | + * @return a version of the InputStream that ignores calls to close |
| 137 | + */ |
| 138 | + public static InputStream nonClosing(InputStream in) { |
| 139 | + Assert.notNull(in, "No InputStream specified"); |
| 140 | + return new NonClosingInputStream(in); |
| 141 | + } |
| 142 | + |
| 143 | + /** |
| 144 | + * Returns a variant of the given {@link OutputStream} where calling |
| 145 | + * {@link OutputStream#close() close()} has no effect. |
| 146 | + * @param in the OutputStream to decorate |
| 147 | + * @return a version of the OutputStream that ignores calls to close |
| 148 | + */ |
| 149 | + public static OutputStream nonClosing(OutputStream out) { |
| 150 | + Assert.notNull(out, "No OutputStream specified"); |
| 151 | + return new NonClosingOutputStream(out); |
| 152 | + } |
| 153 | + |
| 154 | + |
| 155 | + private static class NonClosingInputStream extends FilterInputStream { |
| 156 | + |
| 157 | + public NonClosingInputStream(InputStream in) { |
| 158 | + super(in); |
| 159 | + } |
| 160 | + |
| 161 | + @Override |
| 162 | + public void close() throws IOException { |
| 163 | + } |
| 164 | + } |
| 165 | + |
| 166 | + |
| 167 | + private static class NonClosingOutputStream extends FilterOutputStream { |
| 168 | + |
| 169 | + public NonClosingOutputStream(OutputStream out) { |
| 170 | + super(out); |
| 171 | + } |
| 172 | + |
| 173 | + @Override |
| 174 | + public void write(byte[] b, int off, int let) throws IOException { |
| 175 | + // It is critical that we override this method for performance |
| 176 | + out.write(b, off, let); |
| 177 | + } |
| 178 | + |
| 179 | + @Override |
| 180 | + public void close() throws IOException { |
| 181 | + } |
| 182 | + } |
| 183 | +} |
0 commit comments