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