📄 davutilities.java
字号:
output[11] = DAVUtilities.toHexDigit((int) (number >> 16));
output[12] = DAVUtilities.toHexDigit((int) (number >> 12));
output[13] = DAVUtilities.toHexDigit((int) (number >> 8));
output[14] = DAVUtilities.toHexDigit((int) (number >> 4));
output[15] = DAVUtilities.toHexDigit((int) (number));
return new String(output);
}
/**
* <p>
* Return the HEX representation of an integer.
* </p>
*
* @param number the int to convert in a HEX {@link String}.
* @return a <b>non-null</b> 8-characters {@link String} instance.
*/
public static String toHexString(int number) {
char output[] = new char[8];
output[0] = DAVUtilities.toHexDigit((int) (number >> 28));
output[1] = DAVUtilities.toHexDigit((int) (number >> 24));
output[2] = DAVUtilities.toHexDigit((int) (number >> 20));
output[3] = DAVUtilities.toHexDigit((int) (number >> 16));
output[4] = DAVUtilities.toHexDigit((int) (number >> 12));
output[5] = DAVUtilities.toHexDigit((int) (number >> 8));
output[6] = DAVUtilities.toHexDigit((int) (number >> 4));
output[7] = DAVUtilities.toHexDigit((int) (number));
return new String(output);
}
/**
* <p>
* Return the HEX representation of a char.
* </p>
*
* @param number the char to convert in a HEX {@link String}.
* @return a <b>non-null</b> 4-characters {@link String} instance.
*/
public static String toHexString(char number) {
char output[] = new char[4];
output[0] = DAVUtilities.toHexDigit((int) (number >> 12));
output[1] = DAVUtilities.toHexDigit((int) (number >> 8));
output[2] = DAVUtilities.toHexDigit((int) (number >> 4));
output[3] = DAVUtilities.toHexDigit((int) (number));
return new String(output);
}
/**
* <p>
* Return the HEX representation of a byte.
* </p>
*
* @param number the byte to convert in a HEX {@link String}.
* @return a <b>non-null</b> 2-characters {@link String} instance.
*/
public static String toHexString(byte number) {
char output[] = new char[2];
output[0] = DAVUtilities.toHexDigit((int) (number >> 4));
output[1] = DAVUtilities.toHexDigit((int) (number));
return new String(output);
}
/**
* <p>
* Return the single digit character representing the HEX encoding of the
* lower four bits of a given integer.
* </p>
*
* @param number number to conver
* @return hex character
*/
private static char toHexDigit(int number) {
switch (number & 0x0F) {
case 0x00:
return '0';
case 0x01:
return '1';
case 0x02:
return '2';
case 0x03:
return '3';
case 0x04:
return '4';
case 0x05:
return '5';
case 0x06:
return '6';
case 0x07:
return '7';
case 0x08:
return '8';
case 0x09:
return '9';
case 0x0A:
return 'A';
case 0x0B:
return 'B';
case 0x0C:
return 'C';
case 0x0D:
return 'D';
case 0x0E:
return 'E';
case 0x0F:
return 'F';
}
String message = "Invalid HEX digit " + Integer.toHexString(number);
throw new IllegalArgumentException(message);
}
/**
* Encode a path suitable for use in a URI. Forward slashes will not be encoded.
*
* @param path
* @return encoding path
*/
public static String encodePath(String path) {
return encodePath(path, false, "UTF-8");
}
public static String encodePath(String path, boolean encodeSlash) {
return encodePath(path, encodeSlash, "UTF-8");
}
public static String encodePath(String path, String charset) {
return encodePath(path, false, charset);
}
/**
* Process a URI for replacements and encode the result correctly
*
* @param uri
* @param session session info to use for replacements
* @return processed uri
* @throws MalformedURIException
*/
public static URI processAndEncodeURI(String uri, SessionInfo session, String charset) throws MalformedURIException {
// TODO We have problems with passwords containing @ characters here
String path = CoreUtil.doStandardReplacements(session, uri);
URI nuri = new URI(path);
if(nuri.getUserinfo() != null) {
nuri.setUserinfo(encodeURIUserInfo(nuri.getUserinfo()));
}
if(nuri.getPath() != null && !nuri.getPath().equals("")) {
nuri.setPath(encodePath(nuri.getPath(), charset));
}
return nuri;
}
// NOTE this method is for the password hack in prcess
public static URI processAndEncodeURI(String uri, SessionInfo session) throws MalformedURIException {
return processAndEncodeURI(uri, session, "UTF-8");
}
/**
* Encode a path suitable for use in a URI.
*
* @param path path
* @param encodeSlash encode forward slashes (/)
* @return encoded path
*/
public static String encodePath(String path, boolean encodeSlash, String charset) {
/* Encode the string */
StringBuffer buffer = new StringBuffer();
byte encoded[];
try {
if(charset==null)
encoded = path.getBytes();
else
encoded = path.getBytes(charset);
for (int x = 0; x < encoded.length; x++) {
if (((int) encoded[x] == '%' && encodeSlash) || ACCEPTABLE.indexOf((int) encoded[x]) < 0) {
buffer.append('%');
buffer.append(DAVUtilities.toHexString(encoded[x]));
continue;
}
buffer.append((char) encoded[x]);
}
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
return path;
}
return buffer.toString();
}
/**
* Encode the user info part of a URI
*
* @param uriUserInfo URI user info
* @return encoded URI user info
*/
public static String encodeURIUserInfo(String uriUserInfo) {
int idx = uriUserInfo.indexOf(':');
if(idx != -1) {
return Util.urlEncode(uriUserInfo.substring(0, idx)) + ":" +
Util.urlEncode(uriUserInfo.substring(idx + 1));
}
return Util.urlEncode(uriUserInfo);
}
/**
* Get ETAG
*
* @param path
* @param lastModified
* @return ETAG
*/
public static String getETAG(String path, Date lastModified) {
StringBuffer etag = new StringBuffer();
etag.append('"');
/* Append the MD5 hash of this resource name */
try {
MessageDigest digester = MessageDigest.getInstance("MD5");
digester.reset();
digester.update(path.getBytes("UTF8"));
etag.append(DAVUtilities.toHexString(digester.digest()));
etag.append('-');
} catch (Exception e) {
// If we can't get the MD5 HASH, let's ignore and hope...
}
/* Append the hashCode of this resource name */
etag.append(DAVUtilities.toHexString(path.hashCode()));
/* Append the last modification date if possible */
if (lastModified != null) {
etag.append('-');
etag.append(DAVUtilities.toHexString(lastModified.getTime()));
}
/* Close the ETag */
etag.append('"');
return (etag.toString());
}
/**
* Strip the first element in a path
*
* @param path
* @return new path
*/
public static String stripFirstPath(String path) {
if (path.length() < 1) {
return path;
}
int idx = path.indexOf('/', 1);
return idx == -1 ? path : path.substring(idx);
}
/**
* Strip all leading slashes
*
* @param path
* @return new path
*/
public static String stripLeadingSlash(String path) {
while (path != null && path.startsWith("/")) {
path = path.substring(1);
}
return path;
}
/**
* Strip all trailing slashes
*
* @param path
* @return new path
*/
public static String stripTrailingSlash(String path) {
while (path.endsWith("/")) {
path = path.substring(0, path.length() - 1);
}
return path;
}
/**
* Strips the directory from a file path (if any) using the specified
* character as a separator. If an empty path is supplied then an empty is
* returned.
*
* @param path path
* @param separator separator
* @return path
*/
public static String basename(String path, char separator) {
if (path.equals("")) {
return path;
}
while (path.endsWith(String.valueOf(separator))) {
path = path.substring(0, path.length() - 1);
}
int idx = path.lastIndexOf(separator);
return idx == -1 ? path : path.substring(idx + 1);
}
/**
* Concatent two paths
*
* @param original original path
* @param append path to append
* @return new path
*/
public static String concatenatePaths(String original, String append) {
if (append != null) {
if (original.endsWith("/")) {
original = original.concat(stripLeadingSlash(append));
} else {
if (append.startsWith("/")) {
original = original.concat(append);
} else {
original = original.concat("/".concat(append));
}
}
}
return original;
}
/**
* Get the parent path or <code>null</code> if at the root
*
* @param path
* @return parent path
*/
public static String getParentPath(String path) {
path = stripTrailingSlash(path);
String parent = null;
if (!path.equals("")) {
int idx = path.lastIndexOf("/");
if (idx == -1) {
parent = "/";
} else {
parent = path.substring(0, idx + 1);
}
}
return parent;
}
/**
* Strips any trailing slashes then returns anything up to last slash (i.e.
* the filename part is stripped leave the directory name). If the path is
* already /, <code>null</code> will be returned.
*
* @param path path
* @return dirname
*/
public static String dirname(String path) {
String s = stripTrailingSlash(path);
if (s.equals("")) {
return null;
}
return s.substring(0, s.lastIndexOf("/"));
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -