1 /*
2  * JaLingo, http://jalingo.sourceforge.net/
3  *
4  * Copyright (c) 2002-2006 Oleksandr Shyshko
5  *
6  * This program is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License as published by
8  * the Free Software Foundation; either version 2 of the License, or
9  * (at your option) any later version.
10  *
11  * This program is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with this program; if not, write to the Free Software
18  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
19  */
20 
21 package ja.centre.util.io;
22 
23 import ja.centre.util.assertions.Arguments;
24 import org.apache.commons.logging.Log;
25 import org.apache.commons.logging.LogFactory;
26 
27 import java.io.*;
28 import java.nio.ByteBuffer;
29 import java.nio.channels.FileChannel;
30 
31 public class Files {
32     private static final Log LOG = LogFactory.getLog( Files.class );
33     private static final int COPY_BUFFER_SIZE = 32768;
34 
Files()35     private Files() {
36     }
37 
ensureDirectoryExists( String directoryName )38     public static void ensureDirectoryExists( String directoryName ) throws IOException {
39         Arguments.assertNotNull( "directoryName", directoryName );
40 
41         File dirFile = new File( directoryName );
42         if ( dirFile.exists() ) {
43             if ( !dirFile.isDirectory() ) {
44                 throw new IOException( "File '" + directoryName
45                         + "' already exists and it's not a directory. It should not exist, or should be a directoryName." );
46             }
47         } else if ( !dirFile.mkdirs() ) {
48             throw new IOException( "Unable to create directory \"" + directoryName + "\"" );
49         }
50     }
51 
getCanonicalPath( String fileName )52     public static String getCanonicalPath( String fileName ) throws IOException {
53         Arguments.assertNotNull( "fileName", fileName );
54 
55         return new File( fileName ).getCanonicalPath();
56     }
57 
exists( String fileName )58     public static boolean exists( String fileName ) {
59         Arguments.assertNotNull( "fileName", fileName );
60 
61         return new File( fileName ).exists();
62     }
63 
delete( String fileName )64     public static void delete( String fileName ) throws FileNotFoundException, IOException {
65         Arguments.assertNotNull( "fileName", fileName );
66 
67         // check existance
68         if ( !exists( fileName ) ) {
69             throw new FileNotFoundException( "File: \"" + fileName + "\"" );
70         }
71 
72         // delete
73         if ( !new File( fileName ).delete() ) {
74             throw new IOException( "File \"" + fileName + "\" could not be deleted due to unknown obstacle " );
75         }
76     }
77 
deleteQuietly( String fileName )78     public static void deleteQuietly( String fileName ) {
79         Arguments.assertNotNull( "fileName", fileName );
80 
81         // check existance
82         if ( !exists( fileName ) ) {
83             LOG.warn( "File: \"" + fileName + "\"" );
84         }
85 
86         // delete
87         if ( !new File( fileName ).delete() ) {
88             LOG.warn( "File \"" + fileName + "\" could not be deleted due to unknown obstacle " );
89         }
90     }
91 
rename( String fileName, String newFileName )92     public static void rename( String fileName, String newFileName ) throws IOException {
93         if ( !new File( fileName ).renameTo( new File( newFileName ) ) ) {
94             throw new IOException( "Unable to rename \"" + fileName
95                     + "\" to \"" + newFileName + "\"" + newFileName + "\" due to unknown obstacle" );
96         }
97     }
98 
createTempFile( String prefix )99     public static File createTempFile( String prefix ) throws IOException {
100         return createTempFile( prefix, null );
101     }
102 
createTempFile( String prefix, String suffix )103     public static File createTempFile( String prefix, String suffix ) throws IOException {
104         File file = File.createTempFile( prefix, suffix );
105         file.deleteOnExit();
106         return file;
107     }
108 
length( String fileName )109     public static long length( String fileName ) {
110         return new File( fileName ).length();
111     }
112 
calculateFileNameInUserHome( String fileName )113     public static String calculateFileNameInUserHome( String fileName ) {
114         Arguments.assertNotNull( "fileName", fileName );
115         return System.getProperty( "user.home" ) + File.separator + fileName;
116     }
117 
deleteRecusrsively( String filenName )118     public static void deleteRecusrsively( String filenName ) throws IOException {
119         Arguments.assertNotNull( "filenName", filenName );
120         deleteRecusrsively( new File( filenName ) );
121     }
122 
deleteRecusrsively( File file )123     public static void deleteRecusrsively( File file ) throws IOException {
124         if ( !file.exists() ) {
125             return;
126         }
127 
128         if ( file.isDirectory() ) {
129             File[] childFiles = file.listFiles();
130 
131             for ( int i = 0; i < childFiles.length; i++ ) {
132                 File childFile = childFiles[i];
133                 deleteRecusrsively( childFile );
134             }
135         }
136 
137         if ( !file.delete() ) {
138             throw new IOException( "Can't delete file \"" + file.getAbsolutePath() + "\"" );
139         }
140     }
141 
makeDirs( String fileName )142     public static void makeDirs( String fileName ) throws IOException {
143         File file = new File( fileName );
144         if ( !file.mkdirs() ) {
145             throw new IOException( "Can't make directories \"" + file.getAbsolutePath() + "\"" );
146         }
147     }
148 
removeUnacceptableSymbols( String fileName )149     public static String removeUnacceptableSymbols( String fileName ) {
150         return fileName.replaceAll( "[/\\\\:\\*\\?\"<>|]", "-" );
151     }
152 
create( String fileName )153     public static File create( String fileName ) throws FileNotFoundException {
154         Arguments.assertNotNull( "fileName", fileName );
155 
156         File file = new File( fileName );
157         if ( !file.exists() ) {
158             throw new FileNotFoundException( fileName );
159         }
160         return file;
161     }
162 
copy( String source, String destination )163     public static void copy( String source, String destination ) throws IOException {
164         FileInputStream fis = null;
165         FileOutputStream fos = null;
166         try {
167             fis = new FileInputStream( source );
168             fos = new FileOutputStream( destination );
169 
170             FileChannel fic = null;
171             FileChannel foc = null;
172             try {
173                 fic = fis.getChannel();
174                 foc = fos.getChannel();
175 
176                 ByteBuffer buffer = ByteBuffer.allocate( COPY_BUFFER_SIZE );
177                 do {
178                     buffer.flip();
179                     foc.write( buffer );
180                     buffer.clear();
181                 } while ( fic.read( buffer ) != -1 );
182             } finally {
183                 closeQuietly( fic );
184                 closeQuietly( foc );
185             }
186         } finally {
187             closeQuietly( fis );
188             closeQuietly( fos );
189         }
190     }
191 
copy( InputStream is, OutputStream os )192     public static void copy( InputStream is, OutputStream os ) throws IOException {
193         byte[] buffer = new byte[COPY_BUFFER_SIZE];
194 
195         int read;
196         while ( (read = is.read( buffer )) != -1 ) {
197             os.write( buffer, 0, read );
198         }
199     }
200 
closeQuietly( Closeable closable )201     public static void closeQuietly( Closeable closable ) {
202         try {
203             close( closable );
204         } catch ( IOException e ) {
205             LOG.error( "Could not close closable \"" + closable + "\" of class \""
206                     + closable.getClass().getName() + "\"", e );
207         }
208     }
209 
close( Closeable closable )210     public static void close( Closeable closable ) throws IOException {
211         if ( closable != null ) {
212             closable.close();
213         }
214     }
215 
readAsBytes( String fileName )216     public static byte[] readAsBytes( String fileName ) throws IOException {
217         ByteArrayOutputStream baos = new ByteArrayOutputStream();
218         FileInputStream fis = null;
219         try {
220             fis = new FileInputStream( fileName );
221             copy( fis, baos );
222         } finally {
223             closeQuietly( fis );
224         }
225         return baos.toByteArray();
226     }
assertExists( String fileName )227     public static void assertExists( String fileName ) throws FileNotFoundException {
228         if ( !exists( fileName ) ) {
229             throw new FileNotFoundException( fileName );
230         }
231     }
232 }
233