BlobImpl.java
2.64 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
/*
* Decompiled with CFR 0_118.
*/
package com.day.crx.delite.impl.support;
import com.day.crx.delite.impl.support.Blob;
import com.day.crx.delite.impl.support.BlobFactoryImpl;
import java.io.ByteArrayInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
public class BlobImpl
implements Blob {
private byte[] data = null;
private File tmpFile;
private final BlobFactoryImpl factory;
private boolean detached;
BlobImpl(BlobFactoryImpl factory, byte[] bytes) {
this.factory = factory;
this.data = bytes;
}
BlobImpl(BlobFactoryImpl factory, File file) {
this.factory = factory;
this.tmpFile = file;
}
public byte[] getBytes() throws IOException {
if (this.data == null) {
this.data = new byte[(int)this.tmpFile.length()];
FileInputStream in = new FileInputStream(this.tmpFile);
in.read(this.data);
in.close();
}
return this.data;
}
public byte[] getBytes(int off, int len) throws IOException {
if ((long)(off + len) > this.size()) {
throw new IndexOutOfBoundsException("" + (off + len));
}
if (off == 0 && (long)len == this.size()) {
return this.getBytes();
}
if (this.data != null) {
byte[] ret = new byte[len];
System.arraycopy(this.data, off, ret, 0, len);
return ret;
}
FileInputStream in = new FileInputStream(this.tmpFile);
byte[] ret = new byte[len];
in.skip(off);
in.read(ret);
in.close();
return ret;
}
public long size() throws IOException {
return this.tmpFile == null ? (long)this.data.length : this.tmpFile.length();
}
void discard() throws IOException {
this.data = null;
if (this.tmpFile != null && !this.detached) {
this.factory.freeTmpFile(this.tmpFile);
}
}
public void close() throws IOException {
this.factory.discard(this);
}
public InputStream getInputStream() throws IOException {
return this.tmpFile == null ? new ByteArrayInputStream(this.data) : new FileInputStream(this.tmpFile);
}
public File getFile() throws IOException {
if (this.tmpFile == null) {
this.tmpFile = this.factory.allocateTmpFile();
FileOutputStream out = new FileOutputStream(this.tmpFile);
out.write(this.data);
out.close();
}
return this.tmpFile;
}
public void detach() {
this.detached = true;
}
}