1 package org.eclipse.aether.internal.test.util;
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22 import java.io.BufferedOutputStream;
23 import java.io.File;
24 import java.io.FileInputStream;
25 import java.io.FileOutputStream;
26 import java.io.IOException;
27 import java.io.InputStream;
28 import java.io.OutputStream;
29 import java.nio.Buffer;
30 import java.nio.ByteBuffer;
31 import java.nio.charset.StandardCharsets;
32
33 import org.eclipse.aether.spi.io.FileProcessor;
34
35
36
37
38 public class TestFileProcessor
39 implements FileProcessor
40 {
41
42 public boolean mkdirs( File directory )
43 {
44 if ( directory == null )
45 {
46 return false;
47 }
48
49 if ( directory.exists() )
50 {
51 return false;
52 }
53 if ( directory.mkdir() )
54 {
55 return true;
56 }
57
58 File canonDir = null;
59 try
60 {
61 canonDir = directory.getCanonicalFile();
62 }
63 catch ( IOException e )
64 {
65 return false;
66 }
67
68 File parentDir = canonDir.getParentFile();
69 return ( parentDir != null && ( mkdirs( parentDir ) || parentDir.exists() ) && canonDir.mkdir() );
70 }
71
72 public void write( File file, String data )
73 throws IOException
74 {
75 mkdirs( file.getParentFile() );
76
77 FileOutputStream fos = null;
78 try
79 {
80 fos = new FileOutputStream( file );
81
82 if ( data != null )
83 {
84 fos.write( data.getBytes( StandardCharsets.UTF_8 ) );
85 }
86
87 fos.close();
88 fos = null;
89 }
90 finally
91 {
92 try
93 {
94 if ( fos != null )
95 {
96 fos.close();
97 }
98 }
99 catch ( final IOException e )
100 {
101
102 }
103 }
104 }
105
106 public void write( File target, InputStream source )
107 throws IOException
108 {
109 mkdirs( target.getAbsoluteFile().getParentFile() );
110
111 OutputStream fos = null;
112 try
113 {
114 fos = new BufferedOutputStream( new FileOutputStream( target ) );
115
116 copy( fos, source, null );
117
118 fos.close();
119 fos = null;
120 }
121 finally
122 {
123 try
124 {
125 if ( fos != null )
126 {
127 fos.close();
128 }
129 }
130 catch ( final IOException e )
131 {
132
133 }
134 }
135 }
136
137 public void copy( File source, File target )
138 throws IOException
139 {
140 copy( source, target, null );
141 }
142
143 public long copy( File source, File target, ProgressListener listener )
144 throws IOException
145 {
146 long total = 0;
147
148 InputStream fis = null;
149 OutputStream fos = null;
150 try
151 {
152 fis = new FileInputStream( source );
153
154 mkdirs( target.getAbsoluteFile().getParentFile() );
155
156 fos = new BufferedOutputStream( new FileOutputStream( target ) );
157
158 total = copy( fos, fis, listener );
159
160 fos.close();
161 fos = null;
162
163 fis.close();
164 fis = null;
165 }
166 finally
167 {
168 try
169 {
170 if ( fos != null )
171 {
172 fos.close();
173 }
174 }
175 catch ( final IOException e )
176 {
177
178 }
179 finally
180 {
181 try
182 {
183 if ( fis != null )
184 {
185 fis.close();
186 }
187 }
188 catch ( final IOException e )
189 {
190
191 }
192 }
193 }
194
195 return total;
196 }
197
198 private long copy( OutputStream os, InputStream is, ProgressListener listener )
199 throws IOException
200 {
201 long total = 0L;
202
203 ByteBuffer buffer = ByteBuffer.allocate( 1024 * 32 );
204 byte[] array = buffer.array();
205
206 while ( true )
207 {
208 int bytes = is.read( array );
209 if ( bytes < 0 )
210 {
211 break;
212 }
213
214 os.write( array, 0, bytes );
215
216 total += bytes;
217
218 if ( listener != null && bytes > 0 )
219 {
220 try
221 {
222 ( (Buffer) buffer ).rewind();
223 ( (Buffer) buffer ).limit( bytes );
224 listener.progressed( buffer );
225 }
226 catch ( Exception e )
227 {
228
229 }
230 }
231 }
232
233 return total;
234 }
235
236 public void move( File source, File target )
237 throws IOException
238 {
239 target.delete();
240
241 if ( !source.renameTo( target ) )
242 {
243 copy( source, target );
244
245 target.setLastModified( source.lastModified() );
246
247 source.delete();
248 }
249 }
250
251 }