View Javadoc
1   /**
2    * BSD-style license; for more info see http://pmd.sourceforge.net/license.html
3    */
4   package net.sourceforge.pmd.util;
5   
6   import java.io.BufferedReader;
7   import java.io.BufferedWriter;
8   import java.io.FileWriter;
9   import java.io.IOException;
10  import java.io.OutputStreamWriter;
11  import java.io.Reader;
12  import java.io.Writer;
13  
14  /**
15   * 
16   * @author Brian Remedios
17   */
18  public final class IOUtil {
19  
20      private IOUtil() {
21      }
22  
23      public static Writer createWriter() {
24          return new OutputStreamWriter(System.out);
25      }
26  
27      public static Writer createWriter(String reportFile) {
28          try {
29              return StringUtil.isEmpty(reportFile) ? createWriter() : new BufferedWriter(new FileWriter(reportFile));
30          } catch (IOException e) {
31              throw new IllegalArgumentException(e);
32          }
33      }
34  
35      public static Reader skipBOM(Reader source) {
36          Reader in = new BufferedReader(source);
37          try {
38              in.mark(1);
39              int firstCharacter = in.read();
40              if (firstCharacter != '\ufeff') {
41                  in.reset();
42              }
43          } catch (IOException e) {
44              throw new RuntimeException("Error while trying to skip BOM marker", e);
45          }
46          return in;
47      }
48  }