Thu, 31 Aug 2017 15:18:52 +0800
merge
aoqi@0 | 1 | /* |
aoqi@0 | 2 | * Copyright (c) 1997, 2012, Oracle and/or its affiliates. All rights reserved. |
aoqi@0 | 3 | * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. |
aoqi@0 | 4 | * |
aoqi@0 | 5 | * This code is free software; you can redistribute it and/or modify it |
aoqi@0 | 6 | * under the terms of the GNU General Public License version 2 only, as |
aoqi@0 | 7 | * published by the Free Software Foundation. Oracle designates this |
aoqi@0 | 8 | * particular file as subject to the "Classpath" exception as provided |
aoqi@0 | 9 | * by Oracle in the LICENSE file that accompanied this code. |
aoqi@0 | 10 | * |
aoqi@0 | 11 | * This code is distributed in the hope that it will be useful, but WITHOUT |
aoqi@0 | 12 | * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
aoqi@0 | 13 | * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
aoqi@0 | 14 | * version 2 for more details (a copy is included in the LICENSE file that |
aoqi@0 | 15 | * accompanied this code). |
aoqi@0 | 16 | * |
aoqi@0 | 17 | * You should have received a copy of the GNU General Public License version |
aoqi@0 | 18 | * 2 along with this work; if not, write to the Free Software Foundation, |
aoqi@0 | 19 | * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. |
aoqi@0 | 20 | * |
aoqi@0 | 21 | * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA |
aoqi@0 | 22 | * or visit www.oracle.com if you need additional information or have any |
aoqi@0 | 23 | * questions. |
aoqi@0 | 24 | */ |
aoqi@0 | 25 | |
aoqi@0 | 26 | package com.sun.xml.internal.org.jvnet.staxex; |
aoqi@0 | 27 | |
aoqi@0 | 28 | import javax.activation.DataHandler; |
aoqi@0 | 29 | import javax.activation.DataSource; |
aoqi@0 | 30 | import java.io.ByteArrayInputStream; |
aoqi@0 | 31 | import java.io.ByteArrayOutputStream; |
aoqi@0 | 32 | import java.io.File; |
aoqi@0 | 33 | import java.io.FileOutputStream; |
aoqi@0 | 34 | import java.io.IOException; |
aoqi@0 | 35 | import java.io.InputStream; |
aoqi@0 | 36 | import java.io.OutputStream; |
aoqi@0 | 37 | import javax.xml.stream.XMLStreamException; |
aoqi@0 | 38 | import javax.xml.stream.XMLStreamWriter; |
aoqi@0 | 39 | import java.util.logging.Level; |
aoqi@0 | 40 | import java.util.logging.Logger; |
aoqi@0 | 41 | |
aoqi@0 | 42 | // for testing method |
aoqi@0 | 43 | //import com.sun.xml.internal.stream.writers.XMLStreamWriterImpl; |
aoqi@0 | 44 | //import java.io.FileNotFoundException; |
aoqi@0 | 45 | //import java.io.FileWriter; |
aoqi@0 | 46 | //import javax.activation.FileDataSource; |
aoqi@0 | 47 | |
aoqi@0 | 48 | /** |
aoqi@0 | 49 | * Binary data represented as base64-encoded string |
aoqi@0 | 50 | * in XML. |
aoqi@0 | 51 | * |
aoqi@0 | 52 | * <p> |
aoqi@0 | 53 | * Used in conjunction with {@link XMLStreamReaderEx} |
aoqi@0 | 54 | * and {@link XMLStreamWriterEx}. |
aoqi@0 | 55 | * |
aoqi@0 | 56 | * @author Kohsuke Kawaguchi, Martin Grebac |
aoqi@0 | 57 | */ |
aoqi@0 | 58 | public class Base64Data implements CharSequence, Cloneable { |
aoqi@0 | 59 | |
aoqi@0 | 60 | // either dataHandler or (data,dataLen,mimeType?) must be present |
aoqi@0 | 61 | // (note that having both is allowed) |
aoqi@0 | 62 | |
aoqi@0 | 63 | private DataHandler dataHandler; |
aoqi@0 | 64 | private byte[] data; |
aoqi@0 | 65 | |
aoqi@0 | 66 | /** |
aoqi@0 | 67 | * Length of the valid data in {@link #data}. |
aoqi@0 | 68 | */ |
aoqi@0 | 69 | private int dataLen; |
aoqi@0 | 70 | /** |
aoqi@0 | 71 | * True if {@link #data} can be cloned by reference |
aoqi@0 | 72 | * if Base64Data instance is cloned. |
aoqi@0 | 73 | */ |
aoqi@0 | 74 | private boolean dataCloneByRef; |
aoqi@0 | 75 | /** |
aoqi@0 | 76 | * Optional MIME type of {@link #data}. |
aoqi@0 | 77 | * |
aoqi@0 | 78 | * Unused when {@link #dataHandler} is set. |
aoqi@0 | 79 | * Use {@link DataHandler#getContentType()} in that case. |
aoqi@0 | 80 | */ |
aoqi@0 | 81 | private String mimeType; |
aoqi@0 | 82 | |
aoqi@0 | 83 | /** |
aoqi@0 | 84 | * Default constructor |
aoqi@0 | 85 | */ |
aoqi@0 | 86 | public Base64Data() { |
aoqi@0 | 87 | } |
aoqi@0 | 88 | |
aoqi@0 | 89 | private static final Logger logger = Logger.getLogger(Base64Data.class.getName()); |
aoqi@0 | 90 | |
aoqi@0 | 91 | /** |
aoqi@0 | 92 | * Clone constructor |
aoqi@0 | 93 | * @param that needs to be cloned |
aoqi@0 | 94 | */ |
aoqi@0 | 95 | public Base64Data(Base64Data that) { |
aoqi@0 | 96 | that.get(); |
aoqi@0 | 97 | if (that.dataCloneByRef) { |
aoqi@0 | 98 | this.data = that.data; |
aoqi@0 | 99 | } else { |
aoqi@0 | 100 | this.data = new byte[that.dataLen]; |
aoqi@0 | 101 | System.arraycopy(that.data, 0, this.data, 0, that.dataLen); |
aoqi@0 | 102 | } |
aoqi@0 | 103 | |
aoqi@0 | 104 | this.dataCloneByRef = true; |
aoqi@0 | 105 | this.dataLen = that.dataLen; |
aoqi@0 | 106 | this.dataHandler = null; |
aoqi@0 | 107 | this.mimeType = that.mimeType; |
aoqi@0 | 108 | } |
aoqi@0 | 109 | |
aoqi@0 | 110 | /** |
aoqi@0 | 111 | * Fills in the data object by a portion of the byte[]. |
aoqi@0 | 112 | * |
aoqi@0 | 113 | * @param data actual data |
aoqi@0 | 114 | * @param len |
aoqi@0 | 115 | * data[0] to data[len-1] are treated as the data. |
aoqi@0 | 116 | * @param mimeType MIME type |
aoqi@0 | 117 | * @param cloneByRef |
aoqi@0 | 118 | * true if data[] can be cloned by reference |
aoqi@0 | 119 | */ |
aoqi@0 | 120 | public void set(byte[] data, int len, String mimeType, boolean cloneByRef) { |
aoqi@0 | 121 | this.data = data; |
aoqi@0 | 122 | this.dataLen = len; |
aoqi@0 | 123 | this.dataCloneByRef = cloneByRef; |
aoqi@0 | 124 | this.dataHandler = null; |
aoqi@0 | 125 | this.mimeType = mimeType; |
aoqi@0 | 126 | } |
aoqi@0 | 127 | |
aoqi@0 | 128 | /** |
aoqi@0 | 129 | * Fills in the data object by a portion of the byte[]. |
aoqi@0 | 130 | * |
aoqi@0 | 131 | * @param data actual data bytes |
aoqi@0 | 132 | * @param len |
aoqi@0 | 133 | * data[0] to data[len-1] are treated as the data. |
aoqi@0 | 134 | * @param mimeType MIME type |
aoqi@0 | 135 | */ |
aoqi@0 | 136 | public void set(byte[] data, int len, String mimeType) { |
aoqi@0 | 137 | set(data,len,mimeType,false); |
aoqi@0 | 138 | } |
aoqi@0 | 139 | |
aoqi@0 | 140 | /** |
aoqi@0 | 141 | * Fills in the data object by the byte[] of the exact length. |
aoqi@0 | 142 | * |
aoqi@0 | 143 | * @param data |
aoqi@0 | 144 | * this buffer may be owned directly by the unmarshaleld JAXB object. |
aoqi@0 | 145 | * @param mimeType MIME type |
aoqi@0 | 146 | */ |
aoqi@0 | 147 | public void set(byte[] data,String mimeType) { |
aoqi@0 | 148 | set(data,data.length,mimeType,false); |
aoqi@0 | 149 | } |
aoqi@0 | 150 | |
aoqi@0 | 151 | /** |
aoqi@0 | 152 | * Fills in the data object by a {@link DataHandler}. |
aoqi@0 | 153 | * |
aoqi@0 | 154 | * @param data DataHandler for the data |
aoqi@0 | 155 | */ |
aoqi@0 | 156 | public void set(DataHandler data) { |
aoqi@0 | 157 | assert data!=null; |
aoqi@0 | 158 | this.dataHandler = data; |
aoqi@0 | 159 | this.data = null; |
aoqi@0 | 160 | } |
aoqi@0 | 161 | |
aoqi@0 | 162 | /** |
aoqi@0 | 163 | * Gets the raw data. If the returned DataHandler is {@link StreamingDataHandler}, |
aoqi@0 | 164 | * callees may need to downcast to take advantage of its capabilities. |
aoqi@0 | 165 | * |
aoqi@0 | 166 | * @see StreamingDataHandler |
aoqi@0 | 167 | * @return DataHandler for the data |
aoqi@0 | 168 | */ |
aoqi@0 | 169 | public DataHandler getDataHandler() { |
aoqi@0 | 170 | if(dataHandler==null){ |
aoqi@0 | 171 | dataHandler = new Base64StreamingDataHandler(new Base64DataSource()); |
aoqi@0 | 172 | } else if (!(dataHandler instanceof StreamingDataHandler)) { |
aoqi@0 | 173 | dataHandler = new FilterDataHandler(dataHandler); |
aoqi@0 | 174 | } |
aoqi@0 | 175 | return dataHandler; |
aoqi@0 | 176 | } |
aoqi@0 | 177 | |
aoqi@0 | 178 | private final class Base64DataSource implements DataSource { |
aoqi@0 | 179 | public String getContentType() { |
aoqi@0 | 180 | return getMimeType(); |
aoqi@0 | 181 | } |
aoqi@0 | 182 | |
aoqi@0 | 183 | public InputStream getInputStream() { |
aoqi@0 | 184 | return new ByteArrayInputStream(data,0,dataLen); |
aoqi@0 | 185 | } |
aoqi@0 | 186 | |
aoqi@0 | 187 | public String getName() { |
aoqi@0 | 188 | return null; |
aoqi@0 | 189 | } |
aoqi@0 | 190 | |
aoqi@0 | 191 | public OutputStream getOutputStream() { |
aoqi@0 | 192 | throw new UnsupportedOperationException(); |
aoqi@0 | 193 | } |
aoqi@0 | 194 | |
aoqi@0 | 195 | } |
aoqi@0 | 196 | |
aoqi@0 | 197 | private final class Base64StreamingDataHandler extends StreamingDataHandler { |
aoqi@0 | 198 | |
aoqi@0 | 199 | Base64StreamingDataHandler(DataSource source) { |
aoqi@0 | 200 | super(source); |
aoqi@0 | 201 | } |
aoqi@0 | 202 | |
aoqi@0 | 203 | public InputStream readOnce() throws IOException { |
aoqi@0 | 204 | return getDataSource().getInputStream(); |
aoqi@0 | 205 | } |
aoqi@0 | 206 | |
aoqi@0 | 207 | public void moveTo(File dst) throws IOException { |
aoqi@0 | 208 | FileOutputStream fout = new FileOutputStream(dst); |
aoqi@0 | 209 | try { |
aoqi@0 | 210 | fout.write(data, 0, dataLen); |
aoqi@0 | 211 | } finally { |
aoqi@0 | 212 | fout.close(); |
aoqi@0 | 213 | } |
aoqi@0 | 214 | } |
aoqi@0 | 215 | |
aoqi@0 | 216 | public void close() throws IOException { |
aoqi@0 | 217 | // nothing to do |
aoqi@0 | 218 | } |
aoqi@0 | 219 | } |
aoqi@0 | 220 | |
aoqi@0 | 221 | private static final class FilterDataHandler extends StreamingDataHandler { |
aoqi@0 | 222 | |
aoqi@0 | 223 | FilterDataHandler(DataHandler dh) { |
aoqi@0 | 224 | super(dh.getDataSource()); |
aoqi@0 | 225 | } |
aoqi@0 | 226 | |
aoqi@0 | 227 | public InputStream readOnce() throws IOException { |
aoqi@0 | 228 | return getDataSource().getInputStream(); |
aoqi@0 | 229 | } |
aoqi@0 | 230 | |
aoqi@0 | 231 | public void moveTo(File dst) throws IOException { |
aoqi@0 | 232 | byte[] buf = new byte[8192]; |
aoqi@0 | 233 | InputStream in = null; |
aoqi@0 | 234 | OutputStream out = null; |
aoqi@0 | 235 | try { |
aoqi@0 | 236 | in = getDataSource().getInputStream(); |
aoqi@0 | 237 | out = new FileOutputStream(dst); |
aoqi@0 | 238 | while (true) { |
aoqi@0 | 239 | int amountRead = in.read(buf); |
aoqi@0 | 240 | if (amountRead == -1) { |
aoqi@0 | 241 | break; |
aoqi@0 | 242 | } |
aoqi@0 | 243 | out.write(buf, 0, amountRead); |
aoqi@0 | 244 | } |
aoqi@0 | 245 | } finally { |
aoqi@0 | 246 | if (in != null) { |
aoqi@0 | 247 | try { |
aoqi@0 | 248 | in.close(); |
aoqi@0 | 249 | } catch(IOException ioe) { |
aoqi@0 | 250 | // nothing to do |
aoqi@0 | 251 | } |
aoqi@0 | 252 | } |
aoqi@0 | 253 | if (out != null) { |
aoqi@0 | 254 | try { |
aoqi@0 | 255 | out.close(); |
aoqi@0 | 256 | } catch(IOException ioe) { |
aoqi@0 | 257 | // nothing to do |
aoqi@0 | 258 | } |
aoqi@0 | 259 | } |
aoqi@0 | 260 | } |
aoqi@0 | 261 | } |
aoqi@0 | 262 | |
aoqi@0 | 263 | public void close() throws IOException { |
aoqi@0 | 264 | // nothing to do |
aoqi@0 | 265 | } |
aoqi@0 | 266 | } |
aoqi@0 | 267 | |
aoqi@0 | 268 | /** |
aoqi@0 | 269 | * Gets the byte[] of the exact length. |
aoqi@0 | 270 | * |
aoqi@0 | 271 | * @return byte[] for data |
aoqi@0 | 272 | */ |
aoqi@0 | 273 | public byte[] getExact() { |
aoqi@0 | 274 | get(); |
aoqi@0 | 275 | if(dataLen!=data.length) { |
aoqi@0 | 276 | byte[] buf = new byte[dataLen]; |
aoqi@0 | 277 | System.arraycopy(data,0,buf,0,dataLen); |
aoqi@0 | 278 | data = buf; |
aoqi@0 | 279 | } |
aoqi@0 | 280 | return data; |
aoqi@0 | 281 | } |
aoqi@0 | 282 | |
aoqi@0 | 283 | /** |
aoqi@0 | 284 | * Gets the data as an {@link InputStream}. |
aoqi@0 | 285 | * |
aoqi@0 | 286 | * @return data as InputStream |
aoqi@0 | 287 | * @throws IOException if i/o error occurs |
aoqi@0 | 288 | */ |
aoqi@0 | 289 | public InputStream getInputStream() throws IOException { |
aoqi@0 | 290 | if(dataHandler!=null) { |
aoqi@0 | 291 | return dataHandler.getInputStream(); |
aoqi@0 | 292 | } else { |
aoqi@0 | 293 | return new ByteArrayInputStream(data,0,dataLen); |
aoqi@0 | 294 | } |
aoqi@0 | 295 | } |
aoqi@0 | 296 | |
aoqi@0 | 297 | /** |
aoqi@0 | 298 | * Returns false if this object only has {@link DataHandler} and therefore |
aoqi@0 | 299 | * {@link #get()} operation is likely going to be expensive. |
aoqi@0 | 300 | * |
aoqi@0 | 301 | * @return false if it has only DataHandler |
aoqi@0 | 302 | */ |
aoqi@0 | 303 | public boolean hasData() { |
aoqi@0 | 304 | return data!=null; |
aoqi@0 | 305 | } |
aoqi@0 | 306 | |
aoqi@0 | 307 | /** |
aoqi@0 | 308 | * Gets the raw data. The size of the byte array maybe larger than the actual length. |
aoqi@0 | 309 | * |
aoqi@0 | 310 | * @return data as byte[], the array may be larger |
aoqi@0 | 311 | */ |
aoqi@0 | 312 | public byte[] get() { |
aoqi@0 | 313 | if(data==null) { |
aoqi@0 | 314 | try { |
aoqi@0 | 315 | ByteArrayOutputStreamEx baos = new ByteArrayOutputStreamEx(1024); |
aoqi@0 | 316 | InputStream is = dataHandler.getDataSource().getInputStream(); |
aoqi@0 | 317 | baos.readFrom(is); |
aoqi@0 | 318 | is.close(); |
aoqi@0 | 319 | data = baos.getBuffer(); |
aoqi@0 | 320 | dataLen = baos.size(); |
aoqi@0 | 321 | dataCloneByRef = true; |
aoqi@0 | 322 | } catch (IOException e) { |
aoqi@0 | 323 | // TODO: report the error to the unmarshaller |
aoqi@0 | 324 | dataLen = 0; // recover by assuming length-0 data |
aoqi@0 | 325 | } |
aoqi@0 | 326 | } |
aoqi@0 | 327 | return data; |
aoqi@0 | 328 | } |
aoqi@0 | 329 | |
aoqi@0 | 330 | /** |
aoqi@0 | 331 | * Gets the length of the binary data counted in bytes. |
aoqi@0 | 332 | * |
aoqi@0 | 333 | * Note that if this object encapsulates {@link DataHandler}, |
aoqi@0 | 334 | * this method would have to read the whole thing into {@code byte[]} |
aoqi@0 | 335 | * just to count the length, because {@link DataHandler} |
aoqi@0 | 336 | * doesn't easily expose the length. |
aoqi@0 | 337 | * |
aoqi@0 | 338 | * @return no of bytes |
aoqi@0 | 339 | */ |
aoqi@0 | 340 | public int getDataLen() { |
aoqi@0 | 341 | get(); |
aoqi@0 | 342 | return dataLen; |
aoqi@0 | 343 | } |
aoqi@0 | 344 | |
aoqi@0 | 345 | public String getMimeType() { |
aoqi@0 | 346 | if (mimeType==null) { |
aoqi@0 | 347 | return "application/octet-stream"; |
aoqi@0 | 348 | } |
aoqi@0 | 349 | return mimeType; |
aoqi@0 | 350 | } |
aoqi@0 | 351 | |
aoqi@0 | 352 | /** |
aoqi@0 | 353 | * Gets the number of characters needed to represent |
aoqi@0 | 354 | * this binary data in the base64 encoding. |
aoqi@0 | 355 | */ |
aoqi@0 | 356 | public int length() { |
aoqi@0 | 357 | // for each 3 bytes you use 4 chars |
aoqi@0 | 358 | // if the remainder is 1 or 2 there will be 4 more |
aoqi@0 | 359 | get(); // fill in the buffer if necessary |
aoqi@0 | 360 | return ((dataLen+2)/3)*4; |
aoqi@0 | 361 | } |
aoqi@0 | 362 | |
aoqi@0 | 363 | /** |
aoqi@0 | 364 | * Encode this binary data in the base64 encoding |
aoqi@0 | 365 | * and returns the character at the specified position. |
aoqi@0 | 366 | */ |
aoqi@0 | 367 | public char charAt(int index) { |
aoqi@0 | 368 | // we assume that the length() method is called before this method |
aoqi@0 | 369 | // (otherwise how would the caller know that the index is valid?) |
aoqi@0 | 370 | // so we assume that the byte[] is already populated |
aoqi@0 | 371 | |
aoqi@0 | 372 | int offset = index%4; |
aoqi@0 | 373 | int base = (index/4)*3; |
aoqi@0 | 374 | |
aoqi@0 | 375 | byte b1,b2; |
aoqi@0 | 376 | |
aoqi@0 | 377 | switch(offset) { |
aoqi@0 | 378 | case 0: |
aoqi@0 | 379 | return Base64Encoder.encode(data[base]>>2); |
aoqi@0 | 380 | case 1: |
aoqi@0 | 381 | if (base+1<dataLen) { |
aoqi@0 | 382 | b1 = data[base+1]; |
aoqi@0 | 383 | } else { |
aoqi@0 | 384 | b1 = 0; |
aoqi@0 | 385 | } |
aoqi@0 | 386 | return Base64Encoder.encode( |
aoqi@0 | 387 | ((data[base]&0x3)<<4) | |
aoqi@0 | 388 | ((b1>>4)&0xF)); |
aoqi@0 | 389 | case 2: |
aoqi@0 | 390 | if (base+1<dataLen) { |
aoqi@0 | 391 | b1 = data[base+1]; |
aoqi@0 | 392 | if (base+2<dataLen) { |
aoqi@0 | 393 | b2 = data[base+2]; |
aoqi@0 | 394 | } else { |
aoqi@0 | 395 | b2 = 0; |
aoqi@0 | 396 | } |
aoqi@0 | 397 | |
aoqi@0 | 398 | return Base64Encoder.encode( |
aoqi@0 | 399 | ((b1&0xF)<<2)| |
aoqi@0 | 400 | ((b2>>6)&0x3)); |
aoqi@0 | 401 | } else { |
aoqi@0 | 402 | return '='; |
aoqi@0 | 403 | } |
aoqi@0 | 404 | case 3: |
aoqi@0 | 405 | if(base+2<dataLen) { |
aoqi@0 | 406 | return Base64Encoder.encode(data[base+2]&0x3F); |
aoqi@0 | 407 | } else { |
aoqi@0 | 408 | return '='; |
aoqi@0 | 409 | } |
aoqi@0 | 410 | } |
aoqi@0 | 411 | |
aoqi@0 | 412 | throw new IllegalStateException(); |
aoqi@0 | 413 | } |
aoqi@0 | 414 | |
aoqi@0 | 415 | /** |
aoqi@0 | 416 | * Internally this is only used to split a text to a list, |
aoqi@0 | 417 | * which doesn't happen that much for base64. |
aoqi@0 | 418 | * So this method should be smaller than faster. |
aoqi@0 | 419 | */ |
aoqi@0 | 420 | public CharSequence subSequence(int start, int end) { |
aoqi@0 | 421 | StringBuilder buf = new StringBuilder(); |
aoqi@0 | 422 | get(); // fill in the buffer if we haven't done so |
aoqi@0 | 423 | for (int i=start; i<end; i++ ) { |
aoqi@0 | 424 | buf.append(charAt(i)); |
aoqi@0 | 425 | } |
aoqi@0 | 426 | return buf; |
aoqi@0 | 427 | } |
aoqi@0 | 428 | |
aoqi@0 | 429 | /** |
aoqi@0 | 430 | * Returns the base64 encoded string of this data. |
aoqi@0 | 431 | */ |
aoqi@0 | 432 | @Override |
aoqi@0 | 433 | public String toString() { |
aoqi@0 | 434 | get(); // fill in the buffer |
aoqi@0 | 435 | return Base64Encoder.print(data, 0, dataLen); |
aoqi@0 | 436 | } |
aoqi@0 | 437 | |
aoqi@0 | 438 | public void writeTo(char[] buf, int start) { |
aoqi@0 | 439 | get(); |
aoqi@0 | 440 | Base64Encoder.print(data, 0, dataLen, buf, start); |
aoqi@0 | 441 | } |
aoqi@0 | 442 | |
aoqi@0 | 443 | private static final int CHUNK_SIZE; |
aoqi@0 | 444 | static { |
aoqi@0 | 445 | int bufSize = 1024; |
aoqi@0 | 446 | try { |
aoqi@0 | 447 | String bufSizeStr = getProperty("com.sun.xml.internal.org.jvnet.staxex.Base64DataStreamWriteBufferSize"); |
aoqi@0 | 448 | if (bufSizeStr != null) { |
aoqi@0 | 449 | bufSize = Integer.parseInt(bufSizeStr); |
aoqi@0 | 450 | } |
aoqi@0 | 451 | } catch (Exception e) { |
aoqi@0 | 452 | logger.log(Level.INFO, "Error reading com.sun.xml.internal.org.jvnet.staxex.Base64DataStreamWriteBufferSize property", e); |
aoqi@0 | 453 | } |
aoqi@0 | 454 | CHUNK_SIZE = bufSize; |
aoqi@0 | 455 | } |
aoqi@0 | 456 | |
aoqi@0 | 457 | public void writeTo(XMLStreamWriter output) throws IOException, XMLStreamException { |
aoqi@0 | 458 | if (data==null) { |
aoqi@0 | 459 | try { |
aoqi@0 | 460 | InputStream is = dataHandler.getDataSource().getInputStream(); |
aoqi@0 | 461 | ByteArrayOutputStream outStream = new ByteArrayOutputStream(); // dev-null stream |
aoqi@0 | 462 | Base64EncoderStream encWriter = new Base64EncoderStream(output, outStream); |
aoqi@0 | 463 | int b; |
aoqi@0 | 464 | byte[] buffer = new byte[CHUNK_SIZE]; |
aoqi@0 | 465 | while ((b = is.read(buffer)) != -1) { |
aoqi@0 | 466 | encWriter.write(buffer, 0, b); |
aoqi@0 | 467 | } |
aoqi@0 | 468 | outStream.close(); |
aoqi@0 | 469 | encWriter.close(); |
aoqi@0 | 470 | } catch (IOException e) { |
aoqi@0 | 471 | dataLen = 0; // recover by assuming length-0 data |
aoqi@0 | 472 | throw e; |
aoqi@0 | 473 | } |
aoqi@0 | 474 | } else { |
aoqi@0 | 475 | // the data is already in memory and not streamed |
aoqi@0 | 476 | String s = Base64Encoder.print(data, 0, dataLen); |
aoqi@0 | 477 | output.writeCharacters(s); |
aoqi@0 | 478 | } |
aoqi@0 | 479 | } |
aoqi@0 | 480 | |
aoqi@0 | 481 | @Override |
aoqi@0 | 482 | public Base64Data clone() { |
aoqi@0 | 483 | try { |
aoqi@0 | 484 | Base64Data clone = (Base64Data) super.clone(); |
aoqi@0 | 485 | clone.get(); |
aoqi@0 | 486 | if (clone.dataCloneByRef) { |
aoqi@0 | 487 | this.data = clone.data; |
aoqi@0 | 488 | } else { |
aoqi@0 | 489 | this.data = new byte[clone.dataLen]; |
aoqi@0 | 490 | System.arraycopy(clone.data, 0, this.data, 0, clone.dataLen); |
aoqi@0 | 491 | } |
aoqi@0 | 492 | |
aoqi@0 | 493 | this.dataCloneByRef = true; |
aoqi@0 | 494 | this.dataLen = clone.dataLen; |
aoqi@0 | 495 | this.dataHandler = null; |
aoqi@0 | 496 | this.mimeType = clone.mimeType; |
aoqi@0 | 497 | return clone; |
aoqi@0 | 498 | } catch (CloneNotSupportedException ex) { |
aoqi@0 | 499 | Logger.getLogger(Base64Data.class.getName()).log(Level.SEVERE, null, ex); |
aoqi@0 | 500 | return null; |
aoqi@0 | 501 | } |
aoqi@0 | 502 | } |
aoqi@0 | 503 | |
aoqi@0 | 504 | static String getProperty(final String propName) { |
aoqi@0 | 505 | if (System.getSecurityManager() == null) { |
aoqi@0 | 506 | return System.getProperty(propName); |
aoqi@0 | 507 | } else { |
aoqi@0 | 508 | return (String) java.security.AccessController.doPrivileged( |
aoqi@0 | 509 | new java.security.PrivilegedAction() { |
aoqi@0 | 510 | public java.lang.Object run() { |
aoqi@0 | 511 | return System.getProperty(propName); |
aoqi@0 | 512 | } |
aoqi@0 | 513 | }); |
aoqi@0 | 514 | } |
aoqi@0 | 515 | } |
aoqi@0 | 516 | |
aoqi@0 | 517 | // public static void main(String[] args) throws FileNotFoundException, IOException, XMLStreamException { |
aoqi@0 | 518 | // Base64Data data = new Base64Data(); |
aoqi@0 | 519 | // |
aoqi@0 | 520 | // File f = new File("/Users/snajper/work/builds/weblogic/wls1211_dev.zip"); |
aoqi@0 | 521 | // FileDataSource fds = new FileDataSource(f); |
aoqi@0 | 522 | // DataHandler dh = new DataHandler(fds); |
aoqi@0 | 523 | // data.set(dh); |
aoqi@0 | 524 | // |
aoqi@0 | 525 | // FileWriter fw = new FileWriter(new File("/Users/snajper/Desktop/b.txt")); |
aoqi@0 | 526 | // XMLStreamWriterImpl wi = new XMLStreamWriterImpl(fw, null); |
aoqi@0 | 527 | // |
aoqi@0 | 528 | // data.writeTo(wi); |
aoqi@0 | 529 | // wi.flush();fw.flush(); |
aoqi@0 | 530 | // //System.out.println("SW: " + sw.toString()); |
aoqi@0 | 531 | // |
aoqi@0 | 532 | // } |
aoqi@0 | 533 | |
aoqi@0 | 534 | } |