View Javadoc

1   /*
2    * ====================================================================
3    * Licensed to the Apache Software Foundation (ASF) under one
4    * or more contributor license agreements.  See the NOTICE file
5    * distributed with this work for additional information
6    * regarding copyright ownership.  The ASF licenses this file
7    * to you under the Apache License, Version 2.0 (the
8    * "License"); you may not use this file except in compliance
9    * with the License.  You may obtain a copy of the License at
10   *
11   *   http://www.apache.org/licenses/LICENSE-2.0
12   *
13   * Unless required by applicable law or agreed to in writing,
14   * software distributed under the License is distributed on an
15   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16   * KIND, either express or implied.  See the License for the
17   * specific language governing permissions and limitations
18   * under the License.
19   * ====================================================================
20   *
21   * This software consists of voluntary contributions made by many
22   * individuals on behalf of the Apache Software Foundation.  For more
23   * information on the Apache Software Foundation, please see
24   * <http://www.apache.org/>.
25   *
26   */
27  
28  package org.apache.http.entity;
29  
30  import java.io.ByteArrayInputStream;
31  import java.io.ByteArrayOutputStream;
32  import java.io.IOException;
33  import java.io.InputStream;
34  import java.io.ObjectOutputStream;
35  import java.io.OutputStream;
36  import java.io.Serializable;
37  
38  import org.apache.http.annotation.NotThreadSafe;
39  
40  /**
41   * A streamed entity that obtains its content from a {@link Serializable}.
42   * The content obtained from the {@link Serializable} instance can
43   * optionally be buffered in a byte array in order to make the
44   * entity self-contained and repeatable.
45   *
46   * @since 4.0
47   */
48  @NotThreadSafe
49  public class SerializableEntity extends AbstractHttpEntity {
50  
51      private byte[] objSer;
52  
53      private Serializable objRef;
54  
55      /**
56       * Creates new instance of this class.
57       *
58       * @param ser input
59       * @param bufferize tells whether the content should be
60       *        stored in an internal buffer
61       * @throws IOException in case of an I/O error
62       */
63      public SerializableEntity(Serializable ser, boolean bufferize) throws IOException {
64          super();
65          if (ser == null) {
66              throw new IllegalArgumentException("Source object may not be null");
67          }
68  
69          if (bufferize) {
70              createBytes(ser);
71          } else {
72              this.objRef = ser;
73          }
74      }
75  
76      private void createBytes(Serializable ser) throws IOException {
77          ByteArrayOutputStream baos = new ByteArrayOutputStream();
78          ObjectOutputStream out = new ObjectOutputStream(baos);
79          out.writeObject(ser);
80          out.flush();
81          this.objSer = baos.toByteArray();
82      }
83  
84      public InputStream getContent() throws IOException, IllegalStateException {
85          if (this.objSer == null) {
86              createBytes(this.objRef);
87          }
88          return new ByteArrayInputStream(this.objSer);
89      }
90  
91      public long getContentLength() {
92          if (this.objSer ==  null) {
93              return -1;
94          } else {
95              return this.objSer.length;
96          }
97      }
98  
99      public boolean isRepeatable() {
100         return true;
101     }
102 
103     public boolean isStreaming() {
104         return this.objSer == null;
105     }
106 
107     public void writeTo(OutputStream outstream) throws IOException {
108         if (outstream == null) {
109             throw new IllegalArgumentException("Output stream may not be null");
110         }
111 
112         if (this.objSer == null) {
113             ObjectOutputStream out = new ObjectOutputStream(outstream);
114             out.writeObject(this.objRef);
115             out.flush();
116         } else {
117             outstream.write(this.objSer);
118             outstream.flush();
119         }
120     }
121 
122 }