public static String decode(String e) { try { return decode(e, "utf-8"); } catch (Exception ex) { return e; } }
public static String encode(CharSequence s, Filter... filters) { try { if (s == null) return null; return encode(s, "utf-8", filters); } catch (UnsupportedEncodingException e) { return null; // shouldn't happen } }
public static String encode(CharSequence s, String enc, Filter... filters) throws UnsupportedEncodingException { if (s == null) return null; StringBuilder sb = new StringBuilder(); for (int n = 0; n < s.length(); n++) { char c = s.charAt(n); if (!CharUtils.isHighSurrogate(c) && check(c, filters)) { encode(sb, String.valueOf(c).getBytes(enc)); } else if (CharUtils.isHighSurrogate(c)) { if (check(c, filters)) { StringBuilder buf = new StringBuilder(); buf.append(c); buf.append(s.charAt(++n)); byte[] b = buf.toString().getBytes(enc); encode(sb, b); } else { sb.append(c); sb.append(s.charAt(++n)); } } else { sb.append(c); } } return sb.toString(); }
public static String encode(Reader reader, String enc, Filter... filters) throws IOException { StringBuilder sb = new StringBuilder(); char[] chunk = new char[1024]; int r = -1; while ((r = reader.read(chunk)) > -1) processChars(sb, CharBuffer.wrap(chunk, 0, r), enc, filters); return sb.toString(); }
@Override public IRI normalize(IRI iri) { StringBuilder buf = new StringBuilder(); int port = (iri.getPort() == getDefaultPort()) ? -1 : iri.getPort(); String host = iri.getHost(); if (host != null) host = host.toLowerCase(); String ui = iri.getUserInfo(); iri.buildAuthority(buf, ui, host, port); String authority = buf.toString(); return new IRI(iri._scheme, iri.getScheme(), authority, ui, host, port, IRI.normalize(iri.getPath()), UrlEncoding.encode(UrlEncoding.decode(iri.getQuery()), Profile.IQUERY.filter()), UrlEncoding .encode(UrlEncoding.decode(iri.getFragment()), Profile.IFRAGMENT.filter())); }
private static void processChars(StringBuilder sb, CharBuffer chars, String enc, Filter... filters) throws IOException { for (int n = 0; n < chars.length(); n++) { char c = chars.charAt(n); if (!CharUtils.isHighSurrogate(c) && check(c, filters)) { encode(sb, String.valueOf(c).getBytes(enc)); } else if (CharUtils.isHighSurrogate(c)) { if (check(c, filters)) { StringBuilder buf = new StringBuilder(); buf.append(c); buf.append(chars.charAt(++n)); byte[] b = buf.toString().getBytes(enc); encode(sb, b); } else { sb.append(c); sb.append(chars.charAt(++n)); } } else { sb.append(c); } } }
public static String encode(Readable readable, String enc, Filter... filters) throws IOException { StringBuilder sb = new StringBuilder(); CharBuffer chars = CharBuffer.allocate(1024); while (readable.read(chars) > -1) { chars.flip(); processChars(sb, chars, enc, filters); } return sb.toString(); }
public static String encode(CharSequence s, int offset, int length, Filter... filters) { try { if (s == null) return null; return encode(s, offset, length, "utf-8", filters); } catch (UnsupportedEncodingException e) { return null; // shouldn't happen } }
public static IRI normalize(IRI iri) { if (iri.isOpaque() || iri.getPath() == null) return iri; IRI normalized = null; if (iri._scheme != null) normalized = iri._scheme.normalize(iri); return (normalized != null) ? normalized : new IRI(iri._scheme, iri.getScheme(), iri.getAuthority(), iri .getUserInfo(), iri.getHost(), iri.getPort(), normalize(iri.getPath()), UrlEncoding.encode(UrlEncoding .decode(iri.getQuery()), Profile.IQUERY.filter()), UrlEncoding .encode(UrlEncoding.decode(iri.getFragment()), Profile.IFRAGMENT.filter())); }
private static String decode(String urlPath) { try { return UrlEncoding.decode(urlPath, "UTF-8"); } catch (UnsupportedEncodingException e) { throw new RuntimeException(e); } } }
public static String encode(char... chars) { return encode(chars, 0, chars.length, DEFAULT_ENCODING, new Filter[0]); }
private static String decode(String urlPath) { try { return UrlEncoding.decode(urlPath, "UTF-8"); } catch (UnsupportedEncodingException e) { throw new RuntimeException(e); } } }
public static String encode(char[] chars, int offset, int length, String enc, Filter Filter) { return encode(chars, offset, length, enc, new Filter[] {Filter}); }
/** * Get's the name of the specific resource requested */ protected String getResourceName(RequestContext request) { String path = request.getTargetPath(); int q = path.indexOf("?"); if (q != -1) { path = path.substring(0, q); } String[] segments = path.split("/"); String id = segments[segments.length - 1]; return UrlEncoding.decode(id); }
public static String encode(InputStream in, String charset, Filter Filter) throws IOException { return encode(in, charset, DEFAULT_ENCODING, new Filter[] {Filter}); }
public String[] getDecodedHeaders(String header) { Object[] headers = getHeaders(header); for (int n = 0; n < headers.length; n++) { headers[n] = UrlEncoding.decode(Rfc2047Helper.decode(headers[n].toString())); } return (String[])headers; }
public static String encode(Reader reader) throws IOException { return encode(reader, DEFAULT_ENCODING, new Filter[0]); }
public String[] getDecodedHeaders(String header) { Object[] headers = getHeaders(header); for (int n = 0; n < headers.length; n++) { headers[n] = UrlEncoding.decode(EncodingUtil.decode(headers[n].toString())); } return (String[])headers; }