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.BufferedInputStream;
25  import java.io.BufferedOutputStream;
26  import java.io.IOException;
27  import java.io.InputStream;
28  import java.io.OutputStream;
29  import java.nio.ByteBuffer;
30  import java.nio.charset.StandardCharsets;
31  import java.nio.file.AccessDeniedException;
32  import java.nio.file.FileSystemException;
33  import java.nio.file.Files;
34  import java.nio.file.Path;
35  import java.nio.file.StandardCopyOption;
36  import java.nio.file.attribute.FileTime;
37  
38  import org.eclipse.aether.spi.io.PathProcessor;
39  import org.eclipse.aether.util.FileUtils;
40  import org.slf4j.Logger;
41  import org.slf4j.LoggerFactory;
42  
43  /**
44   * A utility class helping with file-based operations.
45   */
46  @Singleton
47  @Named
48  public class DefaultPathProcessor implements PathProcessor {
49      private final Logger logger = LoggerFactory.getLogger(getClass());
50  
51      @Override
52      public void setLastModified(Path path, long value) throws IOException {
53          try {
54              Files.setLastModifiedTime(path, FileTime.fromMillis(value));
55          } catch (FileSystemException e) {
56              // MRESOLVER-536: Java uses generic FileSystemException for some weird cases,
57              // but some subclasses like AccessDeniedEx should be re-thrown
58              if (e instanceof AccessDeniedException) {
59                  throw e;
60              }
61              logger.trace("Failed to set last modified date: {}", path, e);
62          }
63      }
64  
65      @Override
66      public void write(Path target, String data) throws IOException {
67          FileUtils.writeFile(target, p -> Files.write(p, data.getBytes(StandardCharsets.UTF_8)));
68      }
69  
70      @Override
71      public void write(Path target, InputStream source) throws IOException {
72          FileUtils.writeFile(target, p -> Files.copy(source, p, StandardCopyOption.REPLACE_EXISTING));
73      }
74  
75      @Override
76      public long copy(Path source, Path target, ProgressListener listener) throws IOException {
77          try (InputStream in = new BufferedInputStream(Files.newInputStream(source));
78                  FileUtils.CollocatedTempFile tempTarget = FileUtils.newTempFile(target);
79                  OutputStream out = new BufferedOutputStream(Files.newOutputStream(tempTarget.getPath()))) {
80              long result = copy(out, in, listener);
81              tempTarget.move();
82              return result;
83          }
84      }
85  
86      private long copy(OutputStream os, InputStream is, ProgressListener listener) throws IOException {
87          long total = 0L;
88          byte[] buffer = new byte[1024 * 32];
89          while (true) {
90              int bytes = is.read(buffer);
91              if (bytes < 0) {
92                  break;
93              }
94  
95              os.write(buffer, 0, bytes);
96  
97              total += bytes;
98  
99              if (listener != null && bytes > 0) {
100                 try {
101                     listener.progressed(ByteBuffer.wrap(buffer, 0, bytes));
102                 } catch (Exception e) {
103                     // too bad
104                 }
105             }
106         }
107 
108         return total;
109     }
110 
111     @Override
112     public void move(Path source, Path target) throws IOException {
113         Files.move(source, target, StandardCopyOption.REPLACE_EXISTING, StandardCopyOption.COPY_ATTRIBUTES);
114     }
115 }