Refactor to avoid creating a new ArrayList on all API calls.
MODIFY
src/main/java/org/apache/commons/io/output/FilterCollectionWriter.java
src/main/java/org/apache/commons/io/output/FilterCollectionWriter.java
@@ -73,21 +73,37 @@ protected FilterCollectionWriter(final Writer... writers) {
this.writers = writers == null ? EMPTY_WRITERS : Arrays.asList(writers);
}
+ /**
+ * Adds an indexed exception to the list.
+ *
+ * @param causeList The target list.
+ * @param i The index.
+ * @param e The cause.
+ * @return the given list or a new list on null input.
+ */
+ private List<Exception> add(List<Exception> causeList, int i, final IOException e) {
+ if (causeList == null) {
+ causeList = new ArrayList<>();
+ }
+ causeList.add(new IOIndexedException(i, e));
+ return causeList;
+ }
+
@Override
public Writer append(final char c) throws IOException {
- final List<Exception> causeList = new ArrayList<>();
+ List<Exception> causeList = null;
int i = 0;
for (final Writer w : writers) {
if (w != null) {
try {
w.append(c);
} catch (final IOException e) {
- causeList.add(new IOIndexedException(i, e));
+ causeList = add(causeList, i, e);
}
}
i++;
}
- if (!causeList.isEmpty()) {
+ if (notEmpty(causeList)) {
throw new IOExceptionList("append", causeList);
}
return this;
@@ -95,19 +111,19 @@ public Writer append(final char c) throws IOException {
@Override
public Writer append(final CharSequence csq) throws IOException {
- final List<Exception> causeList = new ArrayList<>();
+ List<Exception> causeList = null;
int i = 0;
for (final Writer w : writers) {
if (w != null) {
try {
w.append(csq);
} catch (final IOException e) {
- causeList.add(new IOIndexedException(i, e));
+ causeList = add(causeList, i, e);
}
}
i++;
}
- if (!causeList.isEmpty()) {
+ if (notEmpty(causeList)) {
throw new IOExceptionList("append", causeList);
}
return this;
@@ -116,19 +132,19 @@ public Writer append(final CharSequence csq) throws IOException {
@Override
public Writer append(final CharSequence csq, final int start, final int end) throws IOException {
- final List<Exception> causeList = new ArrayList<>();
+ List<Exception> causeList = null;
int i = 0;
for (final Writer w : writers) {
if (w != null) {
try {
w.append(csq, start, end);
} catch (final IOException e) {
- causeList.add(new IOIndexedException(i, e));
+ causeList = add(causeList, i, e);
}
}
i++;
}
- if (!causeList.isEmpty()) {
+ if (notEmpty(causeList)) {
throw new IOExceptionList("append", causeList);
}
return this;
@@ -136,19 +152,19 @@ public Writer append(final CharSequence csq, final int start, final int end) thr
@Override
public void close() throws IOException {
- final List<Exception> causeList = new ArrayList<>();
+ List<Exception> causeList = null;
int i = 0;
for (final Writer w : writers) {
if (w != null) {
try {
w.close();
} catch (final IOException e) {
- causeList.add(new IOIndexedException(i, e));
+ causeList = add(causeList, i, e);
}
}
i++;
}
- if (!causeList.isEmpty()) {
+ if (notEmpty(causeList)) {
throw new IOExceptionList("close", causeList);
}
@@ -161,67 +177,77 @@ public void close() throws IOException {
*/
@Override
public void flush() throws IOException {
- final List<Exception> causeList = new ArrayList<>();
+ List<Exception> causeList = null;
int i = 0;
for (final Writer w : writers) {
if (w != null) {
try {
w.flush();
} catch (final IOException e) {
- causeList.add(new IOIndexedException(i, e));
+ causeList = add(causeList, i, e);
}
}
i++;
}
- if (!causeList.isEmpty()) {
+ if (notEmpty(causeList)) {
throw new IOExceptionList("flush", causeList);
}
}
/**
- * Writes a portion of an array of characters.
- *
- * @param cbuf Buffer of characters to be written
- * @param off Offset from which to start reading characters
- * @param len Number of characters to be written
+ * Tests if the given list is empty in a null-safe manner.
*
- * @exception IOException If an I/O error occurs
+ * @param causeList the list to test.
+ * @return true if empty or null.
*/
+ private boolean notEmpty(List<Exception> causeList) {
+ return causeList != null && !causeList.isEmpty();
+ }
+
@Override
- public void write(final char[] cbuf, final int off, final int len) throws IOException {
- final List<Exception> causeList = new ArrayList<>();
+ public void write(final char[] cbuf) throws IOException {
+ List<Exception> causeList = null;
int i = 0;
for (final Writer w : writers) {
if (w != null) {
try {
- w.write(cbuf, off, len);
+ w.write(cbuf);
} catch (final IOException e) {
- causeList.add(new IOIndexedException(i, e));
+ causeList = add(causeList, i, e);
}
}
i++;
}
- if (!causeList.isEmpty()) {
+ if (notEmpty(causeList)) {
throw new IOExceptionList("write", causeList);
}
}
+ /**
+ * Writes a portion of an array of characters.
+ *
+ * @param cbuf Buffer of characters to be written
+ * @param off Offset from which to start reading characters
+ * @param len Number of characters to be written
+ *
+ * @exception IOException If an I/O error occurs
+ */
@Override
- public void write(final char[] cbuf) throws IOException {
- final List<Exception> causeList = new ArrayList<>();
+ public void write(final char[] cbuf, final int off, final int len) throws IOException {
+ List<Exception> causeList = null;
int i = 0;
for (final Writer w : writers) {
if (w != null) {
try {
- w.write(cbuf);
+ w.write(cbuf, off, len);
} catch (final IOException e) {
- causeList.add(new IOIndexedException(i, e));
+ causeList = add(causeList, i, e);
}
}
i++;
}
- if (!causeList.isEmpty()) {
+ if (notEmpty(causeList)) {
throw new IOExceptionList("write", causeList);
}
}
@@ -233,38 +259,38 @@ public void write(final char[] cbuf) throws IOException {
*/
@Override
public void write(final int c) throws IOException {
- final List<Exception> causeList = new ArrayList<>();
+ List<Exception> causeList = null;
int i = 0;
for (final Writer w : writers) {
if (w != null) {
try {
w.write(c);
} catch (final IOException e) {
- causeList.add(new IOIndexedException(i, e));
+ causeList = add(causeList, i, e);
}
}
i++;
}
- if (!causeList.isEmpty()) {
+ if (notEmpty(causeList)) {
throw new IOExceptionList("write", causeList);
}
}
@Override
public void write(final String str) throws IOException {
- final List<Exception> causeList = new ArrayList<>();
+ List<Exception> causeList = null;
int i = 0;
for (final Writer w : writers) {
if (w != null) {
try {
w.write(str);
} catch (final IOException e) {
- causeList.add(new IOIndexedException(i, e));
+ causeList = add(causeList, i, e);
}
}
i++;
}
- if (!causeList.isEmpty()) {
+ if (notEmpty(causeList)) {
throw new IOExceptionList("write", causeList);
}
@@ -281,19 +307,19 @@ public void write(final String str) throws IOException {
*/
@Override
public void write(final String str, final int off, final int len) throws IOException {
- final List<Exception> causeList = new ArrayList<>();
+ List<Exception> causeList = null;
int i = 0;
for (final Writer w : writers) {
if (w != null) {
try {
w.write(str, off, len);
} catch (final IOException e) {
- causeList.add(new IOIndexedException(i, e));
+ causeList = add(causeList, i, e);
}
}
i++;
}
- if (!causeList.isEmpty()) {
+ if (notEmpty(causeList)) {
throw new IOExceptionList("write", causeList);
}

Refactor to avoid creating a new ArrayList on all API calls.