1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
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 }