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.StandardCopyOption;
29  
30  import org.eclipse.aether.spi.io.FileProcessor;
31  import org.eclipse.aether.util.FileUtils;
32  
33  /**
34   * A utility class helping with file-based operations.
35   */
36  @Singleton
37  @Named
38  public class DefaultFileProcessor implements FileProcessor {
39  
40      /**
41       * Thread-safe variant of {@link File#mkdirs()}. Creates the directory named by the given abstract pathname,
42       * including any necessary but nonexistent parent directories. Note that if this operation fails it may have
43       * succeeded in creating some of the necessary parent directories.
44       *
45       * @param directory The directory to create, may be {@code null}.
46       * @return {@code true} if and only if the directory was created, along with all necessary parent directories;
47       * {@code false} otherwise
48       */
49      @Override
50      public boolean mkdirs(File directory) {
51          if (directory == null) {
52              return false;
53          }
54  
55          if (directory.exists()) {
56              return false;
57          }
58          if (directory.mkdir()) {
59              return true;
60          }
61  
62          File canonDir;
63          try {
64              canonDir = directory.getCanonicalFile();
65          } catch (IOException e) {
66              throw new UncheckedIOException(e);
67          }
68  
69          File parentDir = canonDir.getParentFile();
70          return (parentDir != null && (mkdirs(parentDir) || parentDir.exists()) && canonDir.mkdir());
71      }
72  
73      @Override
74      public void write(File target, String data) throws IOException {
75          FileUtils.writeFile(target.toPath(), p -> Files.write(p, data.getBytes(StandardCharsets.UTF_8)));
76      }
77  
78      @Override
79      public void write(File target, InputStream source) throws IOException {
80          FileUtils.writeFile(target.toPath(), p -> Files.copy(source, p, StandardCopyOption.REPLACE_EXISTING));
81      }
82  
83      @Override
84      public void copy(File source, File target) throws IOException {
85          copy(source, target, null);
86      }
87  
88      @Override
89      public long copy(File source, File target, ProgressListener listener) throws IOException {
90          try (InputStream in = new BufferedInputStream(Files.newInputStream(source.toPath()));
91                  FileUtils.CollocatedTempFile tempTarget = FileUtils.newTempFile(target.toPath());
92                  OutputStream out = new BufferedOutputStream(Files.newOutputStream(tempTarget.getPath()))) {
93              long result = copy(out, in, listener);
94              tempTarget.move();
95              return result;
96          }
97      }
98  
99      private long copy(OutputStream os, InputStream is, ProgressListener listener) throws IOException {
100         long total = 0L;
101         byte[] buffer = new byte[1024 * 32];
102         while (true) {
103             int bytes = is.read(buffer);
104             if (bytes < 0) {
105                 break;
106             }
107 
108             os.write(buffer, 0, bytes);
109 
110             total += bytes;
111 
112             if (listener != null && bytes > 0) {
113                 try {
114                     listener.progressed(ByteBuffer.wrap(buffer, 0, bytes));
115                 } catch (Exception e) {
116                     // too bad
117                 }
118             }
119         }
120 
121         return total;
122     }
123 
124     @Override
125     public void move(File source, File target) throws IOException {
126         if (!source.renameTo(target)) {
127             copy(source, target);
128 
129             target.setLastModified(source.lastModified());
130 
131             source.delete();
132         }
133     }
134 
135     @Override
136     public String readChecksum(final File checksumFile) throws IOException {
137         // for now do exactly same as happened before, but FileProcessor is a component and can be replaced
138         String checksum = "";
139         try (BufferedReader br = new BufferedReader(
140                 new InputStreamReader(Files.newInputStream(checksumFile.toPath()), StandardCharsets.UTF_8), 512)) {
141             while (true) {
142                 String line = br.readLine();
143                 if (line == null) {
144                     break;
145                 }
146                 line = line.trim();
147                 if (!line.isEmpty()) {
148                     checksum = line;
149                     break;
150                 }
151             }
152         }
153 
154         if (checksum.matches(".+= [0-9A-Fa-f]+")) {
155             int lastSpacePos = checksum.lastIndexOf(' ');
156             checksum = checksum.substring(lastSpacePos + 1);
157         } else {
158             int spacePos = checksum.indexOf(' ');
159 
160             if (spacePos != -1) {
161                 checksum = checksum.substring(0, spacePos);
162             }
163         }
164 
165         return checksum;
166     }
167 
168     @Override
169     public void writeChecksum(final File checksumFile, final String checksum) throws IOException {
170         // for now do exactly same as happened before, but FileProcessor is a component and can be replaced
171         write(checksumFile, checksum);
172     }
173 }