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 32 package org.apache.http.util; 33 34 import java.io.IOException; 35 import java.io.InputStream; 36 import java.io.InputStreamReader; 37 import java.io.Reader; 38 39 import org.apache.http.HeaderElement; 40 import org.apache.http.HttpEntity; 41 import org.apache.http.NameValuePair; 42 import org.apache.http.ParseException; 43 import 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 * 54 * @deprecated Please use {@link java.net.URL#openConnection} instead. 55 * Please visit <a href="http://android-developers.blogspot.com/2011/09/androids-http-clients.html">this webpage</a> 56 * for further details. 57 */ 58 @Deprecated 59 public final class EntityUtils { 60 61 /** Disabled default constructor. */ EntityUtils()62 private EntityUtils() { 63 } 64 toByteArray(final HttpEntity entity)65 public static byte[] toByteArray(final HttpEntity entity) throws IOException { 66 if (entity == null) { 67 throw new IllegalArgumentException("HTTP entity may not be null"); 68 } 69 InputStream instream = entity.getContent(); 70 if (instream == null) { 71 return new byte[] {}; 72 } 73 if (entity.getContentLength() > Integer.MAX_VALUE) { 74 throw new IllegalArgumentException("HTTP entity too large to be buffered in memory"); 75 } 76 int i = (int)entity.getContentLength(); 77 if (i < 0) { 78 i = 4096; 79 } 80 ByteArrayBuffer buffer = new ByteArrayBuffer(i); 81 try { 82 byte[] tmp = new byte[4096]; 83 int l; 84 while((l = instream.read(tmp)) != -1) { 85 buffer.append(tmp, 0, l); 86 } 87 } finally { 88 instream.close(); 89 } 90 return buffer.toByteArray(); 91 } 92 getContentCharSet(final HttpEntity entity)93 public static String getContentCharSet(final HttpEntity entity) 94 throws ParseException { 95 96 if (entity == null) { 97 throw new IllegalArgumentException("HTTP entity may not be null"); 98 } 99 String charset = null; 100 if (entity.getContentType() != null) { 101 HeaderElement values[] = entity.getContentType().getElements(); 102 if (values.length > 0) { 103 NameValuePair param = values[0].getParameterByName("charset"); 104 if (param != null) { 105 charset = param.getValue(); 106 } 107 } 108 } 109 return charset; 110 } 111 toString( final HttpEntity entity, final String defaultCharset)112 public static String toString( 113 final HttpEntity entity, final String defaultCharset) throws IOException, ParseException { 114 if (entity == null) { 115 throw new IllegalArgumentException("HTTP entity may not be null"); 116 } 117 InputStream instream = entity.getContent(); 118 if (instream == null) { 119 return ""; 120 } 121 if (entity.getContentLength() > Integer.MAX_VALUE) { 122 throw new IllegalArgumentException("HTTP entity too large to be buffered in memory"); 123 } 124 int i = (int)entity.getContentLength(); 125 if (i < 0) { 126 i = 4096; 127 } 128 String charset = getContentCharSet(entity); 129 if (charset == null) { 130 charset = defaultCharset; 131 } 132 if (charset == null) { 133 charset = HTTP.DEFAULT_CONTENT_CHARSET; 134 } 135 Reader reader = new InputStreamReader(instream, charset); 136 CharArrayBuffer buffer = new CharArrayBuffer(i); 137 try { 138 char[] tmp = new char[1024]; 139 int l; 140 while((l = reader.read(tmp)) != -1) { 141 buffer.append(tmp, 0, l); 142 } 143 } finally { 144 reader.close(); 145 } 146 return buffer.toString(); 147 } 148 toString(final HttpEntity entity)149 public static String toString(final HttpEntity entity) 150 throws IOException, ParseException { 151 return toString(entity, null); 152 } 153 154 } 155