1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17 package org.yaml.snakeyaml.util;
18
19 import java.io.UnsupportedEncodingException;
20 import java.net.URLDecoder;
21 import java.nio.ByteBuffer;
22 import java.nio.CharBuffer;
23 import java.nio.charset.CharacterCodingException;
24 import java.nio.charset.Charset;
25 import java.nio.charset.CharsetDecoder;
26 import java.nio.charset.CodingErrorAction;
27
28 import org.yaml.snakeyaml.error.YAMLException;
29 import org.yaml.snakeyaml.external.com.google.gdata.util.common.base.Escaper;
30 import org.yaml.snakeyaml.external.com.google.gdata.util.common.base.PercentEscaper;
31
32 public abstract class UriEncoder {
33 private static final CharsetDecoder UTF8Decoder = Charset.forName("UTF-8").newDecoder()
34 .onMalformedInput(CodingErrorAction.REPORT);
35
36
37
38 private static final String SAFE_CHARS = PercentEscaper.SAFEPATHCHARS_URLENCODER + "[]/";
39 private static final Escaper escaper = new PercentEscaper(SAFE_CHARS, false);
40
41
42
43
44 public static String encode(String uri) {
45 return escaper.escape(uri);
46 }
47
48
49
50
51 public static String decode(ByteBuffer buff) throws CharacterCodingException {
52 CharBuffer chars = UTF8Decoder.decode(buff);
53 return chars.toString();
54 }
55
56 public static String decode(String buff) {
57 try {
58 return URLDecoder.decode(buff, "UTF-8");
59 } catch (UnsupportedEncodingException e) {
60 throw new YAMLException(e);
61 }
62 }
63 }