001: /*
002: * SSHTools - Java SSH2 API
003: *
004: * Copyright (C) 2002-2003 Lee David Painter and Contributors.
005: *
006: * Contributions made by:
007: *
008: * Brett Smith
009: * Richard Pernavas
010: * Erwin Bolwidt
011: *
012: * This program is free software; you can redistribute it and/or
013: * modify it under the terms of the GNU General Public License
014: * as published by the Free Software Foundation; either version 2
015: * of the License, or (at your option) any later version.
016: *
017: * This program is distributed in the hope that it will be useful,
018: * but WITHOUT ANY WARRANTY; without even the implied warranty of
019: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
020: * GNU General Public License for more details.
021: *
022: * You should have received a copy of the GNU General Public License
023: * along with this program; if not, write to the Free Software
024: * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
025: */
026: package com.sshtools.j2ssh.io;
027:
028: import java.io.*;
029:
030: /**
031: *
032: *
033: * @author $author$
034: * @version $Revision: 1.20 $
035: */
036: public class IOUtil {
037: /**
038: *
039: *
040: * @param in
041: *
042: * @return
043: */
044: public static boolean closeStream(InputStream in) {
045: try {
046: if (in != null) {
047: in.close();
048: }
049:
050: return true;
051: } catch (IOException ioe) {
052: return false;
053: }
054: }
055:
056: /**
057: *
058: *
059: * @param out
060: *
061: * @return
062: */
063: public static boolean closeStream(OutputStream out) {
064: try {
065: if (out != null) {
066: out.close();
067: }
068:
069: return true;
070: } catch (IOException ioe) {
071: return false;
072: }
073: }
074:
075: public static boolean delTree(File file) {
076: if (file.isFile()) {
077: return file.delete();
078: } else {
079: File[] list = file.listFiles();
080:
081: for (int i = 0; i < list.length; i++) {
082: if (!delTree(list[i])) {
083: return false;
084: }
085: }
086: }
087:
088: return true;
089: }
090:
091: public static void recurseDeleteDirectory(File dir) {
092: File[] files = dir.listFiles(new FileFilter() {
093: public boolean accept(File file) {
094: return file.isDirectory();
095: }
096: });
097:
098: if (files == null) {
099: return; // Directory could not be read
100: }
101:
102: for (int i = 0; i < files.length; i++) {
103: recurseDeleteDirectory(files[i]);
104: files[i].delete();
105: }
106:
107: files = dir.listFiles(new FileFilter() {
108: public boolean accept(File file) {
109: return !file.isDirectory();
110: }
111: });
112:
113: for (int i = 0; i < files.length; i++) {
114: files[i].delete();
115: }
116:
117: dir.delete();
118: }
119:
120: public static void copyFile(File from, File to) throws IOException {
121: if (from.isDirectory()) {
122: if (!to.exists()) {
123: to.mkdir();
124: }
125:
126: File[] children = from.listFiles();
127:
128: for (int i = 0; i < children.length; i++) {
129: if (children[i].getName().equals(".")
130: || children[i].getName().equals("..")) {
131: continue;
132: }
133:
134: if (children[i].isDirectory()) {
135: File f = new File(to, children[i].getName());
136: copyFile(children[i], f);
137: } else {
138: copyFile(children[i], to);
139: }
140: }
141: } else if (from.isFile() && (to.isDirectory() || to.isFile())) {
142: if (to.isDirectory()) {
143: to = new File(to, from.getName());
144: }
145:
146: FileInputStream in = new FileInputStream(from);
147: FileOutputStream out = new FileOutputStream(to);
148: byte[] buf = new byte[32678];
149: int read;
150:
151: while ((read = in.read(buf)) > -1) {
152: out.write(buf, 0, read);
153: }
154:
155: closeStream(in);
156: closeStream(out);
157: }
158: }
159:
160: public static void transfer(InputStream in, OutputStream out)
161: throws IOException {
162: try {
163: long bytesSoFar = 0;
164: byte[] buffer = new byte[65535];
165: int read;
166:
167: while ((read = in.read(buffer)) > -1) {
168: if (read > 0) {
169: out.write(buffer, 0, read);
170:
171: //out.flush();
172: bytesSoFar += read;
173: }
174: }
175: } finally {
176: closeStream(in);
177: closeStream(out);
178: }
179: }
180: }
|