themeinstaller/source/src/com/nokia/tools/themeinstaller/defrep/operations/FileOperationUtils.java
branchRCL_3
changeset 17 fe49e33862e2
parent 16 b685c59de105
child 18 04b7640f6fb5
equal deleted inserted replaced
16:b685c59de105 17:fe49e33862e2
     1 /*
       
     2 * Copyright (c) 2007 Nokia Corporation and/or its subsidiary(-ies). 
       
     3 * All rights reserved.
       
     4 * This component and the accompanying materials are made available
       
     5 * under the terms of "Eclipse Public License v1.0"
       
     6 * which accompanies this distribution, and is available
       
     7 * at the URL "http://www.eclipse.org/legal/epl-v10.html".
       
     8 *
       
     9 * Initial Contributors:
       
    10 * Nokia Corporation - initial contribution.
       
    11 *
       
    12 * Contributors:
       
    13 *
       
    14 * Description:  Utils for file operations
       
    15  *
       
    16 */
       
    17 
       
    18 
       
    19 package com.nokia.tools.themeinstaller.defrep.operations;
       
    20 
       
    21 import java.io.File;
       
    22 import java.io.FileInputStream;
       
    23 import java.io.FileNotFoundException;
       
    24 import java.io.FileOutputStream;
       
    25 import java.io.IOException;
       
    26 
       
    27 
       
    28 /**
       
    29  * Utils for file operations.
       
    30  */
       
    31 public class FileOperationUtils
       
    32     {
       
    33 
       
    34     // CONSTANTS
       
    35     private static final int READ_BUFFER_SIZE = 1024;
       
    36     private static final String REGEXP_PREFIX = "\\";
       
    37     private static final char EPOC_DRIVE_SEP = ':';
       
    38     private static final String EPOC_PATH_SEP = "\\";
       
    39     private static final char EPOC_DRIVE_LETTER_FIRST_U = 'A';
       
    40     private static final char EPOC_DRIVE_LETTER_LAST_U = 'Z';
       
    41     private static final char EPOC_DRIVE_LETTER_FIRST_L = 'a';
       
    42     private static final char EPOC_DRIVE_LETTER_LAST_L = 'z';
       
    43     private static final String EPOC32_DIR = "epoc32";
       
    44 
       
    45     /**
       
    46      * Copy a file to an another location.
       
    47      *
       
    48      * @param aSource Source file
       
    49      * @param aDestination Destination file
       
    50      * @param aAppend If true, the source file contents will be appended to the end of
       
    51      * the destination file. Otherwise, the original file will be overwritten.
       
    52      * @throws FileNotFoundException Thrown if the file can not be found
       
    53      * @throws IOException Thrown if IOException occurs, open streams are also closed
       
    54      */
       
    55     public static void copyFile( File aSource, File aDestination, boolean aAppend )
       
    56         throws FileNotFoundException, IOException
       
    57         {
       
    58         // Overwrite the existing file, if any
       
    59         if( aDestination.exists() && !aAppend )
       
    60             {
       
    61             aDestination.delete();
       
    62             }
       
    63 
       
    64         // Create the required directory structure for the destination file
       
    65         // (ignore return value)
       
    66         createDirs( aDestination );
       
    67 
       
    68         // Open streams for input and output
       
    69         FileInputStream input = new FileInputStream( aSource );
       
    70         FileOutputStream output = new FileOutputStream( aDestination, aAppend );
       
    71 
       
    72         // Create buffer for the data transfer
       
    73         byte[] buffer = new byte[ READ_BUFFER_SIZE ];
       
    74         int i = 0;
       
    75 
       
    76         try
       
    77             {
       
    78             // Read data to the buffer and write it to the output stream until
       
    79             // the whole file is processed
       
    80             while( ( i = input.read( buffer ) ) != -1 )
       
    81                 {
       
    82                 output.write( buffer, 0, i );
       
    83                 }
       
    84             }
       
    85         finally
       
    86             {
       
    87             if( input != null )
       
    88                 {
       
    89                 input.close();
       
    90                 }
       
    91             if( output != null )
       
    92                 {
       
    93                 output.close();
       
    94                 }
       
    95             }
       
    96         }
       
    97 
       
    98     /**
       
    99      * Create the whole directory structure for the file
       
   100      *
       
   101      * @param aFile File object to be placed at the end of the tree
       
   102      * @return true, if new directories were created
       
   103      */
       
   104     public static boolean createDirs( File aFile )
       
   105         {
       
   106         // Extract the parent directory
       
   107         String parent = aFile.getParent();
       
   108 
       
   109         // Create the whole directory structure
       
   110         File dir = new File( parent );
       
   111         return dir.mkdirs();
       
   112         }
       
   113 
       
   114     /**
       
   115      * Parse Symbian OS file system format path of a Winscw environment path.
       
   116      * This method will search for a directory of which name is one character
       
   117      * long and from a to z.
       
   118      * @param aFile The file to process
       
   119      * @return Path and file name in Symbian OS file system. Returns null if
       
   120      * the file name can not be parsed.
       
   121      */
       
   122     public static String parseSymbianFSPath( File aFile )
       
   123         {
       
   124         String result = null;
       
   125         String path = aFile.getPath();
       
   126 
       
   127         File file = aFile.getParentFile();
       
   128 
       
   129         // Go through all parent directories of the file
       
   130         while( file != null )
       
   131             {
       
   132             // Find a directory of which name is 1 character long
       
   133             String name = file.getName();
       
   134             if( name.length() == 1 )
       
   135                 {
       
   136                 // Check that the directory name is an character
       
   137                 // from A to Z or a to z
       
   138                 char c = name.charAt( 0 );
       
   139                 if( ( c >= EPOC_DRIVE_LETTER_FIRST_U &&
       
   140                       c <= EPOC_DRIVE_LETTER_LAST_U  ) ||
       
   141                     ( c >= EPOC_DRIVE_LETTER_FIRST_L &&
       
   142                       c <= EPOC_DRIVE_LETTER_LAST_L  ) )
       
   143                     {
       
   144                     result = name;
       
   145                     }
       
   146                 }
       
   147             // Stop if already reached to the epoc32 directory
       
   148             else if( EPOC32_DIR.equalsIgnoreCase( name ) )
       
   149                 {
       
   150                 break;
       
   151                 }
       
   152 
       
   153             file = file.getParentFile();
       
   154             }
       
   155 
       
   156         if( result != null )
       
   157             {
       
   158             String pattern = File.separatorChar + result + File.separatorChar;
       
   159 
       
   160             // Find the start of the actual path
       
   161             int pathStart = path.indexOf( pattern ) + pattern.length();
       
   162 
       
   163             // Combine the Symbian OS format path of the drive letter,
       
   164             // path separator and rest of the path
       
   165             result = result +
       
   166                      EPOC_DRIVE_SEP +
       
   167                      EPOC_PATH_SEP +
       
   168                      path.substring( pathStart );
       
   169 
       
   170             // Replace path separators with Epoc ones
       
   171             pattern = REGEXP_PREFIX + File.separator;
       
   172             String replacement = REGEXP_PREFIX + EPOC_PATH_SEP;
       
   173             result = result.replaceAll( pattern, replacement );
       
   174             }
       
   175 
       
   176         return result;
       
   177         }
       
   178 
       
   179     /**
       
   180      * Parse Symbian OS file system format path of a Winscw environment path.
       
   181      * @param aFile The file name to process
       
   182      * @return Path and file name in Symbian OS file system. Returns null if
       
   183      * the file name can not be parsed.
       
   184      */
       
   185     public static String parseSymbianFSPath( String aFile )
       
   186         {
       
   187         File f = new File( aFile );
       
   188         return parseSymbianFSPath( f );
       
   189         }
       
   190     }