~ubuntu-branches/ubuntu/trusty/monodevelop/trusty-proposed

« back to all changes in this revision

Viewing changes to external/ikvm/openjdk/sun/nio/fs/UnixUriUtils.java

  • Committer: Package Import Robot
  • Author(s): Jo Shields
  • Date: 2013-05-12 09:46:03 UTC
  • mto: This revision was merged to the branch mainline in revision 29.
  • Revision ID: package-import@ubuntu.com-20130512094603-mad323bzcxvmcam0
Tags: upstream-4.0.5+dfsg
ImportĀ upstreamĀ versionĀ 4.0.5+dfsg

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
 * Copyright (c) 2008, 2011, Oracle and/or its affiliates. All rights reserved.
 
3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
 
4
 *
 
5
 * This code is free software; you can redistribute it and/or modify it
 
6
 * under the terms of the GNU General Public License version 2 only, as
 
7
 * published by the Free Software Foundation.  Oracle designates this
 
8
 * particular file as subject to the "Classpath" exception as provided
 
9
 * by Oracle in the LICENSE file that accompanied this code.
 
10
 *
 
11
 * This code is distributed in the hope that it will be useful, but WITHOUT
 
12
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
 
13
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
 
14
 * version 2 for more details (a copy is included in the LICENSE file that
 
15
 * accompanied this code).
 
16
 *
 
17
 * You should have received a copy of the GNU General Public License version
 
18
 * 2 along with this work; if not, write to the Free Software Foundation,
 
19
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
 
20
 *
 
21
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
 
22
 * or visit www.oracle.com if you need additional information or have any
 
23
 * questions.
 
24
 */
 
25
 
 
26
package sun.nio.fs;
 
27
 
 
28
import java.nio.file.Path;
 
29
import java.io.File;
 
30
import java.net.URI;
 
31
import java.net.URISyntaxException;
 
32
import java.nio.file.Path;
 
33
import java.util.Arrays;
 
34
 
 
35
/**
 
36
 * Unix specific Path <--> URI conversion
 
37
 */
 
