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.hc.core5.http.io.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.hc.core5.util.Args;
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  public class SerializableEntity extends AbstractHttpEntity {
49  
50      private byte[] objSer;
51  
52      private Serializable objRef;
53  
54      /**
55       * Creates new instance of this class.
56       *
57       * @param ser input
58       * @param bufferize tells whether the content should be
59       *        stored in an internal buffer
60       * @throws IOException in case of an I/O error
61       */
62      public SerializableEntity(final Serializable ser, final boolean bufferize) throws IOException {
63          super();
64          Args.notNull(ser, "Source object");
65          if (bufferize) {
66              createBytes(ser);
67          } else {
68              this.objRef = ser;
69          }
70      }
71  
72      /**
73       * Creates new instance of this class.
74       *
75       * @param serializable The object to serialize.
76       *
77       * @since 4.3
78       */
79      public SerializableEntity(final Serializable serializable) {
80          super();
81          Args.notNull(serializable, "Source object");
82          this.objRef = serializable;
83      }
84  
85      private void createBytes(final Serializable ser) throws IOException {
86          final ByteArrayOutputStream baos = new ByteArrayOutputStream();
87          final ObjectOutputStream out = new ObjectOutputStream(baos);
88          out.writeObject(ser);
89          out.flush();
90          this.objSer = baos.toByteArray();
91      }
92  
93      @Override
94      public InputStream getContent() throws IOException, IllegalStateException {
95          if (this.objSer == null) {
96              createBytes(this.objRef);
97          }
98          return new ByteArrayInputStream(this.objSer);
99      }
100 
101     @Override
102     public long getContentLength() {
103         if (this.objSer ==  null) {
104             return -1;
105         }
106         return this.objSer.length;
107     }
108 
109     @Override
110     public boolean isRepeatable() {
111         return true;
112     }
113 
114     @Override
115     public boolean isStreaming() {
116         return this.objSer == null;
117     }
118 
119     @Override
120     public void writeTo(final OutputStream outStream) throws IOException {
121         Args.notNull(outStream, "Output stream");
122         if (this.objSer == null) {
123             final ObjectOutputStream out = new ObjectOutputStream(outStream);
124             out.writeObject(this.objRef);
125             out.flush();
126         } else {
127             outStream.write(this.objSer);
128             outStream.flush();
129         }
130     }
131 
132     @Override
133     public void close() throws IOException {
134         // do nothing.
135     }
136 
137 }