001: /*
002: * IOUtilities.java - IO related functions
003: * :tabSize=8:indentSize=8:noTabs=false:
004: * :folding=explicit:collapseFolds=1:
005: *
006: * Copyright (C) 2006 Matthieu Casanova
007: *
008: * This program is free software; you can redistribute it and/or
009: * modify it under the terms of the GNU General Public License
010: * as published by the Free Software Foundation; either version 2
011: * of the License, or any later version.
012: *
013: * This program is distributed in the hope that it will be useful,
014: * but WITHOUT ANY WARRANTY; without even the implied warranty of
015: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
016: * GNU General Public License for more details.
017: *
018: * You should have received a copy of the GNU General Public License
019: * along with this program; if not, write to the Free Software
020: * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
021: */
022:
023: package org.gjt.sp.util;
024:
025: import java.io.*;
026:
027: /**
028: * IO tools that depends on JDK only.
029: *
030: * @author Matthieu Casanova
031: * @version $Id: IOUtilities.java 9728 2007-06-09 02:00:10Z Vampire0 $
032: * @since 4.3pre5
033: */
034: public class IOUtilities {
035: //{{{ moveFile() method
036: /**
037: * Moves the source file to the destination.
038: *
039: * If the destination cannot be created or is a read-only file, the
040: * method returns <code>false</code>. Otherwise, the contents of the
041: * source are copied to the destination, the source is deleted,
042: * and <code>true</code> is returned.
043: *
044: * @param source The source file to move.
045: * @param dest The destination where to move the file.
046: * @return true on success, false otherwise.
047: *
048: * @since jEdit 4.3pre9
049: */
050: public static boolean moveFile(File source, File dest) {
051: boolean ok = false;
052:
053: if ((dest.exists() && dest.canWrite())
054: || (!dest.exists() && dest.getParentFile().canWrite())) {
055: OutputStream fos = null;
056: InputStream fis = null;
057: try {
058: fos = new FileOutputStream(dest);
059: fis = new FileInputStream(source);
060: ok = copyStream(32768, null, fis, fos, false);
061: } catch (IOException ioe) {
062: Log.log(Log.WARNING, IOUtilities.class,
063: "Error moving file: " + ioe + " : "
064: + ioe.getMessage());
065: } finally {
066: closeQuietly(fos);
067: closeQuietly(fis);
068: }
069:
070: if (ok)
071: source.delete();
072: }
073: return ok;
074: } //}}}
075:
076: //{{{ copyStream() method
077: /**
078: * Copy an input stream to an output stream.
079: *
080: * @param bufferSize the size of the buffer
081: * @param progress the progress observer it could be null
082: * @param in the input stream
083: * @param out the output stream
084: * @param canStop if true, the copy can be stopped by interrupting the thread
085: * @return <code>true</code> if the copy was done, <code>false</code> if it was interrupted
086: * @throws IOException IOException If an I/O error occurs
087: */
088: public static boolean copyStream(int bufferSize,
089: ProgressObserver progress, InputStream in,
090: OutputStream out, boolean canStop) throws IOException {
091: byte[] buffer = new byte[bufferSize];
092: int n;
093: long copied = 0L;
094: while (-1 != (n = in.read(buffer))) {
095: out.write(buffer, 0, n);
096: copied += n;
097: if (progress != null)
098: progress.setValue(copied);
099: if (canStop && Thread.interrupted())
100: return false;
101: }
102: return true;
103: } //}}}
104:
105: //{{{ copyStream() method
106: /**
107: * Copy an input stream to an output stream with a buffer of 4096 bytes.
108: *
109: * @param progress the progress observer it could be null
110: * @param in the input stream
111: * @param out the output stream
112: * @param canStop if true, the copy can be stopped by interrupting the thread
113: * @return <code>true</code> if the copy was done, <code>false</code> if it was interrupted
114: * @throws IOException IOException If an I/O error occurs
115: */
116: public static boolean copyStream(ProgressObserver progress,
117: InputStream in, OutputStream out, boolean canStop)
118: throws IOException {
119: return copyStream(4096, progress, in, out, canStop);
120: } //}}}
121:
122: //{{{ fileLength() method
123: /**
124: * Returns the length of a file. If it is a directory it will calculate recursively the length.
125: *
126: * @param file the file or directory
127: * @return the length of the file or directory. If the file doesn't exists it will return 0
128: * @since 4.3pre10
129: */
130: public static long fileLength(File file) {
131: long length = 0L;
132: if (file.isFile())
133: length = file.length();
134: else if (file.isDirectory()) {
135: File[] files = file.listFiles();
136: for (int i = 0; i < files.length; i++) {
137: length += fileLength(files[i]);
138: }
139: }
140: return length;
141: } // }}}
142:
143: //{{{ closeQuietly() method
144: /**
145: * Method that will close an {@link InputStream} ignoring it if it is null and ignoring exceptions.
146: *
147: * @param in the InputStream to close.
148: */
149: public static void closeQuietly(InputStream in) {
150: if (in != null) {
151: try {
152: in.close();
153: } catch (IOException e) {
154: //ignore
155: }
156: }
157: } //}}}
158:
159: //{{{ closeQuietly() method
160: /**
161: * Method that will close an {@link OutputStream} ignoring it if it is null and ignoring exceptions.
162: *
163: * @param out the OutputStream to close.
164: */
165: public static void closeQuietly(OutputStream out) {
166: if (out != null) {
167: try {
168: out.close();
169: } catch (IOException e) {
170: //ignore
171: }
172: }
173: } //}}}
174:
175: //{{{ closeQuietly() method
176: /**
177: * Method that will close an {@link Reader} ignoring it if it is null and ignoring exceptions.
178: *
179: * @param r the Reader to close.
180: * @since jEdit 4.3pre5
181: */
182: public static void closeQuietly(Reader r) {
183: if (r != null) {
184: try {
185: r.close();
186: } catch (IOException e) {
187: //ignore
188: }
189: }
190: } //}}}
191:
192: //{{{ closeQuietly() method
193: /**
194: * Method that will close an {@link java.io.Closeable} ignoring it if it is null and ignoring exceptions.
195: *
196: * @param closeable the closeable to close.
197: * @since jEdit 4.3pre8
198: */
199: public static void closeQuietly(Closeable closeable) {
200: if (closeable != null) {
201: try {
202: closeable.close();
203: } catch (IOException e) {
204: //ignore
205: }
206: }
207: } //}}}
208:
209: //{{{ IOUtilities() constructor
210: private IOUtilities() {
211: } //}}}
212: }
|