001// ***************************************************************************************************************************
002// * Licensed to the Apache Software Foundation (ASF) under one or more contributor license agreements.  See the NOTICE file *
003// * distributed with this work for additional information regarding copyright ownership.  The ASF licenses this file        *
004// * to you under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance            *
005// * with the License.  You may obtain a copy of the License at                                                              *
006// *                                                                                                                         *
007// *  http://www.apache.org/licenses/LICENSE-2.0                                                                             *
008// *                                                                                                                         *
009// * Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an  *
010// * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.  See the License for the        *
011// * specific language governing permissions and limitations under the License.                                              *
012// ***************************************************************************************************************************
013package org.apache.juneau.common.internal;
014
015import java.io.*;
016import java.nio.charset.*;
017
018/**
019 * Utility class for creating {@link FileReader} objects.
020 */
021public final class FileReaderBuilder {
022
023   private File file;
024   private Charset cs = Charset.defaultCharset();
025   private boolean allowNoFile;
026
027   /**
028    * Creates a new builder.
029    *
030    * @return A new builder.
031    */
032   public static FileReaderBuilder create() {
033      return new FileReaderBuilder();
034   }
035
036   /**
037    * Creates a new builder initialized with the specified file.
038    *
039    * @param file The file being written to.
040    * @return A new builder.
041    */
042   public static FileReaderBuilder create(File file) {
043      return new FileReaderBuilder().file(file);
044   }
045
046   /**
047    * Sets the file being written from.
048    *
049    * @param file The file being written from.
050    * @return This object.
051    */
052   public FileReaderBuilder file(File file) {
053      this.file = file;
054      return this;
055   }
056
057   /**
058    * Sets the path of the file being written from.
059    *
060    * @param path The path of the file being written from.
061    * @return This object.
062    */
063   public FileReaderBuilder file(String path) {
064      this.file = new File(path);
065      return this;
066   }
067
068   /**
069    * Sets the character encoding of the file.
070    *
071    * @param cs
072    *    The character encoding.
073    *    The default is {@link Charset#defaultCharset()}.
074    * @return This object.
075    */
076   public FileReaderBuilder charset(Charset cs) {
077      this.cs = cs;
078      return this;
079   }
080
081   /**
082    * Sets the character encoding of the file.
083    *
084    * @param cs
085    *    The character encoding.
086    *    The default is {@link Charset#defaultCharset()}.
087    * @return This object.
088    */
089   public FileReaderBuilder charset(String cs) {
090      this.cs = Charset.forName(cs);
091      return this;
092   }
093
094   /**
095    * If called and the file is <jk>null</jk> or non-existent, then the {@link #build()} command will return an empty
096    * reader instead of a {@link FileNotFoundException}.
097    *
098    * @return This object.
099    */
100   public FileReaderBuilder allowNoFile() {
101      this.allowNoFile = true;
102      return this;
103   }
104
105   /**
106    * Creates a new File reader.
107    *
108    * @return A new File reader.
109    * @throws FileNotFoundException If file could not be found.
110    */
111   public Reader build() throws FileNotFoundException {
112      if (allowNoFile && (file == null || ! file.exists()))
113         return new StringReader("");
114      return new InputStreamReader(new FileInputStream(file), cs);
115   }
116}