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