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
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
|
/*
Copyright (c) 2011 James Ahlborn
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package com.healthmarketscience.jackcess.impl.complex;
import java.io.ByteArrayInputStream;
import java.io.DataInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.nio.ByteBuffer;
import java.time.LocalDateTime;
import java.util.Arrays;
import java.util.Date;
import java.util.HashSet;
import java.util.Set;
import java.util.zip.Deflater;
import java.util.zip.DeflaterOutputStream;
import java.util.zip.InflaterInputStream;
import com.healthmarketscience.jackcess.Column;
import com.healthmarketscience.jackcess.Row;
import com.healthmarketscience.jackcess.Table;
import com.healthmarketscience.jackcess.complex.Attachment;
import com.healthmarketscience.jackcess.complex.AttachmentColumnInfo;
import com.healthmarketscience.jackcess.complex.ComplexDataType;
import com.healthmarketscience.jackcess.complex.ComplexValue;
import com.healthmarketscience.jackcess.complex.ComplexValueForeignKey;
import com.healthmarketscience.jackcess.impl.ByteUtil;
import com.healthmarketscience.jackcess.impl.ColumnImpl;
import com.healthmarketscience.jackcess.impl.JetFormat;
import com.healthmarketscience.jackcess.impl.PageChannel;
/**
* Complex column info for a column holding 0 or more attachments per row.
*
* @author James Ahlborn
*/
public class AttachmentColumnInfoImpl extends ComplexColumnInfoImpl<Attachment>
implements AttachmentColumnInfo
{
/** some file formats which may not be worth re-compressing */
private static final Set<String> COMPRESSED_FORMATS = new HashSet<String>(
Arrays.asList("jpg", "zip", "gz", "bz2", "z", "7z", "cab", "rar",
"mp3", "mpg"));
private static final String FILE_NAME_COL_NAME = "FileName";
private static final String FILE_TYPE_COL_NAME = "FileType";
private static final int DATA_TYPE_RAW = 0;
private static final int DATA_TYPE_COMPRESSED = 1;
private static final int UNKNOWN_HEADER_VAL = 1;
private static final int WRAPPER_HEADER_SIZE = 8;
private static final int CONTENT_HEADER_SIZE = 12;
private final Column _fileUrlCol;
private final Column _fileNameCol;
private final Column _fileTypeCol;
private final Column _fileDataCol;
private final Column _fileTimeStampCol;
private final Column _fileFlagsCol;
public AttachmentColumnInfoImpl(Column column, int complexId,
Table typeObjTable, Table flatTable)
throws IOException
{
super(column, complexId, typeObjTable, flatTable);
Column fileUrlCol = null;
Column fileNameCol = null;
Column fileTypeCol = null;
Column fileDataCol = null;
Column fileTimeStampCol = null;
Column fileFlagsCol = null;
for(Column col : getTypeColumns()) {
switch(col.getType()) {
case TEXT:
if(FILE_NAME_COL_NAME.equalsIgnoreCase(col.getName())) {
fileNameCol = col;
} else if(FILE_TYPE_COL_NAME.equalsIgnoreCase(col.getName())) {
fileTypeCol = col;
} else {
// if names don't match, assign in order: name, type
if(fileNameCol == null) {
fileNameCol = col;
} else if(fileTypeCol == null) {
fileTypeCol = col;
}
}
break;
case LONG:
fileFlagsCol = col;
break;
case SHORT_DATE_TIME:
fileTimeStampCol = col;
break;
case OLE:
fileDataCol = col;
break;
case MEMO:
fileUrlCol = col;
break;
default:
// ignore
}
}
_fileUrlCol = fileUrlCol;
_fileNameCol = fileNameCol;
_fileTypeCol = fileTypeCol;
_fileDataCol = fileDataCol;
_fileTimeStampCol = fileTimeStampCol;
_fileFlagsCol = fileFlagsCol;
}
public Column getFileUrlColumn() {
return _fileUrlCol;
}
public Column getFileNameColumn() {
return _fileNameCol;
}
public Column getFileTypeColumn() {
return _fileTypeCol;
}
public Column getFileDataColumn() {
return _fileDataCol;
}
public Column getFileTimeStampColumn() {
return _fileTimeStampCol;
}
public Column getFileFlagsColumn() {
return _fileFlagsCol;
}
@Override
public ComplexDataType getType()
{
return ComplexDataType.ATTACHMENT;
}
@Override
protected AttachmentImpl toValue(ComplexValueForeignKey complexValueFk,
Row rawValue) {
ComplexValue.Id id = getValueId(rawValue);
String url = (String)getFileUrlColumn().getRowValue(rawValue);
String name = (String)getFileNameColumn().getRowValue(rawValue);
String type = (String)getFileTypeColumn().getRowValue(rawValue);
Integer flags = (Integer)getFileFlagsColumn().getRowValue(rawValue);
Object ts = getFileTimeStampColumn().getRowValue(rawValue);
byte[] data = (byte[])getFileDataColumn().getRowValue(rawValue);
return new AttachmentImpl(id, complexValueFk, url, name, type, null,
ts, flags, data);
}
@Override
protected Object[] asRow(Object[] row, Attachment attachment)
throws IOException
{
super.asRow(row, attachment);
getFileUrlColumn().setRowValue(row, attachment.getFileUrl());
getFileNameColumn().setRowValue(row, attachment.getFileName());
getFileTypeColumn().setRowValue(row, attachment.getFileType());
getFileFlagsColumn().setRowValue(row, attachment.getFileFlags());
getFileTimeStampColumn().setRowValue(row, attachment.getFileTimeStampObject());
getFileDataColumn().setRowValue(row, attachment.getEncodedFileData());
return row;
}
public static Attachment newAttachment(byte[] data) {
return newAttachment(INVALID_FK, data);
}
public static Attachment newAttachment(ComplexValueForeignKey complexValueFk,
byte[] data) {
return newAttachment(complexValueFk, null, null, null, data, null, null);
}
public static Attachment newAttachment(
String url, String name, String type, byte[] data,
Object timeStamp, Integer flags)
{
return newAttachment(INVALID_FK, url, name, type, data,
timeStamp, flags);
}
public static Attachment newAttachment(
ComplexValueForeignKey complexValueFk, String url, String name,
String type, byte[] data, Object timeStamp, Integer flags)
{
return new AttachmentImpl(INVALID_ID, complexValueFk, url, name, type,
data, timeStamp, flags, null);
}
public static Attachment newEncodedAttachment(byte[] encodedData) {
return newEncodedAttachment(INVALID_FK, encodedData);
}
public static Attachment newEncodedAttachment(
ComplexValueForeignKey complexValueFk, byte[] encodedData) {
return newEncodedAttachment(complexValueFk, null, null, null, encodedData,
null, null);
}
public static Attachment newEncodedAttachment(
String url, String name, String type, byte[] encodedData,
Object timeStamp, Integer flags)
{
return newEncodedAttachment(INVALID_FK, url, name, type,
encodedData, timeStamp, flags);
}
public static Attachment newEncodedAttachment(
ComplexValueForeignKey complexValueFk, String url, String name,
String type, byte[] encodedData, Object timeStamp, Integer flags)
{
return new AttachmentImpl(INVALID_ID, complexValueFk, url, name, type,
null, timeStamp, flags, encodedData);
}
@SuppressWarnings("deprecation")
private static class AttachmentImpl extends ComplexValueImpl
implements Attachment
{
private String _url;
private String _name;
private String _type;
private byte[] _data;
private Object _timeStamp;
private Integer _flags;
private byte[] _encodedData;
private AttachmentImpl(Id id, ComplexValueForeignKey complexValueFk,
String url, String name, String type, byte[] data,
Object timeStamp, Integer flags, byte[] encodedData)
{
super(id, complexValueFk);
_url = url;
_name = name;
_type = type;
_data = data;
_timeStamp = timeStamp;
_flags = flags;
_encodedData = encodedData;
}
@Override
public byte[] getFileData() throws IOException {
if((_data == null) && (_encodedData != null)) {
_data = decodeData();
}
return _data;
}
@Override
public void setFileData(byte[] data) {
_data = data;
_encodedData = null;
}
@Override
public byte[] getEncodedFileData() throws IOException {
if((_encodedData == null) && (_data != null)) {
_encodedData = encodeData();
}
return _encodedData;
}
@Override
public void setEncodedFileData(byte[] data) {
_encodedData = data;
_data = null;
}
@Override
public String getFileName() {
return _name;
}
@Override
public void setFileName(String fileName) {
_name = fileName;
}
@Override
public String getFileUrl() {
return _url;
}
@Override
public void setFileUrl(String fileUrl) {
_url = fileUrl;
}
@Override
public String getFileType() {
return _type;
}
@Override
public void setFileType(String fileType) {
_type = fileType;
}
@Override
public Date getFileTimeStamp() {
return (Date)_timeStamp;
}
@Override
public void setFileTimeStamp(Date fileTimeStamp) {
_timeStamp = fileTimeStamp;
}
@Override
public LocalDateTime getFileLocalTimeStamp() {
return (LocalDateTime)_timeStamp;
}
@Override
public void setFileLocalTimeStamp(LocalDateTime fileTimeStamp) {
_timeStamp = fileTimeStamp;
}
@Override
public Object getFileTimeStampObject() {
return _timeStamp;
}
@Override
public Integer getFileFlags() {
return _flags;
}
@Override
public void setFileFlags(Integer fileFlags) {
_flags = fileFlags;
}
@Override
public void update() throws IOException {
getComplexValueForeignKey().updateAttachment(this);
}
@Override
public void delete() throws IOException {
getComplexValueForeignKey().deleteAttachment(this);
}
@Override
public String toString() {
String dataStr = null;
try {
dataStr = ByteUtil.toHexString(getFileData());
} catch(IOException e) {
dataStr = e.toString();
}
return "Attachment(" + getComplexValueForeignKey() + "," + getId() +
") " + getFileUrl() + ", " + getFileName() + ", " + getFileType()
+ ", " + getFileTimeStampObject() + ", " + getFileFlags() + ", " +
dataStr;
}
/**
* Decodes the raw attachment file data to get the _actual_ content.
*/
private byte[] decodeData() throws IOException {
if(_encodedData.length < WRAPPER_HEADER_SIZE) {
// nothing we can do
throw new IOException("Unknown encoded attachment data format");
}
// read initial header info
ByteBuffer bb = PageChannel.wrap(_encodedData);
int typeFlag = bb.getInt();
int dataLen = bb.getInt();
DataInputStream contentStream = null;
try {
InputStream bin = new ByteArrayInputStream(
_encodedData, WRAPPER_HEADER_SIZE,
_encodedData.length - WRAPPER_HEADER_SIZE);
if(typeFlag == DATA_TYPE_RAW) {
// nothing else to do
} else if(typeFlag == DATA_TYPE_COMPRESSED) {
// actual content is deflate compressed
bin = new InflaterInputStream(bin);
} else {
throw new IOException(
"Unknown encoded attachment data type " + typeFlag);
}
contentStream = new DataInputStream(bin);
// header is an unknown flag followed by the "file extension" of the
// data (no clue why we need that again since it's already a separate
// field in the attachment table). just skip all of it
byte[] tmpBytes = new byte[4];
contentStream.readFully(tmpBytes);
int headerLen = PageChannel.wrap(tmpBytes).getInt();
ByteUtil.skipFully(contentStream, headerLen - 4);
// calculate actual data length and read it (note, header length
// includes the bytes for the length)
tmpBytes = new byte[dataLen - headerLen];
contentStream.readFully(tmpBytes);
return tmpBytes;
} finally {
ByteUtil.closeQuietly(contentStream);
}
}
/**
* Encodes the actual attachment file data to get the raw, stored format.
*/
private byte[] encodeData() throws IOException {
// possibly compress data based on file type
String type = ((_type != null) ? _type.toLowerCase() : "");
boolean shouldCompress = !COMPRESSED_FORMATS.contains(type);
// encode extension, which ends w/ a null byte
type += '\0';
ByteBuffer typeBytes = ColumnImpl.encodeUncompressedText(
type, JetFormat.VERSION_12.CHARSET);
int headerLen = typeBytes.remaining() + CONTENT_HEADER_SIZE;
int dataLen = _data.length;
ByteUtil.ByteStream dataStream = new ByteUtil.ByteStream(
WRAPPER_HEADER_SIZE + headerLen + dataLen);
// write the wrapper header info
ByteBuffer bb = PageChannel.wrap(dataStream.getBytes());
bb.putInt(shouldCompress ? DATA_TYPE_COMPRESSED : DATA_TYPE_RAW);
bb.putInt(dataLen + headerLen);
dataStream.skip(WRAPPER_HEADER_SIZE);
OutputStream contentStream = dataStream;
Deflater deflater = null;
try {
if(shouldCompress) {
contentStream = new DeflaterOutputStream(
contentStream, deflater = new Deflater(3));
}
// write the header w/ the file extension
byte[] tmpBytes = new byte[CONTENT_HEADER_SIZE];
PageChannel.wrap(tmpBytes)
.putInt(headerLen)
.putInt(UNKNOWN_HEADER_VAL)
.putInt(type.length());
contentStream.write(tmpBytes);
contentStream.write(typeBytes.array(), 0, typeBytes.remaining());
// write the _actual_ contents
contentStream.write(_data);
contentStream.close();
contentStream = null;
return dataStream.toByteArray();
} finally {
ByteUtil.closeQuietly(contentStream);
if(deflater != null) {
deflater.end();
}
}
}
}
}
|