mobile/android/thirdparty/ch/boye/httpclientandroidlib/entity/SerializableEntity.java

changeset 0
6474c204b198
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/mobile/android/thirdparty/ch/boye/httpclientandroidlib/entity/SerializableEntity.java	Wed Dec 31 06:09:35 2014 +0100
     1.3 @@ -0,0 +1,119 @@
     1.4 +/*
     1.5 + * ====================================================================
     1.6 + * Licensed to the Apache Software Foundation (ASF) under one
     1.7 + * or more contributor license agreements.  See the NOTICE file
     1.8 + * distributed with this work for additional information
     1.9 + * regarding copyright ownership.  The ASF licenses this file
    1.10 + * to you under the Apache License, Version 2.0 (the
    1.11 + * "License"); you may not use this file except in compliance
    1.12 + * with the License.  You may obtain a copy of the License at
    1.13 + *
    1.14 + *   http://www.apache.org/licenses/LICENSE-2.0
    1.15 + *
    1.16 + * Unless required by applicable law or agreed to in writing,
    1.17 + * software distributed under the License is distributed on an
    1.18 + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
    1.19 + * KIND, either express or implied.  See the License for the
    1.20 + * specific language governing permissions and limitations
    1.21 + * under the License.
    1.22 + * ====================================================================
    1.23 + *
    1.24 + * This software consists of voluntary contributions made by many
    1.25 + * individuals on behalf of the Apache Software Foundation.  For more
    1.26 + * information on the Apache Software Foundation, please see
    1.27 + * <http://www.apache.org/>.
    1.28 + *
    1.29 + */
    1.30 +
    1.31 +package ch.boye.httpclientandroidlib.entity;
    1.32 +
    1.33 +import java.io.ByteArrayInputStream;
    1.34 +import java.io.ByteArrayOutputStream;
    1.35 +import java.io.IOException;
    1.36 +import java.io.InputStream;
    1.37 +import java.io.ObjectOutputStream;
    1.38 +import java.io.OutputStream;
    1.39 +import java.io.Serializable;
    1.40 +
    1.41 +/**
    1.42 + * A streamed entity that obtains its content from a {@link Serializable}.
    1.43 + * The content obtained from the {@link Serializable} instance can
    1.44 + * optionally be buffered in a byte array in order to make the
    1.45 + * entity self-contained and repeatable.
    1.46 + *
    1.47 + * @since 4.0
    1.48 + */
    1.49 +public class SerializableEntity extends AbstractHttpEntity {
    1.50 +
    1.51 +    private byte[] objSer;
    1.52 +
    1.53 +    private Serializable objRef;
    1.54 +
    1.55 +    /**
    1.56 +     * Creates new instance of this class.
    1.57 +     *
    1.58 +     * @param ser input
    1.59 +     * @param bufferize tells whether the content should be
    1.60 +     *        stored in an internal buffer
    1.61 +     * @throws IOException in case of an I/O error
    1.62 +     */
    1.63 +    public SerializableEntity(Serializable ser, boolean bufferize) throws IOException {
    1.64 +        super();
    1.65 +        if (ser == null) {
    1.66 +            throw new IllegalArgumentException("Source object may not be null");
    1.67 +        }
    1.68 +
    1.69 +        if (bufferize) {
    1.70 +            createBytes(ser);
    1.71 +        } else {
    1.72 +            this.objRef = ser;
    1.73 +        }
    1.74 +    }
    1.75 +
    1.76 +    private void createBytes(Serializable ser) throws IOException {
    1.77 +        ByteArrayOutputStream baos = new ByteArrayOutputStream();
    1.78 +        ObjectOutputStream out = new ObjectOutputStream(baos);
    1.79 +        out.writeObject(ser);
    1.80 +        out.flush();
    1.81 +        this.objSer = baos.toByteArray();
    1.82 +    }
    1.83 +
    1.84 +    public InputStream getContent() throws IOException, IllegalStateException {
    1.85 +        if (this.objSer == null) {
    1.86 +            createBytes(this.objRef);
    1.87 +        }
    1.88 +        return new ByteArrayInputStream(this.objSer);
    1.89 +    }
    1.90 +
    1.91 +    public long getContentLength() {
    1.92 +        if (this.objSer ==  null) {
    1.93 +            return -1;
    1.94 +        } else {
    1.95 +            return this.objSer.length;
    1.96 +        }
    1.97 +    }
    1.98 +
    1.99 +    public boolean isRepeatable() {
   1.100 +        return true;
   1.101 +    }
   1.102 +
   1.103 +    public boolean isStreaming() {
   1.104 +        return this.objSer == null;
   1.105 +    }
   1.106 +
   1.107 +    public void writeTo(OutputStream outstream) throws IOException {
   1.108 +        if (outstream == null) {
   1.109 +            throw new IllegalArgumentException("Output stream may not be null");
   1.110 +        }
   1.111 +
   1.112 +        if (this.objSer == null) {
   1.113 +            ObjectOutputStream out = new ObjectOutputStream(outstream);
   1.114 +            out.writeObject(this.objRef);
   1.115 +            out.flush();
   1.116 +        } else {
   1.117 +            outstream.write(this.objSer);
   1.118 +            outstream.flush();
   1.119 +        }
   1.120 +    }
   1.121 +
   1.122 +}

mercurial