1 /*
2 * $Header: /home/jerenkrantz/tmp/commons/commons-convert/cvs/home/cvs/jakarta-commons//httpclient/src/java/org/apache/commons/httpclient/methods/ByteArrayRequestEntity.java,v 1.3 2004/05/13 02:26:08 mbecke Exp $
3 * $Revision: 155418 $
4 * $Date: 2005-02-26 08:01:52 -0500 (Sat, 26 Feb 2005) $
5 *
6 * ====================================================================
7 *
8 * Copyright 2004 The Apache Software Foundation
9 *
10 * Licensed under the Apache License, Version 2.0 (the "License");
11 * you may not use this file except in compliance with the License.
12 * You may obtain a copy of the License at
13 *
14 * http://www.apache.org/licenses/LICENSE-2.0
15 *
16 * Unless required by applicable law or agreed to in writing, software
17 * distributed under the License is distributed on an "AS IS" BASIS,
18 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
19 * See the License for the specific language governing permissions and
20 * limitations under the License.
21 * ====================================================================
22 *
23 * This software consists of voluntary contributions made by many
24 * individuals on behalf of the Apache Software Foundation. For more
25 * information on the Apache Software Foundation, please see
26 * <http://www.apache.org/>.
27 *
28 * [Additional notices, if required by prior licensing conditions]
29 *
30 */
31 package org.apache.commons.httpclient.methods;
32
33 import java.io.IOException;
34 import java.io.OutputStream;
35
36 /***
37 * A RequestEntity that contains an array of bytes.
38 *
39 * @since 3.0
40 */
41 public class ByteArrayRequestEntity implements RequestEntity {
42
43 /*** The content */
44 private byte[] content;
45
46 /*** The content type */
47 private String contentType;
48
49 /***
50 * Creates a new entity with the given content.
51 * @param content The content to set.
52 */
53 public ByteArrayRequestEntity(byte[] content) {
54 this(content, null);
55 }
56
57 /***
58 * Creates a new entity with the given content and content type.
59 * @param content The content to set.
60 * @param contentType The content type to set or <code>null</code>.
61 */
62 public ByteArrayRequestEntity(byte[] content, String contentType) {
63 super();
64 if (content == null) {
65 throw new IllegalArgumentException("The content cannot be null");
66 }
67 this.content = content;
68 this.contentType = contentType;
69 }
70
71 /***
72 * @return <code>true</code>
73 */
74 public boolean isRepeatable() {
75 return true;
76 }
77
78 /* (non-Javadoc)
79 * @see org.apache.commons.httpclient.methods.RequestEntity#getContentType()
80 */
81 public String getContentType() {
82 return contentType;
83 }
84
85 /* (non-Javadoc)
86 * @see org.apache.commons.httpclient.RequestEntity#writeRequest(java.io.OutputStream)
87 */
88 public void writeRequest(OutputStream out) throws IOException {
89 out.write(content);
90 }
91
92 /***
93 * @return The length of the content.
94 */
95 public long getContentLength() {
96 return content.length;
97 }
98
99 /***
100 * @return Returns the content.
101 */
102 public byte[] getContent() {
103 return content;
104 }
105
106 }