Construct StringWriter instances with appropriate initial size
Closes gh-25789
This commit is contained in:
@@ -1,5 +1,5 @@
|
||||
/*
|
||||
* Copyright 2002-2017 the original author or authors.
|
||||
* Copyright 2002-2020 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.
|
||||
@@ -40,9 +40,13 @@ import java.io.Writer;
|
||||
* @author Juergen Hoeller
|
||||
* @since 06.10.2003
|
||||
* @see StreamUtils
|
||||
* @see FileSystemUtils
|
||||
*/
|
||||
public abstract class FileCopyUtils {
|
||||
|
||||
/**
|
||||
* The default buffer size used when copying bytes.
|
||||
*/
|
||||
public static final int BUFFER_SIZE = StreamUtils.BUFFER_SIZE;
|
||||
|
||||
|
||||
@@ -184,15 +188,15 @@ public abstract class FileCopyUtils {
|
||||
Assert.notNull(out, "No Writer specified");
|
||||
|
||||
try {
|
||||
int byteCount = 0;
|
||||
int charCount = 0;
|
||||
char[] buffer = new char[BUFFER_SIZE];
|
||||
int bytesRead = -1;
|
||||
while ((bytesRead = in.read(buffer)) != -1) {
|
||||
out.write(buffer, 0, bytesRead);
|
||||
byteCount += bytesRead;
|
||||
int charsRead;
|
||||
while ((charsRead = in.read(buffer)) != -1) {
|
||||
out.write(buffer, 0, charsRead);
|
||||
charCount += charsRead;
|
||||
}
|
||||
out.flush();
|
||||
return byteCount;
|
||||
return charCount;
|
||||
}
|
||||
finally {
|
||||
try {
|
||||
@@ -209,7 +213,7 @@ public abstract class FileCopyUtils {
|
||||
}
|
||||
|
||||
/**
|
||||
* Copy the contents of the given String to the given output Writer.
|
||||
* Copy the contents of the given String to the given Writer.
|
||||
* Closes the writer when done.
|
||||
* @param in the String to copy from
|
||||
* @param out the Writer to copy to
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
/*
|
||||
* Copyright 2002-2017 the original author or authors.
|
||||
* Copyright 2002-2020 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.
|
||||
@@ -43,6 +43,9 @@ import java.nio.charset.Charset;
|
||||
*/
|
||||
public abstract class StreamUtils {
|
||||
|
||||
/**
|
||||
* The default buffer size used when copying bytes.
|
||||
*/
|
||||
public static final int BUFFER_SIZE = 4096;
|
||||
|
||||
private static final byte[] EMPTY_CONTENT = new byte[0];
|
||||
@@ -50,7 +53,7 @@ public abstract class StreamUtils {
|
||||
|
||||
/**
|
||||
* Copy the contents of the given InputStream into a new byte array.
|
||||
* Leaves the stream open when done.
|
||||
* <p>Leaves the stream open when done.
|
||||
* @param in the stream to copy from (may be {@code null} or empty)
|
||||
* @return the new byte array that has been copied to (possibly empty)
|
||||
* @throws IOException in case of I/O errors
|
||||
@@ -67,8 +70,9 @@ public abstract class StreamUtils {
|
||||
|
||||
/**
|
||||
* Copy the contents of the given InputStream into a String.
|
||||
* Leaves the stream open when done.
|
||||
* <p>Leaves the stream open when done.
|
||||
* @param in the InputStream to copy from (may be {@code null} or empty)
|
||||
* @param charset the {@link Charset} to use to decode the bytes
|
||||
* @return the String that has been copied to (possibly empty)
|
||||
* @throws IOException in case of I/O errors
|
||||
*/
|
||||
@@ -77,19 +81,19 @@ public abstract class StreamUtils {
|
||||
return "";
|
||||
}
|
||||
|
||||
StringBuilder out = new StringBuilder();
|
||||
StringBuilder out = new StringBuilder(BUFFER_SIZE);
|
||||
InputStreamReader reader = new InputStreamReader(in, charset);
|
||||
char[] buffer = new char[BUFFER_SIZE];
|
||||
int bytesRead = -1;
|
||||
while ((bytesRead = reader.read(buffer)) != -1) {
|
||||
out.append(buffer, 0, bytesRead);
|
||||
int charsRead;
|
||||
while ((charsRead = reader.read(buffer)) != -1) {
|
||||
out.append(buffer, 0, charsRead);
|
||||
}
|
||||
return out.toString();
|
||||
}
|
||||
|
||||
/**
|
||||
* Copy the contents of the given byte array to the given OutputStream.
|
||||
* Leaves the stream open when done.
|
||||
* <p>Leaves the stream open when done.
|
||||
* @param in the byte array to copy from
|
||||
* @param out the OutputStream to copy to
|
||||
* @throws IOException in case of I/O errors
|
||||
@@ -99,11 +103,12 @@ public abstract class StreamUtils {
|
||||
Assert.notNull(out, "No OutputStream specified");
|
||||
|
||||
out.write(in);
|
||||
out.flush();
|
||||
}
|
||||
|
||||
/**
|
||||
* Copy the contents of the given String to the given output OutputStream.
|
||||
* Leaves the stream open when done.
|
||||
* Copy the contents of the given String to the given OutputStream.
|
||||
* <p>Leaves the stream open when done.
|
||||
* @param in the String to copy from
|
||||
* @param charset the Charset
|
||||
* @param out the OutputStream to copy to
|
||||
@@ -111,7 +116,7 @@ public abstract class StreamUtils {
|
||||
*/
|
||||
public static void copy(String in, Charset charset, OutputStream out) throws IOException {
|
||||
Assert.notNull(in, "No input String specified");
|
||||
Assert.notNull(charset, "No charset specified");
|
||||
Assert.notNull(charset, "No Charset specified");
|
||||
Assert.notNull(out, "No OutputStream specified");
|
||||
|
||||
Writer writer = new OutputStreamWriter(out, charset);
|
||||
@@ -121,7 +126,7 @@ public abstract class StreamUtils {
|
||||
|
||||
/**
|
||||
* Copy the contents of the given InputStream to the given OutputStream.
|
||||
* Leaves both streams open when done.
|
||||
* <p>Leaves both streams open when done.
|
||||
* @param in the InputStream to copy from
|
||||
* @param out the OutputStream to copy to
|
||||
* @return the number of bytes copied
|
||||
@@ -133,7 +138,7 @@ public abstract class StreamUtils {
|
||||
|
||||
int byteCount = 0;
|
||||
byte[] buffer = new byte[BUFFER_SIZE];
|
||||
int bytesRead = -1;
|
||||
int bytesRead;
|
||||
while ((bytesRead = in.read(buffer)) != -1) {
|
||||
out.write(buffer, 0, bytesRead);
|
||||
byteCount += bytesRead;
|
||||
@@ -165,7 +170,7 @@ public abstract class StreamUtils {
|
||||
}
|
||||
|
||||
long bytesToCopy = end - start + 1;
|
||||
byte[] buffer = new byte[StreamUtils.BUFFER_SIZE];
|
||||
byte[] buffer = new byte[(int) Math.min(StreamUtils.BUFFER_SIZE, bytesToCopy)];
|
||||
while (bytesToCopy > 0) {
|
||||
int bytesRead = in.read(buffer);
|
||||
if (bytesRead == -1) {
|
||||
@@ -185,7 +190,7 @@ public abstract class StreamUtils {
|
||||
|
||||
/**
|
||||
* Drain the remaining content of the given InputStream.
|
||||
* Leaves the InputStream open when done.
|
||||
* <p>Leaves the InputStream open when done.
|
||||
* @param in the InputStream to drain
|
||||
* @return the number of bytes read
|
||||
* @throws IOException in case of I/O errors
|
||||
@@ -255,7 +260,7 @@ public abstract class StreamUtils {
|
||||
@Override
|
||||
public void write(byte[] b, int off, int let) throws IOException {
|
||||
// It is critical that we override this method for performance
|
||||
out.write(b, off, let);
|
||||
this.out.write(b, off, let);
|
||||
}
|
||||
|
||||
@Override
|
||||
|
||||
Reference in New Issue
Block a user