001/*
002 * This library is part of OpenCms -
003 * the Open Source Content Management System
004 *
005 * Copyright (c) Alkacon Software GmbH & Co. KG (http://www.alkacon.com)
006 *
007 * This library is free software; you can redistribute it and/or
008 * modify it under the terms of the GNU Lesser General Public
009 * License as published by the Free Software Foundation; either
010 * version 2.1 of the License, or (at your option) any later version.
011 *
012 * This library is distributed in the hope that it will be useful,
013 * but WITHOUT ANY WARRANTY; without even the implied warranty of
014 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
015 * Lesser General Public License for more details.
016 *
017 * For further information about Alkacon Software GmbH & Co. KG, please see the
018 * company website: http://www.alkacon.com
019 *
020 * For further information about OpenCms, please see the
021 * project website: http://www.opencms.org
022 *
023 * You should have received a copy of the GNU Lesser General Public
024 * License along with this library; if not, write to the Free Software
025 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
026 */
027
028package org.opencms.util;
029
030import java.io.ByteArrayInputStream;
031import java.io.IOException;
032import java.io.InputStream;
033import java.io.OutputStream;
034
035import javax.activation.DataSource;
036
037/**
038 * A DataSource backed by a byte array. The byte array may be passed in directly, or may be initialized from an InputStream or a String.
039 *
040 * @since 6.3.0
041 */
042public class CmsByteArrayDataSource implements DataSource {
043
044    /** The MIME content type of the data. */
045    private String m_contentType;
046
047    /** The data. */
048    private byte[] m_data;
049
050    /** The name of the data. */
051    private String m_name;
052
053    /**
054     * Creates a ByteArrayDataSource with data from the specified byte array and with the specified MIME type.
055     *
056     * @param name the name of the data
057     * @param data the data
058     * @param contentType the MIME content type of the data
059     */
060    public CmsByteArrayDataSource(String name, byte[] data, String contentType) {
061
062        m_name = name;
063        m_data = data;
064        m_contentType = contentType;
065    }
066
067    /**
068     *
069     * @see javax.activation.DataSource#getContentType()
070     */
071    public String getContentType() {
072
073        return m_contentType;
074    }
075
076    /**
077     *
078     * @see javax.activation.DataSource#getInputStream()
079     */
080    public InputStream getInputStream() {
081
082        return new ByteArrayInputStream(m_data);
083    }
084
085    /**
086     *
087     * @see javax.activation.DataSource#getName()
088     */
089    public String getName() {
090
091        return m_name;
092    }
093
094    /**
095     *
096     * @see javax.activation.DataSource#getOutputStream()
097     */
098    public OutputStream getOutputStream() throws IOException {
099
100        throw new IOException("ByteArrayDataSource cannot support getOutputStream( )");
101    }
102}