Debian Codegraph @

bundles/org.eclipse.equinox.p2.engine/src/org/eclipse/equinox/internal/p2/engine/SlashEncode.java

raw
1 package org.eclipse.equinox.internal.p2.engine;
2
3 /**
4 * Utility class to encode forward slash characters so that strings containing
5 * forward slashes can be used as node names with secure preferences. It is
6 * the responsibility of the consumer to manually encode such strings before
7 * attempting to obtain corresponding nodes from secure preferences.
8 * <p>
9 * Internally, the class uses a subset of JIT encoding. The forward slashes
10 * and backward slashes are encoded.
11 * </p><p>
12 * This class is not intended to be instantiated or subclassed by users.
13 * </p>
14 */
15 final public class SlashEncode {
16
17 final private static char SLASH = '/';
18 final private static char BACK_SLASH = '\\';
19
20 final private static String ENCODED_SLASH = "\\2f"; //$NON-NLS-1$
21 final private static String ENCODED_BACK_SLASH = "\\5c"; //$NON-NLS-1$
22
23 static public String decode(String str) {
24 if (str == null)
25 return null;
26 int size = str.length();
27 if (size == 0)
28 return str;
29
30 StringBuilder processed = new StringBuilder(size);
31 int processedPos = 0;
32
33 for (int i = 0; i < size; i++) {
34 char c = str.charAt(i);
35 if (c == BACK_SLASH) {
36 if (i + 2 >= size)
37 continue;
38 String encoded = str.substring(i, i + 3);
39 char decoded = 0;
40 if (ENCODED_SLASH.equals(encoded))
41 decoded = SLASH;
42 else if (ENCODED_BACK_SLASH.equals(encoded))
43 decoded = BACK_SLASH;
44 if (decoded == 0)
45 continue;
46 if (i > processedPos)
47 processed.append(str.substring(processedPos, i));
48 processed.append(decoded);
49 processedPos = i + 3;
50 i += 2; // skip over remaining encoded portion
51 }
52 }
53 if (processedPos == 0)
54 return str;
55 if (processedPos < size)
56 processed.append(str.substring(processedPos));
57 return new String(processed);
58 }
59
60 static public String encode(String str) {
61 if (str == null)
62 return null;
63 int size = str.length();
64 if (size == 0)
65 return str;
66
67 StringBuilder processed = new StringBuilder(size);
68 int processedPos = 0;
69
70 for (int i = 0; i < size; i++) {
71 char c = str.charAt(i);
72 if (c == SLASH || c == BACK_SLASH) {
73 if (i > processedPos)
74 processed.append(str.substring(processedPos, i));
75 if (c == SLASH)
76 processed.append(ENCODED_SLASH);
77 else if (c == BACK_SLASH)
78 processed.append(ENCODED_BACK_SLASH);
79 processedPos = i + 1;
80 }
81 }
82 if (processedPos == 0)
83 return str;
84 if (processedPos < size)
85 processed.append(str.substring(processedPos));
86 return new String(processed);
87 }
88
89 }