1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19 package org.eclipse.aether.internal.test.util;
20
21 import java.io.BufferedOutputStream;
22 import java.io.File;
23 import java.io.FileInputStream;
24 import java.io.FileOutputStream;
25 import java.io.IOException;
26 import java.io.InputStream;
27 import java.io.OutputStream;
28 import java.nio.Buffer;
29 import java.nio.ByteBuffer;
30 import java.nio.charset.StandardCharsets;
31 import java.nio.file.Files;
32
33
34
35
36
37
38 @SuppressWarnings("deprecation")
39 public class TestFileProcessor implements org.eclipse.aether.spi.io.FileProcessor {
40
41 public boolean mkdirs(File directory) {
42 if (directory == null) {
43 return false;
44 }
45
46 if (directory.exists()) {
47 return false;
48 }
49 if (directory.mkdir()) {
50 return true;
51 }
52
53 File canonDir = null;
54 try {
55 canonDir = directory.getCanonicalFile();
56 } catch (IOException e) {
57 return false;
58 }
59
60 File parentDir = canonDir.getParentFile();
61 return (parentDir != null && (mkdirs(parentDir) || parentDir.exists()) && canonDir.mkdir());
62 }
63
64 public void write(File file, String data) throws IOException {
65 mkdirs(file.getParentFile());
66
67 try (FileOutputStream fos = new FileOutputStream(file)) {
68 if (data != null) {
69 fos.write(data.getBytes(StandardCharsets.UTF_8));
70 }
71 }
72 }
73
74 public void write(File target, InputStream source) throws IOException {
75 mkdirs(target.getAbsoluteFile().getParentFile());
76
77 try (OutputStream fos = new BufferedOutputStream(new FileOutputStream(target))) {
78 copy(fos, source, null);
79 }
80 }
81
82 public void copy(File source, File target) throws IOException {
83 copy(source, target, null);
84 }
85
86 public long copy(File source, File target, ProgressListener listener) throws IOException {
87 long total = 0;
88
89 mkdirs(target.getAbsoluteFile().getParentFile());
90
91 try (InputStream fis = new FileInputStream(source);
92 OutputStream fos = new BufferedOutputStream(new FileOutputStream(target))) {
93 total = copy(fos, fis, listener);
94 }
95
96 return total;
97 }
98
99 private long copy(OutputStream os, InputStream is, ProgressListener listener) throws IOException {
100 long total = 0L;
101
102 ByteBuffer buffer = ByteBuffer.allocate(1024 * 32);
103 byte[] array = buffer.array();
104
105 while (true) {
106 int bytes = is.read(array);
107 if (bytes < 0) {
108 break;
109 }
110
111 os.write(array, 0, bytes);
112
113 total += bytes;
114
115 if (listener != null && bytes > 0) {
116 try {
117 ((Buffer) buffer).rewind();
118 ((Buffer) buffer).limit(bytes);
119 listener.progressed(buffer);
120 } catch (Exception e) {
121
122 }
123 }
124 }
125
126 return total;
127 }
128
129 public void move(File source, File target) throws IOException {
130 target.delete();
131
132 if (!source.renameTo(target)) {
133 copy(source, target);
134
135 target.setLastModified(source.lastModified());
136
137 source.delete();
138 }
139 }
140
141 @Override
142 public String readChecksum(final File checksumFile) throws IOException {
143 return new String(Files.readAllBytes(checksumFile.toPath()), StandardCharsets.UTF_8);
144 }
145
146 @Override
147 public void writeChecksum(final File checksumFile, final String checksum) throws IOException {
148 write(checksumFile, checksum);
149 }
150 }