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.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
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
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 }