use of java.nio.charset.IllegalCharsetNameException in project jdk8u_jdk by JetBrains.
the class Test4625418 method test.
private void test(String string) {
try {
File file = new File("4625418." + this.encoding + ".xml");
FileOutputStream output = new FileOutputStream(file);
XMLEncoder encoder = new XMLEncoder(output, this.encoding, true, 0);
encoder.setExceptionListener(this);
encoder.writeObject(string);
encoder.close();
FileInputStream input = new FileInputStream(file);
XMLDecoder decoder = new XMLDecoder(input);
decoder.setExceptionListener(this);
Object object = decoder.readObject();
decoder.close();
if (!string.equals(object))
throw new Error(this.encoding + " - can't read properly");
file.delete();
} catch (FileNotFoundException exception) {
throw new Error(this.encoding + " - file not found", exception);
} catch (IllegalCharsetNameException exception) {
throw new Error(this.encoding + " - illegal charset name", exception);
} catch (UnsupportedCharsetException exception) {
throw new Error(this.encoding + " - unsupported charset", exception);
} catch (UnsupportedOperationException exception) {
throw new Error(this.encoding + " - unsupported encoder", exception);
}
}
use of java.nio.charset.IllegalCharsetNameException in project Asqatasun by Asqatasun.
the class CSSJsoupPhlocContentAdapterImpl method createNewExternalStyleSheet.
/**
*
* @param cssAbsolutePath
* @return
*/
private StylesheetContent createNewExternalStyleSheet(String cssAbsolutePath) {
LOGGER.debug("createNewExternalStyleSheet " + cssAbsolutePath);
String cssSourceCode;
try {
cssSourceCode = HttpRequestHandler.getInstance().getHttpContent(cssAbsolutePath);
} catch (URISyntaxException ex) {
LOGGER.debug("the resource " + cssAbsolutePath + " can't be retrieved : URISyntaxException");
cssSourceCode = CSS_ON_ERROR;
} catch (UnknownHostException uhe) {
LOGGER.debug("the resource " + cssAbsolutePath + " can't be retrieved : UnknownHostException");
cssSourceCode = CSS_ON_ERROR;
} catch (IOException ioe) {
LOGGER.debug("the resource " + cssAbsolutePath + " can't be retrieved : IOException");
try {
cssSourceCode = FileUtils.readFileToString(new File(cssAbsolutePath));
} catch (IOException ioe2) {
LOGGER.debug("the resource " + cssAbsolutePath + " can't be retrieved : IOException");
cssSourceCode = CSS_ON_ERROR;
}
} catch (IllegalCharsetNameException icne) {
LOGGER.debug("the resource " + cssAbsolutePath + " can't be retrieved : IllegalCharsetNameException");
cssSourceCode = CSS_ON_ERROR;
} catch (IllegalStateException ise) {
LOGGER.debug("the resource " + cssAbsolutePath + " can't be retrieved : IllegalStateException");
cssSourceCode = CSS_ON_ERROR;
}
if (StringUtils.isBlank(cssSourceCode)) {
LOGGER.debug("the resource " + cssAbsolutePath + " has an empty content");
cssSourceCode = CSS_ON_ERROR;
}
StylesheetContent cssContent = getContentDataService().getStylesheetContent(new Date(), cssAbsolutePath, getSSP(), cssSourceCode, 200);
cssContent.setAudit(getSSP().getAudit());
externalCssSet.add(cssContent);
// Some stylesheet may be retrieved during the adaptation. In this case
// these new css are added "manually" to the externalCssRetriever which
// is supposed to request the bdd once at the beginning of the adapting
// phasis.
externalCSSRetriever.addNewStylesheetContent(getSSP(), cssContent);
return cssContent;
}
use of java.nio.charset.IllegalCharsetNameException in project jdk8u_jdk by JetBrains.
the class URLEncoder method encode.
/**
* Translates a string into {@code application/x-www-form-urlencoded}
* format using a specific encoding scheme. This method uses the
* supplied encoding scheme to obtain the bytes for unsafe
* characters.
* <p>
* <em><strong>Note:</strong> The <a href=
* "http://www.w3.org/TR/html40/appendix/notes.html#non-ascii-chars">
* World Wide Web Consortium Recommendation</a> states that
* UTF-8 should be used. Not doing so may introduce
* incompatibilities.</em>
*
* @param s {@code String} to be translated.
* @param enc The name of a supported
* <a href="../lang/package-summary.html#charenc">character
* encoding</a>.
* @return the translated {@code String}.
* @exception UnsupportedEncodingException
* If the named encoding is not supported
* @see URLDecoder#decode(java.lang.String, java.lang.String)
* @since 1.4
*/
public static String encode(String s, String enc) throws UnsupportedEncodingException {
boolean needToChange = false;
StringBuffer out = new StringBuffer(s.length());
Charset charset;
CharArrayWriter charArrayWriter = new CharArrayWriter();
if (enc == null)
throw new NullPointerException("charsetName");
try {
charset = Charset.forName(enc);
} catch (IllegalCharsetNameException e) {
throw new UnsupportedEncodingException(enc);
} catch (UnsupportedCharsetException e) {
throw new UnsupportedEncodingException(enc);
}
for (int i = 0; i < s.length(); ) {
int c = (int) s.charAt(i);
//System.out.println("Examining character: " + c);
if (dontNeedEncoding.get(c)) {
if (c == ' ') {
c = '+';
needToChange = true;
}
//System.out.println("Storing: " + c);
out.append((char) c);
i++;
} else {
// convert to external encoding before hex conversion
do {
charArrayWriter.write(c);
/*
* If this character represents the start of a Unicode
* surrogate pair, then pass in two characters. It's not
* clear what should be done if a bytes reserved in the
* surrogate pairs range occurs outside of a legal
* surrogate pair. For now, just treat it as if it were
* any other character.
*/
if (c >= 0xD800 && c <= 0xDBFF) {
/*
System.out.println(Integer.toHexString(c)
+ " is high surrogate");
*/
if ((i + 1) < s.length()) {
int d = (int) s.charAt(i + 1);
/*
System.out.println("\tExamining "
+ Integer.toHexString(d));
*/
if (d >= 0xDC00 && d <= 0xDFFF) {
/*
System.out.println("\t"
+ Integer.toHexString(d)
+ " is low surrogate");
*/
charArrayWriter.write(d);
i++;
}
}
}
i++;
} while (i < s.length() && !dontNeedEncoding.get((c = (int) s.charAt(i))));
charArrayWriter.flush();
String str = new String(charArrayWriter.toCharArray());
byte[] ba = str.getBytes(charset);
for (int j = 0; j < ba.length; j++) {
out.append('%');
char ch = Character.forDigit((ba[j] >> 4) & 0xF, 16);
// the hex value if ch is a letter.
if (Character.isLetter(ch)) {
ch -= caseDiff;
}
out.append(ch);
ch = Character.forDigit(ba[j] & 0xF, 16);
if (Character.isLetter(ch)) {
ch -= caseDiff;
}
out.append(ch);
}
charArrayWriter.reset();
needToChange = true;
}
}
return (needToChange ? out.toString() : s);
}
use of java.nio.charset.IllegalCharsetNameException in project jdk8u_jdk by JetBrains.
the class Main method initializeConverter.
private static void initializeConverter() throws UnsupportedEncodingException {
Charset cs = null;
try {
cs = (encodingString == null) ? lookupCharset(defaultEncoding) : lookupCharset(encodingString);
encoder = (cs != null) ? cs.newEncoder() : null;
} catch (IllegalCharsetNameException e) {
throw new Error(e);
}
}
use of java.nio.charset.IllegalCharsetNameException in project tika by apache.
the class CharsetUtils method forName.
/** Returns Charset impl, if one exists. This method
* optionally uses ICU4J's CharsetICU.forNameICU,
* if it is found on the classpath, else only uses
* JDK's builtin Charset.forName. */
public static Charset forName(String name) {
if (name == null) {
throw new IllegalArgumentException();
}
// Get rid of cruft around names, like <>, trailing commas, etc.
Matcher m = CHARSET_NAME_PATTERN.matcher(name);
if (!m.matches()) {
throw new IllegalCharsetNameException(name);
}
name = m.group(1);
String lower = name.toLowerCase(Locale.ENGLISH);
Charset charset = COMMON_CHARSETS.get(lower);
if (charset != null) {
return charset;
} else if ("none".equals(lower) || "no".equals(lower)) {
throw new IllegalCharsetNameException(name);
} else {
Matcher iso = ISO_NAME_PATTERN.matcher(lower);
Matcher cp = CP_NAME_PATTERN.matcher(lower);
Matcher win = WIN_NAME_PATTERN.matcher(lower);
if (iso.matches()) {
// Handle "iso 8859-x" error
name = "iso-8859-" + iso.group(1);
charset = COMMON_CHARSETS.get(name);
} else if (cp.matches()) {
// Handle "cp-xxx" error
name = "cp" + cp.group(1);
charset = COMMON_CHARSETS.get(name);
} else if (win.matches()) {
// Handle "winxxx" and "win-xxx" errors
name = "windows-" + win.group(1);
charset = COMMON_CHARSETS.get(name);
}
if (charset != null) {
return charset;
}
}
if (getCharsetICU != null) {
try {
Charset cs = (Charset) getCharsetICU.invoke(null, name);
if (cs != null) {
return cs;
}
} catch (Exception e) {
// ignore
}
}
return Charset.forName(name);
}
Aggregations