本文整理匯總了Java中java.nio.charset.UnsupportedCharsetException類的典型用法代碼示例。如果您正苦於以下問題:Java UnsupportedCharsetException類的具體用法?Java UnsupportedCharsetException怎麽用?Java UnsupportedCharsetException使用的例子?那麽, 這裏精選的類代碼示例或許可以為您提供幫助。
UnsupportedCharsetException類屬於java.nio.charset包,在下文中一共展示了UnsupportedCharsetException類的15個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Java代碼示例。
示例1: getCharset
import java.nio.charset.UnsupportedCharsetException; //導入依賴的package包/類
private static Charset getCharset(String contentType) {
// FIXME the spec default is ISO-8859-1
Charset encoding = Charset.forName("UTF-8"); // NOI18N
if (contentType != null) {
String[] parts = contentType.trim().split(";"); // NOI18N
for (String p : parts) {
String upper = p.toUpperCase(Locale.ENGLISH);
if (upper.startsWith("CHARSET")) { // NOI18N
int index = upper.indexOf("=", 7); // NOI18N
if (index > 0 && index < upper.length() -1) {
try {
encoding = Charset.forName(upper.substring(index + 1).trim());
} catch (UnsupportedCharsetException ex) {
// noop using the UTF-8
}
}
break;
}
}
}
return encoding;
}
示例2: Impl
import java.nio.charset.UnsupportedCharsetException; //導入依賴的package包/類
Impl(String name, String title, String MIMEType, Reader r) {
this.name = name;
this.title = title;
this.MIMEType = MIMEType;
this.r = r;
this.readerSource = null;
this.w = null;
this.file = null;
if (r instanceof InputStreamReader) {
try {
encoding = Charset.forName(((InputStreamReader) r).getEncoding());
} catch (UnsupportedCharsetException e) {
// ignore, encoding will be null
}
}
}
示例3: sendLogResponse
import java.nio.charset.UnsupportedCharsetException; //導入依賴的package包/類
/**
* 開始解析服務器返回參數
*/
private void sendLogResponse(Response response) throws IOException {
String rBody = "";
if (response != null && response.body() != null) {
BufferedSource source = response.body().source();
source.request(Long.MAX_VALUE); // Buffer the entire body.
Buffer buffer = source.buffer();
Charset charset = Charset.forName("UTF-8");
MediaType contentType = response.body().contentType();
if (contentType != null) {
try {
charset = contentType.charset(Charset.forName("UTF-8"));
} catch (UnsupportedCharsetException e) {
e.printStackTrace();
}
}
rBody = buffer.clone().readString(charset);
}
LogUtils.i("接收:" + rBody.toString());
}
示例4: findCharset
import java.nio.charset.UnsupportedCharsetException; //導入依賴的package包/類
static Charset findCharset(String alias) throws UnsupportedEncodingException {
try {
Charset cs = charsetsByAlias.get(alias);
if (cs == null) {
cs = Charset.forName(alias);
Charset oldCs = charsetsByAlias.putIfAbsent(alias, cs);
if (oldCs != null) {
// if the previous value was recently set by another thread we return it instead of value we found here
cs = oldCs;
}
}
return cs;
// We re-throw these runtimes for compatibility with java.io
} catch (UnsupportedCharsetException uce) {
throw new UnsupportedEncodingException(alias);
} catch (IllegalCharsetNameException icne) {
throw new UnsupportedEncodingException(alias);
} catch (IllegalArgumentException iae) {
throw new UnsupportedEncodingException(alias);
}
}
示例5: findCharset
import java.nio.charset.UnsupportedCharsetException; //導入依賴的package包/類
static Charset findCharset(String alias) throws UnsupportedEncodingException {
try {
Charset cs = charsetsByAlias.get(alias);
if (cs == null) {
cs = Charset.forName(alias);
charsetsByAlias.putIfAbsent(alias, cs);
}
return cs;
// We re-throw these runtimes for compatibility with java.io
} catch (UnsupportedCharsetException uce) {
throw new UnsupportedEncodingException(alias);
} catch (IllegalCharsetNameException icne) {
throw new UnsupportedEncodingException(alias);
} catch (IllegalArgumentException iae) {
throw new UnsupportedEncodingException(alias);
}
}
示例6: setCharacterEncoding
import java.nio.charset.UnsupportedCharsetException; //導入依賴的package包/類
@Override
public void setCharacterEncoding(final String env) throws UnsupportedEncodingException {
if (readStarted) {
return;
}
try {
characterEncoding = Charset.forName(env);
final ManagedServlet originalServlet = exchange.getAttachment(ServletRequestContext.ATTACHMENT_KEY).getOriginalServletPathMatch().getServletChain().getManagedServlet();
final FormDataParser parser = originalServlet.getFormParserFactory().createParser(exchange);
if (parser != null) {
parser.setCharacterEncoding(env);
}
} catch (UnsupportedCharsetException e) {
throw new UnsupportedEncodingException();
}
}
示例7: toString
import java.nio.charset.UnsupportedCharsetException; //導入依賴的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,
* or if the entity provided charset is invalid or not available.
* @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
* @throws java.nio.charset.UnsupportedCharsetException Thrown when the named entity's charset is not available in
* this instance of the Java virtual machine and no defaultCharset is provided.
*/
public static String toString(
final HttpEntity entity, final Charset defaultCharset) throws IOException, ParseException {
Args.notNull(entity, "Entity");
ru.radiomayak.http.entity.ContentType contentType = null;
try {
contentType = ru.radiomayak.http.entity.ContentType.get(entity);
} catch (final UnsupportedCharsetException ex) {
if (defaultCharset == null) {
throw new UnsupportedEncodingException(ex.getMessage());
}
}
if (contentType != null) {
if (contentType.getCharset() == null) {
contentType = contentType.withCharset(defaultCharset);
}
} else {
contentType = ru.radiomayak.http.entity.ContentType.DEFAULT_TEXT.withCharset(defaultCharset);
}
return toString(entity, contentType);
}
示例8: StringEntity
import java.nio.charset.UnsupportedCharsetException; //導入依賴的package包/類
/**
* Creates a StringEntity with the specified content and content type.
*
* @param string content to be used. Not {@code null}.
* @param contentType content type to be used. May be {@code null}, in which case the default
* MIME type {@link ContentType#TEXT_PLAIN} is assumed.
*
* @throws IllegalArgumentException if the string parameter is null
* @throws UnsupportedCharsetException Thrown when the named charset is not available in
* this instance of the Java virtual machine
* @since 4.2
*/
public StringEntity(final String string, final ContentType contentType) throws UnsupportedCharsetException {
super();
Args.notNull(string, "Source string");
Charset charset = contentType != null ? contentType.getCharset() : null;
if (charset == null) {
charset = HTTP.DEF_CONTENT_CHARSET;
}
try {
this.content = string.getBytes(charset.name());
} catch (final UnsupportedEncodingException ex) {
// should never happen
throw new UnsupportedCharsetException(charset.name());
}
if (contentType != null) {
setContentType(contentType.toString());
}
}
示例9: intercept
import java.nio.charset.UnsupportedCharsetException; //導入依賴的package包/類
@Override
public Response intercept(Chain chain) throws IOException {
Request request = chain.request();
Response response = chain.proceed(request);
ResponseBody body = response.body();
long length = body.contentLength();
BufferedSource source = body.source();
source.request(Long.MAX_VALUE);
Buffer buffer = source.buffer();
Charset charset = Charset.forName("UTF-8");
MediaType type = body.contentType();
if (type != null) {
try {
charset = type.charset(charset);
} catch (UnsupportedCharsetException e) {
e.printStackTrace();
return response;
}
}
if (length != 0) {
System.out.println("shenhua sout:--------------------------------------------開始打印返回數據----------------------------------------------------");
System.out.println("shenhua sout:" + buffer.clone().readString(charset));
}
return response;
}
示例10: JavaCTBConverter
import java.nio.charset.UnsupportedCharsetException; //導入依賴的package包/類
public JavaCTBConverter(OSFCodeSetRegistry.Entry codeset,
int alignmentForEncoding) {
try {
ctb = cache.getCharToByteConverter(codeset.getName());
if (ctb == null) {
Charset tmpCharset = Charset.forName(codeset.getName());
ctb = tmpCharset.newEncoder();
cache.setConverter(codeset.getName(), ctb);
}
} catch(IllegalCharsetNameException icne) {
// This can only happen if one of our Entries has
// an invalid name.
throw wrapper.invalidCtbConverterName(icne,codeset.getName());
} catch(UnsupportedCharsetException ucne) {
// This can only happen if one of our Entries has
// an unsupported name.
throw wrapper.invalidCtbConverterName(ucne,codeset.getName());
}
this.codeset = codeset;
alignment = alignmentForEncoding;
}
示例11: detectCharsetImpl
import java.nio.charset.UnsupportedCharsetException; //導入依賴的package包/類
@Override
protected Charset detectCharsetImpl(byte[] buffer) throws Exception
{
CharsetDetector detector = new CharsetDetector();
detector.setText(buffer);
CharsetMatch match = detector.detect();
if(match != null && match.getConfidence() > threshold)
{
try
{
return Charset.forName(match.getName());
}
catch(UnsupportedCharsetException e)
{
logger.info("Charset detected as " + match.getName() + " but the JVM does not support this, detection skipped");
}
}
return null;
}
示例12: create
import java.nio.charset.UnsupportedCharsetException; //導入依賴的package包/類
private static ContentType create(final String mimeType, final NameValuePair[] params, final boolean strict) {
Charset charset = null;
for (final NameValuePair param: params) {
if (param.getName().equalsIgnoreCase("charset")) {
final String s = param.getValue();
if (!TextUtils.isBlank(s)) {
try {
charset = Charset.forName(s);
} catch (final UnsupportedCharsetException ex) {
if (strict) {
throw ex;
}
}
}
break;
}
}
return new ContentType(mimeType, charset, params != null && params.length > 0 ? params : null);
}
示例13: toString
import java.nio.charset.UnsupportedCharsetException; //導入依賴的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,
* or if the entity provided charset is invalid or not available.
* @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
* @throws java.nio.charset.UnsupportedCharsetException Thrown when the named entity's charset is not available in
* this instance of the Java virtual machine and no defaultCharset is provided.
*/
public static String toString(
final HttpEntity entity, final Charset defaultCharset) throws IOException, ParseException {
Args.notNull(entity, "Entity");
ContentType contentType = null;
try {
contentType = ContentType.get(entity);
} catch (final UnsupportedCharsetException ex) {
if (defaultCharset == null) {
throw new UnsupportedEncodingException(ex.getMessage());
}
}
if (contentType != null) {
if (contentType.getCharset() == null) {
contentType = contentType.withCharset(defaultCharset);
}
} else {
contentType = ContentType.DEFAULT_TEXT.withCharset(defaultCharset);
}
return toString(entity, contentType);
}
示例14: bomStream
import java.nio.charset.UnsupportedCharsetException; //導入依賴的package包/類
/**
* Convert stream to ByteArrayInputStream by given character set.
* @param charset target character set.
* @param file a file that contains no BOM head content.
* @return a ByteArrayInputStream contains BOM heads and bytes in original
* stream
* @throws IOException I/O operation failed or unsupported character set.
*/
public static InputStream bomStream(String charset, String file)
throws IOException {
String localCharset = charset;
if (charset.equals("UTF-16") || charset.equals("UTF-32")) {
localCharset
+= ByteOrder.nativeOrder() == ByteOrder.BIG_ENDIAN ? "BE" : "LE";
}
if (!bom.containsKey(localCharset))
throw new UnsupportedCharsetException("Charset:" + localCharset);
byte[] content = Files.readAllLines(Paths.get(file)).stream().
collect(Collectors.joining()).getBytes(localCharset);
byte[] head = bom.get(localCharset);
ByteBuffer bb = ByteBuffer.allocate(content.length + head.length);
bb.put(head);
bb.put(content);
return new ByteArrayInputStream(bb.array());
}
示例15: setOutput
import java.nio.charset.UnsupportedCharsetException; //導入依賴的package包/類
public void setOutput(OutputStream os, String encoding)
throws IOException, IllegalArgumentException, IllegalStateException {
if (os == null) {
throw new IllegalArgumentException();
}
try {
charset = Charset.forName(encoding).newEncoder();
} catch (IllegalCharsetNameException | UnsupportedCharsetException e) {
throw (UnsupportedEncodingException) (new UnsupportedEncodingException(encoding).initCause(e));
}
out = os;
}