Construct StringWriter instances with appropriate initial size

Closes gh-25789

(cherry picked from commit 9dfef59af2)
This commit is contained in:
Juergen Hoeller
2020-09-18 18:14:57 +02:00
parent 547a13985a
commit 5b4f3e871f
6 changed files with 26 additions and 24 deletions

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2002-2018 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.
@@ -181,15 +181,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 {
@@ -206,7 +206,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
@@ -240,7 +240,7 @@ public abstract class FileCopyUtils {
return "";
}
StringWriter out = new StringWriter();
StringWriter out = new StringWriter(BUFFER_SIZE);
copy(in, out);
return out.toString();
}