Save This Page
Home » activemq-parent-5.3.1-source-release » org.apache.kahadb.util » [javadoc | source]
    1   /**
    2    * Licensed to the Apache Software Foundation (ASF) under one or more
    3    * contributor license agreements.  See the NOTICE file distributed with
    4    * this work for additional information regarding copyright ownership.
    5    * The ASF licenses this file to You under the Apache License, Version 2.0
    6    * (the "License"); you may not use this file except in compliance with
    7    * the License.  You may obtain a copy of the License at
    8    *
    9    *      http://www.apache.org/licenses/LICENSE-2.0
   10    *
   11    * Unless required by applicable law or agreed to in writing, software
   12    * distributed under the License is distributed on an "AS IS" BASIS,
   13    * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
   14    * See the License for the specific language governing permissions and
   15    * limitations under the License.
   16    */
   17   package org.apache.kahadb.util;
   18   
   19   import java.io.File;
   20   import java.io.FileInputStream;
   21   import java.io.FileNotFoundException;
   22   import java.io.FileOutputStream;
   23   import java.io.IOException;
   24   import java.io.InputStream;
   25   import java.io.OutputStream;
   26   
   27   /**
   28    * @version $Revision: 712224 $
   29    */
   30   public final class IOHelper {
   31       protected static final int MAX_DIR_NAME_LENGTH;
   32       protected static final int MAX_FILE_NAME_LENGTH;
   33       private static final int DEFAULT_BUFFER_SIZE = 4096;
   34       private IOHelper() {
   35       }
   36   
   37       public static String getDefaultDataDirectory() {
   38           return getDefaultDirectoryPrefix() + "activemq-data";
   39       }
   40   
   41       public static String getDefaultStoreDirectory() {
   42           return getDefaultDirectoryPrefix() + "amqstore";
   43       }
   44   
   45       /**
   46        * Allows a system property to be used to overload the default data
   47        * directory which can be useful for forcing the test cases to use a target/
   48        * prefix
   49        */
   50       public static String getDefaultDirectoryPrefix() {
   51           try {
   52               return System.getProperty("org.apache.activemq.default.directory.prefix", "");
   53           } catch (Exception e) {
   54               return "";
   55           }
   56       }
   57   
   58       /**
   59        * Converts any string into a string that is safe to use as a file name.
   60        * The result will only include ascii characters and numbers, and the "-","_", and "." characters.
   61        *
   62        * @param name
   63        * @return
   64        */
   65       public static String toFileSystemDirectorySafeName(String name) {
   66           return toFileSystemSafeName(name, true, MAX_DIR_NAME_LENGTH);
   67       }
   68       
   69       public static String toFileSystemSafeName(String name) {
   70           return toFileSystemSafeName(name, false, MAX_FILE_NAME_LENGTH);
   71       }
   72       
   73       /**
   74        * Converts any string into a string that is safe to use as a file name.
   75        * The result will only include ascii characters and numbers, and the "-","_", and "." characters.
   76        *
   77        * @param name
   78        * @param dirSeparators 
   79        * @param maxFileLength 
   80        * @return
   81        */
   82       public static String toFileSystemSafeName(String name,boolean dirSeparators,int maxFileLength) {
   83           int size = name.length();
   84           StringBuffer rc = new StringBuffer(size * 2);
   85           for (int i = 0; i < size; i++) {
   86               char c = name.charAt(i);
   87               boolean valid = c >= 'a' && c <= 'z';
   88               valid = valid || (c >= 'A' && c <= 'Z');
   89               valid = valid || (c >= '0' && c <= '9');
   90               valid = valid || (c == '_') || (c == '-') || (c == '.') || (c=='#')
   91                       ||(dirSeparators && ( (c == '/') || (c == '\\')));
   92   
   93               if (valid) {
   94                   rc.append(c);
   95               } else {
   96                   // Encode the character using hex notation
   97                   rc.append('#');
   98                   rc.append(HexSupport.toHexFromInt(c, true));
   99               }
  100           }
  101           String result = rc.toString();
  102           if (result.length() > maxFileLength) {
  103               result = result.substring(result.length()-maxFileLength,result.length());
  104           }
  105           return result;
  106       }
  107       
  108       public static boolean deleteFile(File fileToDelete) {
  109           if (fileToDelete == null || !fileToDelete.exists()) {
  110               return true;
  111           }
  112           boolean result = deleteChildren(fileToDelete);
  113           result &= fileToDelete.delete();
  114           return result;
  115       }
  116       
  117       public static boolean deleteChildren(File parent) {
  118           if (parent == null || !parent.exists()) {
  119               return false;
  120           }
  121           boolean result = true;
  122           if (parent.isDirectory()) {
  123               File[] files = parent.listFiles();
  124               if (files == null) {
  125                   result = false;
  126               } else {
  127                   for (int i = 0; i < files.length; i++) {
  128                       File file = files[i];
  129                       if (file.getName().equals(".")
  130                               || file.getName().equals("..")) {
  131                           continue;
  132                       }
  133                       if (file.isDirectory()) {
  134                           result &= deleteFile(file);
  135                       } else {
  136                           result &= file.delete();
  137                       }
  138                   }
  139               }
  140           }
  141          
  142           return result;
  143       }
  144       
  145       
  146       public static void moveFile(File src, File targetDirectory) throws IOException {
  147           if (!src.renameTo(new File(targetDirectory, src.getName()))) {
  148               throw new IOException("Failed to move " + src + " to " + targetDirectory);
  149           }
  150       }
  151       
  152       public static void copyFile(File src, File dest) throws IOException {
  153           FileInputStream fileSrc = new FileInputStream(src);
  154           FileOutputStream fileDest = new FileOutputStream(dest);
  155           copyInputStream(fileSrc, fileDest);
  156       }
  157       
  158       public static void copyInputStream(InputStream in, OutputStream out) throws IOException {
  159           byte[] buffer = new byte[DEFAULT_BUFFER_SIZE];
  160           int len = in.read(buffer);
  161           while (len >= 0) {
  162               out.write(buffer, 0, len);
  163               len = in.read(buffer);
  164           }
  165           in.close();
  166           out.close();
  167       }
  168       
  169       static {
  170           MAX_DIR_NAME_LENGTH = Integer.valueOf(System.getProperty("MaximumDirNameLength","200")).intValue();  
  171           MAX_FILE_NAME_LENGTH = Integer.valueOf(System.getProperty("MaximumFileNameLength","64")).intValue();             
  172       }
  173   
  174       
  175       public static void mkdirs(File dir) throws IOException {
  176           if (dir.exists()) {
  177               if (!dir.isDirectory()) {
  178                   throw new IOException("Failed to create directory '" + dir +"', regular file already existed with that name");
  179               }
  180               
  181           } else {
  182               if (!dir.mkdirs()) {
  183                   throw new IOException("Failed to create directory '" + dir+"'");
  184               }
  185           }
  186       }
  187   }

Save This Page
Home » activemq-parent-5.3.1-source-release » org.apache.kahadb.util » [javadoc | source]