001package org.eclipse.aether.internal.test.util;
002
003/*
004 * Licensed to the Apache Software Foundation (ASF) under one
005 * or more contributor license agreements.  See the NOTICE file
006 * distributed with this work for additional information
007 * regarding copyright ownership.  The ASF licenses this file
008 * to you under the Apache License, Version 2.0 (the
009 * "License"); you may not use this file except in compliance
010 * with the License.  You may obtain a copy of the License at
011 *
012 *  http://www.apache.org/licenses/LICENSE-2.0
013 *
014 * Unless required by applicable law or agreed to in writing,
015 * software distributed under the License is distributed on an
016 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
017 * KIND, either express or implied.  See the License for the
018 * specific language governing permissions and limitations
019 * under the License.
020 */
021
022import java.io.BufferedOutputStream;
023import java.io.File;
024import java.io.FileInputStream;
025import java.io.FileOutputStream;
026import java.io.IOException;
027import java.io.InputStream;
028import java.io.OutputStream;
029import java.nio.Buffer;
030import java.nio.ByteBuffer;
031import java.nio.charset.StandardCharsets;
032
033import org.eclipse.aether.spi.io.FileProcessor;
034
035/**
036 * A simple file processor implementation to help satisfy component requirements during tests.
037 */
038public class TestFileProcessor
039    implements FileProcessor
040{
041
042    public boolean mkdirs( File directory )
043    {
044        if ( directory == null )
045        {
046            return false;
047        }
048
049        if ( directory.exists() )
050        {
051            return false;
052        }
053        if ( directory.mkdir() )
054        {
055            return true;
056        }
057
058        File canonDir = null;
059        try
060        {
061            canonDir = directory.getCanonicalFile();
062        }
063        catch ( IOException e )
064        {
065            return false;
066        }
067
068        File parentDir = canonDir.getParentFile();
069        return ( parentDir != null && ( mkdirs( parentDir ) || parentDir.exists() ) && canonDir.mkdir() );
070    }
071
072    public void write( File file, String data )
073        throws IOException
074    {
075        mkdirs( file.getParentFile() );
076
077        FileOutputStream fos = null;
078        try
079        {
080            fos = new FileOutputStream( file );
081
082            if ( data != null )
083            {
084                fos.write( data.getBytes( StandardCharsets.UTF_8 ) );
085            }
086
087            fos.close();
088            fos = null;
089        }
090        finally
091        {
092            try
093            {
094                if ( fos != null )
095                {
096                    fos.close();
097                }
098            }
099            catch ( final IOException e )
100            {
101                // Suppressed due to an exception already thrown in the try block.
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                // Suppressed due to an exception already thrown in the try block.
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                // Suppressed due to an exception already thrown in the try block.
178            }
179            finally
180            {
181                try
182                {
183                    if ( fis != null )
184                    {
185                        fis.close();
186                    }
187                }
188                catch ( final IOException e )
189                {
190                    // Suppressed due to an exception already thrown in the try block.
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                    // too bad
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}