1a8b46a3d3b6ed1488df10740653829283572903bNarayan Kamath/* 2a8b46a3d3b6ed1488df10740653829283572903bNarayan Kamath * $Header: /home/jerenkrantz/tmp/commons/commons-convert/cvs/home/cvs/jakarta-commons//httpclient/src/java/org/apache/commons/httpclient/methods/multipart/ByteArrayPartSource.java,v 1.7 2004/04/18 23:51:37 jsdever Exp $ 3a8b46a3d3b6ed1488df10740653829283572903bNarayan Kamath * $Revision: 480424 $ 4a8b46a3d3b6ed1488df10740653829283572903bNarayan Kamath * $Date: 2006-11-29 06:56:49 +0100 (Wed, 29 Nov 2006) $ 5a8b46a3d3b6ed1488df10740653829283572903bNarayan Kamath * 6a8b46a3d3b6ed1488df10740653829283572903bNarayan Kamath * ==================================================================== 7a8b46a3d3b6ed1488df10740653829283572903bNarayan Kamath * 8a8b46a3d3b6ed1488df10740653829283572903bNarayan Kamath * Licensed to the Apache Software Foundation (ASF) under one or more 9a8b46a3d3b6ed1488df10740653829283572903bNarayan Kamath * contributor license agreements. See the NOTICE file distributed with 10a8b46a3d3b6ed1488df10740653829283572903bNarayan Kamath * this work for additional information regarding copyright ownership. 11a8b46a3d3b6ed1488df10740653829283572903bNarayan Kamath * The ASF licenses this file to You under the Apache License, Version 2.0 12a8b46a3d3b6ed1488df10740653829283572903bNarayan Kamath * (the "License"); you may not use this file except in compliance with 13a8b46a3d3b6ed1488df10740653829283572903bNarayan Kamath * the License. You may obtain a copy of the License at 14a8b46a3d3b6ed1488df10740653829283572903bNarayan Kamath * 15a8b46a3d3b6ed1488df10740653829283572903bNarayan Kamath * http://www.apache.org/licenses/LICENSE-2.0 16a8b46a3d3b6ed1488df10740653829283572903bNarayan Kamath * 17a8b46a3d3b6ed1488df10740653829283572903bNarayan Kamath * Unless required by applicable law or agreed to in writing, software 18a8b46a3d3b6ed1488df10740653829283572903bNarayan Kamath * distributed under the License is distributed on an "AS IS" BASIS, 19a8b46a3d3b6ed1488df10740653829283572903bNarayan Kamath * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 20a8b46a3d3b6ed1488df10740653829283572903bNarayan Kamath * See the License for the specific language governing permissions and 21a8b46a3d3b6ed1488df10740653829283572903bNarayan Kamath * limitations under the License. 22a8b46a3d3b6ed1488df10740653829283572903bNarayan Kamath * ==================================================================== 23a8b46a3d3b6ed1488df10740653829283572903bNarayan Kamath * 24a8b46a3d3b6ed1488df10740653829283572903bNarayan Kamath * This software consists of voluntary contributions made by many 25a8b46a3d3b6ed1488df10740653829283572903bNarayan Kamath * individuals on behalf of the Apache Software Foundation. For more 26a8b46a3d3b6ed1488df10740653829283572903bNarayan Kamath * information on the Apache Software Foundation, please see 27a8b46a3d3b6ed1488df10740653829283572903bNarayan Kamath * <http://www.apache.org/>. 28a8b46a3d3b6ed1488df10740653829283572903bNarayan Kamath * 29a8b46a3d3b6ed1488df10740653829283572903bNarayan Kamath */ 30a8b46a3d3b6ed1488df10740653829283572903bNarayan Kamath 31a8b46a3d3b6ed1488df10740653829283572903bNarayan Kamathpackage com.android.internal.http.multipart; 32a8b46a3d3b6ed1488df10740653829283572903bNarayan Kamath 33a8b46a3d3b6ed1488df10740653829283572903bNarayan Kamathimport java.io.ByteArrayInputStream; 34a8b46a3d3b6ed1488df10740653829283572903bNarayan Kamathimport java.io.InputStream; 35a8b46a3d3b6ed1488df10740653829283572903bNarayan Kamath 36a8b46a3d3b6ed1488df10740653829283572903bNarayan Kamath/** 37a8b46a3d3b6ed1488df10740653829283572903bNarayan Kamath * A PartSource that reads from a byte array. This class should be used when 38a8b46a3d3b6ed1488df10740653829283572903bNarayan Kamath * the data to post is already loaded into memory. 39a8b46a3d3b6ed1488df10740653829283572903bNarayan Kamath * 40a8b46a3d3b6ed1488df10740653829283572903bNarayan Kamath * @author <a href="mailto:becke@u.washington.edu">Michael Becke</a> 41a8b46a3d3b6ed1488df10740653829283572903bNarayan Kamath * 42a8b46a3d3b6ed1488df10740653829283572903bNarayan Kamath * @since 2.0 43a8b46a3d3b6ed1488df10740653829283572903bNarayan Kamath */ 44a8b46a3d3b6ed1488df10740653829283572903bNarayan Kamathpublic class ByteArrayPartSource implements PartSource { 45a8b46a3d3b6ed1488df10740653829283572903bNarayan Kamath 46a8b46a3d3b6ed1488df10740653829283572903bNarayan Kamath /** Name of the source file. */ 47a8b46a3d3b6ed1488df10740653829283572903bNarayan Kamath private String fileName; 48a8b46a3d3b6ed1488df10740653829283572903bNarayan Kamath 49a8b46a3d3b6ed1488df10740653829283572903bNarayan Kamath /** Byte array of the source file. */ 50a8b46a3d3b6ed1488df10740653829283572903bNarayan Kamath private byte[] bytes; 51a8b46a3d3b6ed1488df10740653829283572903bNarayan Kamath 52a8b46a3d3b6ed1488df10740653829283572903bNarayan Kamath /** 53a8b46a3d3b6ed1488df10740653829283572903bNarayan Kamath * Constructor for ByteArrayPartSource. 54a8b46a3d3b6ed1488df10740653829283572903bNarayan Kamath * 55a8b46a3d3b6ed1488df10740653829283572903bNarayan Kamath * @param fileName the name of the file these bytes represent 56a8b46a3d3b6ed1488df10740653829283572903bNarayan Kamath * @param bytes the content of this part 57a8b46a3d3b6ed1488df10740653829283572903bNarayan Kamath */ 58a8b46a3d3b6ed1488df10740653829283572903bNarayan Kamath public ByteArrayPartSource(String fileName, byte[] bytes) { 59a8b46a3d3b6ed1488df10740653829283572903bNarayan Kamath 60a8b46a3d3b6ed1488df10740653829283572903bNarayan Kamath this.fileName = fileName; 61a8b46a3d3b6ed1488df10740653829283572903bNarayan Kamath this.bytes = bytes; 62a8b46a3d3b6ed1488df10740653829283572903bNarayan Kamath 63a8b46a3d3b6ed1488df10740653829283572903bNarayan Kamath } 64a8b46a3d3b6ed1488df10740653829283572903bNarayan Kamath 65a8b46a3d3b6ed1488df10740653829283572903bNarayan Kamath /** 66a8b46a3d3b6ed1488df10740653829283572903bNarayan Kamath * @see PartSource#getLength() 67a8b46a3d3b6ed1488df10740653829283572903bNarayan Kamath */ 68a8b46a3d3b6ed1488df10740653829283572903bNarayan Kamath public long getLength() { 69a8b46a3d3b6ed1488df10740653829283572903bNarayan Kamath return bytes.length; 70a8b46a3d3b6ed1488df10740653829283572903bNarayan Kamath } 71a8b46a3d3b6ed1488df10740653829283572903bNarayan Kamath 72a8b46a3d3b6ed1488df10740653829283572903bNarayan Kamath /** 73a8b46a3d3b6ed1488df10740653829283572903bNarayan Kamath * @see PartSource#getFileName() 74a8b46a3d3b6ed1488df10740653829283572903bNarayan Kamath */ 75a8b46a3d3b6ed1488df10740653829283572903bNarayan Kamath public String getFileName() { 76a8b46a3d3b6ed1488df10740653829283572903bNarayan Kamath return fileName; 77a8b46a3d3b6ed1488df10740653829283572903bNarayan Kamath } 78a8b46a3d3b6ed1488df10740653829283572903bNarayan Kamath 79a8b46a3d3b6ed1488df10740653829283572903bNarayan Kamath /** 80a8b46a3d3b6ed1488df10740653829283572903bNarayan Kamath * @see PartSource#createInputStream() 81a8b46a3d3b6ed1488df10740653829283572903bNarayan Kamath */ 82a8b46a3d3b6ed1488df10740653829283572903bNarayan Kamath public InputStream createInputStream() { 83a8b46a3d3b6ed1488df10740653829283572903bNarayan Kamath return new ByteArrayInputStream(bytes); 84a8b46a3d3b6ed1488df10740653829283572903bNarayan Kamath } 85a8b46a3d3b6ed1488df10740653829283572903bNarayan Kamath 86a8b46a3d3b6ed1488df10740653829283572903bNarayan Kamath} 87