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 import org.eclipse.aether.spi.io.FileProcessor;
34
35
36
37
38 public class TestFileProcessor implements FileProcessor {
39
40 public boolean mkdirs(File directory) {
41 if (directory == null) {
42 return false;
43 }
44
45 if (directory.exists()) {
46 return false;
47 }
48 if (directory.mkdir()) {
49 return true;
50 }
51
52 File canonDir = null;
53 try {
54 canonDir = directory.getCanonicalFile();
55 } catch (IOException e) {
56 return false;
57 }
58
59 File parentDir = canonDir.getParentFile();
60 return (parentDir != null && (mkdirs(parentDir) || parentDir.exists()) && canonDir.mkdir());
61 }
62
63 public void write(File file, String data) throws IOException {
64 mkdirs(file.getParentFile());
65
66 FileOutputStream fos = null;
67 try {
68 fos = new FileOutputStream(file);
69
70 if (data != null) {
71 fos.write(data.getBytes(StandardCharsets.UTF_8));
72 }
73
74 fos.close();
75 fos = null;
76 } finally {
77 try {
78 if (fos != null) {
79 fos.close();
80 }
81 } catch (final IOException e) {
82
83 }
84 }
85 }
86
87 public void write(File target, InputStream source) throws IOException {
88 mkdirs(target.getAbsoluteFile().getParentFile());
89
90 OutputStream fos = null;
91 try {
92 fos = new BufferedOutputStream(new FileOutputStream(target));
93
94 copy(fos, source, null);
95
96 fos.close();
97 fos = null;
98 } finally {
99 try {
100 if (fos != null) {
101 fos.close();
102 }
103 } catch (final IOException e) {
104
105 }
106 }
107 }
108
109 public void copy(File source, File target) throws IOException {
110 copy(source, target, null);
111 }
112
113 public long copy(File source, File target, ProgressListener listener) throws IOException {
114 long total = 0;
115
116 InputStream fis = null;
117 OutputStream fos = null;
118 try {
119 fis = new FileInputStream(source);
120
121 mkdirs(target.getAbsoluteFile().getParentFile());
122
123 fos = new BufferedOutputStream(new FileOutputStream(target));
124
125 total = copy(fos, fis, listener);
126
127 fos.close();
128 fos = null;
129
130 fis.close();
131 fis = null;
132 } finally {
133 try {
134 if (fos != null) {
135 fos.close();
136 }
137 } catch (final IOException e) {
138
139 } finally {
140 try {
141 if (fis != null) {
142 fis.close();
143 }
144 } catch (final IOException e) {
145
146 }
147 }
148 }
149
150 return total;
151 }
152
153 private long copy(OutputStream os, InputStream is, ProgressListener listener) throws IOException {
154 long total = 0L;
155
156 ByteBuffer buffer = ByteBuffer.allocate(1024 * 32);
157 byte[] array = buffer.array();
158
159 while (true) {
160 int bytes = is.read(array);
161 if (bytes < 0) {
162 break;
163 }
164
165 os.write(array, 0, bytes);
166
167 total += bytes;
168
169 if (listener != null && bytes > 0) {
170 try {
171 ((Buffer) buffer).rewind();
172 ((Buffer) buffer).limit(bytes);
173 listener.progressed(buffer);
174 } catch (Exception e) {
175
176 }
177 }
178 }
179
180 return total;
181 }
182
183 public void move(File source, File target) throws IOException {
184 target.delete();
185
186 if (!source.renameTo(target)) {
187 copy(source, target);
188
189 target.setLastModified(source.lastModified());
190
191 source.delete();
192 }
193 }
194
195 @Override
196 public String readChecksum(final File checksumFile) throws IOException {
197 return new String(Files.readAllBytes(checksumFile.toPath()), StandardCharsets.UTF_8);
198 }
199
200 @Override
201 public void writeChecksum(final File checksumFile, final String checksum) throws IOException {
202 write(checksumFile, checksum);
203 }
204 }