Wed, 31 Dec 2014 07:22:50 +0100
Correct previous dual key logic pending first delivery installment.
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 */
28 package ch.boye.httpclientandroidlib.entity;
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;
38 /**
39 * A streamed entity that obtains its content from a {@link Serializable}.
40 * The content obtained from the {@link Serializable} instance can
41 * optionally be buffered in a byte array in order to make the
42 * entity self-contained and repeatable.
43 *
44 * @since 4.0
45 */
46 public class SerializableEntity extends AbstractHttpEntity {
48 private byte[] objSer;
50 private Serializable objRef;
52 /**
53 * Creates new instance of this class.
54 *
55 * @param ser input
56 * @param bufferize tells whether the content should be
57 * stored in an internal buffer
58 * @throws IOException in case of an I/O error
59 */
60 public SerializableEntity(Serializable ser, boolean bufferize) throws IOException {
61 super();
62 if (ser == null) {
63 throw new IllegalArgumentException("Source object may not be null");
64 }
66 if (bufferize) {
67 createBytes(ser);
68 } else {
69 this.objRef = ser;
70 }
71 }
73 private void createBytes(Serializable ser) throws IOException {
74 ByteArrayOutputStream baos = new ByteArrayOutputStream();
75 ObjectOutputStream out = new ObjectOutputStream(baos);
76 out.writeObject(ser);
77 out.flush();
78 this.objSer = baos.toByteArray();
79 }
81 public InputStream getContent() throws IOException, IllegalStateException {
82 if (this.objSer == null) {
83 createBytes(this.objRef);
84 }
85 return new ByteArrayInputStream(this.objSer);
86 }
88 public long getContentLength() {
89 if (this.objSer == null) {
90 return -1;
91 } else {
92 return this.objSer.length;
93 }
94 }
96 public boolean isRepeatable() {
97 return true;
98 }
100 public boolean isStreaming() {
101 return this.objSer == null;
102 }
104 public void writeTo(OutputStream outstream) throws IOException {
105 if (outstream == null) {
106 throw new IllegalArgumentException("Output stream may not be null");
107 }
109 if (this.objSer == null) {
110 ObjectOutputStream out = new ObjectOutputStream(outstream);
111 out.writeObject(this.objRef);
112 out.flush();
113 } else {
114 outstream.write(this.objSer);
115 outstream.flush();
116 }
117 }
119 }