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 FileWriter} objects.
024 */
025public class FileWriterBuilder {
026
027   private File file;
028   private Charset cs = Charset.defaultCharset();
029   private boolean append, buffered;
030
031   /**
032    * Creates a new builder.
033    *
034    * @return A new builder.
035    */
036   public static FileWriterBuilder create() {
037      return new FileWriterBuilder();
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 FileWriterBuilder create(File file) {
047      return new FileWriterBuilder().file(file);
048   }
049
050   /**
051    * Creates a new builder initialized with the specified file path.
052    *
053    * @param path The file path being written to.
054    * @return A new builder.
055    */
056   public static FileWriterBuilder create(String path) {
057      return new FileWriterBuilder().file(path);
058   }
059
060   /**
061    * Sets the file being written to.
062    *
063    * @param file The file being written to.
064    * @return This object.
065    */
066   public FileWriterBuilder file(File file) {
067      this.file = file;
068      return this;
069   }
070
071   /**
072    * Sets the path of the file being written to.
073    *
074    * @param path The path of the file being written to.
075    * @return This object.
076    */
077   public FileWriterBuilder file(String path) {
078      this.file = new File(path);
079      return this;
080   }
081
082   /**
083    * Sets the character encoding of the file.
084    *
085    * @param cs
086    *    The character encoding.
087    *    The default is {@link Charset#defaultCharset()}.
088    * @return This object.
089    */
090   public FileWriterBuilder charset(Charset cs) {
091      this.cs = cs;
092      return this;
093   }
094
095   /**
096    * Sets the character encoding of the file.
097    *
098    * @param cs
099    *    The character encoding.
100    *    The default is {@link Charset#defaultCharset()}.
101    * @return This object.
102    */
103   public FileWriterBuilder charset(String cs) {
104      this.cs = Charset.forName(cs);
105      return this;
106   }
107
108   /**
109    * Sets the append mode on the writer to <jk>true</jk>.
110    *
111    * @return This object.
112    */
113   public FileWriterBuilder append() {
114      this.append = true;
115      return this;
116   }
117
118   /**
119    * Sets the buffer mode on the writer to <jk>true</jk>.
120    *
121    * @return This object.
122    */
123   public FileWriterBuilder buffered() {
124      this.buffered = true;
125      return this;
126   }
127
128   /**
129    * Creates a new File writer.
130    *
131    * @return A new File writer.
132    * @throws FileNotFoundException If file could not be found.
133    */
134   public Writer build() throws FileNotFoundException {
135      OutputStream os = new FileOutputStream(file, append);
136      if (buffered)
137         os = new BufferedOutputStream(os);
138      return new OutputStreamWriter(os, cs);
139   }
140}