001/*
002 * ====================================================================
003 * Licensed to the Apache Software Foundation (ASF) under one
004 * or more contributor license agreements.  See the NOTICE file
005 * distributed with this work for additional information
006 * regarding copyright ownership.  The ASF licenses this file
007 * to you under the Apache License, Version 2.0 (the
008 * "License"); you may not use this file except in compliance
009 * with the License.  You may obtain a copy of the License at
010 *
011 *   http://www.apache.org/licenses/LICENSE-2.0
012 *
013 * Unless required by applicable law or agreed to in writing,
014 * software distributed under the License is distributed on an
015 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
016 * KIND, either express or implied.  See the License for the
017 * specific language governing permissions and limitations
018 * under the License.
019 * ====================================================================
020 *
021 * This software consists of voluntary contributions made by many
022 * individuals on behalf of the Apache Software Foundation.  For more
023 * information on the Apache Software Foundation, please see
024 * <http://www.apache.org/>.
025 *
026 */
027
028package org.apache.http.entity;
029
030import java.io.ByteArrayInputStream;
031import java.io.ByteArrayOutputStream;
032import java.io.IOException;
033import java.io.InputStream;
034import java.io.ObjectOutputStream;
035import java.io.OutputStream;
036import java.io.Serializable;
037
038import org.apache.http.util.Args;
039
040/**
041 * A streamed entity that obtains its content from a {@link Serializable}.
042 * The content obtained from the {@link Serializable} instance can
043 * optionally be buffered in a byte array in order to make the
044 * entity self-contained and repeatable.
045 *
046 * @since 4.0
047 */
048public class SerializableEntity extends AbstractHttpEntity {
049
050    private byte[] objSer;
051
052    private Serializable objRef;
053
054    /**
055     * Creates new instance of this class.
056     *
057     * @param ser input
058     * @param bufferize tells whether the content should be
059     *        stored in an internal buffer
060     * @throws IOException in case of an I/O error
061     */
062    public SerializableEntity(final Serializable ser, final boolean bufferize) throws IOException {
063        super();
064        Args.notNull(ser, "Source object");
065        if (bufferize) {
066            createBytes(ser);
067        } else {
068            this.objRef = ser;
069        }
070    }
071
072    /**
073     * @since 4.3
074     */
075    public SerializableEntity(final Serializable ser) {
076        super();
077        Args.notNull(ser, "Source object");
078        this.objRef = ser;
079    }
080
081    private void createBytes(final Serializable ser) throws IOException {
082        final ByteArrayOutputStream baos = new ByteArrayOutputStream();
083        final ObjectOutputStream out = new ObjectOutputStream(baos);
084        out.writeObject(ser);
085        out.flush();
086        this.objSer = baos.toByteArray();
087    }
088
089    @Override
090    public InputStream getContent() throws IOException, IllegalStateException {
091        if (this.objSer == null) {
092            createBytes(this.objRef);
093        }
094        return new ByteArrayInputStream(this.objSer);
095    }
096
097    @Override
098    public long getContentLength() {
099        if (this.objSer ==  null) {
100            return -1;
101        } else {
102            return this.objSer.length;
103        }
104    }
105
106    @Override
107    public boolean isRepeatable() {
108        return true;
109    }
110
111    @Override
112    public boolean isStreaming() {
113        return this.objSer == null;
114    }
115
116    @Override
117    public void writeTo(final OutputStream outstream) throws IOException {
118        Args.notNull(outstream, "Output stream");
119        if (this.objSer == null) {
120            final ObjectOutputStream out = new ObjectOutputStream(outstream);
121            out.writeObject(this.objRef);
122            out.flush();
123        } else {
124            outstream.write(this.objSer);
125            outstream.flush();
126        }
127    }
128
129}