/
Base64Data.java
542 lines (486 loc) · 16 KB
/
Base64Data.java
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
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
/*
* Copyright (c) 1997, 2021 Oracle and/or its affiliates. All rights reserved.
*
* This program and the accompanying materials are made available under the
* terms of the Eclipse Distribution License v. 1.0, which is available at
* http://www.eclipse.org/org/documents/edl-v10.php.
*
* SPDX-License-Identifier: BSD-3-Clause
*/
package org.jvnet.staxex;
import jakarta.activation.DataHandler;
import jakarta.activation.DataSource;
import java.io.ByteArrayInputStream;
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 javax.xml.stream.XMLStreamException;
import javax.xml.stream.XMLStreamWriter;
import java.util.logging.Level;
import java.util.logging.Logger;
// for testing method
//import com.sun.xml.stream.writers.XMLStreamWriterImpl;
//import java.io.FileNotFoundException;
//import java.io.FileWriter;
//import jakarta.activation.FileDataSource;
/**
* Binary data represented as base64-encoded string
* in XML.
*
* <p>
* Used in conjunction with {@link XMLStreamReaderEx}
* and {@link XMLStreamWriterEx}.
*
* @author Kohsuke Kawaguchi, Martin Grebac
*/
public class Base64Data implements CharSequence, Cloneable {
// either dataHandler or (data,dataLen,mimeType?) must be present
// (note that having both is allowed)
private DataHandler dataHandler;
private byte[] data;
private String hrefCid;
/**
* Length of the valid data in {@link #data}.
*/
private int dataLen;
/**
* True if {@link #data} can be cloned by reference
* if Base64Data instance is cloned.
*/
private boolean dataCloneByRef;
/**
* Optional MIME type of {@link #data}.
*
* Unused when {@link #dataHandler} is set.
* Use {@link DataHandler#getContentType()} in that case.
*/
private String mimeType;
/**
* Default constructor
*/
public Base64Data() {
}
private static final Logger logger = Logger.getLogger(Base64Data.class.getName());
/**
* Clone constructor
* @param that needs to be cloned
*/
public Base64Data(Base64Data that) {
that.get();
if (that.dataCloneByRef) {
this.data = that.data;
} else {
this.data = new byte[that.dataLen];
System.arraycopy(that.data, 0, this.data, 0, that.dataLen);
}
this.dataCloneByRef = true;
this.dataLen = that.dataLen;
this.dataHandler = null;
this.mimeType = that.mimeType;
}
/**
* Fills in the data object by a portion of the byte[].
*
* @param data actual data
* @param len
* data[0] to data[len-1] are treated as the data.
* @param mimeType MIME type
* @param cloneByRef
* true if data[] can be cloned by reference
*/
public void set(byte[] data, int len, String mimeType, boolean cloneByRef) {
this.data = data;
this.dataLen = len;
this.dataCloneByRef = cloneByRef;
this.dataHandler = null;
this.mimeType = mimeType;
}
/**
* Fills in the data object by a portion of the byte[].
*
* @param data actual data bytes
* @param len
* data[0] to data[len-1] are treated as the data.
* @param mimeType MIME type
*/
public void set(byte[] data, int len, String mimeType) {
set(data,len,mimeType,false);
}
/**
* Fills in the data object by the byte[] of the exact length.
*
* @param data
* this buffer may be owned directly by the unmarshaleld JAXB object.
* @param mimeType MIME type
*/
public void set(byte[] data,String mimeType) {
set(data,data.length,mimeType,false);
}
/**
* Fills in the data object by a {@link DataHandler}.
*
* @param data DataHandler for the data
*/
public void set(DataHandler data) {
assert data!=null;
this.dataHandler = data;
this.data = null;
}
/**
* Gets the raw data. If the returned DataHandler is {@link StreamingDataHandler},
* callees may need to downcast to take advantage of its capabilities.
*
* @see StreamingDataHandler
* @return DataHandler for the data
*/
public DataHandler getDataHandler() {
if(dataHandler==null){
dataHandler = new Base64StreamingDataHandler(new Base64DataSource());
} else if (!(dataHandler instanceof StreamingDataHandler)) {
dataHandler = new FilterDataHandler(dataHandler);
}
return dataHandler;
}
private final class Base64DataSource implements DataSource {
public String getContentType() {
return getMimeType();
}
public InputStream getInputStream() {
return new ByteArrayInputStream(data,0,dataLen);
}
public String getName() {
return null;
}
public OutputStream getOutputStream() {
throw new UnsupportedOperationException();
}
}
private final class Base64StreamingDataHandler extends StreamingDataHandler {
Base64StreamingDataHandler(DataSource source) {
super(source);
}
@Override
public InputStream readOnce() throws IOException {
return getDataSource().getInputStream();
}
@Override
public void moveTo(File dst) throws IOException {
FileOutputStream fout = new FileOutputStream(dst);
try {
fout.write(data, 0, dataLen);
} finally {
fout.close();
}
}
@Override
public void close() throws IOException {
// nothing to do
}
}
private static final class FilterDataHandler extends StreamingDataHandler {
FilterDataHandler(DataHandler dh) {
super(dh.getDataSource());
}
@Override
public InputStream readOnce() throws IOException {
return getDataSource().getInputStream();
}
@Override
public void moveTo(File dst) throws IOException {
byte[] buf = new byte[8192];
InputStream in = null;
OutputStream out = null;
try {
in = getDataSource().getInputStream();
out = new FileOutputStream(dst);
while (true) {
int amountRead = in.read(buf);
if (amountRead == -1) {
break;
}
out.write(buf, 0, amountRead);
}
} finally {
if (in != null) {
try {
in.close();
} catch(IOException ioe) {
// nothing to do
}
}
if (out != null) {
try {
out.close();
} catch(IOException ioe) {
// nothing to do
}
}
}
}
@Override
public void close() throws IOException {
// nothing to do
}
}
/**
* Gets the byte[] of the exact length.
*
* @return byte[] for data
*/
public byte[] getExact() {
get();
if(dataLen!=data.length) {
byte[] buf = new byte[dataLen];
System.arraycopy(data,0,buf,0,dataLen);
data = buf;
}
return data;
}
/**
* Gets the data as an {@link InputStream}.
*
* @return data as InputStream
* @throws IOException if i/o error occurs
*/
public InputStream getInputStream() throws IOException {
if(dataHandler!=null) {
return dataHandler.getInputStream();
} else {
return new ByteArrayInputStream(data,0,dataLen);
}
}
/**
* Returns false if this object only has {@link DataHandler} and therefore
* {@link #get()} operation is likely going to be expensive.
*
* @return false if it has only DataHandler
*/
public boolean hasData() {
return data!=null;
}
/**
* Gets the raw data. The size of the byte array maybe larger than the actual length.
*
* @return data as byte[], the array may be larger
*/
public byte[] get() {
if(data==null) {
try {
ByteArrayOutputStreamEx baos = new ByteArrayOutputStreamEx(1024);
InputStream is = dataHandler.getDataSource().getInputStream();
baos.readFrom(is);
is.close();
data = baos.getBuffer();
dataLen = baos.size();
dataCloneByRef = true;
} catch (IOException e) {
// TODO: report the error to the unmarshaller
dataLen = 0; // recover by assuming length-0 data
}
}
return data;
}
/**
* Gets the length of the binary data counted in bytes.
*
* Note that if this object encapsulates {@link DataHandler},
* this method would have to read the whole thing into {@code byte[]}
* just to count the length, because {@link DataHandler}
* doesn't easily expose the length.
*
* @return no of bytes
*/
public int getDataLen() {
get();
return dataLen;
}
public String getMimeType() {
if (mimeType==null) {
return "application/octet-stream";
}
return mimeType;
}
/**
* Gets the number of characters needed to represent
* this binary data in the base64 encoding.
*/
@Override
public int length() {
// for each 3 bytes you use 4 chars
// if the remainder is 1 or 2 there will be 4 more
get(); // fill in the buffer if necessary
return ((dataLen+2)/3)*4;
}
/**
* Encode this binary data in the base64 encoding
* and returns the character at the specified position.
*/
@Override
public char charAt(int index) {
// we assume that the length() method is called before this method
// (otherwise how would the caller know that the index is valid?)
// so we assume that the byte[] is already populated
int offset = index%4;
int base = (index/4)*3;
byte b1,b2;
switch(offset) {
case 0:
return Base64Encoder.encode(data[base]>>2);
case 1:
if (base+1<dataLen) {
b1 = data[base+1];
} else {
b1 = 0;
}
return Base64Encoder.encode(
((data[base]&0x3)<<4) |
((b1>>4)&0xF));
case 2:
if (base+1<dataLen) {
b1 = data[base+1];
if (base+2<dataLen) {
b2 = data[base+2];
} else {
b2 = 0;
}
return Base64Encoder.encode(
((b1&0xF)<<2)|
((b2>>6)&0x3));
} else {
return '=';
}
case 3:
if(base+2<dataLen) {
return Base64Encoder.encode(data[base+2]&0x3F);
} else {
return '=';
}
}
throw new IllegalStateException();
}
/**
* Internally this is only used to split a text to a list,
* which doesn't happen that much for base64.
* So this method should be smaller than faster.
*/
@Override
public CharSequence subSequence(int start, int end) {
StringBuilder buf = new StringBuilder();
get(); // fill in the buffer if we haven't done so
for (int i=start; i<end; i++ ) {
buf.append(charAt(i));
}
return buf;
}
/**
* Returns the base64 encoded string of this data.
*/
@Override
public String toString() {
get(); // fill in the buffer
return Base64Encoder.print(data, 0, dataLen);
}
public void writeTo(char[] buf, int start) {
get();
Base64Encoder.print(data, 0, dataLen, buf, start);
}
private static final int CHUNK_SIZE;
static {
int bufSize = 1024;
try {
String bufSizeStr = getProperty("org.jvnet.staxex.Base64DataStreamWriteBufferSize");
if (bufSizeStr != null) {
bufSize = Integer.parseInt(bufSizeStr);
}
} catch (Exception e) {
logger.log(Level.INFO, "Error reading org.jvnet.staxex.Base64DataStreamWriteBufferSize property", e);
}
CHUNK_SIZE = bufSize;
}
public void writeTo(XMLStreamWriter output) throws IOException, XMLStreamException {
if (data==null) {
try {
InputStream is = dataHandler.getDataSource().getInputStream();
ByteArrayOutputStream outStream = new ByteArrayOutputStream(); // dev-null stream
Base64EncoderStream encWriter = new Base64EncoderStream(output, outStream);
int b;
byte[] buffer = new byte[CHUNK_SIZE];
while ((b = is.read(buffer)) != -1) {
encWriter.write(buffer, 0, b);
}
outStream.close();
encWriter.close();
} catch (IOException e) {
dataLen = 0; // recover by assuming length-0 data
throw e;
}
} else {
// the data is already in memory and not streamed
String s = Base64Encoder.print(data, 0, dataLen);
output.writeCharacters(s);
}
}
@Override
public Base64Data clone() {
try {
Base64Data clone = (Base64Data) super.clone();
clone.get();
if (clone.dataCloneByRef) {
this.data = clone.data;
} else {
this.data = new byte[clone.dataLen];
System.arraycopy(clone.data, 0, this.data, 0, clone.dataLen);
}
this.dataCloneByRef = true;
this.dataLen = clone.dataLen;
this.dataHandler = null;
this.mimeType = clone.mimeType;
return clone;
} catch (CloneNotSupportedException ex) {
Logger.getLogger(Base64Data.class.getName()).log(Level.SEVERE, null, ex);
return null;
}
}
static String getProperty(final String propName) {
if (System.getSecurityManager() == null) {
return System.getProperty(propName);
} else {
return (String) java.security.AccessController.doPrivileged(
new java.security.PrivilegedAction<Object>() {
@Override
public java.lang.Object run() {
return System.getProperty(propName);
}
});
}
}
// public static void main(String[] args) throws FileNotFoundException, IOException, XMLStreamException {
// Base64Data data = new Base64Data();
//
// File f = new File("/Users/snajper/work/builds/weblogic/wls1211_dev.zip");
// FileDataSource fds = new FileDataSource(f);
// DataHandler dh = new DataHandler(fds);
// data.set(dh);
//
// FileWriter fw = new FileWriter(new File("/Users/snajper/Desktop/b.txt"));
// XMLStreamWriterImpl wi = new XMLStreamWriterImpl(fw, null);
//
// data.writeTo(wi);
// wi.flush();fw.flush();
// //System.out.println("SW: " + sw.toString());
//
// }
public String getHrefCid() {
if (hrefCid == null && dataHandler != null && dataHandler instanceof StreamingDataHandler) {
hrefCid = ((StreamingDataHandler)dataHandler).getHrefCid();
}
return hrefCid;
}
public void setHrefCid(final String cid) {
this.hrefCid = cid;
if (dataHandler != null && dataHandler instanceof StreamingDataHandler) ((StreamingDataHandler)dataHandler).setHrefCid(cid);
}
}