1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17 package com.healthmarketscience.jackcess.util;
18
19 import java.io.Closeable;
20 import java.io.File;
21 import java.io.FileInputStream;
22 import java.io.FileNotFoundException;
23 import java.io.IOException;
24 import java.io.InputStream;
25 import java.io.OutputStream;
26 import java.sql.Blob;
27 import java.util.stream.Stream;
28 import java.util.stream.StreamSupport;
29
30 import com.healthmarketscience.jackcess.impl.OleUtil;
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 public interface OleBlob extends Blob, Closeable
101 {
102
103
104 public enum ContentType {
105
106
107 LINK,
108
109
110
111 SIMPLE_PACKAGE,
112
113
114
115
116
117 COMPOUND_STORAGE,
118
119
120
121 OTHER,
122
123
124
125 UNKNOWN;
126 }
127
128
129
130
131
132
133
134 public void writeTo(OutputStream out) throws IOException;
135
136
137
138
139 public Content getContent() throws IOException;
140
141
142 public interface Content
143 {
144
145
146
147 public ContentType getType();
148
149
150
151
152 public OleBlob getBlob();
153 }
154
155
156
157
158 public interface PackageContent extends Content
159 {
160 public String getPrettyName() throws IOException;
161
162 public String getClassName() throws IOException;
163
164 public String getTypeName() throws IOException;
165 }
166
167
168
169
170 public interface EmbeddedContent extends Content
171 {
172 public long length();
173
174 public InputStream getStream() throws IOException;
175
176 public void writeTo(OutputStream out) throws IOException;
177 }
178
179
180
181
182
183
184 public interface LinkContent extends PackageContent
185 {
186 public String getFileName();
187
188 public String getLinkPath();
189
190 public String getFilePath();
191
192 public InputStream getLinkStream() throws IOException;
193 }
194
195
196
197
198
199
200
201 public interface SimplePackageContent
202 extends PackageContent, EmbeddedContent
203 {
204 public String getFileName();
205
206 public String getFilePath();
207
208 public String getLocalFilePath();
209 }
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224 public interface CompoundContent extends PackageContent, EmbeddedContent,
225 Iterable<CompoundContent.Entry>
226 {
227 public Entry getEntry(String entryName) throws IOException;
228
229 public boolean hasContentsEntry() throws IOException;
230
231 public Entry getContentsEntry() throws IOException;
232
233
234
235
236 default public Stream<CompoundContent.Entry> stream() {
237 return StreamSupport.stream(spliterator(), false);
238 }
239
240
241
242
243 public interface Entry extends EmbeddedContent
244 {
245 public String getName();
246
247
248
249
250 public CompoundContent getParent();
251 }
252 }
253
254
255
256
257
258
259 public interface OtherContent extends PackageContent, EmbeddedContent
260 {
261 }
262
263
264
265
266
267 public class Builder
268 {
269 public static final String PACKAGE_PRETTY_NAME = "Packager Shell Object";
270 public static final String PACKAGE_TYPE_NAME = "Package";
271
272 private ContentType _type;
273 private byte[] _bytes;
274 private InputStream _stream;
275 private long _contentLen;
276 private String _fileName;
277 private String _filePath;
278 private String _prettyName;
279 private String _className;
280 private String _typeName;
281
282 public ContentType getType() {
283 return _type;
284 }
285
286 public byte[] getBytes() {
287 return _bytes;
288 }
289
290 public InputStream getStream() {
291 return _stream;
292 }
293
294 public long getContentLength() {
295 return _contentLen;
296 }
297
298 public String getFileName() {
299 return _fileName;
300 }
301
302 public String getFilePath() {
303 return _filePath;
304 }
305
306 public String getPrettyName() {
307 return _prettyName;
308 }
309
310 public String getClassName() {
311 return _className;
312 }
313
314 public String getTypeName() {
315 return _typeName;
316 }
317
318 public Builder setSimplePackageBytes(byte[] bytes) {
319 _bytes = bytes;
320 _contentLen = bytes.length;
321 setDefaultPackageType();
322 _type = ContentType.SIMPLE_PACKAGE;
323 return this;
324 }
325
326 public Builder setSimplePackageStream(InputStream in, long length) {
327 _stream = in;
328 _contentLen = length;
329 setDefaultPackageType();
330 _type = ContentType.SIMPLE_PACKAGE;
331 return this;
332 }
333
334 public Builder setSimplePackageFileName(String fileName) {
335 _fileName = fileName;
336 setDefaultPackageType();
337 _type = ContentType.SIMPLE_PACKAGE;
338 return this;
339 }
340
341 public Builder setSimplePackageFilePath(String filePath) {
342 _filePath = filePath;
343 setDefaultPackageType();
344 _type = ContentType.SIMPLE_PACKAGE;
345 return this;
346 }
347
348 public Builder setSimplePackage(File f) throws FileNotFoundException {
349 _fileName = f.getName();
350 _filePath = f.getAbsolutePath();
351 return setSimplePackageStream(new FileInputStream(f), f.length());
352 }
353
354 public Builder setLinkFileName(String fileName) {
355 _fileName = fileName;
356 setDefaultPackageType();
357 _type = ContentType.LINK;
358 return this;
359 }
360
361 public Builder setLinkPath(String link) {
362 _filePath = link;
363 setDefaultPackageType();
364 _type = ContentType.LINK;
365 return this;
366 }
367
368 public Builder setLink(File f) {
369 _fileName = f.getName();
370 _filePath = f.getAbsolutePath();
371 setDefaultPackageType();
372 _type = ContentType.LINK;
373 return this;
374 }
375
376 private void setDefaultPackageType() {
377 if(_prettyName == null) {
378 _prettyName = PACKAGE_PRETTY_NAME;
379 }
380 if(_className == null) {
381 _className = PACKAGE_TYPE_NAME;
382 }
383 }
384
385 public Builder setOtherBytes(byte[] bytes) {
386 _bytes = bytes;
387 _contentLen = bytes.length;
388 _type = ContentType.OTHER;
389 return this;
390 }
391
392 public Builder setOtherStream(InputStream in, long length) {
393 _stream = in;
394 _contentLen = length;
395 _type = ContentType.OTHER;
396 return this;
397 }
398
399 public Builder setOther(File f) throws FileNotFoundException {
400 return setOtherStream(new FileInputStream(f), f.length());
401 }
402
403 public Builder setPackagePrettyName(String prettyName) {
404 _prettyName = prettyName;
405 return this;
406 }
407
408 public Builder setPackageClassName(String className) {
409 _className = className;
410 return this;
411 }
412
413 public Builder setPackageTypeName(String typeName) {
414 _typeName = typeName;
415 return this;
416 }
417
418 public OleBlob toBlob() throws IOException {
419 return OleUtil.createBlob(this);
420 }
421
422 public static OleBlob fromInternalData(byte[] bytes) throws IOException {
423 return OleUtil.parseBlob(bytes);
424 }
425 }
426 }