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.*;
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
35
36 @Singleton
37 @Named
38 public class DefaultFileProcessor implements FileProcessor {
39
40
41
42
43
44
45
46
47
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
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
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
171 write(checksumFile, checksum);
172 }
173 }