DurboOutputStream.java
5.14 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
/*
* Decompiled with CFR 0_118.
*
* Could not load the following classes:
* javax.jcr.Binary
* javax.jcr.RepositoryException
* javax.jcr.Value
* org.apache.commons.io.FileUtils
* org.apache.commons.io.IOUtils
* org.apache.commons.io.output.DeferredFileOutputStream
*/
package com.day.durbo.impl;
import com.day.durbo.DurboValue;
import com.day.durbo.io.ChunkedDeflaterOutputStream;
import org.apache.commons.io.FileUtils;
import org.apache.commons.io.IOUtils;
import org.apache.commons.io.output.DeferredFileOutputStream;
import javax.jcr.Binary;
import javax.jcr.RepositoryException;
import javax.jcr.Value;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
public class DurboOutputStream {
private static final byte[] NULL_BYTES = new byte[]{0, 0, 0, 0};
private static final byte[] OVERFLOW_MARKER = new byte[]{-1, -1, -1, -1};
private OutputStream out;
public DurboOutputStream(OutputStream out) {
this.out = out;
}
public void enableCompression() {
ChunkedDeflaterOutputStream os = new ChunkedDeflaterOutputStream(this.out);
os.setAutoRestart(0x100000);
this.out = os;
}
public void writeInt(int i) throws IOException {
if (i == 0) {
this.out.write(NULL_BYTES);
} else {
this.out.write(new byte[]{(byte)(i >> 24), (byte)(i >> 16), (byte)(i >> 8), (byte)i});
}
}
public void writeSize(long l) throws IOException {
if (l == 0) {
this.out.write(NULL_BYTES);
} else if (l < Integer.MAX_VALUE) {
this.out.write(new byte[]{(byte)(l >> 24), (byte)(l >> 16), (byte)(l >> 8), (byte)l});
} else {
this.out.write(OVERFLOW_MARKER);
this.out.write(new byte[]{(byte)(l >> 56), (byte)(l >> 48), (byte)(l >> 40), (byte)(l >> 32), (byte)(l >> 24), (byte)(l >> 16), (byte)(l >> 8), (byte)l});
}
}
public void writeByte(int b) throws IOException {
this.out.write(b);
}
public void write(String s) throws IOException {
this.write(s.getBytes("utf-8"));
}
public void write(byte[] data) throws IOException {
this.writeInt(data.length);
this.out.write(data);
}
public void write(InputStream in, int size) throws IOException {
this.write(in, (long)size);
}
public void write(InputStream in, long size) throws IOException {
if (size < 0) {
throw new IllegalArgumentException("Size must be greater than zero");
}
this.writeSize(size);
byte[] buffer = new byte[8192];
int rd = 0;
long num = size;
while (num > 0 && rd >= 0) {
rd = in.read(buffer, 0, (int)Math.min(num, (long)buffer.length));
if (rd <= 0) continue;
this.out.write(buffer, 0, rd);
num -= (long)rd;
}
long written = size - num;
for (long left = size - written; left > 0; --left) {
this.out.write(0);
}
}
public void write(InputStream in) throws IOException {
int MAX_MEM = 1048576;
DeferredFileOutputStream fout = new DeferredFileOutputStream(1048576, "__durbo", ".tmp", null);
IOUtils.copy((InputStream)in, (OutputStream)fout);
fout.close();
this.writeSize(fout.getByteCount());
if (fout.isInMemory()) {
this.out.write(fout.getData());
} else {
in = FileUtils.openInputStream((File)fout.getFile());
IOUtils.copy((InputStream)in, (OutputStream)this.out);
IOUtils.closeQuietly((InputStream)in);
fout.getFile().delete();
}
}
/*
* WARNING - Removed try catching itself - possible behaviour change.
*/
public void write(Value value) throws IOException, RepositoryException {
if (value.getType() == 2) {
Binary bin = value.getBinary();
this.writeSize(bin.getSize());
InputStream in = bin.getStream();
try {
IOUtils.copy((InputStream)in, (OutputStream)this.out);
}
finally {
IOUtils.closeQuietly((InputStream)in);
bin.dispose();
}
}
this.write(value.getString());
}
/*
* WARNING - Removed try catching itself - possible behaviour change.
* Enabled aggressive block sorting
* Enabled unnecessary exception pruning
* Enabled aggressive exception aggregation
*/
public void write(DurboValue value) throws IOException {
if (value.getType() == 2) {
if (value.isByteArrayBased()) {
this.write(value.getBytes());
return;
}
this.writeSize(value.getLength());
InputStream in = value.getStream();
try {
IOUtils.copy((InputStream)in, (OutputStream)this.out);
return;
}
finally {
IOUtils.closeQuietly((InputStream)in);
}
} else {
this.write(value.getString());
}
}
public void close() throws IOException {
this.out.close();
}
}