This commit is contained in:
Looly
2023-03-27 02:02:10 +08:00
parent 16f7549c7d
commit 4fca8310e7
96 changed files with 307 additions and 315 deletions

View File

@@ -86,7 +86,7 @@ public class LineIter extends ComputeIter<String> implements IterableIter<String
@Override
public void close() {
super.finish();
IoUtil.close(bufferedReader);
IoUtil.closeQuietly(bufferedReader);
}
/**

View File

@@ -96,7 +96,7 @@ public class Deflate implements Closeable {
@Override
public void close() {
IoUtil.close(this.target);
IoUtil.close(this.source);
IoUtil.closeQuietly(this.target);
IoUtil.closeQuietly(this.source);
}
}

View File

@@ -88,7 +88,7 @@ public class Gzip implements Closeable {
@Override
public void close() {
IoUtil.close(this.target);
IoUtil.close(this.source);
IoUtil.closeQuietly(this.target);
IoUtil.closeQuietly(this.source);
}
}

View File

@@ -192,9 +192,9 @@ public class ZipReader implements Closeable {
@Override
public void close() throws IORuntimeException {
if (null != this.zipFile) {
IoUtil.close(this.zipFile);
IoUtil.closeQuietly(this.zipFile);
} else {
IoUtil.close(this.in);
IoUtil.closeQuietly(this.in);
}
}

View File

@@ -743,7 +743,7 @@ public class ZipUtil {
in = FileUtil.getInputStream(file);
return gzip(in, (int) file.length());
} finally {
IoUtil.close(in);
IoUtil.closeQuietly(in);
}
}
@@ -852,7 +852,7 @@ public class ZipUtil {
in = FileUtil.getInputStream(file);
return zlib(in, level, (int) file.length());
} finally {
IoUtil.close(in);
IoUtil.closeQuietly(in);
}
}

View File

@@ -220,7 +220,7 @@ public class ZipWriter implements Closeable {
} catch (final IOException e) {
throw new IORuntimeException(e);
} finally {
IoUtil.close(this.out);
IoUtil.closeQuietly(this.out);
}
}
@@ -289,7 +289,7 @@ public class ZipWriter implements Closeable {
} catch (final IOException e) {
throw new IORuntimeException(e);
} finally {
IoUtil.close(in);
IoUtil.closeQuietly(in);
}
IoUtil.flush(this.out);

View File

@@ -85,7 +85,7 @@ public class StringConverter extends AbstractConverter {
} catch (final SQLException e) {
throw new ConvertException(e);
} finally {
IoUtil.close(reader);
IoUtil.closeQuietly(reader);
}
}
@@ -104,7 +104,7 @@ public class StringConverter extends AbstractConverter {
} catch (final SQLException e) {
throw new ConvertException(e);
} finally {
IoUtil.close(in);
IoUtil.closeQuietly(in);
}
}
}

View File

@@ -92,7 +92,7 @@ public class CharsetDetector {
} catch (final IOException e) {
throw new IORuntimeException(e);
} finally {
IoUtil.close(in);
IoUtil.closeQuietly(in);
}
return null;
}

View File

@@ -340,7 +340,7 @@ public class IoUtil extends NioUtil {
throw new IORuntimeException(e);
} finally {
if (isClose) {
IoUtil.close(reader);
IoUtil.closeQuietly(reader);
}
}
return builder.toString();
@@ -884,35 +884,6 @@ public class IoUtil extends NioUtil {
}
}
/**
* 关闭<br>
* 关闭失败不会抛出异常
*
* @param closeable 被关闭的对象
*/
public static void close(final AutoCloseable closeable) {
if (null != closeable) {
try {
closeable.close();
} catch (final Exception e) {
// 静默关闭
}
}
}
/**
* 关闭<br>
* 关闭失败不会抛出异常
*
* @param closeable 被关闭的对象
* @throws IOException IO异常
*/
public static void nullSafeClose(final Closeable closeable) throws IOException {
if (null != closeable) {
closeable.close();
}
}
/**
* 尝试关闭指定对象<br>
* 判断对象如果实现了{@link AutoCloseable},则调用之
@@ -922,7 +893,38 @@ public class IoUtil extends NioUtil {
*/
public static void closeIfPossible(final Object obj) {
if (obj instanceof AutoCloseable) {
close((AutoCloseable) obj);
closeQuietly((AutoCloseable) obj);
}
}
/**
* 按照给定顺序连续关闭一系列对象<br>
* 这些对象必须按照顺序关闭,否则会出错。
*
* @param closeables 需要关闭的对象
*/
public static void closeQuietly(final AutoCloseable... closeables) {
for (final AutoCloseable closeable : closeables) {
if (null != closeable) {
try {
closeable.close();
} catch (final Exception e) {
// 静默关闭
}
}
}
}
/**
* 关闭<br>
* 关闭失败抛出{@link IOException}异常
*
* @param closeable 被关闭的对象
* @throws IOException IO异常
*/
public static void nullSafeClose(final Closeable closeable) throws IOException {
if (null != closeable) {
closeable.close();
}
}

