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