View Javadoc
1   package org.eclipse.aether.internal.test.util;
2   
3   /*
4    * Licensed to the Apache Software Foundation (ASF) under one
5    * or more contributor license agreements.  See the NOTICE file
6    * distributed with this work for additional information
7    * regarding copyright ownership.  The ASF licenses this file
8    * to you under the Apache License, Version 2.0 (the
9    * "License"); you may not use this file except in compliance
10   * with the License.  You may obtain a copy of the License at
11   * 
12   *  http://www.apache.org/licenses/LICENSE-2.0
13   * 
14   * Unless required by applicable law or agreed to in writing,
15   * software distributed under the License is distributed on an
16   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
17   * KIND, either express or implied.  See the License for the
18   * specific language governing permissions and limitations
19   * under the License.
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   * A simple file processor implementation to help satisfy component requirements during tests.
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                  // too bad but who cares
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             // allow output to report any flush/close errors
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             // allow output to report any flush/close errors
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             // allow output to report any flush/close errors
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                     // too bad
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 }