use of java.nio.charset.UnsupportedCharsetException in project wicket by apache.
the class UrlEncoder method encode.
/**
* @param unsafeInput
* string to encode
* @param charsetName
* encoding to use
* @return encoded string
* @see java.net.URLEncoder#encode(String, String)
*/
public String encode(final String unsafeInput, final String charsetName) {
final String s = unsafeInput.replace("\0", "NULL");
StringBuilder out = new StringBuilder(s.length());
Charset charset;
CharArrayWriter charArrayWriter = new CharArrayWriter();
Args.notNull(charsetName, "charsetName");
try {
charset = Charset.forName(charsetName);
} catch (IllegalCharsetNameException | UnsupportedCharsetException e) {
throw new RuntimeException(new UnsupportedEncodingException(charsetName));
}
for (int i = 0; i < s.length(); ) {
int c = s.charAt(i);
// System.out.println("Examining character: " + c);
if (dontNeedEncoding.get(c)) {
if (c == ' ') {
c = '+';
}
// 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 = 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 = s.charAt(i))));
charArrayWriter.flush();
String str = new String(charArrayWriter.toCharArray());
byte[] ba = str.getBytes(charset);
for (byte b : ba) {
out.append('%');
char ch = Character.forDigit((b >> 4) & 0xF, 16);
// the hex value if ch is a letter.
if (Character.isLetter(ch)) {
ch -= caseDiff;
}
out.append(ch);
ch = Character.forDigit(b & 0xF, 16);
if (Character.isLetter(ch)) {
ch -= caseDiff;
}
out.append(ch);
}
charArrayWriter.reset();
}
}
return out.toString();
}
use of java.nio.charset.UnsupportedCharsetException in project hale by halestudio.
the class CharsetConfigurationPage method update.
/**
* Update the page state.
*/
protected void update() {
String name = charsetCombo.getText();
if (name != null && !name.isEmpty()) {
try {
Charset cs = Charset.forName(name);
setMessage(successMessage(cs), INFORMATION);
setPageComplete(true);
return;
} catch (UnsupportedCharsetException e) {
setMessage("Charset not supported", ERROR);
} catch (IllegalCharsetNameException e) {
setMessage("Illegal charset name", ERROR);
}
} else {
setMessage("Please specify a character encoding", INFORMATION);
}
setPageComplete(false);
}
use of java.nio.charset.UnsupportedCharsetException in project stanbol by apache.
the class JerseyUtils method parseForm.
/**
* This Method is intended to parse form data from
* {@link MediaType#APPLICATION_FORM_URLENCODED} requests. This functionality
* us usually needed when writing a {@link MessageBodyReader} to get the
* data from the "{@link InputStream} entityStream" parameter of the
* {@link MessageBodyReader#readFrom(Class, Type, java.lang.annotation.Annotation[], MediaType, javax.ws.rs.core.MultivaluedMap, InputStream)}
* method.
* @param entityStream the stream with the form data
* @param charset The charset used for the request (if <code>null</code> or
* empty UTF-8 is used as default.
* @return the parsed form data as key value map
* @throws IOException on any exception while reading the data form the stream
*/
public static Map<String, String> parseForm(InputStream entityStream, String charset) throws IOException {
/* TODO: Question:
* If I get an Post Request with "application/x-www-form-urlencoded"
* and a charset (lets assume "iso-2022-kr") do I need to use the
* charset to read the String from the Stream, or to URL decode the
* String or both?
*
* This code assumes that it needs to be used for both, but this needs
* validation!
*/
if (charset == null || charset.isEmpty()) {
charset = "UTF-8";
}
String data;
try {
data = IOUtils.toString(entityStream, charset);
} catch (UnsupportedCharsetException e) {
throw new IOException(e.getMessage(), e);
}
Map<String, String> form = new HashMap<String, String>();
StringTokenizer tokenizer = new StringTokenizer(data, "&");
String token;
try {
while (tokenizer.hasMoreTokens()) {
token = tokenizer.nextToken();
int index = token.indexOf('=');
if (index < 0) {
form.put(URLDecoder.decode(token, charset), null);
} else if (index > 0) {
form.put(URLDecoder.decode(token.substring(0, index), charset), URLDecoder.decode(token.substring(index + 1), charset));
}
}
} catch (UnsupportedCharsetException e) {
throw new IOException(e.getMessage(), e);
}
return form;
}
use of java.nio.charset.UnsupportedCharsetException in project undertow by undertow-io.
the class HttpServletRequestImpl method getReader.
@Override
public BufferedReader getReader() throws IOException {
if (reader == null) {
if (servletInputStream != null) {
throw UndertowServletMessages.MESSAGES.getInputStreamAlreadyCalled();
}
Charset charSet = null;
if (this.characterEncoding != null) {
charSet = this.characterEncoding;
} else {
final String c = getCharacterEncoding();
if (c != null) {
try {
charSet = Charset.forName(c);
} catch (UnsupportedCharsetException e) {
throw new UnsupportedEncodingException(e.getMessage());
}
}
}
reader = new BufferedReader(charSet == null ? new InputStreamReader(exchange.getInputStream(), StandardCharsets.ISO_8859_1) : new InputStreamReader(exchange.getInputStream(), charSet));
}
readStarted = true;
return reader;
}
use of java.nio.charset.UnsupportedCharsetException in project j2objc by google.
the class Properties method storeToXML.
/**
* Emits an XML document representing all of the properties contained
* in this table, using the specified encoding.
*
* <p>The XML document will have the following DOCTYPE declaration:
* <pre>
* <!DOCTYPE properties SYSTEM "http://java.sun.com/dtd/properties.dtd">
* </pre>
*
* <p>If the specified comment is {@code null} then no comment
* will be stored in the document.
*
* <p> An implementation is required to support writing of XML documents
* that use the "{@code UTF-8}" or "{@code UTF-16}" encoding. An
* implementation may support additional encodings.
*
* <p>The specified stream remains open after this method returns.
*
* @param os the output stream on which to emit the XML document.
* @param comment a description of the property list, or {@code null}
* if no comment is desired.
* @param encoding the name of a supported
* <a href="../lang/package-summary.html#charenc">
* character encoding</a>
*
* @throws IOException if writing to the specified output stream
* results in an <tt>IOException</tt>.
* @throws java.io.UnsupportedEncodingException if the encoding is not
* supported by the implementation.
* @throws NullPointerException if {@code os} is {@code null},
* or if {@code encoding} is {@code null}.
* @throws ClassCastException if this {@code Properties} object
* contains any keys or values that are not
* {@code Strings}.
* @see #loadFromXML(InputStream)
* @see <a href="http://www.w3.org/TR/REC-xml/#charencoding">Character
* Encoding in Entities</a>
* @since 1.5
*/
public void storeToXML(OutputStream os, String comment, String encoding) throws IOException {
if (os == null) {
throw new NullPointerException("os == null");
} else if (encoding == null) {
throw new NullPointerException("encoding == null");
}
/*
* We can write to XML file using encoding parameter but note that some
* aliases for encodings are not supported by the XML parser. Thus we
* have to know canonical name for encoding used to store data in XML
* since the XML parser must recognize encoding name used to store data.
*/
String encodingCanonicalName;
try {
encodingCanonicalName = Charset.forName(encoding).name();
} catch (IllegalCharsetNameException e) {
System.out.println("Warning: encoding name " + encoding + " is illegal, using UTF-8 as default encoding");
encodingCanonicalName = "UTF-8";
} catch (UnsupportedCharsetException e) {
System.out.println("Warning: encoding " + encoding + " is not supported, using UTF-8 as default encoding");
encodingCanonicalName = "UTF-8";
}
PrintStream printStream = new PrintStream(os, false, encodingCanonicalName);
printStream.print("<?xml version=\"1.0\" encoding=\"");
printStream.print(encodingCanonicalName);
printStream.println("\"?>");
printStream.print("<!DOCTYPE properties SYSTEM \"");
printStream.print(PROP_DTD_NAME);
printStream.println("\">");
printStream.println("<properties>");
if (comment != null) {
printStream.print("<comment>");
printStream.print(substitutePredefinedEntries(comment));
printStream.println("</comment>");
}
for (Map.Entry<Object, Object> entry : entrySet()) {
String keyValue = (String) entry.getKey();
String entryValue = (String) entry.getValue();
printStream.print("<entry key=\"");
printStream.print(substitutePredefinedEntries(keyValue));
printStream.print("\">");
printStream.print(substitutePredefinedEntries(entryValue));
printStream.println("</entry>");
}
printStream.println("</properties>");
printStream.flush();
}
Aggregations