michael@0: /* michael@0: * ==================================================================== michael@0: * Licensed to the Apache Software Foundation (ASF) under one michael@0: * or more contributor license agreements. See the NOTICE file michael@0: * distributed with this work for additional information michael@0: * regarding copyright ownership. The ASF licenses this file michael@0: * to you under the Apache License, Version 2.0 (the michael@0: * "License"); you may not use this file except in compliance michael@0: * with the License. You may obtain a copy of the License at michael@0: * michael@0: * http://www.apache.org/licenses/LICENSE-2.0 michael@0: * michael@0: * Unless required by applicable law or agreed to in writing, michael@0: * software distributed under the License is distributed on an michael@0: * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY michael@0: * KIND, either express or implied. See the License for the michael@0: * specific language governing permissions and limitations michael@0: * under the License. michael@0: * ==================================================================== michael@0: * michael@0: * This software consists of voluntary contributions made by many michael@0: * individuals on behalf of the Apache Software Foundation. For more michael@0: * information on the Apache Software Foundation, please see michael@0: * . michael@0: * michael@0: */ michael@0: michael@0: package ch.boye.httpclientandroidlib.util; michael@0: michael@0: import java.io.IOException; michael@0: import java.io.InputStream; michael@0: import java.io.InputStreamReader; michael@0: import java.io.Reader; michael@0: michael@0: import ch.boye.httpclientandroidlib.HeaderElement; michael@0: import ch.boye.httpclientandroidlib.HttpEntity; michael@0: import ch.boye.httpclientandroidlib.NameValuePair; michael@0: import ch.boye.httpclientandroidlib.ParseException; michael@0: import ch.boye.httpclientandroidlib.protocol.HTTP; michael@0: michael@0: /** michael@0: * Static helpers for dealing with {@link HttpEntity}s. michael@0: * michael@0: * @since 4.0 michael@0: */ michael@0: public final class EntityUtils { michael@0: michael@0: private EntityUtils() { michael@0: } michael@0: michael@0: /** michael@0: * Ensures that the entity content is fully consumed and the content stream, if exists, michael@0: * is closed. michael@0: * michael@0: * @param entity michael@0: * @throws IOException if an error occurs reading the input stream michael@0: * michael@0: * @since 4.1 michael@0: */ michael@0: public static void consume(final HttpEntity entity) throws IOException { michael@0: if (entity == null) { michael@0: return; michael@0: } michael@0: if (entity.isStreaming()) { michael@0: InputStream instream = entity.getContent(); michael@0: if (instream != null) { michael@0: instream.close(); michael@0: } michael@0: } michael@0: } michael@0: michael@0: /** michael@0: * Read the contents of an entity and return it as a byte array. michael@0: * michael@0: * @param entity michael@0: * @return byte array containing the entity content. May be null if michael@0: * {@link HttpEntity#getContent()} is null. michael@0: * @throws IOException if an error occurs reading the input stream michael@0: * @throws IllegalArgumentException if entity is null or if content length > Integer.MAX_VALUE michael@0: */ michael@0: public static byte[] toByteArray(final HttpEntity entity) throws IOException { michael@0: if (entity == null) { michael@0: throw new IllegalArgumentException("HTTP entity may not be null"); michael@0: } michael@0: InputStream instream = entity.getContent(); michael@0: if (instream == null) { michael@0: return null; michael@0: } michael@0: try { michael@0: if (entity.getContentLength() > Integer.MAX_VALUE) { michael@0: throw new IllegalArgumentException("HTTP entity too large to be buffered in memory"); michael@0: } michael@0: int i = (int)entity.getContentLength(); michael@0: if (i < 0) { michael@0: i = 4096; michael@0: } michael@0: ByteArrayBuffer buffer = new ByteArrayBuffer(i); michael@0: byte[] tmp = new byte[4096]; michael@0: int l; michael@0: while((l = instream.read(tmp)) != -1) { michael@0: buffer.append(tmp, 0, l); michael@0: } michael@0: return buffer.toByteArray(); michael@0: } finally { michael@0: instream.close(); michael@0: } michael@0: } michael@0: michael@0: /** michael@0: * Obtains character set of the entity, if known. michael@0: * michael@0: * @param entity must not be null michael@0: * @return the character set, or null if not found michael@0: * @throws ParseException if header elements cannot be parsed michael@0: * @throws IllegalArgumentException if entity is null michael@0: */ michael@0: public static String getContentCharSet(final HttpEntity entity) throws ParseException { michael@0: if (entity == null) { michael@0: throw new IllegalArgumentException("HTTP entity may not be null"); michael@0: } michael@0: String charset = null; michael@0: if (entity.getContentType() != null) { michael@0: HeaderElement values[] = entity.getContentType().getElements(); michael@0: if (values.length > 0) { michael@0: NameValuePair param = values[0].getParameterByName("charset"); michael@0: if (param != null) { michael@0: charset = param.getValue(); michael@0: } michael@0: } michael@0: } michael@0: return charset; michael@0: } michael@0: michael@0: /** michael@0: * Obtains mime type of the entity, if known. michael@0: * michael@0: * @param entity must not be null michael@0: * @return the character set, or null if not found michael@0: * @throws ParseException if header elements cannot be parsed michael@0: * @throws IllegalArgumentException if entity is null michael@0: * michael@0: * @since 4.1 michael@0: */ michael@0: public static String getContentMimeType(final HttpEntity entity) throws ParseException { michael@0: if (entity == null) { michael@0: throw new IllegalArgumentException("HTTP entity may not be null"); michael@0: } michael@0: String mimeType = null; michael@0: if (entity.getContentType() != null) { michael@0: HeaderElement values[] = entity.getContentType().getElements(); michael@0: if (values.length > 0) { michael@0: mimeType = values[0].getName(); michael@0: } michael@0: } michael@0: return mimeType; michael@0: } michael@0: michael@0: /** michael@0: * Get the entity content as a String, using the provided default character set michael@0: * if none is found in the entity. michael@0: * If defaultCharset is null, the default "ISO-8859-1" is used. michael@0: * michael@0: * @param entity must not be null michael@0: * @param defaultCharset character set to be applied if none found in the entity michael@0: * @return the entity content as a String. May be null if michael@0: * {@link HttpEntity#getContent()} is null. michael@0: * @throws ParseException if header elements cannot be parsed michael@0: * @throws IllegalArgumentException if entity is null or if content length > Integer.MAX_VALUE michael@0: * @throws IOException if an error occurs reading the input stream michael@0: */ michael@0: public static String toString( michael@0: final HttpEntity entity, final String defaultCharset) throws IOException, ParseException { michael@0: if (entity == null) { michael@0: throw new IllegalArgumentException("HTTP entity may not be null"); michael@0: } michael@0: InputStream instream = entity.getContent(); michael@0: if (instream == null) { michael@0: return null; michael@0: } michael@0: try { michael@0: if (entity.getContentLength() > Integer.MAX_VALUE) { michael@0: throw new IllegalArgumentException("HTTP entity too large to be buffered in memory"); michael@0: } michael@0: int i = (int)entity.getContentLength(); michael@0: if (i < 0) { michael@0: i = 4096; michael@0: } michael@0: String charset = getContentCharSet(entity); michael@0: if (charset == null) { michael@0: charset = defaultCharset; michael@0: } michael@0: if (charset == null) { michael@0: charset = HTTP.DEFAULT_CONTENT_CHARSET; michael@0: } michael@0: Reader reader = new InputStreamReader(instream, charset); michael@0: CharArrayBuffer buffer = new CharArrayBuffer(i); michael@0: char[] tmp = new char[1024]; michael@0: int l; michael@0: while((l = reader.read(tmp)) != -1) { michael@0: buffer.append(tmp, 0, l); michael@0: } michael@0: return buffer.toString(); michael@0: } finally { michael@0: instream.close(); michael@0: } michael@0: } michael@0: michael@0: /** michael@0: * Read the contents of an entity and return it as a String. michael@0: * The content is converted using the character set from the entity (if any), michael@0: * failing that, "ISO-8859-1" is used. michael@0: * michael@0: * @param entity michael@0: * @return String containing the content. michael@0: * @throws ParseException if header elements cannot be parsed michael@0: * @throws IllegalArgumentException if entity is null or if content length > Integer.MAX_VALUE michael@0: * @throws IOException if an error occurs reading the input stream michael@0: */ michael@0: public static String toString(final HttpEntity entity) michael@0: throws IOException, ParseException { michael@0: return toString(entity, null); michael@0: } michael@0: michael@0: }