aoqi@0: /* aoqi@0: * Copyright (c) 1997, 2012, Oracle and/or its affiliates. All rights reserved. aoqi@0: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. aoqi@0: * aoqi@0: * This code is free software; you can redistribute it and/or modify it aoqi@0: * under the terms of the GNU General Public License version 2 only, as aoqi@0: * published by the Free Software Foundation. Oracle designates this aoqi@0: * particular file as subject to the "Classpath" exception as provided aoqi@0: * by Oracle in the LICENSE file that accompanied this code. aoqi@0: * aoqi@0: * This code is distributed in the hope that it will be useful, but WITHOUT aoqi@0: * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or aoqi@0: * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License aoqi@0: * version 2 for more details (a copy is included in the LICENSE file that aoqi@0: * accompanied this code). aoqi@0: * aoqi@0: * You should have received a copy of the GNU General Public License version aoqi@0: * 2 along with this work; if not, write to the Free Software Foundation, aoqi@0: * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. aoqi@0: * aoqi@0: * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA aoqi@0: * or visit www.oracle.com if you need additional information or have any aoqi@0: * questions. aoqi@0: */ aoqi@0: aoqi@0: package com.sun.xml.internal.org.jvnet.staxex; aoqi@0: aoqi@0: import javax.activation.DataHandler; aoqi@0: import javax.activation.DataSource; aoqi@0: import java.io.ByteArrayInputStream; aoqi@0: import java.io.ByteArrayOutputStream; aoqi@0: import java.io.File; aoqi@0: import java.io.FileOutputStream; aoqi@0: import java.io.IOException; aoqi@0: import java.io.InputStream; aoqi@0: import java.io.OutputStream; aoqi@0: import javax.xml.stream.XMLStreamException; aoqi@0: import javax.xml.stream.XMLStreamWriter; aoqi@0: import java.util.logging.Level; aoqi@0: import java.util.logging.Logger; aoqi@0: aoqi@0: // for testing method aoqi@0: //import com.sun.xml.internal.stream.writers.XMLStreamWriterImpl; aoqi@0: //import java.io.FileNotFoundException; aoqi@0: //import java.io.FileWriter; aoqi@0: //import javax.activation.FileDataSource; aoqi@0: aoqi@0: /** aoqi@0: * Binary data represented as base64-encoded string aoqi@0: * in XML. aoqi@0: * aoqi@0: *

