1069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project/*
2069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project * $HeadURL: http://svn.apache.org/repos/asf/httpcomponents/httpcore/trunk/module-main/src/main/java/org/apache/http/util/EntityUtils.java $
3069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project * $Revision: 569637 $
4069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project * $Date: 2007-08-25 00:36:59 -0700 (Sat, 25 Aug 2007) $
5069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project *
6069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project * ====================================================================
7069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project * Licensed to the Apache Software Foundation (ASF) under one
8069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project * or more contributor license agreements.  See the NOTICE file
9069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project * distributed with this work for additional information
10069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project * regarding copyright ownership.  The ASF licenses this file
11069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project * to you under the Apache License, Version 2.0 (the
12069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project * "License"); you may not use this file except in compliance
13069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project * with the License.  You may obtain a copy of the License at
14069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project *
15069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project *   http://www.apache.org/licenses/LICENSE-2.0
16069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project *
17069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project * Unless required by applicable law or agreed to in writing,
18069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project * software distributed under the License is distributed on an
19069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
20069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project * KIND, either express or implied.  See the License for the
21069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project * specific language governing permissions and limitations
22069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project * under the License.
23069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project * ====================================================================
24069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project *
25069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project * This software consists of voluntary contributions made by many
26069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project * individuals on behalf of the Apache Software Foundation.  For more
27069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project * information on the Apache Software Foundation, please see
28069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project * <http://www.apache.org/>.
29069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project *
30069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project */
31069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project
32069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Projectpackage org.apache.http.util;
33069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project
34069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Projectimport java.io.IOException;
35069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Projectimport java.io.InputStream;
36069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Projectimport java.io.InputStreamReader;
37069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Projectimport java.io.Reader;
38069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project
39069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Projectimport org.apache.http.HeaderElement;
40069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Projectimport org.apache.http.HttpEntity;
41069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Projectimport org.apache.http.NameValuePair;
42069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Projectimport org.apache.http.ParseException;
43069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Projectimport org.apache.http.protocol.HTTP;
44069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project
45069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project/**
46069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project * Static helpers for dealing with {@link HttpEntity entities}.
47069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project *
48069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project * @author <a href="mailto:oleg at ural.ru">Oleg Kalnichevski</a>
49069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project *
50069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project * @version $Revision: 569637 $
51069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project *
52069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project * @since 4.0
53069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project */
54069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Projectpublic final class EntityUtils {
55069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project
56069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project    /** Disabled default constructor. */
57069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project    private EntityUtils() {
58069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project    }
59069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project
60069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project    public static byte[] toByteArray(final HttpEntity entity) throws IOException {
61069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project        if (entity == null) {
62069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project            throw new IllegalArgumentException("HTTP entity may not be null");
63069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project        }
64069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project        InputStream instream = entity.getContent();
65069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project        if (instream == null) {
66069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project            return new byte[] {};
67069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project        }
68069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project        if (entity.getContentLength() > Integer.MAX_VALUE) {
69069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project            throw new IllegalArgumentException("HTTP entity too large to be buffered in memory");
70069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project        }
71069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project        int i = (int)entity.getContentLength();
72069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project        if (i < 0) {
73069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project            i = 4096;
74069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project        }
75069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project        ByteArrayBuffer buffer = new ByteArrayBuffer(i);
76069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project        try {
77069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project            byte[] tmp = new byte[4096];
78069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project            int l;
79069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project            while((l = instream.read(tmp)) != -1) {
80069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project                buffer.append(tmp, 0, l);
81069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project            }
82069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project        } finally {
83069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project            instream.close();
84069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project        }
85069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project        return buffer.toByteArray();
86069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project    }
87069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project
88069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project    public static String getContentCharSet(final HttpEntity entity)
89069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project        throws ParseException {
90069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project
91069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project        if (entity == null) {
92069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project            throw new IllegalArgumentException("HTTP entity may not be null");
93069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project        }
94069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project        String charset = null;
95069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project        if (entity.getContentType() != null) {
96069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project            HeaderElement values[] = entity.getContentType().getElements();
97069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project            if (values.length > 0) {
98069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project                NameValuePair param = values[0].getParameterByName("charset");
99069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project                if (param != null) {
100069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project                    charset = param.getValue();
101069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project                }
102069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project            }
103069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project        }
104069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project        return charset;
105069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project    }
106069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project
107069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project    public static String toString(
108069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project            final HttpEntity entity, final String defaultCharset) throws IOException, ParseException {
109069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project        if (entity == null) {
110069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project            throw new IllegalArgumentException("HTTP entity may not be null");
111069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project        }
112069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project        InputStream instream = entity.getContent();
113069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project        if (instream == null) {
114069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project            return "";
115069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project        }
116069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project        if (entity.getContentLength() > Integer.MAX_VALUE) {
117069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project            throw new IllegalArgumentException("HTTP entity too large to be buffered in memory");
118069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project        }
119069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project        int i = (int)entity.getContentLength();
120069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project        if (i < 0) {
121069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project            i = 4096;
122069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project        }
123069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project        String charset = getContentCharSet(entity);
124069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project        if (charset == null) {
125069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project            charset = defaultCharset;
126069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project        }
127069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project        if (charset == null) {
128069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project            charset = HTTP.DEFAULT_CONTENT_CHARSET;
129069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project        }
130069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project        Reader reader = new InputStreamReader(instream, charset);
131069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project        CharArrayBuffer buffer = new CharArrayBuffer(i);
132069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project        try {
133069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project            char[] tmp = new char[1024];
134069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project            int l;
135069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project            while((l = reader.read(tmp)) != -1) {
136069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project                buffer.append(tmp, 0, l);
137069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project            }
138069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project        } finally {
139069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project            reader.close();
140069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project        }
141069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project        return buffer.toString();
142069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project    }
143069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project
144069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project    public static String toString(final HttpEntity entity)
145069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project        throws IOException, ParseException {
146069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project        return toString(entity, null);
147069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project    }
148069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project
149069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project}
150