IO.java
11.2 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
/*
* Decompiled with CFR 0_118.
*
* Could not load the following classes:
* org.slf4j.Logger
* org.slf4j.LoggerFactory
*/
package com.day.io;
import java.io.CharArrayWriter;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.Reader;
import java.io.UTFDataFormatException;
import java.io.Writer;
import java.util.Collection;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
public class IO {
private static final Logger log;
private static final String TEMPDIR = "tmp";
private static File tempDir;
private static File cwd;
public static int spool(InputStream in, OutputStream out) throws IOException {
int rd;
byte[] buffer = new byte[8192];
int total = 0;
while ((rd = in.read(buffer)) > 0) {
out.write(buffer, 0, rd);
total += rd;
}
return total;
}
public static int spool(InputStream in, byte[] buffer) throws IOException {
int rd;
int total;
for (total = 0; total < buffer.length && (rd = in.read(buffer, total, buffer.length - total)) >= 0; total += rd) {
}
return total;
}
public static int spool(Reader in, Writer out) throws IOException {
int rd;
char[] buffer = new char[8192];
int total = 0;
while (in.ready() && (rd = in.read(buffer)) > 0) {
out.write(buffer, 0, rd);
total += rd;
}
return total;
}
public static int spool(InputStream in, OutputStream out, int num) throws IOException {
byte[] buffer = new byte[8192];
int rd = 0;
int total = num;
while (num > 0 && rd >= 0) {
rd = in.read(buffer, 0, num > buffer.length ? buffer.length : num);
if (rd <= 0) continue;
out.write(buffer, 0, rd);
num -= rd;
}
return total - num;
}
public static void tryClose(InputStream in) {
if (in != null) {
try {
in.close();
}
catch (IOException ioe) {
// empty catch block
}
}
}
public static void tryClose(OutputStream out) {
if (out != null) {
try {
out.close();
}
catch (IOException ioe) {
// empty catch block
}
}
}
public static void tryClose(Reader reader) {
if (reader != null) {
try {
reader.close();
}
catch (IOException ioe) {
// empty catch block
}
}
}
public static void tryClose(Writer writer) {
if (writer != null) {
try {
writer.close();
}
catch (IOException ioe) {
// empty catch block
}
}
}
/*
* WARNING - Removed try catching itself - possible behaviour change.
*/
public static boolean rename0(File src, File dst) {
if (dst.exists()) {
dst.delete();
}
dst.getParentFile().mkdirs();
if (!src.renameTo(dst)) {
FileInputStream in = null;
FileOutputStream out = null;
try {
in = new FileInputStream(src);
out = new FileOutputStream(dst);
IO.spool((InputStream)in, out);
}
catch (IOException e) {
boolean bl = false;
return bl;
}
finally {
if (in != null) {
try {
in.close();
}
catch (IOException e) {}
}
if (out != null) {
try {
out.close();
}
catch (IOException e) {}
}
}
if (!src.delete()) {
try {
new FileOutputStream(src).close();
}
catch (IOException e) {
// empty catch block
}
src.deleteOnExit();
}
}
return true;
}
public static String readUTF(InputStream in) throws UTFDataFormatException, IOException {
int c;
CharArrayWriter out = new CharArrayWriter(256);
block5 : while ((c = in.read()) != -1) {
switch (c >> 4) {
int char2;
case 0:
case 1:
case 2:
case 3:
case 4:
case 5:
case 6:
case 7: {
out.write(c);
continue block5;
}
case 12:
case 13: {
char2 = in.read();
if (char2 == -1) {
throw new UTFDataFormatException("unexpected end of input");
}
if ((char2 & 192) != 128) {
throw new UTFDataFormatException("consecutive byte should have bit 6 cleared");
}
out.write((c & 31) << 6 | char2 & 63);
continue block5;
}
case 14: {
char2 = in.read();
if (char2 == -1) {
throw new UTFDataFormatException("unexpected end of input");
}
int char3 = in.read();
if (char3 == -1) {
throw new UTFDataFormatException("unexpected end of input");
}
if ((char2 & 192) != 128 || (char3 & 192) != 128) {
throw new UTFDataFormatException("consecutive byte should have bit 6 cleared");
}
out.write((c & 15) << 12 | (char2 & 63) << 6 | char3 & 63);
continue block5;
}
}
throw new UTFDataFormatException("invalid UTF-8 byte");
}
return out.toString();
}
public static void setCWD(File cwd) {
if (IO.cwd == null) {
IO.cwd = cwd;
IO.setTempDir(IO.getAbsoluteFile("tmp"));
} else {
Exception e = new Exception("Caller stack trace");
log.warn("Attempt to initialize IO twice", (Throwable)e);
}
}
public static void setCQ3Home(File cq3Home) {
IO.setCWD(cq3Home);
}
public static void setTempDir(File tempDir) {
if (IO.tempDir == null) {
IO.tempDir = tempDir;
IO.tempDir.mkdirs();
} else {
Exception e = new Exception("Caller stack trace");
log.warn("Attempt to initialize IO twice", (Throwable)e);
}
}
public static File getTempDir() {
if (tempDir == null) {
throw new InternalError("IO has not been initialized");
}
return tempDir;
}
public static String getAppHome() {
return IO.getCWD().getPath();
}
public static File getAppHomeDirectory() {
return IO.getCWD();
}
public static File getCWD() {
if (cwd == null) {
throw new InternalError("IO has not been initialized");
}
return cwd;
}
public static File getCanonicalFile(String path) {
try {
path = path.replace('/', File.separatorChar);
return new File(path).getCanonicalFile();
}
catch (IOException e) {
log.warn("Unable to canonicalize path: {}", (Object)path);
return null;
}
}
public static boolean isParent(File parent, File child) {
if (!parent.isAbsolute() || !child.isAbsolute()) {
throw new IllegalArgumentException("parent and child must be absolute");
}
while (child != null) {
if (parent.equals(child)) {
return true;
}
child = child.getParentFile();
}
return false;
}
public static File createTempFile(Collection tracker) throws IOException {
return IO.createTempFile(tracker, "cq3", "");
}
public static File createTempFile(Collection tracker, String prefix, String suffix) throws IOException {
if (tracker == null) {
throw new IllegalArgumentException("context == null");
}
File tempFile = File.createTempFile(prefix, suffix, IO.getTempDir());
tracker.add(tempFile);
return tempFile;
}
public static File createTempFile() throws IOException {
return IO.createTempFile("cq3", "");
}
public static File createTempFile(String prefix, String suffix) throws IOException {
return File.createTempFile(prefix, suffix, IO.getTempDir());
}
public static String getAbsolutePath(String path) {
return IO.getAbsoluteFile(path).getPath();
}
public static File getAbsoluteFile(String path) {
File file;
if (path == null) {
path = "";
}
if (!(file = new File(path = path.replace('/', File.separatorChar))).isAbsolute()) {
file = new File(IO.getCWD(), path);
}
return file;
}
/*
* WARNING - Removed try catching itself - possible behaviour change.
*/
public static boolean rename(File src, File dst) {
if (dst.exists() && !dst.delete()) {
log.warn("Error while deleting destination file {}. might be in use.", (Object)dst.getPath());
}
dst.getParentFile().mkdirs();
if (!src.renameTo(dst)) {
log.warn("Error while renaming {} to {}. Copying content.", (Object)src.getPath(), (Object)dst.getPath());
FileInputStream in = null;
FileOutputStream out = null;
try {
in = new FileInputStream(src);
out = new FileOutputStream(dst);
IO.spool((InputStream)in, out);
}
catch (IOException e) {
log.error("Error while spooling {} to {}: {}", new Object[]{src.getPath(), dst.getPath(), e.getMessage()});
boolean bl = false;
return bl;
}
finally {
if (in != null) {
try {
in.close();
}
catch (IOException e) {}
}
if (out != null) {
try {
out.close();
}
catch (IOException e) {}
}
}
if (!src.delete()) {
log.warn("Error while deleting src after spool {}", (Object)src.getPath());
try {
new FileOutputStream(src).close();
}
catch (IOException e) {
// empty catch block
}
src.deleteOnExit();
}
}
return true;
}
static {
Class class_ = IO.class;
log = LoggerFactory.getLogger((Class)class_);
}
}