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  import org.apache.http.util.Args;
40  
41  /**
42   * A streamed entity that obtains its content from a {@link Serializable}.
43   * The content obtained from the {@link Serializable} instance can
44   * optionally be buffered in a byte array in order to make the
45   * entity self-contained and repeatable.
46   *
47   * @since 4.0
48   */
49  @NotThreadSafe
50  public class SerializableEntity extends AbstractHttpEntity {
51  
52      private byte[] objSer;
53  
54      private Serializable objRef;
55  
56      /**
57       * Creates new instance of this class.
58       *
59       * @param ser input
60       * @param bufferize tells whether the content should be
61       *        stored in an internal buffer
62       * @throws IOException in case of an I/O error
63       */
64      public SerializableEntity(final Serializable ser, final boolean bufferize) throws IOException {
65          super();
66          Args.notNull(ser, "Source object");
67          if (bufferize) {
68              createBytes(ser);
69          } else {
70              this.objRef = ser;
71          }
72      }
73  
74      /**
75       * @since 4.3
76       */
77      public SerializableEntity(final Serializable ser) {
78          super();
79          Args.notNull(ser, "Source object");
80          this.objRef = ser;
81      }
82  
83      private void createBytes(final Serializable ser) throws IOException {
84          final ByteArrayOutputStream baos = new ByteArrayOutputStream();
85          final ObjectOutputStream out = new ObjectOutputStream(baos);
86          out.writeObject(ser);
87          out.flush();
88          this.objSer = baos.toByteArray();
89      }
90  
91      @Override
92      public InputStream getContent() throws IOException, IllegalStateException {
93          if (this.objSer == null) {
94              createBytes(this.objRef);
95          }
96          return new ByteArrayInputStream(this.objSer);
97      }
98  
99      @Override
100     public long getContentLength() {
101         if (this.objSer ==  null) {
102             return -1;
103         } else {
104             return this.objSer.length;
105         }
106     }
107 
108     @Override
109     public boolean isRepeatable() {
110         return true;
111     }
112 
113     @Override
114     public boolean isStreaming() {
115         return this.objSer == null;
116     }
117 
118     @Override
119     public void writeTo(final OutputStream outstream) throws IOException {
120         Args.notNull(outstream, "Output stream");
121         if (this.objSer == null) {
122             final ObjectOutputStream out = new ObjectOutputStream(outstream);
123             out.writeObject(this.objRef);
124             out.flush();
125         } else {
126             outstream.write(this.objSer);
127             outstream.flush();
128         }
129     }
130 
131 }