View Javadoc
1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one
3    * or more contributor license agreements.  See the NOTICE file
4    * distributed with this work for additional information
5    * regarding copyright ownership.  The ASF licenses this file
6    * to you under the Apache License, Version 2.0 (the
7    * "License"); you may not use this file except in compliance
8    * with the License.  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,
13   * software distributed under the License is distributed on an
14   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15   * KIND, either express or implied.  See the License for the
16   * specific language governing permissions and limitations
17   * under the License.
18   */
19  package org.eclipse.aether.internal.impl;
20  
21  import javax.inject.Named;
22  import javax.inject.Singleton;
23  
24  import java.io.*;
25  import java.nio.ByteBuffer;
26  import java.nio.charset.StandardCharsets;
27  import java.nio.file.Files;
28  import java.nio.file.Path;
29  import java.nio.file.StandardCopyOption;
30  
31  import org.eclipse.aether.spi.io.PathProcessor;
32  import org.eclipse.aether.util.FileUtils;
33  
34  /**
35   * A utility class helping with file-based operations.
36   */
37  @Singleton
38  @Named
39  public class DefaultPathProcessor implements PathProcessor {
40      @Override
41      public void write(Path target, String data) throws IOException {
42          FileUtils.writeFile(target, p -> Files.write(p, data.getBytes(StandardCharsets.UTF_8)));
43      }
44  
45      @Override
46      public void write(Path target, InputStream source) throws IOException {
47          FileUtils.writeFile(target, p -> Files.copy(source, p, StandardCopyOption.REPLACE_EXISTING));
48      }
49  
50      @Override
51      public void copy(Path source, Path target) throws IOException {
52          copy(source, target, null);
53      }
54  
55      @Override
56      public long copy(Path source, Path target, ProgressListener listener) throws IOException {
57          try (InputStream in = new BufferedInputStream(Files.newInputStream(source));
58                  FileUtils.CollocatedTempFile tempTarget = FileUtils.newTempFile(target);
59                  OutputStream out = new BufferedOutputStream(Files.newOutputStream(tempTarget.getPath()))) {
60              long result = copy(out, in, listener);
61              tempTarget.move();
62              return result;
63          }
64      }
65  
66      private long copy(OutputStream os, InputStream is, ProgressListener listener) throws IOException {
67          long total = 0L;
68          byte[] buffer = new byte[1024 * 32];
69          while (true) {
70              int bytes = is.read(buffer);
71              if (bytes < 0) {
72                  break;
73              }
74  
75              os.write(buffer, 0, bytes);
76  
77              total += bytes;
78  
79              if (listener != null && bytes > 0) {
80                  try {
81                      listener.progressed(ByteBuffer.wrap(buffer, 0, bytes));
82                  } catch (Exception e) {
83                      // too bad
84                  }
85              }
86          }
87  
88          return total;
89      }
90  
91      @Override
92      public void move(Path source, Path target) throws IOException {
93          Files.move(source, target, StandardCopyOption.REPLACE_EXISTING, StandardCopyOption.COPY_ATTRIBUTES);
94      }
95  }