View Javadoc

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