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 public class SerializableEntity extends AbstractHttpEntity {
     43 
     44     private byte[] objSer;
     45 
     46     private Serializable objRef;
     47 
     48     public SerializableEntity(Serializable ser, boolean bufferize) throws IOException {
     49         super();
     50         if (ser == null) {
     51             throw new IllegalArgumentException("Source object may not be null");
     52         }
     53 
     54         if (bufferize) {
     55             createBytes(ser);
     56         } else {
     57             this.objRef = ser;
     58         }
     59     }
     60 
     61     private void createBytes(Serializable ser) throws IOException {
     62         ByteArrayOutputStream baos = new ByteArrayOutputStream();
     63         ObjectOutputStream out = new ObjectOutputStream(baos);
     64         out.writeObject(ser);
     65         out.flush();
     66         this.objSer = baos.toByteArray();
     67     }
     68 
     69     public InputStream getContent() throws IOException, IllegalStateException {
     70         if (this.objSer == null) {
     71             createBytes(this.objRef);
     72         }
     73         return new ByteArrayInputStream(this.objSer);
     74     }
     75 
     76     public long getContentLength() {
     77         if (this.objSer ==  null) {
     78             return -1;
     79         } else {
     80             return this.objSer.length;
     81         }
     82     }
     83 
     84     public boolean isRepeatable() {
     85         return true;
     86     }
     87 
     88     public boolean isStreaming() {
     89         return this.objSer == null;
     90     }
     91 
     92     public void writeTo(OutputStream outstream) throws IOException {
     93         if (outstream == null) {
     94             throw new IllegalArgumentException("Output stream may not be null");
     95         }
     96 
     97         if (this.objSer == null) {
     98             ObjectOutputStream out = new ObjectOutputStream(outstream);
     99             out.writeObject(this.objRef);
    100             out.flush();
    101         } else {
    102             outstream.write(this.objSer);
    103             outstream.flush();
    104         }
    105     }
    106 
    107 }
    108