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.io.StringWriter;
23  import java.nio.charset.Charset;
24  import java.nio.file.Files;
25  import java.nio.file.Path;
26  import java.util.Arrays;
27  import java.util.Objects;
28  
29  /**
30   * Caching Writer to avoid overwriting a file with
31   * the same content.
32   */
33  public class CachingWriter extends OutputStreamWriter
34  {
35      private final CachingOutputStream cos;
36  
37      public CachingWriter( File path, Charset charset ) throws IOException
38      {
39          this( Objects.requireNonNull( path ).toPath(), charset );
40      }
41  
42      public CachingWriter( Path path, Charset charset ) throws IOException
43      {
44          this( path, charset, 32 * 1024 );
45      }
46  
47      public CachingWriter( Path path, Charset charset, int bufferSize ) throws IOException
48      {
49          this( new CachingOutputStream( path, bufferSize ), charset );
50      }
51  
52      private CachingWriter( CachingOutputStream outputStream, Charset charset ) throws IOException
53      {
54          super( outputStream, charset );
55          this.cos = outputStream;
56      }
57  
58      public boolean isModified()
59      {
60          return cos.isModified();
61      }
62  }