IOUtils.java 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521
  1. /*
  2. * Licensed to the Apache Software Foundation (ASF) under one or more
  3. * contributor license agreements. See the NOTICE file distributed with
  4. * this work for additional information regarding copyright ownership.
  5. * The ASF licenses this file to You under the Apache License, Version 2.0
  6. * (the "License"); you may not use this file except in compliance with
  7. * the License. You may obtain a copy of the License at
  8. *
  9. * http://www.apache.org/licenses/LICENSE-2.0
  10. *
  11. * Unless required by applicable law or agreed to in writing, software
  12. * distributed under the License is distributed on an "AS IS" BASIS,
  13. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  14. * See the License for the specific language governing permissions and
  15. * limitations under the License.
  16. */
  17. package com.baomidou.mybatisplus.toolkit;
  18. import java.io.Closeable;
  19. import java.io.IOException;
  20. import java.io.InputStream;
  21. import java.io.OutputStream;
  22. import java.io.Reader;
  23. import java.io.Writer;
  24. import java.net.HttpURLConnection;
  25. import java.net.ServerSocket;
  26. import java.net.Socket;
  27. import java.net.URLConnection;
  28. import java.nio.channels.Selector;
  29. import java.sql.Connection;
  30. import java.sql.ResultSet;
  31. import java.sql.Statement;
  32. import org.apache.ibatis.logging.Log;
  33. import org.apache.ibatis.logging.LogFactory;
  34. /**
  35. * <p>
  36. * IOUtils Copy org.apache.commons.io.IOUtils
  37. * </p>
  38. *
  39. * @author Caratacus
  40. * @Date 2016-11-23
  41. */
  42. public class IOUtils {
  43. private static final Log logger = LogFactory.getLog(IOUtils.class);
  44. private IOUtils() {
  45. }
  46. /**
  47. * Closes a URLConnection.
  48. *
  49. * @param conn the connection to close.
  50. * @since 2.4
  51. */
  52. public static void close(final URLConnection conn) {
  53. if (conn instanceof HttpURLConnection) {
  54. ((HttpURLConnection) conn).disconnect();
  55. }
  56. }
  57. /**
  58. * Closes an <code>Reader</code> unconditionally.
  59. * <p>
  60. * Equivalent to {@link Reader#close()}, except any exceptions will be ignored. This is typically used in finally
  61. * blocks.
  62. * <p>
  63. * Example code:
  64. * <p>
  65. * <pre>
  66. * char[] data = new char[1024];
  67. * Reader in = null;
  68. * try {
  69. * in = new FileReader(&quot;foo.txt&quot;);
  70. * in.read(data);
  71. * in.close(); // close errors are handled
  72. * } catch (Exception e) {
  73. * // error handling
  74. * } finally {
  75. * IOUtils.closeQuietly(in);
  76. * }
  77. * </pre>
  78. *
  79. * @param input the Reader to close, may be null or already closed
  80. */
  81. public static void closeQuietly(final Reader input) {
  82. closeQuietly((Closeable) input);
  83. }
  84. /**
  85. * Closes an <code>Writer</code> unconditionally.
  86. * <p>
  87. * Equivalent to {@link Writer#close()}, except any exceptions will be ignored. This is typically used in finally
  88. * blocks.
  89. * <p>
  90. * Example code:
  91. * <p>
  92. * <pre>
  93. * Writer out = null;
  94. * try {
  95. * out = new StringWriter();
  96. * out.write(&quot;Hello World&quot;);
  97. * out.close(); // close errors are handled
  98. * } catch (Exception e) {
  99. * // error handling
  100. * } finally {
  101. * IOUtils.closeQuietly(out);
  102. * }
  103. * </pre>
  104. *
  105. * @param output the Writer to close, may be null or already closed
  106. */
  107. public static void closeQuietly(final Writer output) {
  108. closeQuietly((Closeable) output);
  109. }
  110. /**
  111. * Closes an <code>InputStream</code> unconditionally.
  112. * <p>
  113. * Equivalent to {@link InputStream#close()}, except any exceptions will be ignored. This is typically used in
  114. * finally blocks.
  115. * <p>
  116. * Example code:
  117. * <p>
  118. * <pre>
  119. * byte[] data = new byte[1024];
  120. * InputStream in = null;
  121. * try {
  122. * in = new FileInputStream(&quot;foo.txt&quot;);
  123. * in.read(data);
  124. * in.close(); // close errors are handled
  125. * } catch (Exception e) {
  126. * // error handling
  127. * } finally {
  128. * IOUtils.closeQuietly(in);
  129. * }
  130. * </pre>
  131. *
  132. * @param input the InputStream to close, may be null or already closed
  133. */
  134. public static void closeQuietly(final InputStream input) {
  135. closeQuietly((Closeable) input);
  136. }
  137. /**
  138. * Closes an <code>OutputStream</code> unconditionally.
  139. * <p>
  140. * Equivalent to {@link OutputStream#close()}, except any exceptions will be ignored. This is typically used in
  141. * finally blocks.
  142. * <p>
  143. * Example code:
  144. * <p>
  145. * <pre>
  146. * byte[] data = &quot;Hello, World&quot;.getBytes();
  147. *
  148. * OutputStream out = null;
  149. * try {
  150. * out = new FileOutputStream(&quot;foo.txt&quot;);
  151. * out.write(data);
  152. * out.close(); // close errors are handled
  153. * } catch (IOException e) {
  154. * // error handling
  155. * } finally {
  156. * IOUtils.closeQuietly(out);
  157. * }
  158. * </pre>
  159. *
  160. * @param output the OutputStream to close, may be null or already closed
  161. */
  162. public static void closeQuietly(final OutputStream output) {
  163. closeQuietly((Closeable) output);
  164. }
  165. /**
  166. * Closes a <code>Closeable</code> unconditionally.
  167. * <p>
  168. * Equivalent to {@link Closeable#close()}, except any exceptions will be ignored. This is typically used in finally
  169. * blocks.
  170. * <p>
  171. * Example code:
  172. * </p>
  173. * <p>
  174. * <pre>
  175. * Closeable closeable = null;
  176. * try {
  177. * closeable = new FileReader(&quot;foo.txt&quot;);
  178. * // process closeable
  179. * closeable.close();
  180. * } catch (Exception e) {
  181. * // error handling
  182. * } finally {
  183. * IOUtils.closeQuietly(closeable);
  184. * }
  185. * </pre>
  186. * <p>
  187. * Closing all streams:
  188. * </p>
  189. * <p>
  190. * <pre>
  191. * try {
  192. * return IOUtils.copy(inputStream, outputStream);
  193. * } finally {
  194. * IOUtils.closeQuietly(inputStream);
  195. * IOUtils.closeQuietly(outputStream);
  196. * }
  197. * </pre>
  198. *
  199. * @param closeable the objects to close, may be null or already closed
  200. * @since 2.0
  201. */
  202. public static void closeQuietly(final Closeable closeable) {
  203. try {
  204. if (closeable != null) {
  205. closeable.close();
  206. }
  207. } catch (final IOException ioe) {
  208. logger.error("error close io", ioe);
  209. }
  210. }
  211. /**
  212. * Closes a <code>Closeable</code> unconditionally.
  213. * <p>
  214. * Equivalent to {@link Closeable#close()}, except any exceptions will be ignored.
  215. * <p>
  216. * This is typically used in finally blocks to ensure that the closeable is closed even if an Exception was thrown
  217. * before the normal close statement was reached. <br>
  218. * <b>It should not be used to replace the close statement(s) which should be present for the non-exceptional
  219. * case.</b> <br>
  220. * It is only intended to simplify tidying up where normal processing has already failed and reporting close failure
  221. * as well is not necessary or useful.
  222. * <p>
  223. * Example code:
  224. * </p>
  225. * <p>
  226. * <pre>
  227. * Closeable closeable = null;
  228. * try {
  229. * closeable = new FileReader(&quot;foo.txt&quot;);
  230. * // processing using the closeable; may throw an Exception
  231. * closeable.close(); // Normal close - exceptions not ignored
  232. * } catch (Exception e) {
  233. * // error handling
  234. * } finally {
  235. * <b>IOUtils.closeQuietly(closeable); // In case normal close was skipped due to Exception</b>
  236. * }
  237. * </pre>
  238. * <p>
  239. * Closing all streams: <br>
  240. * <p>
  241. * <pre>
  242. * try {
  243. * return IOUtils.copy(inputStream, outputStream);
  244. * } finally {
  245. * IOUtils.closeQuietly(inputStream, outputStream);
  246. * }
  247. * </pre>
  248. *
  249. * @param closeables the objects to close, may be null or already closed
  250. * @see #closeQuietly(Closeable)
  251. * @since 2.5
  252. */
  253. public static void closeQuietly(final Closeable... closeables) {
  254. if (closeables == null) {
  255. return;
  256. }
  257. for (final Closeable closeable : closeables) {
  258. closeQuietly(closeable);
  259. }
  260. }
  261. /**
  262. * Closes a <code>Socket</code> unconditionally.
  263. * <p>
  264. * Equivalent to {@link Socket#close()}, except any exceptions will be ignored. This is typically used in finally
  265. * blocks.
  266. * <p>
  267. * Example code:
  268. * <p>
  269. * <pre>
  270. * Socket socket = null;
  271. * try {
  272. * socket = new Socket(&quot;http://www.foo.com/&quot;, 80);
  273. * // process socket
  274. * socket.close();
  275. * } catch (Exception e) {
  276. * // error handling
  277. * } finally {
  278. * IOUtils.closeQuietly(socket);
  279. * }
  280. * </pre>
  281. *
  282. * @param sock the Socket to close, may be null or already closed
  283. * @since 2.0
  284. */
  285. public static void closeQuietly(final Socket sock) {
  286. if (sock != null) {
  287. try {
  288. sock.close();
  289. } catch (final IOException ioe) {
  290. logger.error("error close io", ioe);
  291. }
  292. }
  293. }
  294. /**
  295. * Closes a <code>Selector</code> unconditionally.
  296. * <p>
  297. * Equivalent to {@link Selector#close()}, except any exceptions will be ignored. This is typically used in finally
  298. * blocks.
  299. * <p>
  300. * Example code:
  301. * <p>
  302. * <pre>
  303. * Selector selector = null;
  304. * try {
  305. * selector = Selector.open();
  306. * // process socket
  307. *
  308. * } catch (Exception e) {
  309. * // error handling
  310. * } finally {
  311. * IOUtils.closeQuietly(selector);
  312. * }
  313. * </pre>
  314. *
  315. * @param selector the Selector to close, may be null or already closed
  316. * @since 2.2
  317. */
  318. public static void closeQuietly(final Selector selector) {
  319. if (selector != null) {
  320. try {
  321. selector.close();
  322. } catch (final IOException ioe) {
  323. logger.error("error close io", ioe);
  324. }
  325. }
  326. }
  327. /**
  328. * Closes a <code>ServerSocket</code> unconditionally.
  329. * <p>
  330. * Equivalent to {@link ServerSocket#close()}, except any exceptions will be ignored. This is typically used in
  331. * finally blocks.
  332. * <p>
  333. * Example code:
  334. * <p>
  335. * <pre>
  336. * ServerSocket socket = null;
  337. * try {
  338. * socket = new ServerSocket();
  339. * // process socket
  340. * socket.close();
  341. * } catch (Exception e) {
  342. * // error handling
  343. * } finally {
  344. * IOUtils.closeQuietly(socket);
  345. * }
  346. * </pre>
  347. *
  348. * @param sock the ServerSocket to close, may be null or already closed
  349. * @since 2.2
  350. */
  351. public static void closeQuietly(final ServerSocket sock) {
  352. if (sock != null) {
  353. try {
  354. sock.close();
  355. } catch (final IOException ioe) {
  356. logger.error("error close io", ioe);
  357. }
  358. }
  359. }
  360. /**
  361. * Closes a <code>Connection</code> unconditionally.
  362. * <p>
  363. * Equivalent to {@link Connection#close()}, except any exceptions will be ignored. This is typically used in
  364. * finally blocks.
  365. * <p>
  366. * Example code:
  367. * <p>
  368. * <pre>
  369. * Connection conn = null;
  370. * try {
  371. * conn = new Connection();
  372. * // process close
  373. * conn.close();
  374. * } catch (Exception e) {
  375. * // error handling
  376. * } finally {
  377. * IOUtils.closeQuietly(conn);
  378. * }
  379. * </pre>
  380. *
  381. * @param conn the Connection to close, may be null or already closed
  382. * @since 2.2
  383. */
  384. public static void closeQuietly(final Connection conn) {
  385. if (conn != null) {
  386. try {
  387. conn.close();
  388. } catch (Exception e) {
  389. logger.error("error close conn", e);
  390. }
  391. }
  392. }
  393. /**
  394. * Closes a <code>AutoCloseable</code> unconditionally.
  395. * <p>
  396. * Equivalent to {@link ResultSet#close()}, except any exceptions will be ignored. This is typically used in finally
  397. * blocks.
  398. * <p>
  399. * Example code:
  400. * <p>
  401. * <pre>
  402. * AutoCloseable statement = null;
  403. * try {
  404. * statement = new Connection();
  405. * // process close
  406. * statement.close();
  407. * } catch (Exception e) {
  408. * // error handling
  409. * } finally {
  410. * IOUtils.closeQuietly(conn);
  411. * }
  412. * </pre>
  413. *
  414. * @param resultSet the Connection to close, may be null or already closed
  415. * @since 2.2
  416. */
  417. public static void closeQuietly(final ResultSet resultSet) {
  418. if (resultSet != null) {
  419. try {
  420. resultSet.close();
  421. } catch (Exception e) {
  422. logger.error("error close resultSet", e);
  423. }
  424. }
  425. }
  426. /**
  427. * Closes a <code>AutoCloseable</code> unconditionally.
  428. * <p>
  429. * Equivalent to {@link Statement#close()}, except any exceptions will be ignored. This is typically used in finally
  430. * blocks.
  431. * <p>
  432. * Example code:
  433. * <p>
  434. * <pre>
  435. * AutoCloseable statement = null;
  436. * try {
  437. * statement = new Connection();
  438. * // process close
  439. * statement.close();
  440. * } catch (Exception e) {
  441. * // error handling
  442. * } finally {
  443. * IOUtils.closeQuietly(conn);
  444. * }
  445. * </pre>
  446. *
  447. * @param statement the Connection to close, may be null or already closed
  448. * @since 2.2
  449. */
  450. public static void closeQuietly(final Statement statement) {
  451. if (statement != null) {
  452. try {
  453. statement.close();
  454. } catch (Exception e) {
  455. logger.error("error close statement", e);
  456. }
  457. }
  458. }
  459. /**
  460. * Closes a <code>AutoCloseable</code> unconditionally.
  461. * <p>
  462. * Equivalent to {@link AutoCloseable#close()}, except any exceptions will be ignored.
  463. * <p>
  464. * This is typically used in finally blocks to ensure that the closeable is closed even if an Exception was thrown
  465. * before the normal close statement was reached. <br>
  466. * <b>It should not be used to replace the close statement(s) which should be present for the non-exceptional
  467. * case.</b> <br>
  468. * It is only intended to simplify tidying up where normal processing has already failed and reporting close failure
  469. * as well is not necessary or useful.
  470. * <p>
  471. * Example code:
  472. * </p>
  473. * <p>
  474. * <pre>
  475. * AutoCloseable closeable = null;
  476. * try {
  477. * closeable = new AutoCloseable();
  478. * // processing using the closeable; may throw an Exception
  479. * closeable.close(); // Normal close - exceptions not ignored
  480. * } catch (Exception e) {
  481. * // error handling
  482. * } finally {
  483. * <b>IOUtils.closeQuietly(closeable); // In case normal close was skipped due to Exception</b>
  484. * }
  485. * </pre>
  486. * <p>
  487. * Closing all streams: <br>
  488. *
  489. * @param statements the objects to close, may be null or already closed
  490. * @see #closeQuietly(Statement)
  491. * @since 2.5
  492. */
  493. public static void closeQuietly(final Statement... statements) {
  494. if (statements == null) {
  495. return;
  496. }
  497. for (final Statement statement : statements) {
  498. closeQuietly(statement);
  499. }
  500. }
  501. }