1/*
2 * $HeadURL: http://svn.apache.org/repos/asf/httpcomponents/httpcore/trunk/module-main/src/main/java/org/apache/http/util/EntityUtils.java $
3 * $Revision: 569637 $
4 * $Date: 2007-08-25 00:36:59 -0700 (Sat, 25 Aug 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.util;
33
34import java.io.IOException;
35import java.io.InputStream;
36import java.io.InputStreamReader;
37import java.io.Reader;
38
39import org.apache.http.HeaderElement;
40import org.apache.http.HttpEntity;
41import org.apache.http.NameValuePair;
42import org.apache.http.ParseException;
43import org.apache.http.protocol.HTTP;
44
45/**
46 * Static helpers for dealing with {@link HttpEntity entities}.
47 *
48 * @author <a href="mailto:oleg at ural.ru">Oleg Kalnichevski</a>
49 *
50 * @version $Revision: 569637 $
51 *
52 * @since 4.0
53 */
54public final class EntityUtils {
55
56    /** Disabled default constructor. */
57    private EntityUtils() {
58    }
59
60    public static byte[] toByteArray(final HttpEntity entity) throws IOException {
61        if (entity == null) {
62            throw new IllegalArgumentException("HTTP entity may not be null");
63        }
64        InputStream instream = entity.getContent();
65        if (instream == null) {
66            return new byte[] {};
67        }
68        if (entity.getContentLength() > Integer.MAX_VALUE) {
69            throw new IllegalArgumentException("HTTP entity too large to be buffered in memory");
70        }
71        int i = (int)entity.getContentLength();
72        if (i < 0) {
73            i = 4096;
74        }
75        ByteArrayBuffer buffer = new ByteArrayBuffer(i);
76        try {
77            byte[] tmp = new byte[4096];
78            int l;
79            while((l = instream.read(tmp)) != -1) {
80                buffer.append(tmp, 0, l);
81            }
82        } finally {
83            instream.close();
84        }
85        return buffer.toByteArray();
86    }
87
88    public static String getContentCharSet(final HttpEntity entity)
89        throws ParseException {
90
91        if (entity == null) {
92            throw new IllegalArgumentException("HTTP entity may not be null");
93        }
94        String charset = null;
95        if (entity.getContentType() != null) {
96            HeaderElement values[] = entity.getContentType().getElements();
97            if (values.length > 0) {
98                NameValuePair param = values[0].getParameterByName("charset");
99                if (param != null) {
100                    charset = param.getValue();
101                }
102            }
103        }
104        return charset;
105    }
106
107    public static String toString(
108            final HttpEntity entity, final String defaultCharset) throws IOException, ParseException {
109        if (entity == null) {
110            throw new IllegalArgumentException("HTTP entity may not be null");
111        }
112        InputStream instream = entity.getContent();
113        if (instream == null) {
114            return "";
115        }
116        if (entity.getContentLength() > Integer.MAX_VALUE) {
117            throw new IllegalArgumentException("HTTP entity too large to be buffered in memory");
118        }
119        int i = (int)entity.getContentLength();
120        if (i < 0) {
121            i = 4096;
122        }
123        String charset = getContentCharSet(entity);
124        if (charset == null) {
125            charset = defaultCharset;
126        }
127        if (charset == null) {
128            charset = HTTP.DEFAULT_CONTENT_CHARSET;
129        }
130        Reader reader = new InputStreamReader(instream, charset);
131        CharArrayBuffer buffer = new CharArrayBuffer(i);
132        try {
133            char[] tmp = new char[1024];
134            int l;
135            while((l = reader.read(tmp)) != -1) {
136                buffer.append(tmp, 0, l);
137            }
138        } finally {
139            reader.close();
140        }
141        return buffer.toString();
142    }
143
144    public static String toString(final HttpEntity entity)
145        throws IOException, ParseException {
146        return toString(entity, null);
147    }
148
149}
150