1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
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
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
57
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
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 }