src/share/jaxws_classes/com/sun/xml/internal/bind/v2/runtime/unmarshaller/Base64Data.java

changeset 0
373ffda63c9a
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/src/share/jaxws_classes/com/sun/xml/internal/bind/v2/runtime/unmarshaller/Base64Data.java	Wed Apr 27 01:27:09 2016 +0800
     1.3 @@ -0,0 +1,307 @@
     1.4 +/*
     1.5 + * Copyright (c) 1997, 2011, 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.bind.v2.runtime.unmarshaller;
    1.30 +
    1.31 +import java.io.ByteArrayInputStream;
    1.32 +import java.io.IOException;
    1.33 +import java.io.InputStream;
    1.34 +import java.io.OutputStream;
    1.35 +
    1.36 +import javax.activation.DataHandler;
    1.37 +import javax.activation.DataSource;
    1.38 +import javax.xml.stream.XMLStreamException;
    1.39 +
    1.40 +import javax.xml.stream.XMLStreamWriter;
    1.41 +
    1.42 +import com.sun.xml.internal.bind.DatatypeConverterImpl;
    1.43 +import com.sun.xml.internal.bind.v2.runtime.XMLSerializer;
    1.44 +import com.sun.xml.internal.bind.v2.runtime.output.Pcdata;
    1.45 +import com.sun.xml.internal.bind.v2.runtime.output.UTF8XmlOutput;
    1.46 +import com.sun.xml.internal.bind.v2.util.ByteArrayOutputStreamEx;
    1.47 +import com.sun.istack.internal.Nullable;
    1.48 +
    1.49 +/**
    1.50 + * Fed to unmarshaller when the 'text' data is actually
    1.51 + * a virtual image of base64 encoding of the binary data
    1.52 + * transferred on the wire.
    1.53 + *
    1.54 + * Used for the MTOM support.
    1.55 + *
    1.56 + * This object is mutable and the owner of this object can
    1.57 + * reuse it with new data.
    1.58 + *
    1.59 + * Also used by the marshaller to write out the binary data
    1.60 + * that could be possibly attached.
    1.61 + *
    1.62 + * @see XmlVisitor#text(CharSequence)
    1.63 + * @see XMLSerializer#text(Pcdata,String)
    1.64 + *
    1.65 + * @author Kohsuke Kawaguchi, Martin Grebac
    1.66 + */
    1.67 +public final class Base64Data extends Pcdata {
    1.68 +
    1.69 +    // either dataHandler or (data,dataLen,mimeType?) must be present
    1.70 +    private DataHandler dataHandler;
    1.71 +    private byte[] data;
    1.72 +    /**
    1.73 +     * Length of the valid data in {@link #data}.
    1.74 +     */
    1.75 +    private int dataLen;
    1.76 +    /**
    1.77 +     * Optional MIME type of {@link #data}.
    1.78 +     *
    1.79 +     * Unused when {@link #dataHandler} is set.
    1.80 +     * Use {@link DataHandler#getContentType()} in that case.
    1.81 +     */
    1.82 +    private @Nullable
    1.83 +    String mimeType;
    1.84 +
    1.85 +    /**
    1.86 +     * Fills in the data object by a portion of the byte[].
    1.87 +     *
    1.88 +     * @param len
    1.89 +     *      data[0] to data[len-1] are treated as the data.
    1.90 +     */
    1.91 +    public void set(byte[] data, int len, @Nullable String mimeType) {
    1.92 +        this.data = data;
    1.93 +        this.dataLen = len;
    1.94 +        this.dataHandler = null;
    1.95 +        this.mimeType = mimeType;
    1.96 +    }
    1.97 +
    1.98 +    /**
    1.99 +     * Fills in the data object by the byte[] of the exact length.
   1.100 +     *
   1.101 +     * @param data
   1.102 +     *      this buffer may be owned directly by the unmarshaleld JAXB object.
   1.103 +     */
   1.104 +    public void set(byte[] data, @Nullable String mimeType) {
   1.105 +        set(data, data.length, mimeType);
   1.106 +    }
   1.107 +
   1.108 +    /**
   1.109 +     * Fills in the data object by a {@link DataHandler}.
   1.110 +     */
   1.111 +    public void set(DataHandler data) {
   1.112 +        assert data != null;
   1.113 +        this.dataHandler = data;
   1.114 +        this.data = null;
   1.115 +    }
   1.116 +
   1.117 +    /**
   1.118 +     * Gets the raw data.
   1.119 +     */
   1.120 +    public DataHandler getDataHandler() {
   1.121 +        if (dataHandler == null) {
   1.122 +            dataHandler = new DataHandler(new DataSource() {
   1.123 +
   1.124 +                public String getContentType() {
   1.125 +                    return getMimeType();
   1.126 +                }
   1.127 +
   1.128 +                public InputStream getInputStream() {
   1.129 +                    return new ByteArrayInputStream(data, 0, dataLen);
   1.130 +                }
   1.131 +
   1.132 +                public String getName() {
   1.133 +                    return null;
   1.134 +                }
   1.135 +
   1.136 +                public OutputStream getOutputStream() {
   1.137 +                    throw new UnsupportedOperationException();
   1.138 +                }
   1.139 +            });
   1.140 +        }
   1.141 +
   1.142 +        return dataHandler;
   1.143 +    }
   1.144 +
   1.145 +    /**
   1.146 +     * Gets the byte[] of the exact length.
   1.147 +     */
   1.148 +    public byte[] getExact() {
   1.149 +        get();
   1.150 +        if (dataLen != data.length) {
   1.151 +            byte[] buf = new byte[dataLen];
   1.152 +            System.arraycopy(data, 0, buf, 0, dataLen);
   1.153 +            data = buf;
   1.154 +        }
   1.155 +        return data;
   1.156 +    }
   1.157 +
   1.158 +    /**
   1.159 +     * Gets the data as an {@link InputStream}.
   1.160 +     */
   1.161 +    public InputStream getInputStream() throws IOException {
   1.162 +        if (dataHandler != null) {
   1.163 +            return dataHandler.getInputStream();
   1.164 +        } else {
   1.165 +            return new ByteArrayInputStream(data, 0, dataLen);
   1.166 +        }
   1.167 +    }
   1.168 +
   1.169 +    /**
   1.170 +     * Returns false if this object only has {@link DataHandler} and therefore
   1.171 +     * {@link #get()} operation is likely going to be expensive.
   1.172 +     */
   1.173 +    public boolean hasData() {
   1.174 +        return data != null;
   1.175 +    }
   1.176 +
   1.177 +    /**
   1.178 +     * Gets the raw data. The size of the byte array maybe larger than the actual length.
   1.179 +     */
   1.180 +    public byte[] get() {
   1.181 +        if (data == null) {
   1.182 +            try {
   1.183 +                ByteArrayOutputStreamEx baos = new ByteArrayOutputStreamEx(1024);
   1.184 +                InputStream is = dataHandler.getDataSource().getInputStream();
   1.185 +                baos.readFrom(is);
   1.186 +                is.close();
   1.187 +                data = baos.getBuffer();
   1.188 +                dataLen = baos.size();
   1.189 +            } catch (IOException e) {
   1.190 +                // TODO: report the error to the unmarshaller
   1.191 +                dataLen = 0;    // recover by assuming length-0 data
   1.192 +            }
   1.193 +        }
   1.194 +        return data;
   1.195 +    }
   1.196 +
   1.197 +    public int getDataLen() {
   1.198 +        return dataLen;
   1.199 +    }
   1.200 +
   1.201 +    public String getMimeType() {
   1.202 +        if (mimeType == null) {
   1.203 +            return "application/octet-stream";
   1.204 +        }
   1.205 +        return mimeType;
   1.206 +    }
   1.207 +
   1.208 +    /**
   1.209 +     * Gets the number of characters needed to represent
   1.210 +     * this binary data in the base64 encoding.
   1.211 +     */
   1.212 +    public int length() {
   1.213 +        // for each 3 bytes you use 4 chars
   1.214 +        // if the remainder is 1 or 2 there will be 4 more
   1.215 +        get();  // fill in the buffer if necessary
   1.216 +        return ((dataLen + 2) / 3) * 4;
   1.217 +    }
   1.218 +
   1.219 +    /**
   1.220 +     * Encode this binary data in the base64 encoding
   1.221 +     * and returns the character at the specified position.
   1.222 +     */
   1.223 +    public char charAt(int index) {
   1.224 +        // we assume that the length() method is called before this method
   1.225 +        // (otherwise how would the caller know that the index is valid?)
   1.226 +        // so we assume that the byte[] is already populated
   1.227 +
   1.228 +        int offset = index % 4;
   1.229 +        int base = (index / 4) * 3;
   1.230 +
   1.231 +        byte b1, b2;
   1.232 +
   1.233 +        switch (offset) {
   1.234 +            case 0:
   1.235 +                return DatatypeConverterImpl.encode(data[base] >> 2);
   1.236 +            case 1:
   1.237 +                if (base + 1 < dataLen) {
   1.238 +                    b1 = data[base + 1];
   1.239 +                } else {
   1.240 +                    b1 = 0;
   1.241 +                }
   1.242 +                return DatatypeConverterImpl.encode(
   1.243 +                        ((data[base] & 0x3) << 4)
   1.244 +                        | ((b1 >> 4) & 0xF));
   1.245 +            case 2:
   1.246 +                if (base + 1 < dataLen) {
   1.247 +                    b1 = data[base + 1];
   1.248 +                    if (base + 2 < dataLen) {
   1.249 +                        b2 = data[base + 2];
   1.250 +                    } else {
   1.251 +                        b2 = 0;
   1.252 +                    }
   1.253 +
   1.254 +                    return DatatypeConverterImpl.encode(
   1.255 +                            ((b1 & 0xF) << 2)
   1.256 +                            | ((b2 >> 6) & 0x3));
   1.257 +                } else {
   1.258 +                    return '=';
   1.259 +                }
   1.260 +            case 3:
   1.261 +                if (base + 2 < dataLen) {
   1.262 +                    return DatatypeConverterImpl.encode(data[base + 2] & 0x3F);
   1.263 +                } else {
   1.264 +                    return '=';
   1.265 +                }
   1.266 +        }
   1.267 +
   1.268 +        throw new IllegalStateException();
   1.269 +    }
   1.270 +
   1.271 +    /**
   1.272 +     * Internally this is only used to split a text to a list,
   1.273 +     * which doesn't happen that much for base64.
   1.274 +     * So this method should be smaller than faster.
   1.275 +     */
   1.276 +    public CharSequence subSequence(int start, int end) {
   1.277 +        StringBuilder buf = new StringBuilder();
   1.278 +        get();  // fill in the buffer if we haven't done so
   1.279 +        for (int i = start; i < end; i++) {
   1.280 +            buf.append(charAt(i));
   1.281 +        }
   1.282 +        return buf;
   1.283 +    }
   1.284 +
   1.285 +    /**
   1.286 +     * Returns the base64 encoded string of this data.
   1.287 +     */
   1.288 +    public String toString() {
   1.289 +        get();  // fill in the buffer
   1.290 +        return DatatypeConverterImpl._printBase64Binary(data, 0, dataLen);
   1.291 +    }
   1.292 +
   1.293 +    @Override
   1.294 +    public void writeTo(char[] buf, int start) {
   1.295 +        get();
   1.296 +        DatatypeConverterImpl._printBase64Binary(data, 0, dataLen, buf, start);
   1.297 +    }
   1.298 +
   1.299 +    public void writeTo(UTF8XmlOutput output) throws IOException {
   1.300 +        // TODO: this is inefficient if the data source is note byte[] but DataHandler
   1.301 +        get();
   1.302 +        output.text(data, dataLen);
   1.303 +    }
   1.304 +
   1.305 +    public void writeTo(XMLStreamWriter output) throws IOException, XMLStreamException {
   1.306 +        get();
   1.307 +        DatatypeConverterImpl._printBase64Binary(data, 0, dataLen, output);
   1.308 +    }
   1.309 +
   1.310 +}

mercurial