View File

@@ -221,20 +221,4 @@ public class NioUtil {
}
return StrUtil.str(buffer, charset);
}
/**
* 关闭<br>
* 关闭失败不会抛出异常
*
* @param closeable 被关闭的对象
*/
public static void close(final AutoCloseable closeable) {
if (null != closeable) {
try {
closeable.close();
} catch (final Exception e) {
// 静默关闭
}
}
}
}

View File

@@ -81,7 +81,7 @@ public class ChecksumUtil {
in = new CheckedInputStream(in, checksum);
IoUtil.copy(in, EmptyOutputStream.INSTANCE);
} finally {
IoUtil.close(in);
IoUtil.closeQuietly(in);
}
return checksum;
}

View File

@@ -73,8 +73,8 @@ public class FileChannelCopier extends IoCopier<FileChannel, FileChannel> {
outChannel = out.getChannel();
return copy(inChannel, outChannel);
} finally {
IoUtil.close(outChannel);
IoUtil.close(inChannel);
IoUtil.closeQuietly(outChannel);
IoUtil.closeQuietly(inChannel);
}
}

View File

@@ -81,7 +81,7 @@ public class FileReader extends FileWrapper {
} catch (final Exception e) {
throw new IORuntimeException(e);
} finally {
IoUtil.close(in);
IoUtil.closeQuietly(in);
}
return bytes;
@@ -140,7 +140,7 @@ public class FileReader extends FileWrapper {
reader = FileUtil.getReader(file, charset);
IoUtil.readLines(reader, lineHandler);
} finally {
IoUtil.close(reader);
IoUtil.closeQuietly(reader);
}
}
@@ -177,7 +177,7 @@ public class FileReader extends FileWrapper {
throw new UtilException(e);
}
} finally {
IoUtil.close(reader);
IoUtil.closeQuietly(reader);
}
return result;
}
@@ -233,7 +233,7 @@ public class FileReader extends FileWrapper {
throw new IORuntimeException(e);
} finally{
if(isCloseOut){
IoUtil.close(out);
IoUtil.closeQuietly(out);
}
}
}

View File

@@ -201,7 +201,7 @@ public class FileTypeUtil {
in = IoUtil.toStream(file);
return getType(in, file.getName(),isExact);
} finally {
IoUtil.close(in);
IoUtil.closeQuietly(in);
}
}

View File

@@ -249,7 +249,7 @@ public class FileUtil extends PathUtil {
} catch (final IOException e) {
throw new IORuntimeException(StrUtil.format("Can not read file path of [{}]", path), e);
} finally {
IoUtil.close(jarFile);
IoUtil.closeQuietly(jarFile);
}
}
@@ -1247,8 +1247,8 @@ public class FileUtil extends PathUtil {
return IoUtil.contentEquals(input1, input2);
} finally {
IoUtil.close(input1);
IoUtil.close(input2);
IoUtil.closeQuietly(input1);
IoUtil.closeQuietly(input2);
}
}
@@ -1294,8 +1294,8 @@ public class FileUtil extends PathUtil {
input2 = getReader(file2, charset);
return IoUtil.contentEqualsIgnoreEOL(input1, input2);
} finally {
IoUtil.close(input1);
IoUtil.close(input2);
IoUtil.closeQuietly(input1);
IoUtil.closeQuietly(input2);
}
}
@@ -1676,7 +1676,7 @@ public class FileUtil extends PathUtil {
} catch (final IOException e) {
throw new IORuntimeException(e);
} finally {
IoUtil.close(in);
IoUtil.closeQuietly(in);
}
}
@@ -1796,7 +1796,7 @@ public class FileUtil extends PathUtil {
} catch (final IOException e) {
throw new IORuntimeException(e);
} finally {
IoUtil.close(in);
IoUtil.closeQuietly(in);
}
}

View File

@@ -130,7 +130,7 @@ public class FileWriter extends FileWrapper {
} catch (final IOException e) {
throw new IORuntimeException(e);
} finally {
IoUtil.close(writer);
IoUtil.closeQuietly(writer);
}
return file;
}
@@ -344,9 +344,9 @@ public class FileWriter extends FileWrapper {
} catch (final IOException e) {
throw new IORuntimeException(e);
} finally {
IoUtil.close(out);
IoUtil.closeQuietly(out);
if (isCloseIn) {
IoUtil.close(in);
IoUtil.closeQuietly(in);
}
}
return file;

