aoqi@0: /* aoqi@0: * Copyright (c) 1997, 2011, 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.bind.v2.runtime.unmarshaller; aoqi@0: aoqi@0: import java.io.ByteArrayInputStream; aoqi@0: import java.io.IOException; aoqi@0: import java.io.InputStream; aoqi@0: import java.io.OutputStream; aoqi@0: aoqi@0: import javax.activation.DataHandler; aoqi@0: import javax.activation.DataSource; aoqi@0: import javax.xml.stream.XMLStreamException; aoqi@0: aoqi@0: import javax.xml.stream.XMLStreamWriter; aoqi@0: aoqi@0: import com.sun.xml.internal.bind.DatatypeConverterImpl; aoqi@0: import com.sun.xml.internal.bind.v2.runtime.XMLSerializer; aoqi@0: import com.sun.xml.internal.bind.v2.runtime.output.Pcdata; aoqi@0: import com.sun.xml.internal.bind.v2.runtime.output.UTF8XmlOutput; aoqi@0: import com.sun.xml.internal.bind.v2.util.ByteArrayOutputStreamEx; aoqi@0: import com.sun.istack.internal.Nullable; aoqi@0: aoqi@0: /** aoqi@0: * Fed to unmarshaller when the 'text' data is actually aoqi@0: * a virtual image of base64 encoding of the binary data aoqi@0: * transferred on the wire. aoqi@0: * aoqi@0: * Used for the MTOM support. aoqi@0: * aoqi@0: * This object is mutable and the owner of this object can aoqi@0: * reuse it with new data. aoqi@0: * aoqi@0: * Also used by the marshaller to write out the binary data aoqi@0: * that could be possibly attached. aoqi@0: * aoqi@0: * @see XmlVisitor#text(CharSequence) aoqi@0: * @see XMLSerializer#text(Pcdata,String) aoqi@0: * aoqi@0: * @author Kohsuke Kawaguchi, Martin Grebac aoqi@0: */ aoqi@0: public final class Base64Data extends Pcdata { aoqi@0: aoqi@0: // either dataHandler or (data,dataLen,mimeType?) must be present aoqi@0: private DataHandler dataHandler; aoqi@0: private byte[] data; aoqi@0: /** aoqi@0: * Length of the valid data in {@link #data}. aoqi@0: */ aoqi@0: private int dataLen; 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 @Nullable aoqi@0: String mimeType; aoqi@0: aoqi@0: /** aoqi@0: * Fills in the data object by a portion of the byte[]. aoqi@0: * aoqi@0: * @param len aoqi@0: * data[0] to data[len-1] are treated as the data. aoqi@0: */ aoqi@0: public void set(byte[] data, int len, @Nullable String mimeType) { aoqi@0: this.data = data; aoqi@0: this.dataLen = len; aoqi@0: this.dataHandler = null; aoqi@0: this.mimeType = mimeType; 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: */ aoqi@0: public void set(byte[] data, @Nullable String mimeType) { aoqi@0: set(data, data.length, mimeType); aoqi@0: } aoqi@0: aoqi@0: /** aoqi@0: * Fills in the data object by a {@link DataHandler}. 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. aoqi@0: */ aoqi@0: public DataHandler getDataHandler() { aoqi@0: if (dataHandler == null) { aoqi@0: dataHandler = new DataHandler(new DataSource() { aoqi@0: 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: return dataHandler; aoqi@0: } aoqi@0: aoqi@0: /** aoqi@0: * Gets the byte[] of the exact length. 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: 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: 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: 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: } 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: public int getDataLen() { 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 DatatypeConverterImpl.encode(data[base] >> 2); aoqi@0: case 1: aoqi@0: if (base + 1 < dataLen) { aoqi@0: b1 = data[base + 1]; aoqi@0: } else { aoqi@0: b1 = 0; aoqi@0: } aoqi@0: return DatatypeConverterImpl.encode( aoqi@0: ((data[base] & 0x3) << 4) aoqi@0: | ((b1 >> 4) & 0xF)); aoqi@0: case 2: aoqi@0: if (base + 1 < dataLen) { aoqi@0: b1 = data[base + 1]; aoqi@0: if (base + 2 < dataLen) { aoqi@0: b2 = data[base + 2]; aoqi@0: } else { aoqi@0: b2 = 0; aoqi@0: } aoqi@0: aoqi@0: return DatatypeConverterImpl.encode( aoqi@0: ((b1 & 0xF) << 2) aoqi@0: | ((b2 >> 6) & 0x3)); aoqi@0: } else { aoqi@0: return '='; aoqi@0: } aoqi@0: case 3: aoqi@0: if (base + 2 < dataLen) { aoqi@0: return DatatypeConverterImpl.encode(data[base + 2] & 0x3F); aoqi@0: } else { aoqi@0: return '='; aoqi@0: } aoqi@0: } aoqi@0: aoqi@0: throw new IllegalStateException(); aoqi@0: } aoqi@0: aoqi@0: /** aoqi@0: * Internally this is only used to split a text to a list, aoqi@0: * which doesn't happen that much for base64. aoqi@0: * So this method should be smaller than faster. aoqi@0: */ aoqi@0: public CharSequence subSequence(int start, int end) { aoqi@0: StringBuilder buf = new StringBuilder(); aoqi@0: get(); // fill in the buffer if we haven't done so aoqi@0: for (int i = start; i < end; i++) { aoqi@0: buf.append(charAt(i)); aoqi@0: } aoqi@0: return buf; aoqi@0: } aoqi@0: aoqi@0: /** aoqi@0: * Returns the base64 encoded string of this data. aoqi@0: */ aoqi@0: public String toString() { aoqi@0: get(); // fill in the buffer aoqi@0: return DatatypeConverterImpl._printBase64Binary(data, 0, dataLen); aoqi@0: } aoqi@0: aoqi@0: @Override aoqi@0: public void writeTo(char[] buf, int start) { aoqi@0: get(); aoqi@0: DatatypeConverterImpl._printBase64Binary(data, 0, dataLen, buf, start); aoqi@0: } aoqi@0: aoqi@0: public void writeTo(UTF8XmlOutput output) throws IOException { aoqi@0: // TODO: this is inefficient if the data source is note byte[] but DataHandler aoqi@0: get(); aoqi@0: output.text(data, dataLen); aoqi@0: } aoqi@0: aoqi@0: public void writeTo(XMLStreamWriter output) throws IOException, XMLStreamException { aoqi@0: get(); aoqi@0: DatatypeConverterImpl._printBase64Binary(data, 0, dataLen, output); aoqi@0: } aoqi@0: aoqi@0: }