mobile/android/thirdparty/ch/boye/httpclientandroidlib/util/EntityUtils.java

Wed, 31 Dec 2014 07:22:50 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Wed, 31 Dec 2014 07:22:50 +0100
branch
TOR_BUG_3246
changeset 4
fc2d59ddac77
permissions
-rw-r--r--

Correct previous dual key logic pending first delivery installment.

michael@0 1 /*
michael@0 2 * ====================================================================
michael@0 3 * Licensed to the Apache Software Foundation (ASF) under one
michael@0 4 * or more contributor license agreements. See the NOTICE file
michael@0 5 * distributed with this work for additional information
michael@0 6 * regarding copyright ownership. The ASF licenses this file
michael@0 7 * to you under the Apache License, Version 2.0 (the
michael@0 8 * "License"); you may not use this file except in compliance
michael@0 9 * with the License. You may obtain a copy of the License at
michael@0 10 *
michael@0 11 * http://www.apache.org/licenses/LICENSE-2.0
michael@0 12 *
michael@0 13 * Unless required by applicable law or agreed to in writing,
michael@0 14 * software distributed under the License is distributed on an
michael@0 15 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
michael@0 16 * KIND, either express or implied. See the License for the
michael@0 17 * specific language governing permissions and limitations
michael@0 18 * under the License.
michael@0 19 * ====================================================================
michael@0 20 *
michael@0 21 * This software consists of voluntary contributions made by many
michael@0 22 * individuals on behalf of the Apache Software Foundation. For more
michael@0 23 * information on the Apache Software Foundation, please see
michael@0 24 * <http://www.apache.org/>.
michael@0 25 *
michael@0 26 */
michael@0 27
michael@0 28 package ch.boye.httpclientandroidlib.util;
michael@0 29
michael@0 30 import java.io.IOException;
michael@0 31 import java.io.InputStream;
michael@0 32 import java.io.InputStreamReader;
michael@0 33 import java.io.Reader;
michael@0 34
michael@0 35 import ch.boye.httpclientandroidlib.HeaderElement;
michael@0 36 import ch.boye.httpclientandroidlib.HttpEntity;
michael@0 37 import ch.boye.httpclientandroidlib.NameValuePair;
michael@0 38 import ch.boye.httpclientandroidlib.ParseException;
michael@0 39 import ch.boye.httpclientandroidlib.protocol.HTTP;
michael@0 40
michael@0 41 /**
michael@0 42 * Static helpers for dealing with {@link HttpEntity}s.
michael@0 43 *
michael@0 44 * @since 4.0
michael@0 45 */
michael@0 46 public final class EntityUtils {
michael@0 47
michael@0 48 private EntityUtils() {
michael@0 49 }
michael@0 50
michael@0 51 /**
michael@0 52 * Ensures that the entity content is fully consumed and the content stream, if exists,
michael@0 53 * is closed.
michael@0 54 *
michael@0 55 * @param entity
michael@0 56 * @throws IOException if an error occurs reading the input stream
michael@0 57 *
michael@0 58 * @since 4.1
michael@0 59 */
michael@0 60 public static void consume(final HttpEntity entity) throws IOException {
michael@0 61 if (entity == null) {
michael@0 62 return;
michael@0 63 }
michael@0 64 if (entity.isStreaming()) {
michael@0 65 InputStream instream = entity.getContent();
michael@0 66 if (instream != null) {
michael@0 67 instream.close();
michael@0 68 }
michael@0 69 }
michael@0 70 }
michael@0 71
michael@0 72 /**
michael@0 73 * Read the contents of an entity and return it as a byte array.
michael@0 74 *
michael@0 75 * @param entity
michael@0 76 * @return byte array containing the entity content. May be null if
michael@0 77 * {@link HttpEntity#getContent()} is null.
michael@0 78 * @throws IOException if an error occurs reading the input stream
michael@0 79 * @throws IllegalArgumentException if entity is null or if content length > Integer.MAX_VALUE
michael@0 80 */
michael@0 81 public static byte[] toByteArray(final HttpEntity entity) throws IOException {
michael@0 82 if (entity == null) {
michael@0 83 throw new IllegalArgumentException("HTTP entity may not be null");
michael@0 84 }
michael@0 85 InputStream instream = entity.getContent();
michael@0 86 if (instream == null) {
michael@0 87 return null;
michael@0 88 }
michael@0 89 try {
michael@0 90 if (entity.getContentLength() > Integer.MAX_VALUE) {
michael@0 91 throw new IllegalArgumentException("HTTP entity too large to be buffered in memory");
michael@0 92 }
michael@0 93 int i = (int)entity.getContentLength();
michael@0 94 if (i < 0) {
michael@0 95 i = 4096;
michael@0 96 }
michael@0 97 ByteArrayBuffer buffer = new ByteArrayBuffer(i);
michael@0 98 byte[] tmp = new byte[4096];
michael@0 99 int l;
michael@0 100 while((l = instream.read(tmp)) != -1) {
michael@0 101 buffer.append(tmp, 0, l);
michael@0 102 }
michael@0 103 return buffer.toByteArray();
michael@0 104 } finally {
michael@0 105 instream.close();
michael@0 106 }
michael@0 107 }
michael@0 108
michael@0 109 /**
michael@0 110 * Obtains character set of the entity, if known.
michael@0 111 *
michael@0 112 * @param entity must not be null
michael@0 113 * @return the character set, or null if not found
michael@0 114 * @throws ParseException if header elements cannot be parsed
michael@0 115 * @throws IllegalArgumentException if entity is null
michael@0 116 */
michael@0 117 public static String getContentCharSet(final HttpEntity entity) throws ParseException {
michael@0 118 if (entity == null) {
michael@0 119 throw new IllegalArgumentException("HTTP entity may not be null");
michael@0 120 }
michael@0 121 String charset = null;
michael@0 122 if (entity.getContentType() != null) {
michael@0 123 HeaderElement values[] = entity.getContentType().getElements();
michael@0 124 if (values.length > 0) {
michael@0 125 NameValuePair param = values[0].getParameterByName("charset");
michael@0 126 if (param != null) {
michael@0 127 charset = param.getValue();
michael@0 128 }
michael@0 129 }
michael@0 130 }
michael@0 131 return charset;
michael@0 132 }
michael@0 133
michael@0 134 /**
michael@0 135 * Obtains mime type of the entity, if known.
michael@0 136 *
michael@0 137 * @param entity must not be null
michael@0 138 * @return the character set, or null if not found
michael@0 139 * @throws ParseException if header elements cannot be parsed
michael@0 140 * @throws IllegalArgumentException if entity is null
michael@0 141 *
michael@0 142 * @since 4.1
michael@0 143 */
michael@0 144 public static String getContentMimeType(final HttpEntity entity) throws ParseException {
michael@0 145 if (entity == null) {
michael@0 146 throw new IllegalArgumentException("HTTP entity may not be null");
michael@0 147 }
michael@0 148 String mimeType = null;
michael@0 149 if (entity.getContentType() != null) {
michael@0 150 HeaderElement values[] = entity.getContentType().getElements();
michael@0 151 if (values.length > 0) {
michael@0 152 mimeType = values[0].getName();
michael@0 153 }
michael@0 154 }
michael@0 155 return mimeType;
michael@0 156 }
michael@0 157
michael@0 158 /**
michael@0 159 * Get the entity content as a String, using the provided default character set
michael@0 160 * if none is found in the entity.
michael@0 161 * If defaultCharset is null, the default "ISO-8859-1" is used.
michael@0 162 *
michael@0 163 * @param entity must not be null
michael@0 164 * @param defaultCharset character set to be applied if none found in the entity
michael@0 165 * @return the entity content as a String. May be null if
michael@0 166 * {@link HttpEntity#getContent()} is null.
michael@0 167 * @throws ParseException if header elements cannot be parsed
michael@0 168 * @throws IllegalArgumentException if entity is null or if content length > Integer.MAX_VALUE
michael@0 169 * @throws IOException if an error occurs reading the input stream
michael@0 170 */
michael@0 171 public static String toString(
michael@0 172 final HttpEntity entity, final String defaultCharset) throws IOException, ParseException {
michael@0 173 if (entity == null) {
michael@0 174 throw new IllegalArgumentException("HTTP entity may not be null");
michael@0 175 }
michael@0 176 InputStream instream = entity.getContent();
michael@0 177 if (instream == null) {
michael@0 178 return null;
michael@0 179 }
michael@0 180 try {
michael@0 181 if (entity.getContentLength() > Integer.MAX_VALUE) {
michael@0 182 throw new IllegalArgumentException("HTTP entity too large to be buffered in memory");
michael@0 183 }
michael@0 184 int i = (int)entity.getContentLength();
michael@0 185 if (i < 0) {
michael@0 186 i = 4096;
michael@0 187 }
michael@0 188 String charset = getContentCharSet(entity);
michael@0 189 if (charset == null) {
michael@0 190 charset = defaultCharset;
michael@0 191 }
michael@0 192 if (charset == null) {
michael@0 193 charset = HTTP.DEFAULT_CONTENT_CHARSET;
michael@0 194 }
michael@0 195 Reader reader = new InputStreamReader(instream, charset);
michael@0 196 CharArrayBuffer buffer = new CharArrayBuffer(i);
michael@0 197 char[] tmp = new char[1024];
michael@0 198 int l;
michael@0 199 while((l = reader.read(tmp)) != -1) {
michael@0 200 buffer.append(tmp, 0, l);
michael@0 201 }
michael@0 202 return buffer.toString();
michael@0 203 } finally {
michael@0 204 instream.close();
michael@0 205 }
michael@0 206 }
michael@0 207
michael@0 208 /**
michael@0 209 * Read the contents of an entity and return it as a String.
michael@0 210 * The content is converted using the character set from the entity (if any),
michael@0 211 * failing that, "ISO-8859-1" is used.
michael@0 212 *
michael@0 213 * @param entity
michael@0 214 * @return String containing the content.
michael@0 215 * @throws ParseException if header elements cannot be parsed
michael@0 216 * @throws IllegalArgumentException if entity is null or if content length > Integer.MAX_VALUE
michael@0 217 * @throws IOException if an error occurs reading the input stream
michael@0 218 */
michael@0 219 public static String toString(final HttpEntity entity)
michael@0 220 throws IOException, ParseException {
michael@0 221 return toString(entity, null);
michael@0 222 }
michael@0 223
michael@0 224 }

mercurial