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