ohair@286: /* alanb@368: * Copyright (c) 1997, 2012, Oracle and/or its affiliates. All rights reserved. ohair@286: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. ohair@286: * ohair@286: * This code is free software; you can redistribute it and/or modify it ohair@286: * under the terms of the GNU General Public License version 2 only, as ohair@286: * published by the Free Software Foundation. Oracle designates this ohair@286: * particular file as subject to the "Classpath" exception as provided ohair@286: * by Oracle in the LICENSE file that accompanied this code. ohair@286: * ohair@286: * This code is distributed in the hope that it will be useful, but WITHOUT ohair@286: * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or ohair@286: * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License ohair@286: * version 2 for more details (a copy is included in the LICENSE file that ohair@286: * accompanied this code). ohair@286: * ohair@286: * You should have received a copy of the GNU General Public License version ohair@286: * 2 along with this work; if not, write to the Free Software Foundation, ohair@286: * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. ohair@286: * ohair@286: * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA ohair@286: * or visit www.oracle.com if you need additional information or have any ohair@286: * questions. ohair@286: */ ohair@286: ohair@286: package com.sun.xml.internal.org.jvnet.staxex; ohair@286: ohair@286: import javax.activation.DataHandler; ohair@286: import javax.activation.DataSource; ohair@286: import java.io.ByteArrayInputStream; ohair@286: import java.io.ByteArrayOutputStream; ohair@286: import java.io.File; ohair@286: import java.io.FileOutputStream; ohair@286: import java.io.IOException; ohair@286: import java.io.InputStream; ohair@286: import java.io.OutputStream; ohair@286: import javax.xml.stream.XMLStreamException; ohair@286: import javax.xml.stream.XMLStreamWriter; alanb@368: import java.util.logging.Level; alanb@368: import java.util.logging.Logger; ohair@286: ohair@286: // for testing method ohair@286: //import com.sun.xml.internal.stream.writers.XMLStreamWriterImpl; ohair@286: //import java.io.FileNotFoundException; alanb@368: //import java.io.FileWriter; ohair@286: //import javax.activation.FileDataSource; ohair@286: ohair@286: /** ohair@286: * Binary data represented as base64-encoded string ohair@286: * in XML. ohair@286: * ohair@286: *
ohair@286: * Used in conjunction with {@link XMLStreamReaderEx}
ohair@286: * and {@link XMLStreamWriterEx}.
ohair@286: *
ohair@286: * @author Kohsuke Kawaguchi, Martin Grebac
ohair@286: */
ohair@286: public class Base64Data implements CharSequence, Cloneable {
ohair@286:
ohair@286: // either dataHandler or (data,dataLen,mimeType?) must be present
ohair@286: // (note that having both is allowed)
ohair@286:
ohair@286: private DataHandler dataHandler;
alanb@368: private byte[] data;
ohair@286:
ohair@286: /**
ohair@286: * Length of the valid data in {@link #data}.
ohair@286: */
ohair@286: private int dataLen;
ohair@286: /**
ohair@286: * True if {@link #data} can be cloned by reference
ohair@286: * if Base64Data instance is cloned.
ohair@286: */
ohair@286: private boolean dataCloneByRef;
ohair@286: /**
ohair@286: * Optional MIME type of {@link #data}.
ohair@286: *
ohair@286: * Unused when {@link #dataHandler} is set.
ohair@286: * Use {@link DataHandler#getContentType()} in that case.
ohair@286: */
ohair@286: private String mimeType;
ohair@286:
ohair@286: /**
ohair@286: * Default constructor
ohair@286: */
ohair@286: public Base64Data() {
ohair@286: }
ohair@286:
alanb@368: private static final Logger logger = Logger.getLogger(Base64Data.class.getName());
alanb@368:
ohair@286: /**
ohair@286: * Clone constructor
ohair@286: * @param that needs to be cloned
ohair@286: */
ohair@286: public Base64Data(Base64Data that) {
ohair@286: that.get();
ohair@286: if (that.dataCloneByRef) {
ohair@286: this.data = that.data;
ohair@286: } else {
ohair@286: this.data = new byte[that.dataLen];
ohair@286: System.arraycopy(that.data, 0, this.data, 0, that.dataLen);
ohair@286: }
ohair@286:
ohair@286: this.dataCloneByRef = true;
ohair@286: this.dataLen = that.dataLen;
ohair@286: this.dataHandler = null;
ohair@286: this.mimeType = that.mimeType;
ohair@286: }
ohair@286:
ohair@286: /**
ohair@286: * Fills in the data object by a portion of the byte[].
ohair@286: *
ohair@286: * @param data actual data
ohair@286: * @param len
ohair@286: * data[0] to data[len-1] are treated as the data.
ohair@286: * @param mimeType MIME type
ohair@286: * @param cloneByRef
ohair@286: * true if data[] can be cloned by reference
ohair@286: */
ohair@286: public void set(byte[] data, int len, String mimeType, boolean cloneByRef) {
ohair@286: this.data = data;
ohair@286: this.dataLen = len;
ohair@286: this.dataCloneByRef = cloneByRef;
ohair@286: this.dataHandler = null;
ohair@286: this.mimeType = mimeType;
ohair@286: }
ohair@286:
ohair@286: /**
ohair@286: * Fills in the data object by a portion of the byte[].
ohair@286: *
ohair@286: * @param data actual data bytes
ohair@286: * @param len
ohair@286: * data[0] to data[len-1] are treated as the data.
ohair@286: * @param mimeType MIME type
ohair@286: */
ohair@286: public void set(byte[] data, int len, String mimeType) {
ohair@286: set(data,len,mimeType,false);
ohair@286: }
ohair@286:
ohair@286: /**
ohair@286: * Fills in the data object by the byte[] of the exact length.
ohair@286: *
ohair@286: * @param data
ohair@286: * this buffer may be owned directly by the unmarshaleld JAXB object.
ohair@286: * @param mimeType MIME type
ohair@286: */
ohair@286: public void set(byte[] data,String mimeType) {
ohair@286: set(data,data.length,mimeType,false);
ohair@286: }
ohair@286:
ohair@286: /**
ohair@286: * Fills in the data object by a {@link DataHandler}.
ohair@286: *
ohair@286: * @param data DataHandler for the data
ohair@286: */
ohair@286: public void set(DataHandler data) {
ohair@286: assert data!=null;
ohair@286: this.dataHandler = data;
ohair@286: this.data = null;
ohair@286: }
ohair@286:
ohair@286: /**
ohair@286: * Gets the raw data. If the returned DataHandler is {@link StreamingDataHandler},
ohair@286: * callees may need to downcast to take advantage of its capabilities.
ohair@286: *
ohair@286: * @see StreamingDataHandler
ohair@286: * @return DataHandler for the data
ohair@286: */
ohair@286: public DataHandler getDataHandler() {
ohair@286: if(dataHandler==null){
ohair@286: dataHandler = new Base64StreamingDataHandler(new Base64DataSource());
ohair@286: } else if (!(dataHandler instanceof StreamingDataHandler)) {
ohair@286: dataHandler = new FilterDataHandler(dataHandler);
ohair@286: }
ohair@286: return dataHandler;
ohair@286: }
ohair@286:
ohair@286: private final class Base64DataSource implements DataSource {
ohair@286: public String getContentType() {
ohair@286: return getMimeType();
ohair@286: }
ohair@286:
ohair@286: public InputStream getInputStream() {
ohair@286: return new ByteArrayInputStream(data,0,dataLen);
ohair@286: }
ohair@286:
ohair@286: public String getName() {
ohair@286: return null;
ohair@286: }
ohair@286:
ohair@286: public OutputStream getOutputStream() {
ohair@286: throw new UnsupportedOperationException();
ohair@286: }
ohair@286:
ohair@286: }
ohair@286:
ohair@286: private final class Base64StreamingDataHandler extends StreamingDataHandler {
ohair@286:
ohair@286: Base64StreamingDataHandler(DataSource source) {
ohair@286: super(source);
ohair@286: }
ohair@286:
ohair@286: public InputStream readOnce() throws IOException {
ohair@286: return getDataSource().getInputStream();
ohair@286: }
ohair@286:
ohair@286: public void moveTo(File dst) throws IOException {
ohair@286: FileOutputStream fout = new FileOutputStream(dst);
ohair@286: try {
ohair@286: fout.write(data, 0, dataLen);
ohair@286: } finally {
ohair@286: fout.close();
ohair@286: }
ohair@286: }
ohair@286:
ohair@286: public void close() throws IOException {
ohair@286: // nothing to do
ohair@286: }
ohair@286: }
ohair@286:
ohair@286: private static final class FilterDataHandler extends StreamingDataHandler {
ohair@286:
ohair@286: FilterDataHandler(DataHandler dh) {
ohair@286: super(dh.getDataSource());
ohair@286: }
ohair@286:
ohair@286: public InputStream readOnce() throws IOException {
ohair@286: return getDataSource().getInputStream();
ohair@286: }
ohair@286:
ohair@286: public void moveTo(File dst) throws IOException {
ohair@286: byte[] buf = new byte[8192];
ohair@286: InputStream in = null;
ohair@286: OutputStream out = null;
ohair@286: try {
ohair@286: in = getDataSource().getInputStream();
ohair@286: out = new FileOutputStream(dst);
ohair@286: while (true) {
ohair@286: int amountRead = in.read(buf);
ohair@286: if (amountRead == -1) {
ohair@286: break;
ohair@286: }
ohair@286: out.write(buf, 0, amountRead);
ohair@286: }
ohair@286: } finally {
ohair@286: if (in != null) {
ohair@286: try {
ohair@286: in.close();
ohair@286: } catch(IOException ioe) {
ohair@286: // nothing to do
ohair@286: }
ohair@286: }
ohair@286: if (out != null) {
ohair@286: try {
ohair@286: out.close();
ohair@286: } catch(IOException ioe) {
ohair@286: // nothing to do
ohair@286: }
ohair@286: }
ohair@286: }
ohair@286: }
ohair@286:
ohair@286: public void close() throws IOException {
ohair@286: // nothing to do
ohair@286: }
ohair@286: }
ohair@286:
ohair@286: /**
ohair@286: * Gets the byte[] of the exact length.
ohair@286: *
ohair@286: * @return byte[] for data
ohair@286: */
ohair@286: public byte[] getExact() {
ohair@286: get();
ohair@286: if(dataLen!=data.length) {
ohair@286: byte[] buf = new byte[dataLen];
ohair@286: System.arraycopy(data,0,buf,0,dataLen);
ohair@286: data = buf;
ohair@286: }
ohair@286: return data;
ohair@286: }
ohair@286:
ohair@286: /**
ohair@286: * Gets the data as an {@link InputStream}.
ohair@286: *
ohair@286: * @return data as InputStream
ohair@286: * @throws IOException if i/o error occurs
ohair@286: */
ohair@286: public InputStream getInputStream() throws IOException {
ohair@286: if(dataHandler!=null)
ohair@286: return dataHandler.getInputStream();
ohair@286: else
ohair@286: return new ByteArrayInputStream(data,0,dataLen);
ohair@286: }
ohair@286:
ohair@286: /**
ohair@286: * Returns false if this object only has {@link DataHandler} and therefore
ohair@286: * {@link #get()} operation is likely going to be expensive.
ohair@286: *
ohair@286: * @return false if it has only DataHandler
ohair@286: */
ohair@286: public boolean hasData() {
ohair@286: return data!=null;
ohair@286: }
ohair@286:
ohair@286: /**
ohair@286: * Gets the raw data. The size of the byte array maybe larger than the actual length.
ohair@286: *
ohair@286: * @return data as byte[], the array may be larger
ohair@286: */
ohair@286: public byte[] get() {
ohair@286: if(data==null) {
ohair@286: try {
ohair@286: ByteArrayOutputStreamEx baos = new ByteArrayOutputStreamEx(1024);
ohair@286: InputStream is = dataHandler.getDataSource().getInputStream();
ohair@286: baos.readFrom(is);
ohair@286: is.close();
ohair@286: data = baos.getBuffer();
ohair@286: dataLen = baos.size();
ohair@286: dataCloneByRef = true;
ohair@286: } catch (IOException e) {
ohair@286: // TODO: report the error to the unmarshaller
ohair@286: dataLen = 0; // recover by assuming length-0 data
ohair@286: }
ohair@286: }
ohair@286: return data;
ohair@286: }
ohair@286:
ohair@286: /**
ohair@286: * Gets the length of the binary data counted in bytes.
ohair@286: *
ohair@286: * Note that if this object encapsulates {@link DataHandler},
ohair@286: * this method would have to read the whole thing into {@code byte[]}
ohair@286: * just to count the length, because {@link DataHandler}
ohair@286: * doesn't easily expose the length.
ohair@286: *
ohair@286: * @return no of bytes
ohair@286: */
ohair@286: public int getDataLen() {
ohair@286: get();
ohair@286: return dataLen;
ohair@286: }
ohair@286:
ohair@286: public String getMimeType() {
ohair@286: if(mimeType==null)
ohair@286: return "application/octet-stream";
ohair@286: return mimeType;
ohair@286: }
ohair@286:
ohair@286: /**
ohair@286: * Gets the number of characters needed to represent
ohair@286: * this binary data in the base64 encoding.
ohair@286: */
ohair@286: public int length() {
ohair@286: // for each 3 bytes you use 4 chars
ohair@286: // if the remainder is 1 or 2 there will be 4 more
ohair@286: get(); // fill in the buffer if necessary
ohair@286: return ((dataLen+2)/3)*4;
ohair@286: }
ohair@286:
ohair@286: /**
ohair@286: * Encode this binary data in the base64 encoding
ohair@286: * and returns the character at the specified position.
ohair@286: */
ohair@286: public char charAt(int index) {
ohair@286: // we assume that the length() method is called before this method
ohair@286: // (otherwise how would the caller know that the index is valid?)
ohair@286: // so we assume that the byte[] is already populated
ohair@286:
ohair@286: int offset = index%4;
ohair@286: int base = (index/4)*3;
ohair@286:
ohair@286: byte b1,b2;
ohair@286:
ohair@286: switch(offset) {
ohair@286: case 0:
ohair@286: return Base64Encoder.encode(data[base]>>2);
ohair@286: case 1:
ohair@286: if(base+1