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.annotation.Contract;
39  import org.apache.hc.core5.annotation.ThreadingBehavior;
40  import org.apache.hc.core5.http.ContentType;
41  import org.apache.hc.core5.util.Args;
42  
43  /**
44   * A streamed entity that obtains its content from a {@link Serializable}.
45   *
46   * @since 4.0
47   */
48  @Contract(threading = ThreadingBehavior.IMMUTABLE_CONDITIONAL)
49  public class SerializableEntity extends AbstractHttpEntity {
50  
51      private final Serializable serializable;
52  
53      /**
54       * Creates new instance of this class.
55       *
56       * @param serializable the serializable object.
57       * @param contentType the content type.
58       * @param contentEncoding the content encoding.
59       */
60      public SerializableEntity(
61              final Serializable serializable, final ContentType contentType, final String contentEncoding) {
62          super(contentType, contentEncoding);
63          this.serializable = Args.notNull(serializable, "Source object");
64      }
65  
66      /**
67       * Creates new instance of this class.
68       *
69       * @param serializable the serializable object.
70       * @param contentType the content type.
71       */
72      public SerializableEntity(final Serializable serializable, final ContentType contentType) {
73          this(serializable, contentType, null);
74      }
75  
76      @Override
77      public final InputStream getContent() throws IOException, IllegalStateException {
78          final ByteArrayOutputStream buf = new ByteArrayOutputStream();
79          writeTo(buf);
80          return new ByteArrayInputStream(buf.toByteArray());
81      }
82  
83      @Override
84      public final long getContentLength() {
85          return -1;
86      }
87  
88      @Override
89      public final boolean isRepeatable() {
90          return true;
91      }
92  
93      @Override
94      public final boolean isStreaming() {
95          return false;
96      }
97  
98      @Override
99      public final void writeTo(final OutputStream outStream) throws IOException {
100         Args.notNull(outStream, "Output stream");
101         final ObjectOutputStream out = new ObjectOutputStream(outStream);
102         out.writeObject(this.serializable);
103         out.flush();
104     }
105 
106     @Override
107     public final void close() throws IOException {
108     }
109 
110 }