001    /**
002     * Licensed to the Apache Software Foundation (ASF) under one or more
003     * contributor license agreements.  See the NOTICE file distributed with
004     * this work for additional information regarding copyright ownership.
005     * The ASF licenses this file to You under the Apache License, Version 2.0
006     * (the "License"); you may not use this file except in compliance with
007     * the License.  You may obtain a copy of the License at
008     *
009     *      http://www.apache.org/licenses/LICENSE-2.0
010     *
011     * Unless required by applicable law or agreed to in writing, software
012     * distributed under the License is distributed on an "AS IS" BASIS,
013     * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014     * See the License for the specific language governing permissions and
015     * limitations under the License.
016     */
017    package org.apache.activemq.util;
018    
019    import java.io.File;
020    import java.io.FileInputStream;
021    import java.io.FileOutputStream;
022    import java.io.IOException;
023    import java.io.InputStream;
024    import java.io.OutputStream;
025    
026    /**
027     * @version $Revision: 661435 $
028     */
029    public final class IOHelper {
030        protected static final int MAX_DIR_NAME_LENGTH;
031        protected static final int MAX_FILE_NAME_LENGTH;
032        private static final int DEFAULT_BUFFER_SIZE = 4096;
033        private IOHelper() {
034        }
035    
036        public static String getDefaultDataDirectory() {
037            return getDefaultDirectoryPrefix() + "activemq-data";
038        }
039    
040        public static String getDefaultStoreDirectory() {
041            return getDefaultDirectoryPrefix() + "amqstore";
042        }
043    
044        /**
045         * Allows a system property to be used to overload the default data
046         * directory which can be useful for forcing the test cases to use a target/
047         * prefix
048         */
049        public static String getDefaultDirectoryPrefix() {
050            try {
051                return System.getProperty("org.apache.activemq.default.directory.prefix", "");
052            } catch (Exception e) {
053                return "";
054            }
055        }
056        
057        public static boolean deleteFile(File fileToDelete) {
058            if (fileToDelete == null || !fileToDelete.exists()) {
059                return true;
060            }
061            boolean result = deleteChildren(fileToDelete);
062            result &= fileToDelete.delete();
063            return result;
064        }
065        
066        public static boolean deleteChildren(File parent) {
067            if (parent == null || !parent.exists()) {
068                return false;
069            }
070            boolean result = true;
071            if (parent.isDirectory()) {
072                File[] files = parent.listFiles();
073                if (files == null) {
074                    result = false;
075                } else {
076                    for (int i = 0; i < files.length; i++) {
077                        File file = files[i];
078                        if (file.getName().equals(".")
079                                || file.getName().equals("..")) {
080                            continue;
081                        }
082                        if (file.isDirectory()) {
083                            result &= deleteFile(file);
084                        } else {
085                            result &= file.delete();
086                        }
087                    }
088                }
089            }
090           
091            return result;
092        }
093        
094        
095        public static void moveFile(File src, File targetDirectory) throws IOException {
096            if (!src.renameTo(new File(targetDirectory, src.getName()))) {
097                throw new IOException("Failed to move " + src + " to " + targetDirectory);
098            }
099        }
100        
101        public static void copyFile(File src, File dest) throws IOException {
102            FileInputStream fileSrc = new FileInputStream(src);
103            FileOutputStream fileDest = new FileOutputStream(dest);
104            copyInputStream(fileSrc, fileDest);
105        }
106        
107        public static void copyInputStream(InputStream in, OutputStream out) throws IOException {
108            byte[] buffer = new byte[DEFAULT_BUFFER_SIZE];
109            int len = in.read(buffer);
110            while (len >= 0) {
111                out.write(buffer, 0, len);
112                len = in.read(buffer);
113            }
114            in.close();
115            out.close();
116        }
117        
118        static {
119            MAX_DIR_NAME_LENGTH = Integer.valueOf(System.getProperty("MaximumDirNameLength","200")).intValue();  
120            MAX_FILE_NAME_LENGTH = Integer.valueOf(System.getProperty("MaximumFileNameLength","64")).intValue();             
121        }
122    
123        
124        public static void mkdirs(File dir) throws IOException {
125            if (dir.exists()) {
126                if (!dir.isDirectory()) {
127                    throw new IOException("Failed to create directory '" + dir +"', regular file already existed with that name");
128                }
129                
130            } else {
131                if (!dir.mkdirs()) {
132                    throw new IOException("Failed to create directory '" + dir+"'");
133                }
134            }
135        }
136    }