aoqi@0: * Used in conjunction with {@link XMLStreamReaderEx} aoqi@0: * and {@link XMLStreamWriterEx}. aoqi@0: * aoqi@0: * @author Kohsuke Kawaguchi, Martin Grebac aoqi@0: */ aoqi@0: public class Base64Data implements CharSequence, Cloneable { aoqi@0: aoqi@0: // either dataHandler or (data,dataLen,mimeType?) must be present aoqi@0: // (note that having both is allowed) aoqi@0: aoqi@0: private DataHandler dataHandler; aoqi@0: private byte[] data; aoqi@0: aoqi@0: /** aoqi@0: * Length of the valid data in {@link #data}. aoqi@0: */ aoqi@0: private int dataLen; aoqi@0: /** aoqi@0: * True if {@link #data} can be cloned by reference aoqi@0: * if Base64Data instance is cloned. aoqi@0: */ aoqi@0: private boolean dataCloneByRef; aoqi@0: /** aoqi@0: * Optional MIME type of {@link #data}. aoqi@0: * aoqi@0: * Unused when {@link #dataHandler} is set. aoqi@0: * Use {@link DataHandler#getContentType()} in that case. aoqi@0: */ aoqi@0: private String mimeType; aoqi@0: aoqi@0: /** aoqi@0: * Default constructor aoqi@0: */ aoqi@0: public Base64Data() { aoqi@0: } aoqi@0: aoqi@0: private static final Logger logger = Logger.getLogger(Base64Data.class.getName()); aoqi@0: aoqi@0: /** aoqi@0: * Clone constructor aoqi@0: * @param that needs to be cloned aoqi@0: */ aoqi@0: public Base64Data(Base64Data that) { aoqi@0: that.get(); aoqi@0: if (that.dataCloneByRef) { aoqi@0: this.data = that.data; aoqi@0: } else { aoqi@0: this.data = new byte[that.dataLen]; aoqi@0: System.arraycopy(that.data, 0, this.data, 0, that.dataLen); aoqi@0: } aoqi@0: aoqi@0: this.dataCloneByRef = true; aoqi@0: this.dataLen = that.dataLen; aoqi@0: this.dataHandler = null; aoqi@0: this.mimeType = that.mimeType; aoqi@0: } aoqi@0: aoqi@0: /** aoqi@0: * Fills in the data object by a portion of the byte[]. aoqi@0: * aoqi@0: * @param data actual data aoqi@0: * @param len aoqi@0: * data[0] to data[len-1] are treated as the data. aoqi@0: * @param mimeType MIME type aoqi@0: * @param cloneByRef aoqi@0: * true if data[] can be cloned by reference aoqi@0: */ aoqi@0: public void set(byte[] data, int len, String mimeType, boolean cloneByRef) { aoqi@0: this.data = data; aoqi@0: this.dataLen = len; aoqi@0: this.dataCloneByRef = cloneByRef; aoqi@0: this.dataHandler = null; aoqi@0: this.mimeType = mimeType; aoqi@0: } aoqi@0: aoqi@0: /** aoqi@0: * Fills in the data object by a portion of the byte[]. aoqi@0: * aoqi@0: * @param data actual data bytes aoqi@0: * @param len aoqi@0: * data[0] to data[len-1] are treated as the data. aoqi@0: * @param mimeType MIME type aoqi@0: */ aoqi@0: public void set(byte[] data, int len, String mimeType) { aoqi@0: set(data,len,mimeType,false); aoqi@0: } aoqi@0: aoqi@0: /** aoqi@0: * Fills in the data object by the byte[] of the exact length. aoqi@0: * aoqi@0: * @param data aoqi@0: * this buffer may be owned directly by the unmarshaleld JAXB object. aoqi@0: * @param mimeType MIME type aoqi@0: */ aoqi@0: public void set(byte[] data,String mimeType) { aoqi@0: set(data,data.length,mimeType,false); aoqi@0: } aoqi@0: aoqi@0: /** aoqi@0: * Fills in the data object by a {@link DataHandler}. aoqi@0: * aoqi@0: * @param data DataHandler for the data aoqi@0: */ aoqi@0: public void set(DataHandler data) { aoqi@0: assert data!=null; aoqi@0: this.dataHandler = data; aoqi@0: this.data = null; aoqi@0: } aoqi@0: aoqi@0: /** aoqi@0: * Gets the raw data. If the returned DataHandler is {@link StreamingDataHandler}, aoqi@0: * callees may need to downcast to take advantage of its capabilities. aoqi@0: * aoqi@0: * @see StreamingDataHandler aoqi@0: * @return DataHandler for the data aoqi@0: */ aoqi@0: public DataHandler getDataHandler() { aoqi@0: if(dataHandler==null){ aoqi@0: dataHandler = new Base64StreamingDataHandler(new Base64DataSource()); aoqi@0: } else if (!(dataHandler instanceof StreamingDataHandler)) { aoqi@0: dataHandler = new FilterDataHandler(dataHandler); aoqi@0: } aoqi@0: return dataHandler; aoqi@0: } aoqi@0: aoqi@0: private final class Base64DataSource implements DataSource { aoqi@0: public String getContentType() { aoqi@0: return getMimeType(); aoqi@0: } aoqi@0: aoqi@0: public InputStream getInputStream() { aoqi@0: return new ByteArrayInputStream(data,0,dataLen); aoqi@0: } aoqi@0: aoqi@0: public String getName() { aoqi@0: return null; aoqi@0: } aoqi@0: aoqi@0: public OutputStream getOutputStream() { aoqi@0: throw new UnsupportedOperationException(); aoqi@0: } aoqi@0: aoqi@0: } aoqi@0: aoqi@0: private final class Base64StreamingDataHandler extends StreamingDataHandler { aoqi@0: aoqi@0: Base64StreamingDataHandler(DataSource source) { aoqi@0: super(source); aoqi@0: } aoqi@0: aoqi@0: public InputStream readOnce() throws IOException { aoqi@0: return getDataSource().getInputStream(); aoqi@0: } aoqi@0: aoqi@0: public void moveTo(File dst) throws IOException { aoqi@0: FileOutputStream fout = new FileOutputStream(dst); aoqi@0: try { aoqi@0: fout.write(data, 0, dataLen); aoqi@0: } finally { aoqi@0: fout.close(); aoqi@0: } aoqi@0: } aoqi@0: aoqi@0: public void close() throws IOException { aoqi@0: // nothing to do aoqi@0: } aoqi@0: } aoqi@0: aoqi@0: private static final class FilterDataHandler extends StreamingDataHandler { aoqi@0: aoqi@0: FilterDataHandler(DataHandler dh) { aoqi@0: super(dh.getDataSource()); aoqi@0: } aoqi@0: aoqi@0: public InputStream readOnce() throws IOException { aoqi@0: return getDataSource().getInputStream(); aoqi@0: } aoqi@0: aoqi@0: public void moveTo(File dst) throws IOException { aoqi@0: byte[] buf = new byte[8192]; aoqi@0: InputStream in = null; aoqi@0: OutputStream out = null; aoqi@0: try { aoqi@0: in = getDataSource().getInputStream(); aoqi@0: out = new FileOutputStream(dst); aoqi@0: while (true) { aoqi@0: int amountRead = in.read(buf); aoqi@0: if (amountRead == -1) { aoqi@0: break; aoqi@0: } aoqi@0: out.write(buf, 0, amountRead); aoqi@0: } aoqi@0: } finally { aoqi@0: if (in != null) { aoqi@0: try { aoqi@0: in.close(); aoqi@0: } catch(IOException ioe) { aoqi@0: // nothing to do aoqi@0: } aoqi@0: } aoqi@0: if (out != null) { aoqi@0: try { aoqi@0: out.close(); aoqi@0: } catch(IOException ioe) { aoqi@0: // nothing to do aoqi@0: } aoqi@0: } aoqi@0: } aoqi@0: } aoqi@0: aoqi@0: public void close() throws IOException { aoqi@0: // nothing to do aoqi@0: } aoqi@0: } aoqi@0: aoqi@0: /** aoqi@0: * Gets the byte[] of the exact length. aoqi@0: * aoqi@0: * @return byte[] for data aoqi@0: */ aoqi@0: public byte[] getExact() { aoqi@0: get(); aoqi@0: if(dataLen!=data.length) { aoqi@0: byte[] buf = new byte[dataLen]; aoqi@0: System.arraycopy(data,0,buf,0,dataLen); aoqi@0: data = buf; aoqi@0: } aoqi@0: return data; aoqi@0: } aoqi@0: aoqi@0: /** aoqi@0: * Gets the data as an {@link InputStream}. aoqi@0: * aoqi@0: * @return data as InputStream aoqi@0: * @throws IOException if i/o error occurs aoqi@0: */ aoqi@0: public InputStream getInputStream() throws IOException { aoqi@0: if(dataHandler!=null) { aoqi@0: return dataHandler.getInputStream(); aoqi@0: } else { aoqi@0: return new ByteArrayInputStream(data,0,dataLen); aoqi@0: } aoqi@0: } aoqi@0: aoqi@0: /** aoqi@0: * Returns false if this object only has {@link DataHandler} and therefore aoqi@0: * {@link #get()} operation is likely going to be expensive. aoqi@0: * aoqi@0: * @return false if it has only DataHandler aoqi@0: */ aoqi@0: public boolean hasData() { aoqi@0: return data!=null; aoqi@0: } aoqi@0: aoqi@0: /** aoqi@0: * Gets the raw data. The size of the byte array maybe larger than the actual length. aoqi@0: * aoqi@0: * @return data as byte[], the array may be larger aoqi@0: */ aoqi@0: public byte[] get() { aoqi@0: if(data==null) { aoqi@0: try { aoqi@0: ByteArrayOutputStreamEx baos = new ByteArrayOutputStreamEx(1024); aoqi@0: InputStream is = dataHandler.getDataSource().getInputStream(); aoqi@0: baos.readFrom(is); aoqi@0: is.close(); aoqi@0: data = baos.getBuffer(); aoqi@0: dataLen = baos.size(); aoqi@0: dataCloneByRef = true; aoqi@0: } catch (IOException e) { aoqi@0: // TODO: report the error to the unmarshaller aoqi@0: dataLen = 0; // recover by assuming length-0 data aoqi@0: } aoqi@0: } aoqi@0: return data; aoqi@0: } aoqi@0: aoqi@0: /** aoqi@0: * Gets the length of the binary data counted in bytes. aoqi@0: * aoqi@0: * Note that if this object encapsulates {@link DataHandler}, aoqi@0: * this method would have to read the whole thing into {@code byte[]} aoqi@0: * just to count the length, because {@link DataHandler} aoqi@0: * doesn't easily expose the length. aoqi@0: * aoqi@0: * @return no of bytes aoqi@0: */ aoqi@0: public int getDataLen() { aoqi@0: get(); aoqi@0: return dataLen; aoqi@0: } aoqi@0: aoqi@0: public String getMimeType() { aoqi@0: if (mimeType==null) { aoqi@0: return "application/octet-stream"; aoqi@0: } aoqi@0: return mimeType; aoqi@0: } aoqi@0: aoqi@0: /** aoqi@0: * Gets the number of characters needed to represent aoqi@0: * this binary data in the base64 encoding. aoqi@0: */ aoqi@0: public int length() { aoqi@0: // for each 3 bytes you use 4 chars aoqi@0: // if the remainder is 1 or 2 there will be 4 more aoqi@0: get(); // fill in the buffer if necessary aoqi@0: return ((dataLen+2)/3)*4; aoqi@0: } aoqi@0: aoqi@0: /** aoqi@0: * Encode this binary data in the base64 encoding aoqi@0: * and returns the character at the specified position. aoqi@0: */ aoqi@0: public char charAt(int index) { aoqi@0: // we assume that the length() method is called before this method aoqi@0: // (otherwise how would the caller know that the index is valid?) aoqi@0: // so we assume that the byte[] is already populated aoqi@0: aoqi@0: int offset = index%4; aoqi@0: int base = (index/4)*3; aoqi@0: aoqi@0: byte b1,b2; aoqi@0: aoqi@0: switch(offset) { aoqi@0: case 0: aoqi@0: return Base64Encoder.encode(data[base]>>2); aoqi@0: case 1: aoqi@0: if (base+1>4)&0xF)); aoqi@0: case 2: aoqi@0: if (base+1>6)&0x3)); aoqi@0: } else { aoqi@0: return '='; aoqi@0: } aoqi@0: case 3: aoqi@0: if(base+2