Home | History | Annotate | Download | only in entity
      1 /*
      2  * $HeadURL: http://svn.apache.org/repos/asf/httpcomponents/httpcore/trunk/module-main/src/main/java/org/apache/http/entity/SerializableEntity.java $
      3  * $Revision: 647816 $
      4  * $Date: 2008-04-14 07:37:13 -0700 (Mon, 14 Apr 2008) $
      5  *
      6  * ====================================================================
      7  * Licensed to the Apache Software Foundation (ASF) under one
      8  * or more contributor license agreements.  See the NOTICE file
      9  * distributed with this work for additional information
     10  * regarding copyright ownership.  The ASF licenses this file
     11  * to you under the Apache License, Version 2.0 (the
     12  * "License"); you may not use this file except in compliance
     13  * with the License.  You may obtain a copy of the License at
     14  *
     15  *   http://www.apache.org/licenses/LICENSE-2.0
     16  *
     17  * Unless required by applicable law or agreed to in writing,
     18  * software distributed under the License is distributed on an
     19  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
     20  * KIND, either express or implied.  See the License for the
     21  * specific language governing permissions and limitations
     22  * under the License.
     23  * ====================================================================
     24  *
     25  * This software consists of voluntary contributions made by many
     26  * individuals on behalf of the Apache Software Foundation.  For more
     27  * information on the Apache Software Foundation, please see
     28  * <http://www.apache.org/>.
     29  *
     30  */
     31 
     32 package org.apache.http.entity;
     33 
     34 import java.io.ByteArrayInputStream;
     35 import java.io.ByteArrayOutputStream;
     36 import java.io.IOException;
     37 import java.io.InputStream;
     38 import java.io.ObjectOutputStream;
     39 import java.io.OutputStream;
     40 import java.io.Serializable;
     41 /**
     42  * @deprecated Please use {@link java.net.URL#openConnection} instead.
     43  *     Please visit <a href="http://android-developers.blogspot.com/2011/09/androids-http-clients.html">this webpage</a>
     44  *     for further details.
     45 */
     46 
     47 @Deprecated
     48 public class SerializableEntity extends AbstractHttpEntity {
     49 
     50     private byte[] objSer;
     51 
     52     private Serializable objRef;
     53 
     54     public SerializableEntity(Serializable ser, boolean bufferize) throws IOException {
     55         super();
     56         if (ser == null) {
     57             throw new IllegalArgumentException("Source object may not be null");
     58         }
     59 
     60         if (bufferize) {
     61             createBytes(ser);
     62         } else {
     63             this.objRef = ser;
     64         }
     65     }
     66 
     67     private void createBytes(Serializable ser) throws IOException {
     68         ByteArrayOutputStream baos = new ByteArrayOutputStream();
     69         ObjectOutputStream out = new ObjectOutputStream(baos);
     70         out.writeObject(ser);
     71         out.flush();
     72         this.objSer = baos.toByteArray();
     73     }
     74 
     75     public InputStream getContent() throws IOException, IllegalStateException {
     76         if (this.objSer == null) {
     77             createBytes(this.objRef);
     78         }
     79         return new ByteArrayInputStream(this.objSer);
     80     }
     81 
     82     public long getContentLength() {
     83         if (this.objSer ==  null) {
     84             return -1;
     85         } else {
     86             return this.objSer.length;
     87         }
     88     }
     89 
     90     public boolean isRepeatable() {
     91         return true;
     92     }
     93 
     94     public boolean isStreaming() {
     95         return this.objSer == null;
     96     }
     97 
     98     public void writeTo(OutputStream outstream) throws IOException {
     99         if (outstream == null) {
    100             throw new IllegalArgumentException("Output stream may not be null");
    101         }
    102 
    103         if (this.objSer == null) {
    104             ObjectOutputStream out = new ObjectOutputStream(outstream);
    105             out.writeObject(this.objRef);
    106             out.flush();
    107         } else {
    108             outstream.write(this.objSer);
    109             outstream.flush();
    110         }
    111     }
    112 
    113 }
    114