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