FileUtilities.java
13.4 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
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
/*
* Decompiled with CFR 0_118.
*
* Could not load the following classes:
* com.adobe.pdfg.exception.PDFGBaseException
* com.adobe.pdfg.logging.PDFGLogger
*/
package com.adobe.pdfg.common;
import com.adobe.pdfg.common.Guid;
import com.adobe.pdfg.exception.PDFGBaseException;
import com.adobe.pdfg.logging.PDFGLogger;
import java.io.BufferedInputStream;
import java.io.BufferedWriter;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.ObjectOutputStream;
import java.io.OutputStream;
import java.io.OutputStreamWriter;
import java.io.Reader;
import java.io.Serializable;
import java.io.Writer;
import java.util.zip.ZipEntry;
import java.util.zip.ZipOutputStream;
public class FileUtilities {
private static PDFGLogger pdfgLogger = PDFGLogger.getPDFGLogger(FileUtilities.class);
private static String pdfgUserId = null;
protected static final int BUFFER = 2048;
protected static File guidRootDir = null;
public static File getGuidRootDir() {
if (guidRootDir == null) {
String tmpDir = null;
try {
tmpDir = System.getProperty("com.adobe.tempDirectory");
}
catch (Exception e) {
pdfgLogger.trace(e.getMessage(), null, (Throwable)e);
}
if (tmpDir == null || "".equals(tmpDir)) {
try {
tmpDir = System.getProperty("java.io.tmpdir");
}
catch (Exception e) {
pdfgLogger.trace(e.getMessage(), null, (Throwable)e);
}
} else {
tmpDir = new File(tmpDir).getParent();
}
String userId = System.getProperty("user.name");
if (pdfgUserId == null || "".equals(pdfgUserId)) {
pdfgUserId = userId.replaceAll("[\\$,\\\\,\\/,\\?,\\@,\\*,\\+,\\\",\\|,\\:,\\;\\,\\=,>,<,\\[,\\]]", "_");
}
guidRootDir = new File(tmpDir, "pdfg-" + pdfgUserId);
}
return guidRootDir;
}
public static File getGuidDir(String guid) throws IllegalArgumentException {
if (guid == null || guid.length() < 6) {
throw new IllegalArgumentException("Invalid ID - '" + guid + "'");
}
File aesDir = FileUtilities.getGuidRootDir();
File dir1 = new File(aesDir, guid.substring(0, 2));
File directory = new File(dir1, guid.substring(2));
return directory;
}
public static File createGuidDir(String guid) throws IOException {
File directory = FileUtilities.getGuidDir(guid);
while (directory.exists()) {
directory = FileUtilities.getGuidDir(new Guid().toString());
}
if (!directory.mkdirs()) {
throw new IOException("Directory '" + directory.getCanonicalPath() + "' couldn't be created or already existed");
}
return directory;
}
/*
* WARNING - Removed try catching itself - possible behaviour change.
*/
public static File saveStringToUtf8GuidFile(String str, File directory, String filename) throws IOException, PDFGBaseException {
File outputFile = new File(directory, filename);
BufferedWriter writer = null;
try {
writer = new BufferedWriter(new OutputStreamWriter((OutputStream)new FileOutputStream(outputFile), "UTF-8"));
writer.write(str);
}
finally {
if (writer != null) {
writer.flush();
writer.close();
writer = null;
}
}
return outputFile;
}
public static String getExtension(String canonicalPath) {
String result = "";
if (canonicalPath != null) {
int lastDot = canonicalPath.lastIndexOf(46);
int lastSep = canonicalPath.lastIndexOf(File.separatorChar);
int canonicalLength = canonicalPath.length();
if (-1 < lastDot && lastDot + 1 < canonicalLength && lastSep < lastDot) {
result = canonicalPath.substring(lastDot + 1, canonicalLength);
}
}
return result;
}
public static String changeExtension(String canonicalPath, String extension) {
String result = "";
String basePath = "";
if (canonicalPath != null) {
String oldExtension = FileUtilities.getExtension(canonicalPath);
int toRemove = oldExtension.length() + 1;
int canonicalLength = canonicalPath.length();
int toStay = canonicalLength - toRemove;
if (toStay < 0) {
toStay = 0;
}
basePath = canonicalPath.substring(0, toStay);
}
if (extension != null) {
StringBuffer newPath = new StringBuffer(basePath);
newPath.append('.');
newPath.append(extension);
result = newPath.toString();
}
return result;
}
/*
* WARNING - Removed try catching itself - possible behaviour change.
*/
public static byte[] objectToByteArray(Object obj) throws IOException {
if (obj == null || !(obj instanceof Serializable)) {
return new byte[0];
}
ByteArrayOutputStream baos = new ByteArrayOutputStream();
ObjectOutputStream objectStream = null;
try {
objectStream = new ObjectOutputStream(baos);
objectStream.writeObject(obj);
objectStream.flush();
byte[] bytedata = baos.toByteArray();
objectStream.close();
byte[] arrby = bytedata;
return arrby;
}
finally {
if (objectStream != null) {
objectStream.close();
} else {
baos.close();
}
}
}
public static void binaryDataToFile(byte[] sourceData, File dataFile) throws FileNotFoundException, IOException {
FileOutputStream fos = new FileOutputStream(dataFile);
fos.write(sourceData);
fos.close();
}
public static byte[] fileToByteArray(File sourceFile) throws IOException {
byte[] byteData = null;
ByteArrayOutputStream baos = new ByteArrayOutputStream();
FileUtilities.copyStreams(new FileInputStream(sourceFile), baos, -1, -1, true, false);
byteData = baos.toByteArray();
baos.close();
return byteData;
}
public static String fileToString(File sourceFile) throws IOException {
ByteArrayOutputStream baos = new ByteArrayOutputStream();
FileUtilities.copyStreams(new FileInputStream(sourceFile), baos, -1, -1, true, false);
return baos.toString();
}
public static String inputStreamToString(InputStream is) throws IOException {
ByteArrayOutputStream baos = new ByteArrayOutputStream();
FileUtilities.copyStreams(is, baos, -1, -1, true, false);
return baos.toString("UTF-8");
}
public static boolean deleteDir(File dir) {
if (dir == null) {
return false;
}
if (dir.isDirectory()) {
String[] children = dir.list();
for (int i = 0; i < children.length; ++i) {
boolean success = FileUtilities.deleteDir(new File(dir, children[i]));
if (success) continue;
return false;
}
}
return dir.delete();
}
public static long copyStreams(InputStream inputStream, OutputStream outputStream) throws IOException {
return FileUtilities.copyStreams(inputStream, outputStream, -1, -1);
}
protected static long copyStreams(InputStream inputStream, OutputStream outputStream, long length, int buffSize) throws IOException {
long bytesToWrite;
if (buffSize < 0) {
buffSize = 16384;
}
if ((bytesToWrite = length) < 0) {
bytesToWrite = Long.MAX_VALUE;
}
long totalBytesWritten = 0;
int bytesInBuff = 0;
byte[] buff = new byte[buffSize];
while (bytesToWrite > 0 && (bytesInBuff = inputStream.read(buff)) != -1) {
if (bytesToWrite < (long)bytesInBuff) {
bytesInBuff = (int)bytesToWrite;
}
outputStream.write(buff, 0, bytesInBuff);
bytesToWrite -= (long)bytesInBuff;
totalBytesWritten += (long)bytesInBuff;
}
outputStream.flush();
return totalBytesWritten;
}
public static void addFilesToZip(String dest, String path, ZipOutputStream out) {
byte[] data = new byte[2048];
File f = new File(path);
File[] files = f.listFiles();
BufferedInputStream origin = null;
try {
for (int i = 0; i < files.length; ++i) {
if (files[i].isFile() && !files[i].getCanonicalPath().equals(dest)) {
int count;
FileInputStream fi = new FileInputStream(files[i]);
origin = new BufferedInputStream(fi, 2048);
ZipEntry entry = new ZipEntry(files[i].getAbsolutePath());
out.putNextEntry(entry);
while ((count = origin.read(data, 0, 2048)) != -1) {
out.write(data, 0, count);
}
origin.close();
}
if (!files[i].isDirectory()) continue;
FileUtilities.addFilesToZip(dest, files[i].getCanonicalPath(), out);
}
}
catch (FileNotFoundException t) {
pdfgLogger.trace(t.getMessage(), null, (Throwable)t);
}
catch (IOException t) {
pdfgLogger.trace(t.getMessage(), null, (Throwable)t);
}
}
/*
* WARNING - Removed try catching itself - possible behaviour change.
*/
public static long copyStreams(InputStream in, OutputStream out, long copyLength, int preferredCopySize, boolean closeInput, boolean closeOutput) throws IOException {
try {
long l = FileUtilities.copyStreams(in, out, copyLength, preferredCopySize);
return l;
}
finally {
if (closeInput) {
FileUtilities.closeStream(in);
}
if (closeOutput) {
FileUtilities.closeStream(out);
}
}
}
public static void closeStream(InputStream is) {
if (is != null) {
try {
is.close();
is = null;
}
catch (IOException ie) {
pdfgLogger.trace(ie.getMessage(), null, (Throwable)ie);
}
}
}
public static void closeStream(OutputStream os) {
if (os != null) {
try {
os.close();
os = null;
}
catch (IOException ie) {
pdfgLogger.trace(ie.getMessage(), null, (Throwable)ie);
}
}
}
public static void closeReader(Reader in) {
if (in != null) {
try {
in.close();
in = null;
}
catch (IOException e) {
pdfgLogger.trace(e.getMessage(), null, (Throwable)e);
}
}
}
public static void closeWriter(Writer out) {
if (out != null) {
try {
out.close();
out = null;
}
catch (IOException e) {
pdfgLogger.trace(e.getMessage(), null, (Throwable)e);
}
}
}
public static boolean isLegalFileName(String name) {
if (name == null) {
return false;
}
char[] badChars = new char[]{'/', '\\', ':', '*', '?', '\"', '<', '>', '|'};
boolean result = true;
for (int i = 0; result && i < badChars.length; ++i) {
result = name.indexOf(badChars[i]) == -1;
}
return result;
}
/*
* WARNING - Removed try catching itself - possible behaviour change.
*/
public static void moveFile(File source, File destination) {
boolean bRenameSuccessful = source.renameTo(destination);
if (!bRenameSuccessful) {
FileInputStream in = null;
FileOutputStream out = null;
try {
int nBytesRead;
int BUFFER_SIZE_BYTES = 32768;
in = new FileInputStream(source);
out = new FileOutputStream(destination);
byte[] buffer = new byte[32768];
while ((nBytesRead = in.read(buffer)) != -1) {
out.write(buffer, 0, nBytesRead);
}
}
catch (IOException e) {
pdfgLogger.trace(e.getMessage(), null, (Throwable)e);
}
finally {
if (in != null) {
try {
in.close();
}
catch (IOException e) {
pdfgLogger.trace(e.getMessage(), null, (Throwable)e);
}
}
if (out != null) {
try {
out.close();
}
catch (IOException e) {
pdfgLogger.trace(e.getMessage(), null, (Throwable)e);
}
}
}
source.delete();
}
}
public static void setGuidRootDir(File guidRootDir) {
FileUtilities.guidRootDir = guidRootDir;
}
}