1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
|
/*******************************************************************************
* Copyright (c) 2007, 2015 Borland Software Corporation and others.
*
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Eclipse Public License v1.0
* which accompanies this distribution, and is available at
* http://www.eclipse.org/legal/epl-v10.html
*
* Contributors:
* Borland Software Corporation - initial API and implementation
*******************************************************************************/
package org.eclipse.m2m.internal.qvt.oml.common.io;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
public class FileUtil {
private FileUtil() {}
public static void copyFolder(File sourceDir, File destDir) throws IOException {
if (!sourceDir.isDirectory()) {
throw new RuntimeException("sourceDir '" + sourceDir.getAbsolutePath() + "' must be an existing directory"); //$NON-NLS-1$ //$NON-NLS-2$
}
if (!destDir.isDirectory()) {
throw new RuntimeException("destDir '" + destDir.getAbsolutePath() + "' must be an existing directory"); //$NON-NLS-1$ //$NON-NLS-2$
}
File[] children = sourceDir.listFiles();
for(int i = 0; i< children.length; i++) {
File source = children[i];
File dest = new File(destDir, source.getName());
if(source.isDirectory()) {
dest.mkdir();
copyFolder(source, dest);
}
else {
copyFile(source, dest);
}
}
}
public static void copyFile(File from, File to) throws IOException {
FileInputStream input = null;
FileOutputStream output = null;
try {
input = new FileInputStream(from);
output = new FileOutputStream(to);
copyStream(input, output);
}
finally {
if(input != null) {
try { input.close(); } catch(IOException e) {}
}
if(output != null) {
try { output.close(); } catch(IOException e) {}
}
}
}
public static void setContents(File to, InputStream input) throws IOException {
FileOutputStream output = null;
try {
output = new FileOutputStream(to);
copyStream(input, output);
}
finally {
if(output != null) {
try { output.close(); } catch(IOException e) {}
}
}
}
private static void copyStream(InputStream input, OutputStream output) throws IOException {
byte[] buffer = new byte[8192];
int read;
while((read=input.read(buffer)) > 0) {
output.write(buffer, 0, read);
}
}
public static String getStreamContents(InputStream stream, String charset) throws IOException {
StringBuffer contents = new StringBuffer();
char[] buf = new char[4096];
InputStreamReader reader = null;
try {
if(charset == null) {
reader = new InputStreamReader(stream);
}
else {
reader = new InputStreamReader(stream, charset);
}
int read;
while((read = reader.read(buf)) > 0) {
contents.append(buf, 0, read);
}
return contents.toString();
}
finally {
try { reader.close(); } catch(Exception e) {}
}
}
public static void delete(File file) {
if(file.isDirectory()) {
File[] children = file.listFiles();
for(int i = 0; i < children.length; i++) {
delete(children[i]);
}
}
file.delete();
}
}
|