View File

@@ -143,7 +143,7 @@ public class Tailer implements Serializable {
try{
this.executorService.shutdown();
} finally {
IoUtil.close(this.randomAccessFile);
IoUtil.closeQuietly(this.randomAccessFile);
}
}

View File

@@ -105,7 +105,7 @@ public class StreamReader {
IoUtil.copy(in, out, IoUtil.DEFAULT_BUFFER_SIZE, limit, null);
} finally {
if (closeAfterRead) {
IoUtil.close(in);
IoUtil.closeQuietly(in);
}
}
return out;

View File

@@ -57,7 +57,7 @@ public class StreamWriter {
throw new IORuntimeException(e);
} finally {
if (closeAfterWrite) {
IoUtil.close(out);
IoUtil.closeQuietly(out);
}
}
}
@@ -82,7 +82,7 @@ public class StreamWriter {
throw new IORuntimeException(e);
} finally {
if (closeAfterWrite) {
IoUtil.close(osw);
IoUtil.closeQuietly(osw);
}
}
}
@@ -108,7 +108,7 @@ public class StreamWriter {
throw new IORuntimeException(e);
} finally {
if (closeAfterWrite) {
IoUtil.close(osw);
IoUtil.closeQuietly(osw);
}
}
}

View File

@@ -87,7 +87,7 @@ public class SyncInputStream extends FilterInputStream {
// 忽略读取流中的EOF错误
}finally {
// 读取结束
IoUtil.close(in);
IoUtil.closeQuietly(in);
}
return copyLength;
}

View File

@@ -185,6 +185,6 @@ public class WatchServer extends Thread implements Closeable, Serializable {
@Override
public void close() {
isClosed = true;
IoUtil.close(watchService);
IoUtil.closeQuietly(watchService);
}
}

View File

@@ -701,7 +701,7 @@ public class NetUtil {
} catch (final IOException e) {
throw new IORuntimeException(e);
} finally {
IoUtil.close(out);
IoUtil.closeQuietly(out);
}
}

View File

@@ -230,7 +230,7 @@ public class UploadFile {
return false;
}
} finally {
IoUtil.close(out);
IoUtil.closeQuietly(out);
}
return true;
}

View File

@@ -145,7 +145,7 @@ public class RuntimeUtil {
in = process.getInputStream();
return IoUtil.readLines(in, charset, new ArrayList<>());
} finally {
IoUtil.close(in);
IoUtil.closeQuietly(in);
destroy(process);
}
}
@@ -175,7 +175,7 @@ public class RuntimeUtil {
in = process.getInputStream();
return IoUtil.read(in, charset);
} finally {
IoUtil.close(in);
IoUtil.closeQuietly(in);
destroy(process);
}
}
@@ -205,7 +205,7 @@ public class RuntimeUtil {
in = process.getErrorStream();
return IoUtil.read(in, charset);
} finally {
IoUtil.close(in);
IoUtil.closeQuietly(in);
destroy(process);
}
}

View File

@@ -169,7 +169,7 @@ public class XmlUtil {
in = FileUtil.getInputStream(file);
return readXML(in);
} finally {
IoUtil.close(in);
IoUtil.closeQuietly(in);
}
}
@@ -246,7 +246,7 @@ public class XmlUtil {
in = FileUtil.getInputStream(file);
readBySax(new InputSource(in), contentHandler);
} finally {
IoUtil.close(in);
IoUtil.closeQuietly(in);
}
}
@@ -262,7 +262,7 @@ public class XmlUtil {
try {
readBySax(new InputSource(reader), contentHandler);
} finally {
IoUtil.close(reader);
IoUtil.closeQuietly(reader);
}
}
@@ -278,7 +278,7 @@ public class XmlUtil {
try {
readBySax(new InputSource(source), contentHandler);
} finally {
IoUtil.close(source);
IoUtil.closeQuietly(source);
}
}
@@ -489,7 +489,7 @@ public class XmlUtil {
writer = FileUtil.getWriter(path, CharsetUtil.charset(charsetName), false);
write(doc, writer, charsetName, INDENT_DEFAULT);
} finally {
IoUtil.close(writer);
IoUtil.closeQuietly(writer);
}
}
@@ -833,7 +833,7 @@ public class XmlUtil {
xmlenc.writeObject(bean);
} finally {
// 关闭XMLEncoder会相应关闭OutputStream
IoUtil.close(xmlenc);
IoUtil.closeQuietly(xmlenc);
}
}