1/*
2 * $HeadURL: http://svn.apache.org/repos/asf/httpcomponents/httpcore/trunk/module-main/src/main/java/org/apache/http/impl/entity/EntitySerializer.java $
3 * $Revision: 560343 $
4 * $Date: 2007-07-27 11:18:19 -0700 (Fri, 27 Jul 2007) $
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
32package org.apache.http.impl.entity;
33
34import java.io.IOException;
35import java.io.OutputStream;
36
37import org.apache.http.HttpEntity;
38import org.apache.http.HttpException;
39import org.apache.http.HttpMessage;
40import org.apache.http.entity.ContentLengthStrategy;
41import org.apache.http.impl.io.ChunkedOutputStream;
42import org.apache.http.impl.io.ContentLengthOutputStream;
43import org.apache.http.impl.io.IdentityOutputStream;
44import org.apache.http.io.SessionOutputBuffer;
45
46/**
47 * Default implementation of an entity serializer.
48 * <p>
49 * This entity serializer currently supports only "chunked" and "identitiy" transfer-coding</a>
50 * </p>
51 *
52 * @author <a href="mailto:oleg at ural.ru">Oleg Kalnichevski</a>
53 *
54 * @version $Revision: 560343 $
55 *
56 * @since 4.0
57 *
58 * @deprecated Please use {@link java.net.URL#openConnection} instead.
59 *     Please visit <a href="http://android-developers.blogspot.com/2011/09/androids-http-clients.html">this webpage</a>
60 *     for further details.
61 */
62@Deprecated
63public class EntitySerializer {
64
65    private final ContentLengthStrategy lenStrategy;
66
67    public EntitySerializer(final ContentLengthStrategy lenStrategy) {
68        super();
69        if (lenStrategy == null) {
70            throw new IllegalArgumentException("Content length strategy may not be null");
71        }
72        this.lenStrategy = lenStrategy;
73    }
74
75    protected OutputStream doSerialize(
76            final SessionOutputBuffer outbuffer,
77            final HttpMessage message) throws HttpException, IOException {
78        long len = this.lenStrategy.determineLength(message);
79        if (len == ContentLengthStrategy.CHUNKED) {
80            return new ChunkedOutputStream(outbuffer);
81        } else if (len == ContentLengthStrategy.IDENTITY) {
82            return new IdentityOutputStream(outbuffer);
83        } else {
84            return new ContentLengthOutputStream(outbuffer, len);
85        }
86    }
87
88    public void serialize(
89            final SessionOutputBuffer outbuffer,
90            final HttpMessage message,
91            final HttpEntity entity) throws HttpException, IOException {
92        if (outbuffer == null) {
93            throw new IllegalArgumentException("Session output buffer may not be null");
94        }
95        if (message == null) {
96            throw new IllegalArgumentException("HTTP message may not be null");
97        }
98        if (entity == null) {
99            throw new IllegalArgumentException("HTTP entity may not be null");
100        }
101        OutputStream outstream = doSerialize(outbuffer, message);
102        entity.writeTo(outstream);
103        outstream.close();
104    }
105
106}
107