1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/src/share/jaxws_classes/com/sun/xml/internal/org/jvnet/staxex/Base64Data.java Wed Apr 27 01:27:09 2016 +0800 1.3 @@ -0,0 +1,534 @@ 1.4 +/* 1.5 + * Copyright (c) 1997, 2012, Oracle and/or its affiliates. All rights reserved. 1.6 + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 1.7 + * 1.8 + * This code is free software; you can redistribute it and/or modify it 1.9 + * under the terms of the GNU General Public License version 2 only, as 1.10 + * published by the Free Software Foundation. Oracle designates this 1.11 + * particular file as subject to the "Classpath" exception as provided 1.12 + * by Oracle in the LICENSE file that accompanied this code. 1.13 + * 1.14 + * This code is distributed in the hope that it will be useful, but WITHOUT 1.15 + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 1.16 + * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 1.17 + * version 2 for more details (a copy is included in the LICENSE file that 1.18 + * accompanied this code). 1.19 + * 1.20 + * You should have received a copy of the GNU General Public License version 1.21 + * 2 along with this work; if not, write to the Free Software Foundation, 1.22 + * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 1.23 + * 1.24 + * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 1.25 + * or visit www.oracle.com if you need additional information or have any 1.26 + * questions. 1.27 + */ 1.28 + 1.29 +package com.sun.xml.internal.org.jvnet.staxex; 1.30 + 1.31 +import javax.activation.DataHandler; 1.32 +import javax.activation.DataSource; 1.33 +import java.io.ByteArrayInputStream; 1.34 +import java.io.ByteArrayOutputStream; 1.35 +import java.io.File; 1.36 +import java.io.FileOutputStream; 1.37 +import java.io.IOException; 1.38 +import java.io.InputStream; 1.39 +import java.io.OutputStream; 1.40 +import javax.xml.stream.XMLStreamException; 1.41 +import javax.xml.stream.XMLStreamWriter; 1.42 +import java.util.logging.Level; 1.43 +import java.util.logging.Logger; 1.44 + 1.45 +// for testing method 1.46 +//import com.sun.xml.internal.stream.writers.XMLStreamWriterImpl; 1.47 +//import java.io.FileNotFoundException; 1.48 +//import java.io.FileWriter; 1.49 +//import javax.activation.FileDataSource; 1.50 + 1.51 +/** 1.52 + * Binary data represented as base64-encoded string 1.53 + * in XML. 1.54 + * 1.55 + * <p> 1.56 + * Used in conjunction with {@link XMLStreamReaderEx} 1.57 + * and {@link XMLStreamWriterEx}. 1.58 + * 1.59 + * @author Kohsuke Kawaguchi, Martin Grebac 1.60 + */ 1.61 +public class Base64Data implements CharSequence, Cloneable { 1.62 + 1.63 + // either dataHandler or (data,dataLen,mimeType?) must be present 1.64 + // (note that having both is allowed) 1.65 + 1.66 + private DataHandler dataHandler; 1.67 + private byte[] data; 1.68 + 1.69 + /** 1.70 + * Length of the valid data in {@link #data}. 1.71 + */ 1.72 + private int dataLen; 1.73 + /** 1.74 + * True if {@link #data} can be cloned by reference 1.75 + * if Base64Data instance is cloned. 1.76 + */ 1.77 + private boolean dataCloneByRef; 1.78 + /** 1.79 + * Optional MIME type of {@link #data}. 1.80 + * 1.81 + * Unused when {@link #dataHandler} is set. 1.82 + * Use {@link DataHandler#getContentType()} in that case. 1.83 + */ 1.84 + private String mimeType; 1.85 + 1.86 + /** 1.87 + * Default constructor 1.88 + */ 1.89 + public Base64Data() { 1.90 + } 1.91 + 1.92 + private static final Logger logger = Logger.getLogger(Base64Data.class.getName()); 1.93 + 1.94 + /** 1.95 + * Clone constructor 1.96 + * @param that needs to be cloned 1.97 + */ 1.98 + public Base64Data(Base64Data that) { 1.99 + that.get(); 1.100 + if (that.dataCloneByRef) { 1.101 + this.data = that.data; 1.102 + } else { 1.103 + this.data = new byte[that.dataLen]; 1.104 + System.arraycopy(that.data, 0, this.data, 0, that.dataLen); 1.105 + } 1.106 + 1.107 + this.dataCloneByRef = true; 1.108 + this.dataLen = that.dataLen; 1.109 + this.dataHandler = null; 1.110 + this.mimeType = that.mimeType; 1.111 + } 1.112 + 1.113 + /** 1.114 + * Fills in the data object by a portion of the byte[]. 1.115 + * 1.116 + * @param data actual data 1.117 + * @param len 1.118 + * data[0] to data[len-1] are treated as the data. 1.119 + * @param mimeType MIME type 1.120 + * @param cloneByRef 1.121 + * true if data[] can be cloned by reference 1.122 + */ 1.123 + public void set(byte[] data, int len, String mimeType, boolean cloneByRef) { 1.124 + this.data = data; 1.125 + this.dataLen = len; 1.126 + this.dataCloneByRef = cloneByRef; 1.127 + this.dataHandler = null; 1.128 + this.mimeType = mimeType; 1.129 + } 1.130 + 1.131 + /** 1.132 + * Fills in the data object by a portion of the byte[]. 1.133 + * 1.134 + * @param data actual data bytes 1.135 + * @param len 1.136 + * data[0] to data[len-1] are treated as the data. 1.137 + * @param mimeType MIME type 1.138 + */ 1.139 + public void set(byte[] data, int len, String mimeType) { 1.140 + set(data,len,mimeType,false); 1.141 + } 1.142 + 1.143 + /** 1.144 + * Fills in the data object by the byte[] of the exact length. 1.145 + * 1.146 + * @param data 1.147 + * this buffer may be owned directly by the unmarshaleld JAXB object. 1.148 + * @param mimeType MIME type 1.149 + */ 1.150 + public void set(byte[] data,String mimeType) { 1.151 + set(data,data.length,mimeType,false); 1.152 + } 1.153 + 1.154 + /** 1.155 + * Fills in the data object by a {@link DataHandler}. 1.156 + * 1.157 + * @param data DataHandler for the data 1.158 + */ 1.159 + public void set(DataHandler data) { 1.160 + assert data!=null; 1.161 + this.dataHandler = data; 1.162 + this.data = null; 1.163 + } 1.164 + 1.165 + /** 1.166 + * Gets the raw data. If the returned DataHandler is {@link StreamingDataHandler}, 1.167 + * callees may need to downcast to take advantage of its capabilities. 1.168 + * 1.169 + * @see StreamingDataHandler 1.170 + * @return DataHandler for the data 1.171 + */ 1.172 + public DataHandler getDataHandler() { 1.173 + if(dataHandler==null){ 1.174 + dataHandler = new Base64StreamingDataHandler(new Base64DataSource()); 1.175 + } else if (!(dataHandler instanceof StreamingDataHandler)) { 1.176 + dataHandler = new FilterDataHandler(dataHandler); 1.177 + } 1.178 + return dataHandler; 1.179 + } 1.180 + 1.181 + private final class Base64DataSource implements DataSource { 1.182 + public String getContentType() { 1.183 + return getMimeType(); 1.184 + } 1.185 + 1.186 + public InputStream getInputStream() { 1.187 + return new ByteArrayInputStream(data,0,dataLen); 1.188 + } 1.189 + 1.190 + public String getName() { 1.191 + return null; 1.192 + } 1.193 + 1.194 + public OutputStream getOutputStream() { 1.195 + throw new UnsupportedOperationException(); 1.196 + } 1.197 + 1.198 + } 1.199 + 1.200 + private final class Base64StreamingDataHandler extends StreamingDataHandler { 1.201 + 1.202 + Base64StreamingDataHandler(DataSource source) { 1.203 + super(source); 1.204 + } 1.205 + 1.206 + public InputStream readOnce() throws IOException { 1.207 + return getDataSource().getInputStream(); 1.208 + } 1.209 + 1.210 + public void moveTo(File dst) throws IOException { 1.211 + FileOutputStream fout = new FileOutputStream(dst); 1.212 + try { 1.213 + fout.write(data, 0, dataLen); 1.214 + } finally { 1.215 + fout.close(); 1.216 + } 1.217 + } 1.218 + 1.219 + public void close() throws IOException { 1.220 + // nothing to do 1.221 + } 1.222 + } 1.223 + 1.224 + private static final class FilterDataHandler extends StreamingDataHandler { 1.225 + 1.226 + FilterDataHandler(DataHandler dh) { 1.227 + super(dh.getDataSource()); 1.228 + } 1.229 + 1.230 + public InputStream readOnce() throws IOException { 1.231 + return getDataSource().getInputStream(); 1.232 + } 1.233 + 1.234 + public void moveTo(File dst) throws IOException { 1.235 + byte[] buf = new byte[8192]; 1.236 + InputStream in = null; 1.237 + OutputStream out = null; 1.238 + try { 1.239 + in = getDataSource().getInputStream(); 1.240 + out = new FileOutputStream(dst); 1.241 + while (true) { 1.242 + int amountRead = in.read(buf); 1.243 + if (amountRead == -1) { 1.244 + break; 1.245 + } 1.246 + out.write(buf, 0, amountRead); 1.247 + } 1.248 + } finally { 1.249 + if (in != null) { 1.250 + try { 1.251 + in.close(); 1.252 + } catch(IOException ioe) { 1.253 + // nothing to do 1.254 + } 1.255 + } 1.256 + if (out != null) { 1.257 + try { 1.258 + out.close(); 1.259 + } catch(IOException ioe) { 1.260 + // nothing to do 1.261 + } 1.262 + } 1.263 + } 1.264 + } 1.265 + 1.266 + public void close() throws IOException { 1.267 + // nothing to do 1.268 + } 1.269 + } 1.270 + 1.271 + /** 1.272 + * Gets the byte[] of the exact length. 1.273 + * 1.274 + * @return byte[] for data 1.275 + */ 1.276 + public byte[] getExact() { 1.277 + get(); 1.278 + if(dataLen!=data.length) { 1.279 + byte[] buf = new byte[dataLen]; 1.280 + System.arraycopy(data,0,buf,0,dataLen); 1.281 + data = buf; 1.282 + } 1.283 + return data; 1.284 + } 1.285 + 1.286 + /** 1.287 + * Gets the data as an {@link InputStream}. 1.288 + * 1.289 + * @return data as InputStream 1.290 + * @throws IOException if i/o error occurs 1.291 + */ 1.292 + public InputStream getInputStream() throws IOException { 1.293 + if(dataHandler!=null) { 1.294 + return dataHandler.getInputStream(); 1.295 + } else { 1.296 + return new ByteArrayInputStream(data,0,dataLen); 1.297 + } 1.298 + } 1.299 + 1.300 + /** 1.301 + * Returns false if this object only has {@link DataHandler} and therefore 1.302 + * {@link #get()} operation is likely going to be expensive. 1.303 + * 1.304 + * @return false if it has only DataHandler 1.305 + */ 1.306 + public boolean hasData() { 1.307 + return data!=null; 1.308 + } 1.309 + 1.310 + /** 1.311 + * Gets the raw data. The size of the byte array maybe larger than the actual length. 1.312 + * 1.313 + * @return data as byte[], the array may be larger 1.314 + */ 1.315 + public byte[] get() { 1.316 + if(data==null) { 1.317 + try { 1.318 + ByteArrayOutputStreamEx baos = new ByteArrayOutputStreamEx(1024); 1.319 + InputStream is = dataHandler.getDataSource().getInputStream(); 1.320 + baos.readFrom(is); 1.321 + is.close(); 1.322 + data = baos.getBuffer(); 1.323 + dataLen = baos.size(); 1.324 + dataCloneByRef = true; 1.325 + } catch (IOException e) { 1.326 + // TODO: report the error to the unmarshaller 1.327 + dataLen = 0; // recover by assuming length-0 data 1.328 + } 1.329 + } 1.330 + return data; 1.331 + } 1.332 + 1.333 + /** 1.334 + * Gets the length of the binary data counted in bytes. 1.335 + * 1.336 + * Note that if this object encapsulates {@link DataHandler}, 1.337 + * this method would have to read the whole thing into {@code byte[]} 1.338 + * just to count the length, because {@link DataHandler} 1.339 + * doesn't easily expose the length. 1.340 + * 1.341 + * @return no of bytes 1.342 + */ 1.343 + public int getDataLen() { 1.344 + get(); 1.345 + return dataLen; 1.346 + } 1.347 + 1.348 + public String getMimeType() { 1.349 + if (mimeType==null) { 1.350 + return "application/octet-stream"; 1.351 + } 1.352 + return mimeType; 1.353 + } 1.354 + 1.355 + /** 1.356 + * Gets the number of characters needed to represent 1.357 + * this binary data in the base64 encoding. 1.358 + */ 1.359 + public int length() { 1.360 + // for each 3 bytes you use 4 chars 1.361 + // if the remainder is 1 or 2 there will be 4 more 1.362 + get(); // fill in the buffer if necessary 1.363 + return ((dataLen+2)/3)*4; 1.364 + } 1.365 + 1.366 + /** 1.367 + * Encode this binary data in the base64 encoding 1.368 + * and returns the character at the specified position. 1.369 + */ 1.370 + public char charAt(int index) { 1.371 + // we assume that the length() method is called before this method 1.372 + // (otherwise how would the caller know that the index is valid?) 1.373 + // so we assume that the byte[] is already populated 1.374 + 1.375 + int offset = index%4; 1.376 + int base = (index/4)*3; 1.377 + 1.378 + byte b1,b2; 1.379 + 1.380 + switch(offset) { 1.381 + case 0: 1.382 + return Base64Encoder.encode(data[base]>>2); 1.383 + case 1: 1.384 + if (base+1<dataLen) { 1.385 + b1 = data[base+1]; 1.386 + } else { 1.387 + b1 = 0; 1.388 + } 1.389 + return Base64Encoder.encode( 1.390 + ((data[base]&0x3)<<4) | 1.391 + ((b1>>4)&0xF)); 1.392 + case 2: 1.393 + if (base+1<dataLen) { 1.394 + b1 = data[base+1]; 1.395 + if (base+2<dataLen) { 1.396 + b2 = data[base+2]; 1.397 + } else { 1.398 + b2 = 0; 1.399 + } 1.400 + 1.401 + return Base64Encoder.encode( 1.402 + ((b1&0xF)<<2)| 1.403 + ((b2>>6)&0x3)); 1.404 + } else { 1.405 + return '='; 1.406 + } 1.407 + case 3: 1.408 + if(base+2<dataLen) { 1.409 + return Base64Encoder.encode(data[base+2]&0x3F); 1.410 + } else { 1.411 + return '='; 1.412 + } 1.413 + } 1.414 + 1.415 + throw new IllegalStateException(); 1.416 + } 1.417 + 1.418 + /** 1.419 + * Internally this is only used to split a text to a list, 1.420 + * which doesn't happen that much for base64. 1.421 + * So this method should be smaller than faster. 1.422 + */ 1.423 + public CharSequence subSequence(int start, int end) { 1.424 + StringBuilder buf = new StringBuilder(); 1.425 + get(); // fill in the buffer if we haven't done so 1.426 + for (int i=start; i<end; i++ ) { 1.427 + buf.append(charAt(i)); 1.428 + } 1.429 + return buf; 1.430 + } 1.431 + 1.432 + /** 1.433 + * Returns the base64 encoded string of this data. 1.434 + */ 1.435 + @Override 1.436 + public String toString() { 1.437 + get(); // fill in the buffer 1.438 + return Base64Encoder.print(data, 0, dataLen); 1.439 + } 1.440 + 1.441 + public void writeTo(char[] buf, int start) { 1.442 + get(); 1.443 + Base64Encoder.print(data, 0, dataLen, buf, start); 1.444 + } 1.445 + 1.446 + private static final int CHUNK_SIZE; 1.447 + static { 1.448 + int bufSize = 1024; 1.449 + try { 1.450 + String bufSizeStr = getProperty("com.sun.xml.internal.org.jvnet.staxex.Base64DataStreamWriteBufferSize"); 1.451 + if (bufSizeStr != null) { 1.452 + bufSize = Integer.parseInt(bufSizeStr); 1.453 + } 1.454 + } catch (Exception e) { 1.455 + logger.log(Level.INFO, "Error reading com.sun.xml.internal.org.jvnet.staxex.Base64DataStreamWriteBufferSize property", e); 1.456 + } 1.457 + CHUNK_SIZE = bufSize; 1.458 + } 1.459 + 1.460 + public void writeTo(XMLStreamWriter output) throws IOException, XMLStreamException { 1.461 + if (data==null) { 1.462 + try { 1.463 + InputStream is = dataHandler.getDataSource().getInputStream(); 1.464 + ByteArrayOutputStream outStream = new ByteArrayOutputStream(); // dev-null stream 1.465 + Base64EncoderStream encWriter = new Base64EncoderStream(output, outStream); 1.466 + int b; 1.467 + byte[] buffer = new byte[CHUNK_SIZE]; 1.468 + while ((b = is.read(buffer)) != -1) { 1.469 + encWriter.write(buffer, 0, b); 1.470 + } 1.471 + outStream.close(); 1.472 + encWriter.close(); 1.473 + } catch (IOException e) { 1.474 + dataLen = 0; // recover by assuming length-0 data 1.475 + throw e; 1.476 + } 1.477 + } else { 1.478 + // the data is already in memory and not streamed 1.479 + String s = Base64Encoder.print(data, 0, dataLen); 1.480 + output.writeCharacters(s); 1.481 + } 1.482 + } 1.483 + 1.484 + @Override 1.485 + public Base64Data clone() { 1.486 + try { 1.487 + Base64Data clone = (Base64Data) super.clone(); 1.488 + clone.get(); 1.489 + if (clone.dataCloneByRef) { 1.490 + this.data = clone.data; 1.491 + } else { 1.492 + this.data = new byte[clone.dataLen]; 1.493 + System.arraycopy(clone.data, 0, this.data, 0, clone.dataLen); 1.494 + } 1.495 + 1.496 + this.dataCloneByRef = true; 1.497 + this.dataLen = clone.dataLen; 1.498 + this.dataHandler = null; 1.499 + this.mimeType = clone.mimeType; 1.500 + return clone; 1.501 + } catch (CloneNotSupportedException ex) { 1.502 + Logger.getLogger(Base64Data.class.getName()).log(Level.SEVERE, null, ex); 1.503 + return null; 1.504 + } 1.505 + } 1.506 + 1.507 + static String getProperty(final String propName) { 1.508 + if (System.getSecurityManager() == null) { 1.509 + return System.getProperty(propName); 1.510 + } else { 1.511 + return (String) java.security.AccessController.doPrivileged( 1.512 + new java.security.PrivilegedAction() { 1.513 + public java.lang.Object run() { 1.514 + return System.getProperty(propName); 1.515 + } 1.516 + }); 1.517 + } 1.518 + } 1.519 + 1.520 +// public static void main(String[] args) throws FileNotFoundException, IOException, XMLStreamException { 1.521 +// Base64Data data = new Base64Data(); 1.522 +// 1.523 +// File f = new File("/Users/snajper/work/builds/weblogic/wls1211_dev.zip"); 1.524 +// FileDataSource fds = new FileDataSource(f); 1.525 +// DataHandler dh = new DataHandler(fds); 1.526 +// data.set(dh); 1.527 +// 1.528 +// FileWriter fw = new FileWriter(new File("/Users/snajper/Desktop/b.txt")); 1.529 +// XMLStreamWriterImpl wi = new XMLStreamWriterImpl(fw, null); 1.530 +// 1.531 +// data.writeTo(wi); 1.532 +// wi.flush();fw.flush(); 1.533 +// //System.out.println("SW: " + sw.toString()); 1.534 +// 1.535 +// } 1.536 + 1.537 +}