tracesrv/tracecompiler/src/com.nokia.tracecompiler/src/com/nokia/tracecompiler/file/FileUtils.java
changeset 56 aa2539c91954
equal deleted inserted replaced
54:a151135b0cf9 56:aa2539c91954
       
     1 /*
       
     2 * Copyright (c) 2008-2010 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:
       
    15 *
       
    16 * File utility functions
       
    17 *
       
    18 */
       
    19 package com.nokia.tracecompiler.file;
       
    20 
       
    21 import java.io.BufferedOutputStream;
       
    22 import java.io.File;
       
    23 import java.io.FileInputStream;
       
    24 import java.io.FileNotFoundException;
       
    25 import java.io.FileOutputStream;
       
    26 import java.io.IOException;
       
    27 import java.io.OutputStream;
       
    28 
       
    29 import com.nokia.tracecompiler.source.SourceConstants;
       
    30 
       
    31 /**
       
    32  * File utility functions
       
    33  * 
       
    34  */
       
    35 public final class FileUtils {
       
    36 
       
    37 	/**
       
    38 	 * File copy buffer size
       
    39 	 */
       
    40 	private static final int COPY_BUFFER_SIZE = 4096; // CodForChk_Dis_Magic
       
    41 
       
    42 	/**
       
    43 	 * cpp extension
       
    44 	 */
       
    45 	public final static String CPP_EXTENSION = "cpp"; //$NON-NLS-1$	
       
    46 
       
    47 	/**
       
    48 	 * c extension
       
    49 	 */
       
    50 	public final static String C_EXTENSION = "c"; //$NON-NLS-1$	
       
    51 
       
    52 	/**
       
    53 	 * Allowed files
       
    54 	 */
       
    55 	private final static String[] FILE_FILTERS = { ".cpp", //$NON-NLS-1$
       
    56 			".c", //$NON-NLS-1$
       
    57 			".inl", //$NON-NLS-1$
       
    58 			".h" //$NON-NLS-1$
       
    59 	};
       
    60 
       
    61 	/**
       
    62 	 * MMP file extension
       
    63 	 */
       
    64 	public static final String MMP = ".mmp"; //$NON-NLS-1$
       
    65 
       
    66 	/**
       
    67 	 * number of times trying to create a directy(ies)
       
    68 	 */
       
    69 	private static final int MAX_DIRECTORY_CREATION_TRIES = 5;
       
    70 	
       
    71 	/**
       
    72 	 * Time to sleep between tries to create a directory(ies)
       
    73 	 */
       
    74 	private static final int SLEEP_TIME_BETWEEN_DIRECTORY_CREATION_TRIES = 100;
       
    75 
       
    76 	
       
    77 	/**
       
    78 	 * Creates a file output stream. This creates directories and overwriting
       
    79 	 * possible read-only flag
       
    80 	 * 
       
    81 	 * @param file
       
    82 	 *            the file
       
    83 	 * @return the file output stream
       
    84 	 * @throws FileNotFoundException
       
    85 	 *             if file cannot be created
       
    86 	 */
       
    87 	public static OutputStream createOutputStream(File file)
       
    88 			throws FileNotFoundException {
       
    89 		File parent = file.getParentFile();
       
    90 		if (!parent.exists()) {
       
    91 			createDirectories(parent);
       
    92 		}
       
    93 		FileOutputStream fos;
       
    94 		try {
       
    95 			fos = new FileOutputStream(file);
       
    96 		} catch (IOException e) {
       
    97 			if (file.exists()) {
       
    98 				file.delete();
       
    99 			}
       
   100 			fos = new FileOutputStream(file);
       
   101 		}
       
   102 		return new BufferedOutputStream(fos);
       
   103 	}
       
   104 
       
   105 	/**
       
   106 	 * Creates a copy of a file
       
   107 	 * 
       
   108 	 * @param source
       
   109 	 *            the source file
       
   110 	 * @param target
       
   111 	 *            the target file
       
   112 	 * @return true if written successfully
       
   113 	 */
       
   114 	public static boolean copyFile(File source, File target) {
       
   115 		boolean backup = true;
       
   116 		try {
       
   117 			byte[] buf = new byte[COPY_BUFFER_SIZE];
       
   118 			FileInputStream fis = new FileInputStream(source);
       
   119 			OutputStream fos = createOutputStream(target);
       
   120 			int len;
       
   121 			do {
       
   122 				len = fis.read(buf);
       
   123 				if (len > 0) {
       
   124 					fos.write(buf, 0, len);
       
   125 				}
       
   126 			} while (len > 0);
       
   127 			fis.close();
       
   128 			fos.close();
       
   129 		} catch (Exception e) {
       
   130 			backup = false;
       
   131 		}
       
   132 		return backup;
       
   133 	}
       
   134 
       
   135 	/**
       
   136 	 * Converts file separator characters
       
   137 	 * 
       
   138 	 * @param separator
       
   139 	 *            separator to be used
       
   140 	 * @param path
       
   141 	 *            string to be converted
       
   142 	 * @param addLast
       
   143 	 *            true if the converted string should end with a separator
       
   144 	 * @return the converted string
       
   145 	 */
       
   146 	public static String convertSeparators(char separator, String path,
       
   147 			boolean addLast) {
       
   148 		path = path.replace(SourceConstants.FORWARD_SLASH_CHAR, separator);
       
   149 		path = path.replace(SourceConstants.BACKSLASH_CHAR, separator);
       
   150 		String sepStr = String.valueOf(separator);
       
   151 		if (addLast && !path.endsWith(sepStr)) {
       
   152 			path += separator;
       
   153 		} else if (!addLast && path.endsWith(sepStr)) {
       
   154 			path = path.substring(0, path.length() - 1);
       
   155 		}
       
   156 		return path;
       
   157 	}
       
   158 
       
   159 	/**
       
   160 	 * Checks if given file is allowed to be opened into TraceCompiler
       
   161 	 * 
       
   162 	 * @param fileName
       
   163 	 *            the file to be checked
       
   164 	 * @return true if filtered, false if not
       
   165 	 */
       
   166 	public static boolean isFileAllowed(String fileName) {
       
   167 		boolean allowed = false;
       
   168 		fileName = fileName.toLowerCase();
       
   169 		for (String filter : FILE_FILTERS) {
       
   170 			if (fileName.endsWith(filter)) {
       
   171 				allowed = true;
       
   172 				break;
       
   173 			}
       
   174 		}
       
   175 		return allowed;
       
   176 	}
       
   177 
       
   178 	/**
       
   179 	 * Create directories safely in a multiple instances case
       
   180 	 * @param path
       
   181 	 * @param access
       
   182 	 * @return boolean pass/fail
       
   183 	 */
       
   184 	public static boolean createDirectories(File path) {
       
   185 
       
   186 		boolean retVal = true;
       
   187 
       
   188 		int tries = 0;
       
   189 
       
   190 		if (path.exists()) {
       
   191 			return retVal;
       
   192 		}
       
   193 
       
   194 		// try few times
       
   195 		while (tries < MAX_DIRECTORY_CREATION_TRIES) {
       
   196 
       
   197 			retVal = path.mkdirs();
       
   198 			// mkdirs can fail for a number of reasons including the case where
       
   199 			// the directory is open by another process. The API does not make
       
   200 			// any difference so we assume the latter case (worst case)
       
   201 			if (!retVal) { // sleep for moment and try again.
       
   202 
       
   203 				tries++;
       
   204 
       
   205 				try {
       
   206 					Thread.sleep(SLEEP_TIME_BETWEEN_DIRECTORY_CREATION_TRIES);
       
   207 				} catch (InterruptedException e) {
       
   208 					// Do nothing
       
   209 				}
       
   210 			} else { // pass
       
   211 				break;
       
   212 			}
       
   213 		}
       
   214 
       
   215 		return retVal;
       
   216 
       
   217 	}
       
   218 }