1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/mobile/android/thirdparty/ch/boye/httpclientandroidlib/util/EntityUtils.java Wed Dec 31 06:09:35 2014 +0100 1.3 @@ -0,0 +1,224 @@ 1.4 +/* 1.5 + * ==================================================================== 1.6 + * Licensed to the Apache Software Foundation (ASF) under one 1.7 + * or more contributor license agreements. See the NOTICE file 1.8 + * distributed with this work for additional information 1.9 + * regarding copyright ownership. The ASF licenses this file 1.10 + * to you under the Apache License, Version 2.0 (the 1.11 + * "License"); you may not use this file except in compliance 1.12 + * with the License. You may obtain a copy of the License at 1.13 + * 1.14 + * http://www.apache.org/licenses/LICENSE-2.0 1.15 + * 1.16 + * Unless required by applicable law or agreed to in writing, 1.17 + * software distributed under the License is distributed on an 1.18 + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY 1.19 + * KIND, either express or implied. See the License for the 1.20 + * specific language governing permissions and limitations 1.21 + * under the License. 1.22 + * ==================================================================== 1.23 + * 1.24 + * This software consists of voluntary contributions made by many 1.25 + * individuals on behalf of the Apache Software Foundation. For more 1.26 + * information on the Apache Software Foundation, please see 1.27 + * <http://www.apache.org/>. 1.28 + * 1.29 + */ 1.30 + 1.31 +package ch.boye.httpclientandroidlib.util; 1.32 + 1.33 +import java.io.IOException; 1.34 +import java.io.InputStream; 1.35 +import java.io.InputStreamReader; 1.36 +import java.io.Reader; 1.37 + 1.38 +import ch.boye.httpclientandroidlib.HeaderElement; 1.39 +import ch.boye.httpclientandroidlib.HttpEntity; 1.40 +import ch.boye.httpclientandroidlib.NameValuePair; 1.41 +import ch.boye.httpclientandroidlib.ParseException; 1.42 +import ch.boye.httpclientandroidlib.protocol.HTTP; 1.43 + 1.44 +/** 1.45 + * Static helpers for dealing with {@link HttpEntity}s. 1.46 + * 1.47 + * @since 4.0 1.48 + */ 1.49 +public final class EntityUtils { 1.50 + 1.51 + private EntityUtils() { 1.52 + } 1.53 + 1.54 + /** 1.55 + * Ensures that the entity content is fully consumed and the content stream, if exists, 1.56 + * is closed. 1.57 + * 1.58 + * @param entity 1.59 + * @throws IOException if an error occurs reading the input stream 1.60 + * 1.61 + * @since 4.1 1.62 + */ 1.63 + public static void consume(final HttpEntity entity) throws IOException { 1.64 + if (entity == null) { 1.65 + return; 1.66 + } 1.67 + if (entity.isStreaming()) { 1.68 + InputStream instream = entity.getContent(); 1.69 + if (instream != null) { 1.70 + instream.close(); 1.71 + } 1.72 + } 1.73 + } 1.74 + 1.75 + /** 1.76 + * Read the contents of an entity and return it as a byte array. 1.77 + * 1.78 + * @param entity 1.79 + * @return byte array containing the entity content. May be null if 1.80 + * {@link HttpEntity#getContent()} is null. 1.81 + * @throws IOException if an error occurs reading the input stream 1.82 + * @throws IllegalArgumentException if entity is null or if content length > Integer.MAX_VALUE 1.83 + */ 1.84 + public static byte[] toByteArray(final HttpEntity entity) throws IOException { 1.85 + if (entity == null) { 1.86 + throw new IllegalArgumentException("HTTP entity may not be null"); 1.87 + } 1.88 + InputStream instream = entity.getContent(); 1.89 + if (instream == null) { 1.90 + return null; 1.91 + } 1.92 + try { 1.93 + if (entity.getContentLength() > Integer.MAX_VALUE) { 1.94 + throw new IllegalArgumentException("HTTP entity too large to be buffered in memory"); 1.95 + } 1.96 + int i = (int)entity.getContentLength(); 1.97 + if (i < 0) { 1.98 + i = 4096; 1.99 + } 1.100 + ByteArrayBuffer buffer = new ByteArrayBuffer(i); 1.101 + byte[] tmp = new byte[4096]; 1.102 + int l; 1.103 + while((l = instream.read(tmp)) != -1) { 1.104 + buffer.append(tmp, 0, l); 1.105 + } 1.106 + return buffer.toByteArray(); 1.107 + } finally { 1.108 + instream.close(); 1.109 + } 1.110 + } 1.111 + 1.112 + /** 1.113 + * Obtains character set of the entity, if known. 1.114 + * 1.115 + * @param entity must not be null 1.116 + * @return the character set, or null if not found 1.117 + * @throws ParseException if header elements cannot be parsed 1.118 + * @throws IllegalArgumentException if entity is null 1.119 + */ 1.120 + public static String getContentCharSet(final HttpEntity entity) throws ParseException { 1.121 + if (entity == null) { 1.122 + throw new IllegalArgumentException("HTTP entity may not be null"); 1.123 + } 1.124 + String charset = null; 1.125 + if (entity.getContentType() != null) { 1.126 + HeaderElement values[] = entity.getContentType().getElements(); 1.127 + if (values.length > 0) { 1.128 + NameValuePair param = values[0].getParameterByName("charset"); 1.129 + if (param != null) { 1.130 + charset = param.getValue(); 1.131 + } 1.132 + } 1.133 + } 1.134 + return charset; 1.135 + } 1.136 + 1.137 + /** 1.138 + * Obtains mime type of the entity, if known. 1.139 + * 1.140 + * @param entity must not be null 1.141 + * @return the character set, or null if not found 1.142 + * @throws ParseException if header elements cannot be parsed 1.143 + * @throws IllegalArgumentException if entity is null 1.144 + * 1.145 + * @since 4.1 1.146 + */ 1.147 + public static String getContentMimeType(final HttpEntity entity) throws ParseException { 1.148 + if (entity == null) { 1.149 + throw new IllegalArgumentException("HTTP entity may not be null"); 1.150 + } 1.151 + String mimeType = null; 1.152 + if (entity.getContentType() != null) { 1.153 + HeaderElement values[] = entity.getContentType().getElements(); 1.154 + if (values.length > 0) { 1.155 + mimeType = values[0].getName(); 1.156 + } 1.157 + } 1.158 + return mimeType; 1.159 + } 1.160 + 1.161 + /** 1.162 + * Get the entity content as a String, using the provided default character set 1.163 + * if none is found in the entity. 1.164 + * If defaultCharset is null, the default "ISO-8859-1" is used. 1.165 + * 1.166 + * @param entity must not be null 1.167 + * @param defaultCharset character set to be applied if none found in the entity 1.168 + * @return the entity content as a String. May be null if 1.169 + * {@link HttpEntity#getContent()} is null. 1.170 + * @throws ParseException if header elements cannot be parsed 1.171 + * @throws IllegalArgumentException if entity is null or if content length > Integer.MAX_VALUE 1.172 + * @throws IOException if an error occurs reading the input stream 1.173 + */ 1.174 + public static String toString( 1.175 + final HttpEntity entity, final String defaultCharset) throws IOException, ParseException { 1.176 + if (entity == null) { 1.177 + throw new IllegalArgumentException("HTTP entity may not be null"); 1.178 + } 1.179 + InputStream instream = entity.getContent(); 1.180 + if (instream == null) { 1.181 + return null; 1.182 + } 1.183 + try { 1.184 + if (entity.getContentLength() > Integer.MAX_VALUE) { 1.185 + throw new IllegalArgumentException("HTTP entity too large to be buffered in memory"); 1.186 + } 1.187 + int i = (int)entity.getContentLength(); 1.188 + if (i < 0) { 1.189 + i = 4096; 1.190 + } 1.191 + String charset = getContentCharSet(entity); 1.192 + if (charset == null) { 1.193 + charset = defaultCharset; 1.194 + } 1.195 + if (charset == null) { 1.196 + charset = HTTP.DEFAULT_CONTENT_CHARSET; 1.197 + } 1.198 + Reader reader = new InputStreamReader(instream, charset); 1.199 + CharArrayBuffer buffer = new CharArrayBuffer(i); 1.200 + char[] tmp = new char[1024]; 1.201 + int l; 1.202 + while((l = reader.read(tmp)) != -1) { 1.203 + buffer.append(tmp, 0, l); 1.204 + } 1.205 + return buffer.toString(); 1.206 + } finally { 1.207 + instream.close(); 1.208 + } 1.209 + } 1.210 + 1.211 + /** 1.212 + * Read the contents of an entity and return it as a String. 1.213 + * The content is converted using the character set from the entity (if any), 1.214 + * failing that, "ISO-8859-1" is used. 1.215 + * 1.216 + * @param entity 1.217 + * @return String containing the content. 1.218 + * @throws ParseException if header elements cannot be parsed 1.219 + * @throws IllegalArgumentException if entity is null or if content length > Integer.MAX_VALUE 1.220 + * @throws IOException if an error occurs reading the input stream 1.221 + */ 1.222 + public static String toString(final HttpEntity entity) 1.223 + throws IOException, ParseException { 1.224 + return toString(entity, null); 1.225 + } 1.226 + 1.227 +}