public class Util extends Object
| Modifier and Type | Field and Description |
|---|---|
static boolean |
NO_SYMLINK
On Unix environment that cannot run "ln", set this to true.
|
static org.apache.commons.lang.time.FastDateFormat |
RFC822_DATETIME_FORMATTER |
static boolean |
SYMLINK_ESCAPEHATCH |
static org.apache.commons.lang.time.FastDateFormat |
XS_DATETIME_FORMATTER |
| Constructor and Description |
|---|
Util() |
| Modifier and Type | Method and Description |
|---|---|
static File |
changeExtension(File dst,
String ext)
Returns a file name by changing its extension.
|
static String |
combine(long n,
String suffix)
Deprecated.
Use individual localization methods instead.
See
Messages.Util_year(Object) for an example.
Deprecated since 2009-06-24, remove method after 2009-12-24. |
static void |
copyFile(File src,
File dst)
Copies a single file by using Ant.
|
static void |
copyStream(InputStream in,
OutputStream out) |
static void |
copyStream(Reader in,
Writer out) |
static void |
copyStreamAndClose(InputStream in,
OutputStream out) |
static void |
copyStreamAndClose(Reader in,
Writer out) |
static org.apache.tools.ant.types.FileSet |
createFileSet(File baseDir,
String includes) |
static org.apache.tools.ant.types.FileSet |
createFileSet(File baseDir,
String includes,
String excludes)
Creates Ant
FileSet with the base dir and include pattern. |
static <T> List<T> |
createSubList(Collection<?> source,
Class<T> type)
Create a sub-list by only picking up instances of the specified type.
|
static void |
createSymlink(File baseDir,
String targetPath,
String symlinkPath,
TaskListener listener)
Creates a symlink to targetPath at baseDir+symlinkPath.
|
static File |
createTempDir()
Creates a new temporary directory.
|
static void |
deleteContentsRecursive(File file)
Deletes the contents of the given directory (but not the directory itself)
recursively.
|
static void |
deleteFile(File f)
Deletes this file (and does not take no for an answer).
|
static void |
deleteRecursive(File dir) |
static void |
displayIOException(IOException e,
TaskListener listener)
On Windows, error messages for IOException aren't very helpful.
|
static String |
encode(String s)
Escapes non-ASCII characters in URL.
|
static String |
encodeRFC2396(String url)
Deprecated.
since 2008-05-13. This method is broken (see ISSUE#1666). It should probably
be removed but I'm not sure if it is considered part of the public API
that needs to be maintained for backwards compatibility.
Use
encode(String) instead. |
static String |
ensureEndsWith(String subject,
String suffix)
Ensure string ends with suffix
|
static String |
escape(String text)
Escapes HTML unsafe characters like <, & to the respective character entities.
|
static <T> List<T> |
filter(Iterable<?> base,
Class<T> type)
Creates a filtered sublist.
|
static <T> List<T> |
filter(List<?> base,
Class<T> type)
Creates a filtered sublist.
|
static String |
fixEmpty(String s)
Convert empty string to null.
|
static String |
fixEmptyAndTrim(String s)
Convert empty string to null, and trim whitespace.
|
static <T> Collection<T> |
fixNull(Collection<T> l) |
static <T> Iterable<T> |
fixNull(Iterable<T> l) |
static <T> List<T> |
fixNull(List<T> l) |
static <T> Set<T> |
fixNull(Set<T> l) |
static String |
fixNull(String s)
Convert null to "".
|
static byte[] |
fromHexString(String data) |
static String |
getDigestOf(File file)
Computes the MD5 digest of a file.
|
static String |
getDigestOf(InputStream source)
Computes MD5 digest of the given input stream.
|
static String |
getDigestOf(String text) |
static String |
getFileName(String filePath)
Cuts all the leading path portion and get just the file name.
|
static String |
getHostName()
Guesses the current host name.
|
static String |
getPastTimeString(long duration)
Get a human readable string representing strings like "xxx days ago",
which should be used to point to the occurrence of an event in the past.
|
static String |
getTimeSpanString(long duration)
Returns a human readable text of the time duration, for example "3 minutes 40 seconds".
|
static String |
getWin32ErrorMessage(int n)
Gets a human readable message for the given Win32 error code.
|
static String |
getWin32ErrorMessage(IOException e) |
static String |
getWin32ErrorMessage(Throwable e)
Extracts the Win32 error message from
Throwable if possible. |
static String |
intern(String s)
Null-safe String intern method.
|
static boolean |
isAbsoluteUri(String uri)
Deprecated.
Use
isAbsoluteOrSchemeRelativeUri instead if your goal is to prevent open redirects |
static boolean |
isOverridden(Class base,
Class derived,
String methodName,
Class... types)
Checks if the method defined on the base type with the given arguments
is overridden in the given derived type.
|
static boolean |
isRelativePath(String path)
A mostly accurate check of whether a path is a relative path or not.
|
static boolean |
isSafeToRedirectTo(String uri)
Return true iff the parameter does not denote an absolute URI and not a scheme-relative URI.
|
static boolean |
isSymlink(File file)
Checks if the given file represents a symlink.
|
static String |
join(Collection<?> strings,
String separator)
Concatenate multiple strings by inserting a separator.
|
static <T> List<T> |
join(Collection<? extends T>... items)
Combines all the given collections into a single list.
|
static String |
loadFile(File logfile)
Loads the contents of a file into a string.
|
static String |
loadFile(File logfile,
Charset charset) |
static Properties |
loadProperties(String properties)
Loads a key/value pair string as
Properties |
static String[] |
mapToEnv(Map<String,String> m)
Converts the map format of the environment variables to the K=V format in the array.
|
static int |
min(int x,
int... values) |
static String |
nullify(String v) |
static String |
rawEncode(String s)
Encode a single path component for use in an HTTP URL.
|
static String |
removeTrailingSlash(String s) |
static String |
replaceMacro(String s,
Map<String,String> properties)
Replaces the occurrence of '$key' by properties.get('key').
|
static String |
replaceMacro(String s,
VariableResolver<String> resolver)
Replaces the occurrence of '$key' by resolver.get('key').
|
static String |
resolveSymlink(File link)
Resolves symlink, if the given file is a symlink.
|
static String |
resolveSymlink(File link,
TaskListener listener)
Deprecated.
as of 1.456
Use
resolveSymlink(File) |
static File |
resolveSymlinkToFile(File link)
Resolves a symlink to the
File that points to. |
static String |
singleQuote(String s)
Surrounds by a single-quote.
|
static SecretKey |
toAes128Key(String s)
Converts a string into 128-bit AES key.
|
static String |
toHexString(byte[] bytes) |
static String |
toHexString(byte[] data,
int start,
int len) |
static String[] |
tokenize(String s) |
static String[] |
tokenize(String s,
String delimiter)
Tokenizes the text separated by delimiters.
|
static void |
touch(File file)
Creates an empty file.
|
static Number |
tryParseNumber(String numberStr,
Number defaultNumber)
Returns the parsed string if parsed successful; otherwise returns the default number.
|
static String |
wrapToErrorSpan(String s)
Wraps with the error icon and the CSS class to render error message.
|
static String |
xmlEscape(String text) |
public static final org.apache.commons.lang.time.FastDateFormat XS_DATETIME_FORMATTER
public static final org.apache.commons.lang.time.FastDateFormat RFC822_DATETIME_FORMATTER
public static boolean NO_SYMLINK
public static boolean SYMLINK_ESCAPEHATCH
@Nonnull public static <T> List<T> filter(@Nonnull Iterable<?> base, @Nonnull Class<T> type)
@Nonnull public static <T> List<T> filter(@Nonnull List<?> base, @Nonnull Class<T> type)
@Nullable public static String replaceMacro(@CheckForNull String s, @Nonnull Map<String,String> properties)
Unlike shell, undefined variables are left as-is (this behavior is the same as Ant.)
@Nullable public static String replaceMacro(@CheckForNull String s, @Nonnull VariableResolver<String> resolver)
Unlike shell, undefined variables are left as-is (this behavior is the same as Ant.)
@Nonnull public static String loadFile(@Nonnull File logfile) throws IOException
IOException@Nonnull public static String loadFile(@Nonnull File logfile, @Nonnull Charset charset) throws IOException
IOExceptionpublic static void deleteContentsRecursive(@Nonnull File file) throws IOException
IOException - if the operation fails.public static void deleteFile(@Nonnull File f) throws IOException
f - a file to deleteIOException - if it exists but could not be successfully deletedpublic static void deleteRecursive(@Nonnull File dir) throws IOException
IOExceptionpublic static boolean isSymlink(@Nonnull File file) throws IOException
IOExceptionpublic static boolean isRelativePath(String path)
path - the path.true if the path looks relative.public static File createTempDir() throws IOException
IOExceptionpublic static void displayIOException(@Nonnull IOException e, @Nonnull TaskListener listener)
@CheckForNull public static String getWin32ErrorMessage(@Nonnull IOException e)
@CheckForNull public static String getWin32ErrorMessage(Throwable e)
Throwable if possible.@CheckForNull public static String getWin32ErrorMessage(int n)
public static void copyStream(@Nonnull InputStream in, @Nonnull OutputStream out) throws IOException
IOExceptionpublic static void copyStream(@Nonnull Reader in, @Nonnull Writer out) throws IOException
IOExceptionpublic static void copyStreamAndClose(@Nonnull InputStream in, @Nonnull OutputStream out) throws IOException
IOExceptionpublic static void copyStreamAndClose(@Nonnull Reader in, @Nonnull Writer out) throws IOException
IOException@Nonnull public static String[] tokenize(@Nonnull String s, @CheckForNull String delimiter)
In 1.210, this method was changed to handle quotes like Unix shell does.
Before that, this method just used StringTokenizer.
QuotedStringTokenizer@Nonnull public static String[] mapToEnv(@Nonnull Map<String,String> m)
public static int min(int x,
@Nonnull
int... values)
@CheckForNull public static String nullify(@CheckForNull String v)
@Nullable public static String ensureEndsWith(@CheckForNull String subject, @CheckForNull String suffix)
subject - Examined stringsuffix - Desired suffix@Nonnull public static String getDigestOf(@Nonnull InputStream source) throws IOException
source - The stream will be closed by this method at the end of this method.IOExceptionDigestUtils.md5Hex(InputStream)@Nonnull public static String getDigestOf(@Nonnull File file) throws IOException
file - a fileIOException - in case reading fails@Nonnull public static SecretKey toAes128Key(@Nonnull String s)
@Nonnull public static String getTimeSpanString(long duration)
duration - number of milliseconds.@Nonnull public static String getPastTimeString(long duration)
@Nonnull @Deprecated public static String combine(long n, @Nonnull String suffix)
Messages.Util_year(Object) for an example.
Deprecated since 2009-06-24, remove method after 2009-12-24.@Nonnull public static <T> List<T> createSubList(@Nonnull Collection<?> source, @Nonnull Class<T> type)
@Nonnull public static String encode(@Nonnull String s)
Note that this methods only escapes non-ASCII but leaves other URL-unsafe characters,
such as '#'.
rawEncode(String) should generally be used instead, though be careful to pass only
a single path component to that method (it will encode /, but this method does not).
@Nonnull public static String rawEncode(@Nonnull String s)
"#%<>[\]^`{|}~)
and HTTP special characters (/;:?) as specified in RFC1738.
(so alphanumeric and !@$&*()-_=+',. are not encoded)
Note that slash (/) is encoded, so the given string should be a
single path component used in constructing a URL.
Method name inspired by PHP's rawurlencode.@Nonnull public static String escape(@Nonnull String text)
public static void touch(@Nonnull File file) throws IOException
IOExceptionpublic static void copyFile(@Nonnull File src, @Nonnull File dst) throws org.apache.tools.ant.BuildException
org.apache.tools.ant.BuildException@Nonnull public static String fixNull(@CheckForNull String s)
@CheckForNull public static String fixEmpty(@CheckForNull String s)
@CheckForNull public static String fixEmptyAndTrim(@CheckForNull String s)
@Nonnull public static <T> List<T> fixNull(@CheckForNull List<T> l)
@Nonnull public static <T> Set<T> fixNull(@CheckForNull Set<T> l)
@Nonnull public static <T> Collection<T> fixNull(@CheckForNull Collection<T> l)
@Nonnull public static <T> Iterable<T> fixNull(@CheckForNull Iterable<T> l)
@Nonnull public static String getFileName(@Nonnull String filePath)
@Nonnull public static String join(@Nonnull Collection<?> strings, @Nonnull String separator)
@Nonnull public static <T> List<T> join(@Nonnull Collection<? extends T>... items)
@Nonnull public static org.apache.tools.ant.types.FileSet createFileSet(@Nonnull File baseDir, @Nonnull String includes, @CheckForNull String excludes)
FileSet with the base dir and include pattern.
The difference with this and using AbstractFileSet.setIncludes(String)
is that this method doesn't treat whitespace as a pattern separator,
which makes it impossible to use space in the file path.
includes - String like "foo/bar/*.xml" Multiple patterns can be separated
by ',', and whitespace can surround ',' (so that you can write
"abc, def" and "abc,def" to mean the same thing.excludes - Exclusion pattern. Follows the same format as the 'includes' parameter.
Can be null.@Nonnull public static org.apache.tools.ant.types.FileSet createFileSet(@Nonnull File baseDir, @Nonnull String includes)
public static void createSymlink(@Nonnull File baseDir, @Nonnull String targetPath, @Nonnull String symlinkPath, @Nonnull TaskListener listener) throws InterruptedException
If there's a prior symlink at baseDir+symlinkPath, it will be overwritten.
baseDir - Base directory to resolve the 'symlinkPath' parameter.targetPath - The file that the symlink should point to. Usually relative to the directory of the symlink but may instead be an absolute path.symlinkPath - Where to create a symlink in (relative to baseDir)InterruptedException@Deprecated public static String resolveSymlink(File link, TaskListener listener) throws InterruptedException, IOException
resolveSymlink(File)InterruptedExceptionIOException@CheckForNull public static File resolveSymlinkToFile(@Nonnull File link) throws InterruptedException, IOException
File that points to.InterruptedExceptionIOException@CheckForNull public static String resolveSymlink(@Nonnull File link) throws InterruptedException, IOException
If the resolution fails, report an error.
InterruptedExceptionIOException@Deprecated public static String encodeRFC2396(String url)
encode(String) instead.@Nonnull public static String wrapToErrorSpan(@Nonnull String s)
@CheckForNull public static Number tryParseNumber(@CheckForNull String numberStr, @CheckForNull Number defaultNumber)
numberStr - string to parsedefaultNumber - number to return if the string can not be parsedpublic static boolean isOverridden(@Nonnull Class base, @Nonnull Class derived, @Nonnull String methodName, @Nonnull Class... types)
@Nonnull public static File changeExtension(@Nonnull File dst, @Nonnull String ext)
ext - For example, ".zip"@Nullable public static String intern(@CheckForNull String s)
@Deprecated @RestrictedSince(value="1.651.2 / 2.TODO") @Restricted(value=org.kohsuke.accmod.restrictions.NoExternalUse.class) public static boolean isAbsoluteUri(@Nonnull String uri)
isAbsoluteOrSchemeRelativeUri instead if your goal is to prevent open redirectsURI, but
implementing this by ourselves allow it to be more lenient about
escaping of URI.public static boolean isSafeToRedirectTo(@Nonnull String uri)
@Nonnull public static Properties loadProperties(@Nonnull String properties) throws IOException
PropertiesIOExceptionCopyright © 2019. All rights reserved.