src/solaris/classes/sun/nio/fs/UnixUriUtils.java
Print this page
*** 23,34 ****
--- 23,37 ----
* questions.
*/
package sun.nio.fs;
+ import java.nio.file.Path;
+ import java.io.File;
import java.net.URI;
import java.net.URISyntaxException;
+ import java.util.Arrays;
/**
* Unix specific Path <--> URI conversion
*/
*** 36,46 ****
private UnixUriUtils() { }
/**
* Converts URI to Path
*/
! static UnixPath fromUri(UnixFileSystem fs, URI uri) {
if (!uri.isAbsolute())
throw new IllegalArgumentException("URI is not absolute");
if (uri.isOpaque())
throw new IllegalArgumentException("URI is not hierarchical");
String scheme = uri.getScheme();
--- 39,49 ----
private UnixUriUtils() { }
/**
* Converts URI to Path
*/
! static Path fromUri(UnixFileSystem fs, URI uri) {
if (!uri.isAbsolute())
throw new IllegalArgumentException("URI is not absolute");
if (uri.isOpaque())
throw new IllegalArgumentException("URI is not hierarchical");
String scheme = uri.getScheme();
*** 51,76 ****
if (uri.getFragment() != null)
throw new IllegalArgumentException("URI has a fragment component");
if (uri.getQuery() != null)
throw new IllegalArgumentException("URI has a query component");
! String path = uri.getPath();
! if (path.equals(""))
throw new IllegalArgumentException("URI path component is empty");
- if (path.endsWith("/") && (path.length() > 1)) {
- // "/foo/" --> "/foo", but "/" --> "/"
- path = path.substring(0, path.length() - 1);
- }
! // preserve bytes
! byte[] result = new byte[path.length()];
! for (int i=0; i<path.length(); i++) {
! byte v = (byte)(path.charAt(i));
! if (v == 0)
throw new IllegalArgumentException("Nul character not allowed");
! result[i] = v;
}
return new UnixPath(fs, result);
}
/**
* Converts Path to URI
--- 54,98 ----
if (uri.getFragment() != null)
throw new IllegalArgumentException("URI has a fragment component");
if (uri.getQuery() != null)
throw new IllegalArgumentException("URI has a query component");
! // compatability with java.io.File
! if (!uri.toString().startsWith("file:///"))
! return new File(uri).toPath();
!
! // transformation use raw path
! String p = uri.getRawPath();
! int len = p.length();
! if (len == 0)
throw new IllegalArgumentException("URI path component is empty");
! // transform escaped octets and unescaped characters to bytes
! if (p.endsWith("/") && len > 1)
! len--;
! byte[] result = new byte[len];
! int rlen = 0;
! int pos = 0;
! while (pos < len) {
! char c = p.charAt(pos++);
! byte b;
! if (c == '%') {
! assert (pos+2) <= len;
! char c1 = p.charAt(pos++);
! char c2 = p.charAt(pos++);
! b = (byte)(((decode(c1) & 0xf) << 4) | ((decode(c2) & 0xf)));
! if (b == 0)
throw new IllegalArgumentException("Nul character not allowed");
! } else {
! assert c < 0x80;
! b = (byte)c;
}
+ result[rlen++] = b;
+ }
+ if (rlen != result.length)
+ result = Arrays.copyOf(result, rlen);
+
return new UnixPath(fs, result);
}
/**
* Converts Path to URI
*** 84,94 ****
if (match(c, L_PATH, H_PATH)) {
sb.append(c);
} else {
sb.append('%');
sb.append(hexDigits[(c >> 4) & 0x0f]);
! sb.append(hexDigits[(c >> 0) & 0x0f]);
}
}
// trailing slash if directory
if (sb.charAt(sb.length()-1) != '/') {
--- 106,116 ----
if (match(c, L_PATH, H_PATH)) {
sb.append(c);
} else {
sb.append('%');
sb.append(hexDigits[(c >> 4) & 0x0f]);
! sb.append(hexDigits[(c) & 0x0f]);
}
}
// trailing slash if directory
if (sb.charAt(sb.length()-1) != '/') {
*** 162,171 ****
--- 184,204 ----
if (c < 128)
return ((1L << (c - 64)) & highMask) != 0;
return false;
}
+ // decode
+ private static int decode(char c) {
+ if ((c >= '0') && (c <= '9'))
+ return c - '0';
+ if ((c >= 'a') && (c <= 'f'))
+ return c - 'a' + 10;
+ if ((c >= 'A') && (c <= 'F'))
+ return c - 'A' + 10;
+ throw new AssertionError();
+ }
+
// digit = "0" | "1" | "2" | "3" | "4" | "5" | "6" | "7" |
// "8" | "9"
private static final long L_DIGIT = lowMask('0', '9');
private static final long H_DIGIT = 0L;