Add close() method in FileCopyUtils to reduce duplication
Closes gh-24393
This commit is contained in:
parent
550f13e8ed
commit
d624dc084f
|
|
@ -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");
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||||
* you may not use this file except in compliance with the License.
|
* you may not use this file except in compliance with the License.
|
||||||
|
|
@ -18,6 +18,7 @@ package org.springframework.util;
|
||||||
|
|
||||||
import java.io.ByteArrayInputStream;
|
import java.io.ByteArrayInputStream;
|
||||||
import java.io.ByteArrayOutputStream;
|
import java.io.ByteArrayOutputStream;
|
||||||
|
import java.io.Closeable;
|
||||||
import java.io.File;
|
import java.io.File;
|
||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
import java.io.InputStream;
|
import java.io.InputStream;
|
||||||
|
|
@ -37,6 +38,7 @@ import org.springframework.lang.Nullable;
|
||||||
* <p>Mainly for use within the framework, but also useful for application code.
|
* <p>Mainly for use within the framework, but also useful for application code.
|
||||||
*
|
*
|
||||||
* @author Juergen Hoeller
|
* @author Juergen Hoeller
|
||||||
|
* @author Hyunjin Choi
|
||||||
* @since 06.10.2003
|
* @since 06.10.2003
|
||||||
* @see StreamUtils
|
* @see StreamUtils
|
||||||
* @see FileSystemUtils
|
* @see FileSystemUtils
|
||||||
|
|
@ -110,16 +112,8 @@ public abstract class FileCopyUtils {
|
||||||
return StreamUtils.copy(in, out);
|
return StreamUtils.copy(in, out);
|
||||||
}
|
}
|
||||||
finally {
|
finally {
|
||||||
try {
|
close(in);
|
||||||
in.close();
|
close(out);
|
||||||
}
|
|
||||||
catch (IOException ex) {
|
|
||||||
}
|
|
||||||
try {
|
|
||||||
out.close();
|
|
||||||
}
|
|
||||||
catch (IOException ex) {
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -138,11 +132,7 @@ public abstract class FileCopyUtils {
|
||||||
out.write(in);
|
out.write(in);
|
||||||
}
|
}
|
||||||
finally {
|
finally {
|
||||||
try {
|
close(out);
|
||||||
out.close();
|
|
||||||
}
|
|
||||||
catch (IOException ex) {
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -192,16 +182,8 @@ public abstract class FileCopyUtils {
|
||||||
return byteCount;
|
return byteCount;
|
||||||
}
|
}
|
||||||
finally {
|
finally {
|
||||||
try {
|
close(in);
|
||||||
in.close();
|
close(out);
|
||||||
}
|
|
||||||
catch (IOException ex) {
|
|
||||||
}
|
|
||||||
try {
|
|
||||||
out.close();
|
|
||||||
}
|
|
||||||
catch (IOException ex) {
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -220,11 +202,7 @@ public abstract class FileCopyUtils {
|
||||||
out.write(in);
|
out.write(in);
|
||||||
}
|
}
|
||||||
finally {
|
finally {
|
||||||
try {
|
close(out);
|
||||||
out.close();
|
|
||||||
}
|
|
||||||
catch (IOException ex) {
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -245,4 +223,18 @@ public abstract class FileCopyUtils {
|
||||||
return out.toString();
|
return out.toString();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Close the {@link Closeable} as a null-safety.
|
||||||
|
*
|
||||||
|
* @param closeable to close, may be null.
|
||||||
|
*/
|
||||||
|
private static void close(@Nullable Closeable closeable) {
|
||||||
|
if (closeable == null) return;
|
||||||
|
try {
|
||||||
|
closeable.close();
|
||||||
|
} catch (IOException e) {
|
||||||
|
// do nothing
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue