summaryrefslogtreecommitdiffstats
path: root/src/org/apache/http/util/EntityUtils.java
diff options
context:
space:
mode:
Diffstat (limited to 'src/org/apache/http/util/EntityUtils.java')
-rw-r--r--src/org/apache/http/util/EntityUtils.java149
1 files changed, 149 insertions, 0 deletions
diff --git a/src/org/apache/http/util/EntityUtils.java b/src/org/apache/http/util/EntityUtils.java
new file mode 100644
index 0000000..f9a7cf3
--- /dev/null
+++ b/src/org/apache/http/util/EntityUtils.java
@@ -0,0 +1,149 @@
+/*
+ * $HeadURL: http://svn.apache.org/repos/asf/httpcomponents/httpcore/trunk/module-main/src/main/java/org/apache/http/util/EntityUtils.java $
+ * $Revision: 569637 $
+ * $Date: 2007-08-25 00:36:59 -0700 (Sat, 25 Aug 2007) $
+ *
+ * ====================================================================
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied. See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ * ====================================================================
+ *
+ * This software consists of voluntary contributions made by many
+ * individuals on behalf of the Apache Software Foundation. For more
+ * information on the Apache Software Foundation, please see
+ * <http://www.apache.org/>.
+ *
+ */
+
+package org.apache.http.util;
+
+import java.io.IOException;
+import java.io.InputStream;
+import java.io.InputStreamReader;
+import java.io.Reader;
+
+import org.apache.http.HeaderElement;
+import org.apache.http.HttpEntity;
+import org.apache.http.NameValuePair;
+import org.apache.http.ParseException;
+import org.apache.http.protocol.HTTP;
+
+/**
+ * Static helpers for dealing with {@link HttpEntity entities}.
+ *
+ * @author <a href="mailto:oleg at ural.ru">Oleg Kalnichevski</a>
+ *
+ * @version $Revision: 569637 $
+ *
+ * @since 4.0
+ */
+public final class EntityUtils {
+
+ /** Disabled default constructor. */
+ private EntityUtils() {
+ }
+
+ public static byte[] toByteArray(final HttpEntity entity) throws IOException {
+ if (entity == null) {
+ throw new IllegalArgumentException("HTTP entity may not be null");
+ }
+ InputStream instream = entity.getContent();
+ if (instream == null) {
+ return new byte[] {};
+ }
+ if (entity.getContentLength() > Integer.MAX_VALUE) {
+ throw new IllegalArgumentException("HTTP entity too large to be buffered in memory");
+ }
+ int i = (int)entity.getContentLength();
+ if (i < 0) {
+ i = 4096;
+ }
+ ByteArrayBuffer buffer = new ByteArrayBuffer(i);
+ try {
+ byte[] tmp = new byte[4096];
+ int l;
+ while((l = instream.read(tmp)) != -1) {
+ buffer.append(tmp, 0, l);
+ }
+ } finally {
+ instream.close();
+ }
+ return buffer.toByteArray();
+ }
+
+ public static String getContentCharSet(final HttpEntity entity)
+ throws ParseException {
+
+ if (entity == null) {
+ throw new IllegalArgumentException("HTTP entity may not be null");
+ }
+ String charset = null;
+ if (entity.getContentType() != null) {
+ HeaderElement values[] = entity.getContentType().getElements();
+ if (values.length > 0) {
+ NameValuePair param = values[0].getParameterByName("charset");
+ if (param != null) {
+ charset = param.getValue();
+ }
+ }
+ }
+ return charset;
+ }
+
+ public static String toString(
+ final HttpEntity entity, final String defaultCharset) throws IOException, ParseException {
+ if (entity == null) {
+ throw new IllegalArgumentException("HTTP entity may not be null");
+ }
+ InputStream instream = entity.getContent();
+ if (instream == null) {
+ return "";
+ }
+ if (entity.getContentLength() > Integer.MAX_VALUE) {
+ throw new IllegalArgumentException("HTTP entity too large to be buffered in memory");
+ }
+ int i = (int)entity.getContentLength();
+ if (i < 0) {
+ i = 4096;
+ }
+ String charset = getContentCharSet(entity);
+ if (charset == null) {
+ charset = defaultCharset;
+ }
+ if (charset == null) {
+ charset = HTTP.DEFAULT_CONTENT_CHARSET;
+ }
+ Reader reader = new InputStreamReader(instream, charset);
+ CharArrayBuffer buffer = new CharArrayBuffer(i);
+ try {
+ char[] tmp = new char[1024];
+ int l;
+ while((l = reader.read(tmp)) != -1) {
+ buffer.append(tmp, 0, l);
+ }
+ } finally {
+ reader.close();
+ }
+ return buffer.toString();
+ }
+
+ public static String toString(final HttpEntity entity)
+ throws IOException, ParseException {
+ return toString(entity, null);
+ }
+
+}