View Javadoc

1   /*
2    * $HeadURL: https://svn.apache.org/repos/asf/httpcomponents/oac.hc3x/trunk/src/java/org/apache/commons/httpclient/methods/ByteArrayRequestEntity.java $
3    * $Revision$
4    * $Date$
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   * [Additional notices, if required by prior licensing conditions]
30   *
31   */
32  package org.apache.commons.httpclient.methods;
33  
34  import java.io.IOException;
35  import java.io.OutputStream;
36  
37  /***
38   * A RequestEntity that contains an array of bytes.
39   * 
40   * @since 3.0
41   */
42  public class ByteArrayRequestEntity implements RequestEntity {
43  
44      /*** The content */
45      private byte[] content;
46      
47      /*** The content type */
48      private String contentType;
49  
50      /***
51       * Creates a new entity with the given content.
52       * @param content The content to set.
53       */
54      public ByteArrayRequestEntity(byte[] content) {
55          this(content, null);
56      }
57      
58      /***
59       * Creates a new entity with the given content and content type.
60       * @param content The content to set.
61       * @param contentType The content type to set or <code>null</code>.
62       */
63      public ByteArrayRequestEntity(byte[] content, String contentType) {
64          super();
65          if (content == null) {
66              throw new IllegalArgumentException("The content cannot be null");
67          }
68          this.content = content;
69          this.contentType = contentType;
70      }
71  
72      /***
73       * @return <code>true</code>
74       */
75      public boolean isRepeatable() {
76          return true;
77      }
78  
79      /* (non-Javadoc)
80       * @see org.apache.commons.httpclient.methods.RequestEntity#getContentType()
81       */
82      public String getContentType() {
83          return contentType;
84      }
85      
86      /* (non-Javadoc)
87       * @see org.apache.commons.httpclient.RequestEntity#writeRequest(java.io.OutputStream)
88       */
89      public void writeRequest(OutputStream out) throws IOException {
90          out.write(content);
91      }
92  
93      /***
94       * @return The length of the content.
95       */
96      public long getContentLength() {
97          return content.length;
98      }
99  
100     /***
101      * @return Returns the content.
102      */
103     public byte[] getContent() {
104         return content;
105     }
106 
107 }