KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > gjt > sp > util > IOUtilities


1 /*
2  * IOUtilities.java - IO related functions
3  * :tabSize=8:indentSize=8:noTabs=false:
4  * :folding=explicit:collapseFolds=1:
5  *
6  * Copyright (C) 2006 Matthieu Casanova
7  *
8  * This program is free software; you can redistribute it and/or
9  * modify it under the terms of the GNU General Public License
10  * as published by the Free Software Foundation; either version 2
11  * of the License, or any later version.
12  *
13  * This program is distributed in the hope that it will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16  * GNU General Public License for more details.
17  *
18  * You should have received a copy of the GNU General Public License
19  * along with this program; if not, write to the Free Software
20  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
21  */

22
23 package org.gjt.sp.util;
24
25 import java.io.*;
26
27 /**
28  * IO tools that depends on JDK only.
29  *
30  * @author Matthieu Casanova
31  * @version $Id: IOUtilities.java 8579 2007-01-13 10:31:47Z kpouer $
32  * @since 4.3pre5
33  */

34 public class IOUtilities
35 {
36     //{{{ moveFile() method
37
/**
38      * Moves the source file to the destination.
39      *
40      * If the destination cannot be created or is a read-only file, the
41      * method returns <code>false</code>. Otherwise, the contents of the
42      * source are copied to the destination, the source is deleted,
43      * and <code>true</code> is returned.
44      *
45      * @param source The source file to move.
46      * @param dest The destination where to move the file.
47      * @return true on success, false otherwise.
48      *
49      * @since jEdit 4.3pre9
50      */

51     public static boolean moveFile(File source, File dest)
52     {
53         boolean ok = false;
54
55         if ((dest.exists() && dest.canWrite())
56             || (!dest.exists() && dest.getParentFile().canWrite()))
57             {
58                 OutputStream fos = null;
59                 InputStream fis = null;
60                 try
61                 {
62                     fos = new FileOutputStream(dest);
63                     fis = new FileInputStream(source);
64                     ok = copyStream(32768,null,fis,fos,false);
65                 }
66                 catch (IOException ioe)
67                 {
68                     Log.log(Log.WARNING, IOUtilities.class,
69                             "Error moving file: " + ioe + " : " + ioe.getMessage());
70                 }
71                 finally
72                 {
73                     closeQuietly(fos);
74                     closeQuietly(fis);
75                 }
76
77                 if(ok)
78                     source.delete();
79             }
80         return ok;
81     } //}}}
82

83     //{{{ copyStream() method
84
/**
85      * Copy an input stream to an output stream.
86      *
87      * @param bufferSize the size of the buffer
88      * @param progress the progress observer it could be null
89      * @param in the input stream
90      * @param out the output stream
91      * @param canStop if true, the copy can be stopped by interrupting the thread
92      * @return <code>true</code> if the copy was done, <code>false</code> if it was interrupted
93      * @throws IOException IOException If an I/O error occurs
94      */

95     public static boolean copyStream(int bufferSize, ProgressObserver progress,
96                     InputStream in, OutputStream out, boolean canStop)
97         throws IOException
98     {
99         byte[] buffer = new byte[bufferSize];
100         int n;
101         long copied = 0L;
102         while (-1 != (n = in.read(buffer)))
103         {
104             out.write(buffer, 0, n);
105             copied += n;
106             if(progress != null)
107                 progress.setValue(copied);
108             if(canStop && Thread.interrupted()) return false;
109         }
110         return true;
111     } //}}}
112

113     //{{{ copyStream() method
114
/**
115      * Copy an input stream to an output stream with a buffer of 4096 bytes.
116      *
117      * @param progress the progress observer it could be null
118      * @param in the input stream
119      * @param out the output stream
120      * @param canStop if true, the copy can be stopped by interrupting the thread
121      * @return <code>true</code> if the copy was done, <code>false</code> if it was interrupted
122      * @throws IOException IOException If an I/O error occurs
123      */

124     public static boolean copyStream(ProgressObserver progress,
125                      InputStream in, OutputStream out, boolean canStop)
126         throws IOException
127     {
128         return copyStream(4096,progress, in, out, canStop);
129     } //}}}
130

131     //{{{ closeQuietly() method
132
/**
133      * Method that will close an {@link InputStream} ignoring it if it is null and ignoring exceptions.
134      *
135      * @param in the InputStream to close.
136      */

137     public static void closeQuietly(InputStream in)
138     {
139         if(in != null)
140         {
141             try
142             {
143                 in.close();
144             }
145             catch (IOException e)
146             {
147                 //ignore
148
}
149         }
150     } //}}}
151

152     //{{{ closeQuietly() method
153
/**
154      * Method that will close an {@link OutputStream} ignoring it if it is null and ignoring exceptions.
155      *
156      * @param out the OutputStream to close.
157      */

158     public static void closeQuietly(OutputStream out)
159     {
160         if(out != null)
161         {
162             try
163             {
164                 out.close();
165             }
166             catch (IOException e)
167             {
168                 //ignore
169
}
170         }
171     } //}}}
172

173     //{{{ closeQuietly() method
174
/**
175      * Method that will close an {@link Reader} ignoring it if it is null and ignoring exceptions.
176      *
177      * @param r the Reader to close.
178      * @since jEdit 4.3pre5
179      */

180     public static void closeQuietly(Reader r)
181     {
182         if(r != null)
183         {
184             try
185             {
186                 r.close();
187             }
188             catch (IOException e)
189             {
190                 //ignore
191
}
192         }
193     } //}}}
194

195     //{{{ closeQuietly() method
196
/**
197      * Method that will close an {@link java.io.Closeable} ignoring it if it is null and ignoring exceptions.
198      *
199      * @param closeable the closeable to close.
200      * @since jEdit 4.3pre8
201      */

202     public static void closeQuietly(Closeable closeable)
203     {
204         if(closeable != null)
205         {
206             try
207             {
208                 closeable.close();
209             }
210             catch (IOException e)
211             {
212                 //ignore
213
}
214         }
215     }
216
217     private IOUtilities(){}
218 }
219
Popular Tags