List of usage examples for org.apache.commons.codec.net QCodec QCodec
public QCodec(final String charset)
From source file:io.datenwelt.cargo.rest.utils.Rfc2047.java
public static String decodeHeader(String input) { StringTokenizer tokenizer = new StringTokenizer(input, " \t", true); StringBuilder decoded = new StringBuilder(); while (tokenizer.hasMoreTokens()) { String token = tokenizer.nextToken(); if (!token.startsWith("=?") || !token.endsWith("?=")) { decoded.append(token);// w w w .j av a 2s.com continue; } String encodedWord = token; String[] parts = encodedWord.substring(2, encodedWord.length() - 2).split("\\?", 3); if (parts.length != 3) { decoded.append(token); continue; } Charset charset; try { charset = Charset.forName(parts[0]); } catch (Exception ex) { LOG.debug("Skipping the decoding of a header value due to an unknown charset \"{}\" found in: ", parts[1], input, ex); decoded.append(token); continue; } String encoding = parts[1].toUpperCase(); switch (encoding) { case "B": BCodec bcodec = new BCodec(charset); try { decoded.append(bcodec.decode(encodedWord)); } catch (Exception ex) { LOG.debug("Skipping the decoding of BASE64 value from string \"{}\" found in: ", encodedWord, input, ex); decoded.append(token); } break; case "Q": QCodec qcodec = new QCodec(charset); try { decoded.append(qcodec.decode(encodedWord)); } catch (Exception ex) { LOG.debug("Skipping the decoding of Q encoded value from string \"{}\" found in: ", encodedWord, input, ex); decoded.append(token); } break; default: LOG.debug("Skipping the decoding of value from unknown encoding \"{}\" found in: ", encodedWord, input); decoded.append(token); } } return decoded.toString(); }
From source file:io.datenwelt.cargo.rest.utils.Rfc2047.java
public static String encodeHeader(String input) { StringTokenizer tokenizer = new StringTokenizer(input, "\t ,;:-/=+#*", true); CharsetEncoder charsetEncoder = Charset.forName(CharEncoding.ISO_8859_1).newEncoder(); QCodec qcodec = new QCodec(Charset.forName(CharEncoding.UTF_8)); StringBuilder encoded = new StringBuilder(); while (tokenizer.hasMoreTokens()) { String token = tokenizer.nextToken(); if (!charsetEncoder.canEncode(token)) { try { encoded.append(qcodec.encode(token)); } catch (EncoderException ex) { LOG.debug("Skipping the Q encoding of header value for non ISO-8859-1 string: {}", input, ex); encoded.append(token);//from w ww. j a v a 2 s . co m } } else { encoded.append(token); } } return encoded.toString(); }
From source file:org.apache.abdera.i18n.text.Rfc2047Helper.java
/** * Used to encode a string as specified by RFC 2047 * /*from w w w . j a v a2 s. co m*/ * @param value The string to encode * @param charset The character set to use for the encoding */ public static String encode(String value, String charset, Codec codec) { if (value == null) return null; try { switch (codec) { case Q: return (new QCodec(charset)).encode(value); case B: default: return (new BCodec(charset)).encode(value); } } catch (Exception e) { return value; } }
From source file:org.apache.abdera2.common.text.Codec.java
private String _encode(String value, String charset) { if (value == null) return null; try {// w w w . j a v a 2s . c o m StringEncoder e = null; switch (this) { case Q: e = new QCodec(charset); break; case B: e = new BCodec(charset); break; default: e = new StarCodec(charset); break; } return e.encode(value); } catch (Exception e) { return value; } }