View Javadoc

1   /*
2    * Copyright (C) 2006  Tom Gibara
3    *
4    * This library is free software; you can redistribute it and/or
5    * modify it under the terms of the GNU Lesser General Public
6    * License as published by the Free Software Foundation; either
7    * version 2.1 of the License, or (at your option) any later version.
8    *
9    * This library is distributed in the hope that it will be useful,
10   * but WITHOUT ANY WARRANTY; without even the implied warranty of
11   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
12   * Lesser General Public License for more details.
13   *
14   * You should have received a copy of the GNU Lesser General Public
15   * License along with this library; if not, write to the Free Software
16   * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
17   */
18  package com.tomgibara.pronto.util;
19  
20  import java.io.IOException;
21  import java.io.Reader;
22  import java.io.Writer;
23  
24  /**
25   * A collection of static methods for handling readers and writers.
26   * 
27   * @author Tom Gibara
28   * 
29   */
30  
31  public final class CharStreams {
32  
33      /**
34       * Attempts to close the supplied reader. If a null reader is supplied, no
35       * action is taken. If an IOException occurs while closing the reader, the
36       * exception is silently ignored.
37       * 
38       * @param reader
39       *            a Reader instance or null
40       */
41  
42      public static void safeClose(final Reader reader) {
43          try {
44              if (reader != null) reader.close();
45          } catch (IOException e) {
46              /* ignore this exception */
47          }
48      }
49  
50      /**
51       * Attempts to close the supplied writer. If a null writer is supplied, no
52       * action is taken. If an IOException occurs while closing the writer, the
53       * exception is silently ignored.
54       * 
55       * @param writer
56       *            a Writer instance or null
57       */
58  
59      public static void safeClose(final Writer writer) {
60          try {
61              if (writer != null) writer.close();
62          } catch (IOException e) {
63              /* ignore this exception */
64          }
65      }
66  
67      private CharStreams() {
68      }
69  
70  }