当前位置: 首页>>代码示例>>Java>>正文


Java ContentType.getOrDefault方法代码示例

本文整理汇总了Java中org.apache.http.entity.ContentType.getOrDefault方法的典型用法代码示例。如果您正苦于以下问题:Java ContentType.getOrDefault方法的具体用法?Java ContentType.getOrDefault怎么用?Java ContentType.getOrDefault使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在org.apache.http.entity.ContentType的用法示例。


在下文中一共展示了ContentType.getOrDefault方法的3个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。

示例1: toString

import org.apache.http.entity.ContentType; //导入方法依赖的package包/类
/**
 * Get the entity content as a String, using the provided default character set
 * if none is found in the entity.
 * If defaultCharset is null, the default "ISO-8859-1" is used.
 *
 * @param entity must not be null
 * @param defaultCharset character set to be applied if none found in the entity
 * @return the entity content as a String. May be null if
 *   {@link HttpEntity#getContent()} is null.
 * @throws ParseException if header elements cannot be parsed
 * @throws IllegalArgumentException if entity is null or if content length > Integer.MAX_VALUE
 * @throws IOException if an error occurs reading the input stream
 */
public static String toString(
        final HttpEntity entity, final Charset defaultCharset) throws IOException, ParseException {
    if (entity == null) {
        throw new IllegalArgumentException("HTTP entity may not be null");
    }
    InputStream instream = entity.getContent();
    if (instream == null) {
        return null;
    }
    try {
        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;
        }
        ContentType contentType = ContentType.getOrDefault(entity);
        Charset charset = contentType.getCharset();
        if (charset == null) {
            charset = defaultCharset;
        }
        if (charset == null) {
            charset = HTTP.DEF_CONTENT_CHARSET;
        }
        Reader reader = new InputStreamReader(instream, charset);
        CharArrayBuffer buffer = new CharArrayBuffer(i);
        char[] tmp = new char[1024];
        int l;
        while((l = reader.read(tmp)) != -1) {
            buffer.append(tmp, 0, l);
        }
        return buffer.toString();
    } finally {
        instream.close();
    }
}
 
开发者ID:lamsfoundation,项目名称:lams,代码行数:51,代码来源:EntityUtils.java

示例2: getCharset

import org.apache.http.entity.ContentType; //导入方法依赖的package包/类
public static String getCharset(HttpResponse response) {
  ContentType contentType = ContentType.getOrDefault(response.getEntity());
  Charset charset = contentType.getCharset();
  return charset == null ? "UTF-8" : charset.name();
}
 
开发者ID:EHRI,项目名称:rs-aggregator,代码行数:6,代码来源:AbstractUriReader.java

示例3: getResponseCharset

import org.apache.http.entity.ContentType; //导入方法依赖的package包/类
public static String getResponseCharset(HttpResponse resp) {
	ContentType ctype = ContentType.getOrDefault(resp.getEntity());
	if (ctype.getCharset() != null)
		return ctype.getCharset().name();
	return null;
}
 
开发者ID:ichatter,项目名称:dcits-report,代码行数:7,代码来源:HttpHeaderUtil.java


注:本文中的org.apache.http.entity.ContentType.getOrDefault方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。