IOUtils.java
3.59 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
/*
* Decompiled with CFR 0_118.
*
* Could not load the following classes:
* org.apache.felix.scr.annotations.Component
* org.apache.felix.scr.annotations.Reference
* org.apache.felix.scr.annotations.References
* org.slf4j.Logger
* org.slf4j.LoggerFactory
*/
package com.adobe.aemfd.docmanager.io;
import com.adobe.aemfd.docmanager.TempFileManager;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import org.apache.felix.scr.annotations.Component;
import org.apache.felix.scr.annotations.Reference;
import org.apache.felix.scr.annotations.References;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
@Component(immediate=1)
@References(value={@Reference(name="tempFileManager", referenceInterface=TempFileManager.class)})
public class IOUtils {
private static final int BUFFER_SIZE = 4096;
private static final OutputStream NULL_OUTPUT_STREAM = new NullOutputStream();
private static TempFileManager tempFileManager;
private static final Logger logger;
public static long copy(InputStream is, OutputStream os, Long maxToRead) throws IOException {
int bufSize;
long leeway;
int read;
long totalRead = 0;
byte[] buffer = new byte[4096];
long l = leeway = maxToRead == null ? Long.MAX_VALUE : maxToRead;
while (leeway > 0 && (read = is.read(buffer, 0, bufSize = leeway >= (long)buffer.length ? buffer.length : (int)leeway)) != -1) {
os.write(buffer, 0, read);
totalRead += (long)read;
leeway -= (long)read;
}
os.flush();
return totalRead;
}
public static long copy(InputStream is, OutputStream os) throws IOException {
return IOUtils.copy(is, os, null);
}
/*
* WARNING - Removed try catching itself - possible behaviour change.
*/
public static long copyToFile(InputStream is, File f) throws IOException {
FileOutputStream fos = new FileOutputStream(f);
try {
long l = IOUtils.copy(is, fos);
return l;
}
finally {
try {
fos.close();
}
catch (IOException e) {
logger.warn("Error closing output stream for file " + f.getAbsolutePath(), (Throwable)e);
}
}
}
public static long length(InputStream is) throws IOException {
return IOUtils.copy(is, NULL_OUTPUT_STREAM);
}
public static byte[] getBytes(InputStream is, Integer maxToRead) throws IOException {
ByteArrayOutputStream baos = new ByteArrayOutputStream(4096);
IOUtils.copy(is, baos, maxToRead == null ? null : Long.valueOf(maxToRead.longValue()));
return baos.toByteArray();
}
public static File createTempFile(String suffix) throws IOException {
return tempFileManager.getTempFile("docmanager-", suffix == null ? ".dm" : suffix);
}
public static TempFileManager getTempFileManager() {
return tempFileManager;
}
public void bindTempFileManager(TempFileManager tfm) {
tempFileManager = tfm;
}
public void unbindTempFileManager(TempFileManager tfm) {
tempFileManager = null;
}
static {
logger = LoggerFactory.getLogger(IOUtils.class);
}
private static class NullOutputStream
extends OutputStream {
private NullOutputStream() {
}
public void write(int b) throws IOException {
}
public void write(byte[] b, int off, int len) throws IOException {
}
}
}