All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.neo4j.kernel.impl.util.FileUtils Maven / Gradle / Ivy

Go to download

Neo4j kernel is a lightweight, embedded Java database designed to store data structured as graphs rather than tables. For more information, see http://neo4j.org.

There is a newer version: 5.26.0
Show newest version
/**
 * Copyright (c) 2002-2013 "Neo Technology,"
 * Network Engine for Objects in Lund AB [http://neotechnology.com]
 *
 * This file is part of Neo4j.
 *
 * Neo4j is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see .
 */
package org.neo4j.kernel.impl.util;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStreamWriter;
import java.io.RandomAccessFile;
import java.io.Writer;
import java.nio.channels.FileChannel;
import java.util.ArrayList;
import java.util.Collection;
import java.util.LinkedList;
import java.util.List;
import java.util.Stack;
import java.util.regex.Pattern;

import org.neo4j.graphdb.NotFoundException;

public class FileUtils
{
    private static int WINDOWS_RETRY_COUNT = 3;

    public static void deleteRecursively( File directory ) throws IOException
    {
        Stack stack = new Stack();
        List temp = new LinkedList();
        stack.push( directory.getAbsoluteFile() );
        while ( !stack.isEmpty() )
        {
            File top = stack.pop();
            if ( top.listFiles() != null )
            {
                for ( File child : top.listFiles() )
                {
                    if ( child.isFile() )
                    {
                        if ( !deleteFile( child ) )
                        {
                            throw new IOException( "Failed to delete " + child.getCanonicalPath() );
                        }
                    }
                    else
                    {
                        temp.add( child );
                    }
                }
            }
            if ( top.listFiles() == null || top.listFiles().length == 0 )
            {
                if ( !deleteFile( top ) )
                {
                    throw new IOException( "Failed to delete " + top.getCanonicalPath() );
                }
            }
            else
            {
                stack.push( top );
                for ( File f : temp )
                {
                    stack.push( f );
                }
            }
            temp.clear();
        }
    }

    public static boolean deleteFile( File file )
    {
        if ( !file.exists() )
        {
            return true;
        }
        int count = 0;
        boolean deleted = false;
        do
        {
            deleted = file.delete();
            if ( !deleted )
            {
                count++;
                waitSome();
            }
        }
        while ( !deleted && count <= WINDOWS_RETRY_COUNT );
        return deleted;
    }

    public static File[] deleteFiles( File directory, String regexPattern )
            throws IOException
    {
        Pattern pattern = Pattern.compile( regexPattern );
        Collection deletedFiles = new ArrayList();
        for ( File file : directory.listFiles() )
        {
            if ( pattern.matcher( file.getName() ).find() )
            {
                if ( !file.delete() )
                {
                    throw new IOException( "Couldn't delete file '" + file.getAbsolutePath() + "'" );
                }
                deletedFiles.add( file );
            }
        }
        return deletedFiles.toArray( new File[deletedFiles.size()] );
    }
    
    /**
     * Utility method that moves a file from its current location to the
     * new target location. If rename fails (for example if the target is
     * another disk) a copy/delete will be performed instead. This is not a rename,
     * use {@link #renameFile(File, File)} instead.
     * 
     * @param toMove The File object to move.
     * @param target Target file to move to.
     * @return the new file, null iff the move was unsuccessful
     * @throws IOException 
     */
    public static void moveFile( File toMove, File target ) throws IOException
    {
        if ( !toMove.exists() )
            throw new NotFoundException( "Source file[" + toMove.getName()
                    + "] not found" );
        if ( target.exists() )
            throw new NotFoundException( "Target file[" + target.getName()
                    + "] already exists" );
        
        if ( toMove.renameTo( target ) )
            return;
        
        if ( toMove.isDirectory() )
        {
            target.mkdirs();
            copyRecursively( toMove, target );
            deleteRecursively( toMove );
        }
        else
        {
            copyFile( toMove, target );
            deleteFile( toMove );
        }
    }

