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