View Javadoc
1   package org.codehaus.plexus.util.io;
2   
3   /*
4    * Copyright The Codehaus Foundation.
5    *
6    * Licensed under the Apache License, Version 2.0 (the "License");
7    * you may not use this file except in compliance with the License.
8    * You may obtain a copy of the License at
9    *
10   *     http://www.apache.org/licenses/LICENSE-2.0
11   *
12   * Unless required by applicable law or agreed to in writing, software
13   * distributed under the License is distributed on an "AS IS" BASIS,
14   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15   * See the License for the specific language governing permissions and
16   * limitations under the License.
17   */
18  
19  import java.io.File;
20  import java.io.IOException;
21  import java.io.OutputStreamWriter;
22  import java.nio.charset.Charset;
23  import java.nio.file.Path;
24  import java.util.Objects;
25  
26  /**
27   * Caching Writer to avoid overwriting a file with
28   * the same content.
29   */
30  public class CachingWriter extends OutputStreamWriter {
31      private final CachingOutputStream cos;
32  
33      public CachingWriter(File path, Charset charset) throws IOException {
34          this(Objects.requireNonNull(path).toPath(), charset);
35      }
36  
37      public CachingWriter(Path path, Charset charset) throws IOException {
38          this(path, charset, 32 * 1024);
39      }
40  
41      public CachingWriter(Path path, Charset charset, int bufferSize) throws IOException {
42          this(new CachingOutputStream(path, bufferSize), charset);
43      }
44  
45      private CachingWriter(CachingOutputStream outputStream, Charset charset) throws IOException {
46          super(outputStream, charset);
47          this.cos = outputStream;
48      }
49  
50      public boolean isModified() {
51          return cos.isModified();
52      }
53  }