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  import net.sourceforge.pmd.lang.ast.ParseException;
15  
16  /**
17   * 
18   * @author Brian Remedios
19   */
20  public class IOUtil {
21  
22      private IOUtil() {
23      }
24  
25      public static Writer createWriter() {
26          return new OutputStreamWriter(System.out);
27      }
28  
29      public static Writer createWriter(String reportFile) {
30          try {
31              return StringUtil.isEmpty(reportFile) ? createWriter() : new BufferedWriter(new FileWriter(reportFile));
32          } catch (IOException e) {
33              throw new IllegalArgumentException(e);
34          }
35      }
36  
37      public static Reader skipBOM(Reader source) {
38          Reader in = new BufferedReader(source);
39          try {
40              in.mark(1);
41              int firstCharacter = in.read();
42              if (firstCharacter != '\ufeff') {
43                  in.reset();
44              }
45          } catch (IOException e) {
46              throw new RuntimeException("Error while trying to skip BOM marker", e);
47          }
48          return in;
49      }
50  }