Skip to content

Commit 8f2e988

Browse files
author
Niall Pemberton
committed
IO-247 Add closeQuietly() examples to the javadoc
git-svn-id: https://svn.apache.org/repos/asf/commons/proper/io/trunk@995152 13f79535-47bb-0310-9956-ffa450edef68
1 parent 6cc7234 commit 8f2e988

1 file changed

Lines changed: 105 additions & 1 deletion

File tree

src/java/org/apache/commons/io/IOUtils.java

Lines changed: 105 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -149,7 +149,22 @@ public IOUtils() {
149149
* <p>
150150
* Equivalent to {@link Reader#close()}, except any exceptions will be ignored.
151151
* This is typically used in finally blocks.
152-
*
152+
* <p>
153+
* Example code:
154+
* <pre>
155+
* char[] data = new char[1024];
156+
* Reader in = null;
157+
* try {
158+
* in = new FileReader("foo.txt");
159+
* in.read(data);
160+
* in.close(); //close errors are handled
161+
* } catch (Exception e) {
162+
* // error handling
163+
* } finally {
164+
* IOUtils.closeQuietly(in);
165+
* }
166+
* </pre>
167+
*
153168
* @param input the Reader to close, may be null or already closed
154169
*/
155170
public static void closeQuietly(Reader input) {
@@ -161,6 +176,22 @@ public static void closeQuietly(Reader input) {
161176
* <p>
162177
* Equivalent to {@link Channel#close()}, except any exceptions will be ignored.
163178
* This is typically used in finally blocks.
179+
* <p>
180+
* Example code:
181+
* <pre>
182+
* ByteBuffer buffer = ByteBuffer.allocate(1024);
183+
* FileChannel channel = null;
184+
* try {
185+
* FileInputStream in = new FileInputStream("foo.txt");
186+
* channel = in.getChannel();
187+
* channel.read(buffer);
188+
* channel.close(); //close errors are handled
189+
* } catch (Exception e) {
190+
* // error handling
191+
* } finally {
192+
* IOUtils.closeQuietly(channel);
193+
* }
194+
* </pre>
164195
*
165196
* @param channel the Channel to close, may be null or already closed
166197
*/
@@ -173,6 +204,20 @@ public static void closeQuietly(Channel channel) {
173204
* <p>
174205
* Equivalent to {@link Writer#close()}, except any exceptions will be ignored.
175206
* This is typically used in finally blocks.
207+
* <p>
208+
* Example code:
209+
* <pre>
210+
* Writer out = null;
211+
* try {
212+
* out = new StringWriter();
213+
* out.write("Hello World");
214+
* out.close(); //close errors are handled
215+
* } catch (Exception e) {
216+
* // error handling
217+
* } finally {
218+
* IOUtils.closeQuietly(out);
219+
* }
220+
* </pre>
176221
*
177222
* @param output the Writer to close, may be null or already closed
178223
*/
@@ -185,6 +230,21 @@ public static void closeQuietly(Writer output) {
185230
* <p>
186231
* Equivalent to {@link InputStream#close()}, except any exceptions will be ignored.
187232
* This is typically used in finally blocks.
233+
* <p>
234+
* Example code:
235+
* <pre>
236+
* byte[] data = new byte[1024];
237+
* InputStream in = null;
238+
* try {
239+
* in = new FileInputStream("foo.txt");
240+
* in.read(data);
241+
* in.close(); //close errors are handled
242+
* } catch (Exception e) {
243+
* // error handling
244+
* } finally {
245+
* IOUtils.closeQuietly(in);
246+
* }
247+
* </pre>
188248
*
189249
* @param input the InputStream to close, may be null or already closed
190250
*/
@@ -197,6 +257,22 @@ public static void closeQuietly(InputStream input) {
197257
* <p>
198258
* Equivalent to {@link OutputStream#close()}, except any exceptions will be ignored.
199259
* This is typically used in finally blocks.
260+
* <p>
261+
* Example code:
262+
* <pre>
263+
* byte[] data = "Hello, World".getBytes();
264+
*
265+
* OutputStream out = null;
266+
* try {
267+
* out = new FileOutputStream("foo.txt");
268+
* out.write(data);
269+
* out.close(); //close errors are handled
270+
* } catch (IOException e) {
271+
* // error handling
272+
* } finally {
273+
* IOUtils.closeQuietly(out);
274+
* }
275+
* </pre>
200276
*
201277
* @param output the OutputStream to close, may be null or already closed
202278
*/
@@ -209,6 +285,20 @@ public static void closeQuietly(OutputStream output) {
209285
* <p>
210286
* Equivalent to {@link Closeable#close()}, except any exceptions will be ignored.
211287
* This is typically used in finally blocks.
288+
* <p>
289+
* Example code:
290+
* <pre>
291+
* Closeable closeable = null;
292+
* try {
293+
* closeable = new FileReader("foo.txt");
294+
* // process closeable
295+
* closeable.close();
296+
* } catch (Exception e) {
297+
* // error handling
298+
* } finally {
299+
* IOUtils.closeQuietly(closeable);
300+
* }
301+
* </pre>
212302
*
213303
* @param closeable the object to close, may be null or already closed
214304
*/
@@ -227,6 +317,20 @@ public static void closeQuietly(Closeable closeable) {
227317
* <p>
228318
* Equivalent to {@link Socket#close()}, except any exceptions will be ignored.
229319
* This is typically used in finally blocks.
320+
* <p>
321+
* Example code:
322+
* <pre>
323+
* Socket socket = null;
324+
* try {
325+
* socket = new Socket("http://www.foo.com/", 80);
326+
* // process socket
327+
* socket.close();
328+
* } catch (Exception e) {
329+
* // error handling
330+
* } finally {
331+
* IOUtils.closeQuietly(socket);
332+
* }
333+
* </pre>
230334
*
231335
* @param sock the Socket to close, may be null or already closed
232336
*/

0 commit comments

Comments
 (0)