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