001: /*
002: * $HeadURL: https://svn.apache.org/repos/asf/httpcomponents/httpcore/tags/4.0-beta1/module-main/src/main/java/org/apache/http/util/EntityUtils.java $
003: * $Revision: 569637 $
004: * $Date: 2007-08-25 09:36:59 +0200 (Sat, 25 Aug 2007) $
005: *
006: * ====================================================================
007: * Licensed to the Apache Software Foundation (ASF) under one
008: * or more contributor license agreements. See the NOTICE file
009: * distributed with this work for additional information
010: * regarding copyright ownership. The ASF licenses this file
011: * to you under the Apache License, Version 2.0 (the
012: * "License"); you may not use this file except in compliance
013: * with the License. You may obtain a copy of the License at
014: *
015: * http://www.apache.org/licenses/LICENSE-2.0
016: *
017: * Unless required by applicable law or agreed to in writing,
018: * software distributed under the License is distributed on an
019: * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
020: * KIND, either express or implied. See the License for the
021: * specific language governing permissions and limitations
022: * under the License.
023: * ====================================================================
024: *
025: * This software consists of voluntary contributions made by many
026: * individuals on behalf of the Apache Software Foundation. For more
027: * information on the Apache Software Foundation, please see
028: * <http://www.apache.org/>.
029: *
030: */
031:
032: package org.apache.http.util;
033:
034: import java.io.IOException;
035: import java.io.InputStream;
036: import java.io.InputStreamReader;
037: import java.io.Reader;
038:
039: import org.apache.http.HeaderElement;
040: import org.apache.http.HttpEntity;
041: import org.apache.http.NameValuePair;
042: import org.apache.http.ParseException;
043: import org.apache.http.protocol.HTTP;
044:
045: /**
046: * Static helpers for dealing with {@link HttpEntity entities}.
047: *
048: * @author <a href="mailto:oleg at ural.ru">Oleg Kalnichevski</a>
049: *
050: * @version $Revision: 569637 $
051: *
052: * @since 4.0
053: */
054: public final class EntityUtils {
055:
056: /** Disabled default constructor. */
057: private EntityUtils() {
058: }
059:
060: public static byte[] toByteArray(final HttpEntity entity)
061: throws IOException {
062: if (entity == null) {
063: throw new IllegalArgumentException(
064: "HTTP entity may not be null");
065: }
066: InputStream instream = entity.getContent();
067: if (instream == null) {
068: return new byte[] {};
069: }
070: if (entity.getContentLength() > Integer.MAX_VALUE) {
071: throw new IllegalArgumentException(
072: "HTTP entity too large to be buffered in memory");
073: }
074: int i = (int) entity.getContentLength();
075: if (i < 0) {
076: i = 4096;
077: }
078: ByteArrayBuffer buffer = new ByteArrayBuffer(i);
079: try {
080: byte[] tmp = new byte[4096];
081: int l;
082: while ((l = instream.read(tmp)) != -1) {
083: buffer.append(tmp, 0, l);
084: }
085: } finally {
086: instream.close();
087: }
088: return buffer.toByteArray();
089: }
090:
091: public static String getContentCharSet(final HttpEntity entity)
092: throws ParseException {
093:
094: if (entity == null) {
095: throw new IllegalArgumentException(
096: "HTTP entity may not be null");
097: }
098: String charset = null;
099: if (entity.getContentType() != null) {
100: HeaderElement values[] = entity.getContentType()
101: .getElements();
102: if (values.length > 0) {
103: NameValuePair param = values[0]
104: .getParameterByName("charset");
105: if (param != null) {
106: charset = param.getValue();
107: }
108: }
109: }
110: return charset;
111: }
112:
113: public static String toString(final HttpEntity entity,
114: final String defaultCharset) throws IOException,
115: ParseException {
116: if (entity == null) {
117: throw new IllegalArgumentException(
118: "HTTP entity may not be null");
119: }
120: InputStream instream = entity.getContent();
121: if (instream == null) {
122: return "";
123: }
124: if (entity.getContentLength() > Integer.MAX_VALUE) {
125: throw new IllegalArgumentException(
126: "HTTP entity too large to be buffered in memory");
127: }
128: int i = (int) entity.getContentLength();
129: if (i < 0) {
130: i = 4096;
131: }
132: String charset = getContentCharSet(entity);
133: if (charset == null) {
134: charset = defaultCharset;
135: }
136: if (charset == null) {
137: charset = HTTP.DEFAULT_CONTENT_CHARSET;
138: }
139: Reader reader = new InputStreamReader(instream, charset);
140: CharArrayBuffer buffer = new CharArrayBuffer(i);
141: try {
142: char[] tmp = new char[1024];
143: int l;
144: while ((l = reader.read(tmp)) != -1) {
145: buffer.append(tmp, 0, l);
146: }
147: } finally {
148: reader.close();
149: }
150: return buffer.toString();
151: }
152:
153: public static String toString(final HttpEntity entity)
154: throws IOException, ParseException {
155: return toString(entity, null);
156: }
157:
158: }
|