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;
032import java.nio.file.Files;
033
034import org.eclipse.aether.spi.io.FileProcessor;
035
036/**
037 * A simple file processor implementation to help satisfy component requirements during tests.
038 */
039public class TestFileProcessor
040    implements FileProcessor
041{
042
043    public boolean mkdirs( File directory )
044    {
045        if ( directory == null )
046        {
047            return false;
048        }
049
050        if ( directory.exists() )
051        {
052            return false;
053        }
054        if ( directory.mkdir() )
055        {
056            return true;
057        }
058
059        File canonDir = null;
060        try
061        {
062            canonDir = directory.getCanonicalFile();
063        }
064        catch ( IOException e )
065        {
066            return false;
067        }
068
069        File parentDir = canonDir.getParentFile();
070        return ( parentDir != null && ( mkdirs( parentDir ) || parentDir.exists() ) && canonDir.mkdir() );
071    }
072
073    public void write( File file, String data )
074        throws IOException
075    {
076        mkdirs( file.getParentFile() );
077
078        FileOutputStream fos = null;
079        try
080        {
081            fos = new FileOutputStream( file );
082
083            if ( data != null )
084            {
085                fos.write( data.getBytes( StandardCharsets.UTF_8 ) );
086            }
087
088            fos.close();
089            fos = null;
090        }
091        finally
092        {
093            try
094            {
095                if ( fos != null )
096                {
097                    fos.close();
098                }
099            }
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}