    /**
     * Utility method that moves a file from its current location to the
     * provided target directory. If rename fails (for example if the target is
     * another disk) a copy/delete will be performed instead. This is not a rename,
     * use {@link #renameFile(File, File)} instead.
     * 
     * @param toMove The File object to move.
     * @param targetDirectory
     * @return the new file, null iff the move was unsuccessful
     * @throws IOException 
     */
    public static File moveFileToDirectory( File toMove, File targetDirectory ) throws IOException
    {
        if ( !targetDirectory.isDirectory() )
        {
            throw new IllegalArgumentException(
                    "Move target must be a directory, not " + targetDirectory );
        }
        
        File target = new File( targetDirectory, toMove.getName() );
        moveFile( toMove, target );
        return target;
    }

    public static boolean renameFile( File srcFile, File renameToFile )
    {
        if ( !srcFile.exists() )
        {
            throw new NotFoundException( "Source file[" + srcFile.getName()
                    + "] not found" );
        }
        if ( renameToFile.exists() )
        {
            throw new NotFoundException( "Target file[" + renameToFile.getName()
                    + "] already exists" );
        }
        int count = 0;
        boolean renamed = false;
        do
        {
            renamed = srcFile.renameTo( renameToFile );
            if ( !renamed )
            {
                count++;
                waitSome();
            }
        }
        while ( !renamed && count <= WINDOWS_RETRY_COUNT );
        return renamed;
    }

    public static void truncateFile( FileChannel fileChannel, long position )
            throws IOException
    {
        int count = 0;
        boolean success = false;
        IOException cause = null;
        do
        {
            count++;
            try
            {
                fileChannel.truncate( position );
                success = true;
            }
            catch ( IOException e )
            {
                cause = e;
            }

        }
        while ( !success && count <= WINDOWS_RETRY_COUNT );
        if ( !success )
        {
            throw cause;
        }
    }

    public static void truncateFile( File file, long position ) throws IOException
    {
        RandomAccessFile access = new RandomAccessFile( file, "rw" );
        try
        {
            truncateFile( access.getChannel(), position );
        }
        finally
        {
            access.close();
        }
    }

    private static void waitSome()
    {
        try
        {
            Thread.sleep( 500 );
        }
        catch ( InterruptedException ee )
        {
            Thread.interrupted();
        } // ok
        System.gc();
    }

    public static String fixSeparatorsInPath( String path )
    {
        String fileSeparator = System.getProperty( "file.separator" );
        if ( "\\".equals( fileSeparator ) )
        {
            path = path.replace( '/', '\\' );
        }
        else if ( "/".equals( fileSeparator ) )
        {
            path = path.replace( '\\', '/' );
        }
        return path;
    }

    public static void copyFile( File srcFile, File dstFile ) throws IOException
    {
        //noinspection ResultOfMethodCallIgnored
        dstFile.getParentFile().mkdirs();
        FileInputStream input = null;
        FileOutputStream output = null;
        try
        {
            input = new FileInputStream( srcFile );
            output = new FileOutputStream( dstFile );
            int bufferSize = 1024;
            byte[] buffer = new byte[bufferSize];
            int bytesRead;
            while ( (bytesRead = input.read( buffer )) != -1 )
            {
                output.write( buffer, 0, bytesRead );
            }
        }
        catch ( IOException e )
        {
            // Because the message from this cause may not mention which file it's about
            throw new IOException( "Could not copy '" + srcFile + "' to '" + dstFile + "'", e );
        }
        finally
        {
            if ( input != null )
                input.close();
            if ( output != null )
                output.close();
        }
    }

    public static void copyRecursively( File fromDirectory, File toDirectory ) throws IOException
    {
        for ( File fromFile : fromDirectory.listFiles() )
        {
            File toFile = new File( toDirectory, fromFile.getName() );
            if ( fromFile.isDirectory() )
            {
                toFile.mkdir();
                copyRecursively( fromFile, toFile );
            }
            else
            {
                copyFile( fromFile, toFile );
            }
        }
    }
    
    public static void writeToFile( File target, String text, boolean append ) throws IOException
    {
        if ( !target.exists() )
        {
            target.getParentFile().mkdirs();
            target.createNewFile();
        }

        Writer out = new OutputStreamWriter( new FileOutputStream( target, append ), "UTF-8" );
        try
        {
            out.write( text );
        }
        finally
        {
            out.close();
        }
    }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy