FileUtils.java
9.49 KB
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
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
/*
* Decompiled with CFR 0_118.
*
* Could not load the following classes:
* org.slf4j.Logger
* org.slf4j.LoggerFactory
*/
package com.day.io.file;
import com.day.io.file.StreamUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.io.*;
import java.net.URL;
public class FileUtils {
private static final Logger log;
private static byte[] readFirstXBytesFromFile(File fileName, int iNoOfBytes) throws IOException {
log.debug("Starting readFirstXBytesFromFile (fileName : " + fileName + " , " + "iNoOfBytes : " + iNoOfBytes + " , " + ")");
byte[] buffer = new byte[iNoOfBytes];
FileInputStream in = new FileInputStream(fileName);
in.read(buffer);
in.close();
return buffer;
}
/*
* WARNING - Removed try catching itself - possible behaviour change.
*/
public static void writeBytesToFile(byte[] data, File fOutput) throws IOException {
log.debug("Starting writeBytesToFile (data : " + data + " , " + "fOutput : " + fOutput + " , " + ")");
FilterOutputStream out = null;
try {
out = new BufferedOutputStream(new FileOutputStream(fOutput));
out.write(data);
out.flush();
}
finally {
if (out != null) {
out.close();
}
}
}
public static File getFileFromClassesRoot(String fileName) throws FileNotFoundException {
log.debug("Starting getFileFromClassesRoot (fileName : " + fileName + " , " + ")");
Class class_ = FileUtils.class;
return FileUtils.getFileFromClassesRoot(fileName, class_);
}
public static File getFileFromClassesRoot(String fileName, Class relativeTo) throws FileNotFoundException {
log.debug("Starting getFileFromClassesRoot (fileName : " + fileName + " , " + "relativeTo : " + relativeTo + " , " + ")");
URL u = relativeTo.getResource("/resources/" + fileName);
if (u == null || u.getFile() == null) {
throw new FileNotFoundException("File " + fileName + " not found in the root of the classes directory " + "by com.day.io.file.FileUtils - looking at " + relativeTo.getResource("/").getFile() + "/resources/");
}
File f = new File(u.getFile());
return f;
}
public static InputStream getStreamFromClassesRoot(String fileName) throws FileNotFoundException {
log.debug("Starting getStreamFromClassesRoot (fileName : " + fileName + " , " + ")");
Class class_ = FileUtils.class;
return FileUtils.getStreamFromClassesRoot(fileName, class_);
}
public static InputStream getStreamFromClassesRoot(String fileName, Class relativeTo) throws FileNotFoundException {
log.debug("Starting getStreamFromClassesRoot (fileName : " + fileName + " , " + "relativeTo : " + relativeTo + " , " + ")");
while (fileName.startsWith("/")) {
fileName = fileName.substring(1);
}
InputStream in = relativeTo.getResourceAsStream("/resources/" + fileName);
if (in == null) {
try {
throw new FileNotFoundException("File " + fileName + " not found in the root of the classes directory " + "by com.day.io.file.FileUtils - looking at " + relativeTo.getResource("/").getFile() + "/resources/");
}
catch (NullPointerException e) {
throw new FileNotFoundException("File " + fileName + " not found in the root of the classes directory " + "by com.day.io.file.FileUtils - looking at " + relativeTo.getName() + "'s class file.");
}
}
return in;
}
public static byte[] getBytesFromClassesRoot(String fileName) throws IOException {
log.debug("Starting getBytesFromClassesRoot (fileName : " + fileName + " , " + ")");
return StreamUtils.readBytesFromStream(FileUtils.getStreamFromClassesRoot(fileName));
}
public static String getStringFromClassesRoot(String fileName) throws IOException {
log.debug("Starting getStringFromClassesRoot (fileName : " + fileName + " , " + ")");
return StreamUtils.convertStreamToString(FileUtils.getStreamFromClassesRoot(fileName));
}
private static byte[] append(byte[] source, byte[] addition, int length) {
log.debug("Starting append (source : " + source + " , " + "addition : " + addition + " , " + "length : " + length + " , " + ")");
byte[] dest = new byte[source.length + length];
System.arraycopy(source, 0, dest, 0, source.length);
System.arraycopy(addition, 0, dest, source.length, length);
return dest;
}
public static byte[] readBytesFromStream(InputStream in) throws IOException {
log.debug("Starting readBytesFromStream (in : " + in + " , " + ")");
BufferedInputStream bis = new BufferedInputStream(new DataInputStream(in), 8192);
int bytesread = 0;
boolean firstbyte = false;
byte[] content = new byte[]{(byte)bis.read()};
int BUFFER_SIZE = bis.available() + 1;
byte[] readBuffer = new byte[BUFFER_SIZE];
while ((bytesread = bis.read(readBuffer, 0, readBuffer.length)) > 0) {
content = FileUtils.append(content, readBuffer, bytesread);
}
bis.close();
return content;
}
public static byte[] readBytesFromFile(File f) throws IOException {
log.debug("Starting readBytesFromFile (f : " + f + " , " + ")");
FileInputStream in = new FileInputStream(f);
return FileUtils.readBytesFromStream(in);
}
/*
* WARNING - Removed try catching itself - possible behaviour change.
*/
public static void writeStringToFile(String strData, File fOutput) throws IOException {
log.debug("Starting writeStringToFile (strData : " + strData + " , " + "fOutput : " + fOutput + " , " + ")");
BufferedWriter bw = null;
try {
bw = new BufferedWriter(new FileWriter(fOutput));
bw.write(strData);
}
finally {
if (bw != null) {
bw.close();
}
}
}
public static String readFileToString(File fInput) throws IOException {
log.debug("Starting readFileToString (fInput : " + fInput + " , " + ")");
String strResult = null;
FileReader in = new FileReader(fInput);
int iSize = (int)fInput.length();
char[] caData = new char[iSize];
for (int iCharsRead = 0; iCharsRead < iSize; iCharsRead += in.read((char[])caData, (int)iCharsRead, (int)(iSize - iCharsRead))) {
}
in.close();
strResult = new String(caData);
return strResult;
}
public static boolean isSame(byte[] contents, File toCheck) throws IOException {
log.debug("Starting isSame (contents : " + contents + " , " + "toCheck : " + toCheck + " , " + ")");
byte[] buf = new byte[(int)toCheck.length()];
DataInputStream in = new DataInputStream(new FileInputStream(toCheck));
int val = 0;
int pos = 0;
while ((val = in.read()) != -1) {
buf[pos] = (byte)val;
++pos;
}
in.close();
if (contents.length != buf.length) {
return false;
}
for (int i = 0; i < contents.length; ++i) {
if (contents[i] == buf[i]) continue;
return false;
}
return true;
}
/*
* WARNING - Removed try catching itself - possible behaviour change.
*/
public static synchronized void copyFile(File src, File dest) throws IOException {
log.debug("Starting copyFile (src : " + src + " , " + "dest : " + dest + " , " + ")");
if (!src.exists()) {
throw new IOException("FileCopy: no such source file: " + src);
}
if (!src.isFile()) {
throw new IOException("FileCopy: cannot copy directory: " + src);
}
if (!src.canRead()) {
throw new IOException("FileCopy: source file is unreadable: " + src);
}
if (dest.isDirectory()) {
dest = new File(dest, src.getName());
}
if (dest.exists()) {
if (!dest.canWrite()) {
throw new IOException("FileCopy: destination file is unwriteable: " + dest);
}
} else {
File dir;
String parent = dest.getParent();
if (parent == null) {
parent = System.getProperty("user.dir");
}
if (!(dir = new File(parent)).exists()) {
throw new IOException("FileCopy: destination directory doesn't exist: " + parent);
}
if (dir.isFile()) {
throw new IOException("FileCopy: destination is not a directory: " + parent);
}
if (!dir.canWrite()) {
throw new IOException("FileCopy: destination directory is unwriteable: " + parent);
}
}
FileInputStream from = null;
FileOutputStream to = null;
try {
int bytes_read;
from = new FileInputStream(src);
to = new FileOutputStream(dest);
byte[] buffer = new byte[4096];
while ((bytes_read = from.read(buffer)) != -1) {
to.write(buffer, 0, bytes_read);
}
}
finally {
try {
from.close();
}
catch (Exception e) {}
try {
to.close();
}
catch (IOException e) {}
}
}
static {
Class class_ = FileUtils.class;
log = LoggerFactory.getLogger((Class)class_);
}
}