38
 
 
39
class UnixUriUtils {
 
40
    private UnixUriUtils() { }
 
41
 
 
42
    /**
 
43
     * Converts URI to Path
 
44
     */
 
45
    static Path fromUri(NetFileSystem fs, URI uri) {
 
46
        if (!uri.isAbsolute())
 
47
            throw new IllegalArgumentException("URI is not absolute");
 
48
        if (uri.isOpaque())
 
49
            throw new IllegalArgumentException("URI is not hierarchical");
 
50
        String scheme = uri.getScheme();
 
51
        if ((scheme == null) || !scheme.equalsIgnoreCase("file"))
 
52
            throw new IllegalArgumentException("URI scheme is not \"file\"");
 
53
        if (uri.getAuthority() != null)
 
54
            throw new IllegalArgumentException("URI has an authority component");
 
55
        if (uri.getFragment() != null)
 
56
            throw new IllegalArgumentException("URI has a fragment component");
 
57
        if (uri.getQuery() != null)
 
58
            throw new IllegalArgumentException("URI has a query component");
 
59
 
 
60
        // compatability with java.io.File
 
61
        if (!uri.toString().startsWith("file:///"))
 
62
            return new File(uri).toPath();
 
63
 
 
64
        // transformation use raw path
 
65
        String p = uri.getRawPath();
 
66
        int len = p.length();
 
67
        if (len == 0)
 
68
            throw new IllegalArgumentException("URI path component is empty");
 
69
 
 
70
        // transform escaped octets and unescaped characters to bytes
 
71
        if (p.endsWith("/") && len > 1)
 
72
            p = p.substring(0, len - 1);
 
73
 
 
74
        return new NetPath(fs, p);
 
75
    }
 
76
 
 
77
    /**
 
78
     * Converts Path to URI
 
79
     */
 
80
    static URI toUri(NetPath up) {
 
81
        byte[] path = up.toAbsolutePath().toString().getBytes();
 
82
        StringBuilder sb = new StringBuilder("file:///");
 
83
        assert path[0] == '/';
 
84
        for (int i=1; i<path.length; i++) {
 
85
            char c = (char)(path[i] & 0xff);
 
86
            if (match(c, L_PATH, H_PATH)) {
 
87
                sb.append(c);
 
88
            } else {
 
89
               sb.append('%');
 
90
               sb.append(hexDigits[(c >> 4) & 0x0f]);
 
91
               sb.append(hexDigits[(c) & 0x0f]);
 
92
            }
 
93
        }
 
94
 
 
95
        // trailing slash if directory
 
96
        if (sb.charAt(sb.length()-1) != '/') {
 
97
            try {
 
98
                 if (cli.System.IO.Directory.Exists(up.path))
 
99
                     sb.append('/');
 
100
            } catch (Throwable x) {
 
101
                // ignore
 
102
            }
 
103
        }
 
104
 
 
105
        try {
 
106
            return new URI(sb.toString());
 
107
        } catch (URISyntaxException x) {
 
108
            throw new AssertionError(x);  // should not happen
 
109
        }
 
110
    }
 
111
 
 
112
    // The following is copied from java.net.URI
 
113
 
 
114
    // Compute the low-order mask for the characters in the given string
 
115
    private static long lowMask(String chars) {
 
116
        int n = chars.length();
 
117
        long m = 0;
 
118
        for (int i = 0; i < n; i++) {
 
119
            char c = chars.charAt(i);
 
120
            if (c < 64)
 
121
                m |= (1L << c);
 
122
        }
 
123
        return m;
 
124
    }
 
125
 
 
126
    // Compute the high-order mask for the characters in the given string
 
127
    private static long highMask(String chars) {
 
128
        int n = chars.length();
 
129
        long m = 0;
 
130
        for (int i = 0; i < n; i++) {
 
131
            char c = chars.charAt(i);
 
132
            if ((c >= 64) && (c < 128))
 
133
                m |= (1L << (c - 64));
 
134
        }
 
135
        return m;
 
136
    }
 
137
 
 
138
    // Compute a low-order mask for the characters
 
139
    // between first and last, inclusive
 
140
    private static long lowMask(char first, char last) {
 
141
        long m = 0;
 
142
        int f = Math.max(Math.min(first, 63), 0);
 
143
        int l = Math.max(Math.min(last, 63), 0);
 
144
        for (int i = f; i <= l; i++)
 
145
            m |= 1L << i;
 
146
        return m;
 
147
    }
 
148
 
 
149
    // Compute a high-order mask for the characters
 
150
    // between first and last, inclusive
 
151
    private static long highMask(char first, char last) {
 
152
        long m = 0;
 
153
        int f = Math.max(Math.min(first, 127), 64) - 64;
 
154
        int l = Math.max(Math.min(last, 127), 64) - 64;
 
155
        for (int i = f; i <= l; i++)
 
156
            m |= 1L << i;
 
157
        return m;
 
158
    }
 
159
 
 
160
    // Tell whether the given character is permitted by the given mask pair
 
161
    private static boolean match(char c, long lowMask, long highMask) {
 
162
        if (c < 64)
 
163
            return ((1L << c) & lowMask) != 0;
 
164
        if (c < 128)
 
165
            return ((1L << (c - 64)) & highMask) != 0;
 
166
        return false;
 
167
    }
 
168
 
 
169
    // decode
 
170
    private static int decode(char c) {
 
171
        if ((c >= '0') && (c <= '9'))
 
172
            return c - '0';
 
173
        if ((c >= 'a') && (c <= 'f'))
 
174
            return c - 'a' + 10;
 
175
        if ((c >= 'A') && (c <= 'F'))
 
176
            return c - 'A' + 10;
 
177
        throw new AssertionError();
 
178
    }
 
179
 
 
180
    // digit    = "0" | "1" | "2" | "3" | "4" | "5" | "6" | "7" |
 
181
    //            "8" | "9"
 
182
    private static final long L_DIGIT = lowMask('0', '9');
 
183
    private static final long H_DIGIT = 0L;
 
184
 
 
185
    // upalpha  = "A" | "B" | "C" | "D" | "E" | "F" | "G" | "H" | "I" |
 
186
    //            "J" | "K" | "L" | "M" | "N" | "O" | "P" | "Q" | "R" |
 
187
    //            "S" | "T" | "U" | "V" | "W" | "X" | "Y" | "Z"
 
188
    private static final long L_UPALPHA = 0L;
 
189
    private static final long H_UPALPHA = highMask('A', 'Z');
 
190
 
 
191
    // lowalpha = "a" | "b" | "c" | "d" | "e" | "f" | "g" | "h" | "i" |
 
192
    //            "j" | "k" | "l" | "m" | "n" | "o" | "p" | "q" | "r" |
 
193
    //            "s" | "t" | "u" | "v" | "w" | "x" | "y" | "z"
 
194
    private static final long L_LOWALPHA = 0L;
 
195
    private static final long H_LOWALPHA = highMask('a', 'z');
 
196
 
 
197
    // alpha         = lowalpha | upalpha
 
198
    private static final long L_ALPHA = L_LOWALPHA | L_UPALPHA;
 
199
    private static final long H_ALPHA = H_LOWALPHA | H_UPALPHA;
 
200
 
 
201
    // alphanum      = alpha | digit
 
202
    private static final long L_ALPHANUM = L_DIGIT | L_ALPHA;
 
203
    private static final long H_ALPHANUM = H_DIGIT | H_ALPHA;
 
204
 
 
205
    // mark          = "-" | "_" | "." | "!" | "~" | "*" | "'" |
 
206
    //                 "(" | ")"
 
207
    private static final long L_MARK = lowMask("-_.!~*'()");
 
208
    private static final long H_MARK = highMask("-_.!~*'()");
 
209
 
 
210
    // unreserved    = alphanum | mark
 
211
    private static final long L_UNRESERVED = L_ALPHANUM | L_MARK;
 
212
    private static final long H_UNRESERVED = H_ALPHANUM | H_MARK;
 
213
 
 
214
    // pchar         = unreserved | escaped |
 
215
    //                 ":" | "@" | "&" | "=" | "+" | "$" | ","
 
216
    private static final long L_PCHAR
 
217
        = L_UNRESERVED | lowMask(":@&=+$,");
 
218
    private static final long H_PCHAR
 
219
        = H_UNRESERVED | highMask(":@&=+$,");
 
220
 
 
221
   // All valid path characters
 
222
   private static final long L_PATH = L_PCHAR | lowMask(";/");
 
223
   private static final long H_PATH = H_PCHAR | highMask(";/");
 
224
 
 
225
   private final static char[] hexDigits = {
 
226
        '0', '1', '2', '3', '4', '5', '6', '7',
 
227
        '8', '9', 'A', 'B', 'C', 'D', 'E', 'F'
 
228
    };
 
229
}