1
1
package android.net
2
2
3
+ import java.io.File
4
+ import java.io.UnsupportedEncodingException
3
5
import java.net.URI
4
6
import java.util.Collections
5
7
@@ -8,11 +10,105 @@ class Uri(private val uri: URI) {
8
10
companion object {
9
11
@JvmStatic
10
12
fun parse (uriString : String ) = Uri (URI .create(uriString))
13
+
14
+ @JvmStatic
15
+ fun encode (s : String? ): String? = encode(s, null )
16
+
17
+ @JvmStatic
18
+ fun encode (s : String? , allow : String? ): String? {
19
+ if (s == null ) {
20
+ return null
21
+ } else {
22
+ var encoded: StringBuilder ? = null
23
+ val oldLength = s.length
24
+
25
+ var nextAllowed: Int
26
+ var current = 0
27
+ while (current < oldLength) {
28
+ var nextToEncode = current
29
+ while (nextToEncode < oldLength && isAllowed(s[nextToEncode], allow)) {
30
+ ++ nextToEncode
31
+ }
32
+
33
+ if (nextToEncode == oldLength) {
34
+ if (current == 0 ) {
35
+ return s
36
+ }
37
+
38
+ encoded!! .append(s, current, oldLength)
39
+ return encoded.toString()
40
+ }
41
+
42
+ if (encoded == null ) {
43
+ encoded = StringBuilder ()
44
+ }
45
+
46
+ if (nextToEncode > current) {
47
+ encoded.append(s, current, nextToEncode)
48
+ }
49
+
50
+ current = nextToEncode
51
+
52
+ nextAllowed = current + 1
53
+ while (nextAllowed < oldLength && ! isAllowed(s[nextAllowed], allow)) {
54
+ ++ nextAllowed
55
+ }
56
+
57
+ val toEncode = s.substring(current, nextAllowed)
58
+
59
+ try {
60
+ val bytes = toEncode.toByteArray(charset(" UTF-8" ))
61
+ val bytesLength = bytes.size
62
+
63
+ for (i in 0 until bytesLength) {
64
+ encoded.append(' %' )
65
+ encoded.append(HEX_DIGITS [(bytes[i].toInt() and 240 ) shr 4 ])
66
+ encoded.append(HEX_DIGITS [bytes[i].toInt() and 15 ])
67
+ }
68
+ } catch (var11: UnsupportedEncodingException ) {
69
+ val e = var11
70
+ throw AssertionError (e)
71
+ }
72
+ current = nextAllowed
73
+ }
74
+
75
+ return encoded?.toString() ? : s
76
+ }
77
+ }
78
+
79
+ @JvmStatic
80
+ fun fromFile (file : File ? ): Uri {
81
+ TODO ()
82
+ }
83
+
84
+ @JvmStatic
85
+ fun fromParts (scheme : String? , ssp : String? , fragment : String? ): Uri {
86
+ TODO ()
87
+ }
88
+
89
+ @JvmStatic
90
+ fun decode (s : String? ): String? {
91
+ TODO ()
92
+ }
93
+
94
+ @JvmStatic
95
+ fun withAppendedPath (baseUri : Uri , pathSegment : String? ): Uri {
96
+ TODO ()
97
+ }
98
+
99
+ private fun isAllowed (c : Char , allow : String? ): Boolean {
100
+ return ((c >= ' A' && c <= ' Z' )
101
+ || (c >= ' a' && c <= ' z' )
102
+ || (c >= ' 0' && c <= ' 9' )
103
+ || (" _-!.~'()*" .indexOf(c) != NOT_FOUND
104
+ ) || (allow != null && allow.indexOf(c) != NOT_FOUND ))
105
+ }
11
106
}
12
107
13
108
val scheme get() = uri.scheme
14
109
val port get() = uri.port
15
110
val host get() = uri.host
111
+ val path get() = uri.path
16
112
17
113
fun getQueryParameterNames (): Set <String > {
18
114
val query: String = uri.query ? : return emptySet()
@@ -66,3 +162,8 @@ class Uri(private val uri: URI) {
66
162
return null
67
163
}
68
164
}
165
+
166
+ /* * Index of a component which was not found. */
167
+ private const val NOT_FOUND = - 1
168
+
169
+ private val HEX_DIGITS = " 0123456789ABCDEF" .toCharArray()
0 commit comments