325N/A/*
325N/A * Copyright (c) 1997, 2010, Oracle and/or its affiliates. All rights reserved.
325N/A * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
325N/A *
325N/A * This code is free software; you can redistribute it and/or modify it
325N/A * under the terms of the GNU General Public License version 2 only, as
325N/A * published by the Free Software Foundation. Oracle designates this
325N/A * particular file as subject to the "Classpath" exception as provided
325N/A * by Oracle in the LICENSE file that accompanied this code.
325N/A *
325N/A * This code is distributed in the hope that it will be useful, but WITHOUT
325N/A * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
325N/A * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
325N/A * version 2 for more details (a copy is included in the LICENSE file that
325N/A * accompanied this code).
325N/A *
325N/A * You should have received a copy of the GNU General Public License version
325N/A * 2 along with this work; if not, write to the Free Software Foundation,
325N/A * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
325N/A *
325N/A * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
325N/A * or visit www.oracle.com if you need additional information or have any
325N/A * questions.
325N/A */
325N/A
325N/Apackage com.sun.xml.internal.ws.message;
325N/A
325N/Aimport com.sun.istack.internal.NotNull;
325N/Aimport com.sun.xml.internal.ws.api.message.Attachment;
325N/Aimport com.sun.xml.internal.ws.util.ByteArrayDataSource;
325N/Aimport com.sun.xml.internal.ws.encoding.DataSourceStreamingDataHandler;
325N/A
325N/Aimport java.io.ByteArrayInputStream;
325N/A
325N/Aimport javax.activation.DataHandler;
325N/Aimport javax.xml.soap.AttachmentPart;
325N/Aimport javax.xml.soap.SOAPException;
325N/Aimport javax.xml.soap.SOAPMessage;
325N/Aimport javax.xml.transform.Source;
325N/Aimport javax.xml.transform.stream.StreamSource;
325N/Aimport java.io.IOException;
325N/Aimport java.io.InputStream;
325N/Aimport java.io.OutputStream;
325N/A
325N/A/**
325N/A * @author Jitendra Kotamraju
325N/A */
325N/Apublic final class ByteArrayAttachment implements Attachment {
325N/A
325N/A private final String contentId;
325N/A private byte[] data;
325N/A private int start;
325N/A private final int len;
325N/A private final String mimeType;
325N/A
325N/A public ByteArrayAttachment(@NotNull String contentId, byte[] data, int start, int len, String mimeType) {
325N/A this.contentId = contentId;
325N/A this.data = data;
325N/A this.start = start;
325N/A this.len = len;
325N/A this.mimeType = mimeType;
325N/A }
325N/A
325N/A public ByteArrayAttachment(@NotNull String contentId, byte[] data, String mimeType) {
325N/A this(contentId, data, 0, data.length, mimeType);
325N/A }
325N/A
325N/A public String getContentId() {
325N/A return contentId;
325N/A }
325N/A
325N/A public String getContentType() {
325N/A return mimeType;
325N/A }
325N/A
325N/A public byte[] asByteArray() {
325N/A if(start!=0 || len!=data.length) {
325N/A // if our buffer isn't exact, switch to the exact one
325N/A byte[] exact = new byte[len];
325N/A System.arraycopy(data,start,exact,0,len);
325N/A start = 0;
325N/A data = exact;
325N/A }
325N/A return data;
325N/A }
325N/A
325N/A public DataHandler asDataHandler() {
325N/A return new DataSourceStreamingDataHandler(new ByteArrayDataSource(data,start,len,getContentType()));
325N/A }
325N/A
325N/A public Source asSource() {
325N/A return new StreamSource(asInputStream());
325N/A }
325N/A
325N/A public InputStream asInputStream() {
325N/A return new ByteArrayInputStream(data,start,len);
325N/A }
325N/A
325N/A public void writeTo(OutputStream os) throws IOException {
325N/A os.write(asByteArray());
325N/A }
325N/A
325N/A public void writeTo(SOAPMessage saaj) throws SOAPException {
325N/A AttachmentPart part = saaj.createAttachmentPart();
325N/A part.setDataHandler(asDataHandler());
325N/A part.setContentId(contentId);
325N/A saaj.addAttachmentPart(part);
325N/A }
325N/A
325N/A}