JBoss VFS 3.0.1.GA

org.jboss.vfs
Class VFSUtils

java.lang.Object
  extended by org.jboss.vfs.VFSUtils

public class VFSUtils
extends Object

VFS Utilities

Version:
$Revision: 1.1 $
Author:
Adrian Brock, Ales Justin, David M. Lloyd

Field Summary
static int DEFAULT_BUFFER_SIZE
          The default buffer size to use for copies
static String VFS_PROTOCOL
          Constant representing the URL vfs protocol
 
Method Summary
static void addManifestLocations(VirtualFile file, List<VirtualFile> paths)
          Add manifest paths
static void copyChildrenRecursive(VirtualFile original, VirtualFile target)
          Copy all the children from the original VirtualFile the target recursively.
static void copyStream(InputStream is, OutputStream os)
          Copy input stream to output stream without closing streams.
static void copyStream(InputStream is, OutputStream os, int bufferSize)
          Copy input stream to output stream without closing streams.
static void copyStreamAndClose(InputStream is, OutputStream os)
          Copy input stream to output stream and close them both
static void copyStreamAndClose(InputStream is, OutputStream os, int bufferSize)
          Copy input stream to output stream and close them both
static InputStream createJarFileInputStream(VirtualFile virtualFile)
          Get an input stream that will always be consumable as a Zip/Jar file.
static String decode(String path)
          Decode the path with UTF-8 encoding..
static String decode(String path, String encoding)
          Decode the path.
static InputStream emptyStream()
          Get the empty input stream.
static String fixName(String name)
          Fix a name (removes any trailing slash)
static Pattern getGlobPattern(String glob)
          Get a regular expression pattern which matches any path names which match the given glob.
static Manifest getManifest(VirtualFile archive)
          Get a manifest from a virtual file, assuming the virtual file is the root of an archive
static File getMountSource(Closeable handle)
          Return the mount source File for a given mount handle.
static String getName(URI uri)
          Get the name.
static String getPathsString(Collection<VirtualFile> paths)
          Get the paths string for a collection of virtual files
static URI getPhysicalURI(VirtualFile file)
          Get a physical URI for a virtual file.
static URL getPhysicalURL(VirtualFile file)
          Get a physical URL for a virtual file.
static URI getVirtualURI(VirtualFile file)
          Get the virtual URI for a virtual file.
static URL getVirtualURL(VirtualFile file)
          Get the virtual URL for a virtual file.
static Map<String,String> parseURLQuery(String query)
          Take a URL.getQuery string and parse it into name=value pairs
static Manifest readManifest(VirtualFile manifest)
          Read the manifest from given manifest VirtualFile.
static void recursiveCopy(File original, File destDir)
          Recursively copy a file or directory from one location to another.
static void recursiveCopy(File original, VirtualFile destDir)
          Recursively copy a file or directory from one location to another.
static void recursiveCopy(VirtualFile original, File destDir)
          Recursively copy a file or directory from one location to another.
static void recursiveCopy(VirtualFile original, VirtualFile destDir)
          Recursively copy a file or directory from one location to another.
static boolean recursiveDelete(File root)
          Attempt to recursively delete a real file.
static boolean recursiveDelete(VirtualFile root)
          Attempt to recursively delete a virtual file.
static void safeClose(Closeable... closeables)
          Safely close some resource without throwing an exception.
static void safeClose(Closeable c)
          Safely close some resource without throwing an exception.
static void safeClose(Iterable<? extends Closeable> ci)
          Safely close some resources without throwing an exception.
static void safeClose(ZipFile zipFile)
          Safely close some resource without throwing an exception.
static URL sanitizeURL(URL url)
          Ensure the url is convertible to URI by encoding spaces and percent characters if necessary
static URI toURI(URL url)
          Deal with urls that may include spaces.
static void unzip(File zipFile, File destDir)
          Expand a zip file to a destination directory.
static void writeFile(VirtualFile virtualFile, byte[] bytes)
          Write the given bytes to the given virtual file, replacing its current contents (if any) or creating a new file if one does not exist.
static void writeFile(VirtualFile virtualFile, InputStream is)
          Write the content from the given InputStream to the given virtual file, replacing its current contents (if any) or creating a new file if one does not exist.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

VFS_PROTOCOL

public static final String VFS_PROTOCOL
Constant representing the URL vfs protocol

See Also:
Constant Field Values

DEFAULT_BUFFER_SIZE

public static final int DEFAULT_BUFFER_SIZE
The default buffer size to use for copies

See Also:
Constant Field Values
Method Detail

getPathsString

public static String getPathsString(Collection<VirtualFile> paths)
Get the paths string for a collection of virtual files

Parameters:
paths - the paths
Returns:
the string
Throws:
IllegalArgumentException - for null paths

addManifestLocations

public static void addManifestLocations(VirtualFile file,
                                        List<VirtualFile> paths)
                                 throws IOException
Add manifest paths

Parameters:
file - the file
paths - the paths to add to
Throws:
IOException - if there is an error reading the manifest or the virtual file is closed
IllegalStateException - if the file has no parent
IllegalArgumentException - for a null file or paths

getManifest

public static Manifest getManifest(VirtualFile archive)
                            throws IOException
Get a manifest from a virtual file, assuming the virtual file is the root of an archive

Parameters:
archive - the root the archive
Returns:
the manifest or null if not found
Throws:
IOException - if there is an error reading the manifest or the virtual file is closed
IllegalArgumentException - for a null archive

readManifest

public static Manifest readManifest(VirtualFile manifest)
                             throws IOException
Read the manifest from given manifest VirtualFile.

Parameters:
manifest - the VF to read from
Returns:
JAR's manifest
Throws:
IOException - if problems while opening VF stream occur

fixName

public static String fixName(String name)
Fix a name (removes any trailing slash)

Parameters:
name - the name to fix
Returns:
the fixed name
Throws:
IllegalArgumentException - for a null name

decode

public static String decode(String path)
Decode the path with UTF-8 encoding..

Parameters:
path - the path to decode
Returns:
decoded path

decode

public static String decode(String path,
                            String encoding)
Decode the path.

Parameters:
path - the path to decode
encoding - the encoding
Returns:
decoded path

getName

public static String getName(URI uri)
Get the name.

Parameters:
uri - the uri
Returns:
name from uri's path

parseURLQuery

public static Map<String,String> parseURLQuery(String query)
Take a URL.getQuery string and parse it into name=value pairs

Parameters:
query - Possibly empty/null url query string
Returns:
String[] for the name/value pairs in the query. May be empty but never null.

toURI

public static URI toURI(URL url)
                 throws URISyntaxException
Deal with urls that may include spaces.

Parameters:
url - the url
Returns:
uri the uri
Throws:
URISyntaxException - for any error

sanitizeURL

public static URL sanitizeURL(URL url)
                       throws URISyntaxException,
                              MalformedURLException
Ensure the url is convertible to URI by encoding spaces and percent characters if necessary

Parameters:
url - to be sanitized
Returns:
sanitized URL
Throws:
URISyntaxException - if URI conversion can't be fixed
MalformedURLException - if an error occurs

copyChildrenRecursive

public static void copyChildrenRecursive(VirtualFile original,
                                         VirtualFile target)
                                  throws IOException
Copy all the children from the original VirtualFile the target recursively.

Parameters:
original - the file to copy children from
target - the file to copy the children to
Throws:
IOException - if any problems occur copying the files

copyStreamAndClose

public static void copyStreamAndClose(InputStream is,
                                      OutputStream os)
                               throws IOException
Copy input stream to output stream and close them both

Parameters:
is - input stream
os - output stream
Throws:
IOException - for any error

copyStreamAndClose

public static void copyStreamAndClose(InputStream is,
                                      OutputStream os,
                                      int bufferSize)
                               throws IOException
Copy input stream to output stream and close them both

Parameters:
is - input stream
os - output stream
bufferSize - the buffer size to use
Throws:
IOException - for any error

copyStream

public static void copyStream(InputStream is,
                              OutputStream os)
                       throws IOException
Copy input stream to output stream without closing streams. Flushes output stream when done.

Parameters:
is - input stream
os - output stream
Throws:
IOException - for any error

copyStream

public static void copyStream(InputStream is,
                              OutputStream os,
                              int bufferSize)
                       throws IOException
Copy input stream to output stream without closing streams. Flushes output stream when done.

Parameters:
is - input stream
os - output stream
bufferSize - the buffer size to use
Throws:
IOException - for any error

writeFile

public static void writeFile(VirtualFile virtualFile,
                             byte[] bytes)
                      throws IOException
Write the given bytes to the given virtual file, replacing its current contents (if any) or creating a new file if one does not exist.

Parameters:
virtualFile - the virtual file to write
bytes - the bytes
Throws:
IOException - if an error occurs

writeFile

public static void writeFile(VirtualFile virtualFile,
                             InputStream is)
                      throws IOException
Write the content from the given InputStream to the given virtual file, replacing its current contents (if any) or creating a new file if one does not exist.

Parameters:
virtualFile - the virtual file to write
is - the input stream
Throws:
IOException - if an error occurs

getVirtualURL

public static URL getVirtualURL(VirtualFile file)
                         throws MalformedURLException
Get the virtual URL for a virtual file. This URL can be used to access the virtual file; however, taking the file part of the URL and attempting to use it with the File class may fail if the file is not present on the physical filesystem, and in general should not be attempted. Note: if the given VirtualFile refers to a directory at the time of this method invocation, a trailing slash will be appended to the URL; this means that invoking this method may require a filesystem access, and in addition, may not produce consistent results over time.

Parameters:
file - the virtual file
Returns:
the URL
Throws:
MalformedURLException - if the file cannot be coerced into a URL for some reason
See Also:
VirtualFile.asDirectoryURL(), VirtualFile.asFileURL()

getVirtualURI

public static URI getVirtualURI(VirtualFile file)
                         throws URISyntaxException
Get the virtual URI for a virtual file. Note: if the given VirtualFile refers to a directory at the time of this method invocation, a trailing slash will be appended to the URI; this means that invoking this method may require a filesystem access, and in addition, may not produce consistent results over time.

Parameters:
file - the virtual file
Returns:
the URI
Throws:
URISyntaxException - if the file cannot be coerced into a URI for some reason
See Also:
VirtualFile.asDirectoryURI(), VirtualFile.asFileURI()

getPhysicalURL

public static URL getPhysicalURL(VirtualFile file)
                          throws IOException
Get a physical URL for a virtual file. See the warnings on the VirtualFile.getPhysicalFile() method before using this method.

Parameters:
file - the virtual file
Returns:
the physical file URL
Throws:
IOException - if an I/O error occurs getting the physical file

getPhysicalURI

public static URI getPhysicalURI(VirtualFile file)
                          throws IOException
Get a physical URI for a virtual file. See the warnings on the VirtualFile.getPhysicalFile() method before using this method.

Parameters:
file - the virtual file
Returns:
the physical file URL
Throws:
IOException - if an I/O error occurs getting the physical file

safeClose

public static void safeClose(Closeable c)
Safely close some resource without throwing an exception. Any exception will be logged at TRACE level.

Parameters:
c - the resource

safeClose

public static void safeClose(Closeable... closeables)
Safely close some resource without throwing an exception. Any exception will be logged at TRACE level.

Parameters:
closeables - the resources

safeClose

public static void safeClose(Iterable<? extends Closeable> ci)
Safely close some resources without throwing an exception. Any exception will be logged at TRACE level.

Parameters:
ci - the resources

safeClose

public static void safeClose(ZipFile zipFile)
Safely close some resource without throwing an exception. Any exception will be logged at TRACE level.

Parameters:
zipFile - the resource

recursiveDelete

public static boolean recursiveDelete(File root)
Attempt to recursively delete a real file.

Parameters:
root - the real file to delete
Returns:
true if the file was deleted

recursiveDelete

public static boolean recursiveDelete(VirtualFile root)
Attempt to recursively delete a virtual file.

Parameters:
root - the virtual file to delete
Returns:
true if the file was deleted

recursiveCopy

public static void recursiveCopy(File original,
                                 File destDir)
                          throws IOException
Recursively copy a file or directory from one location to another.

Parameters:
original - the original file or directory
destDir - the destination directory
Throws:
IOException - if an I/O error occurs before the copy is complete

recursiveCopy

public static void recursiveCopy(File original,
                                 VirtualFile destDir)
                          throws IOException
Recursively copy a file or directory from one location to another.

Parameters:
original - the original file or directory
destDir - the destination directory
Throws:
IOException - if an I/O error occurs before the copy is complete

recursiveCopy

public static void recursiveCopy(VirtualFile original,
                                 File destDir)
                          throws IOException
Recursively copy a file or directory from one location to another.

Parameters:
original - the original virtual file or directory
destDir - the destination directory
Throws:
IOException - if an I/O error occurs before the copy is complete

recursiveCopy

public static void recursiveCopy(VirtualFile original,
                                 VirtualFile destDir)
                          throws IOException
Recursively copy a file or directory from one location to another.

Parameters:
original - the original virtual file or directory
destDir - the destination virtual directory
Throws:
IOException - if an I/O error occurs before the copy is complete

emptyStream

public static InputStream emptyStream()
Get the empty input stream. This stream always reports an immediate EOF.

Returns:
the empty input stream

createJarFileInputStream

public static InputStream createJarFileInputStream(VirtualFile virtualFile)
                                            throws IOException
Get an input stream that will always be consumable as a Zip/Jar file. The input stream will not be an instance of a JarInputStream, but will stream bytes according to the Zip specification. Using this method, a VFS file or directory can be written to disk as a normal jar/zip file.

Parameters:
virtualFile - The virtual to get a jar file input stream for
Returns:
An input stream returning bytes according to the zip spec
Throws:
IOException - if any problems occur

unzip

public static void unzip(File zipFile,
                         File destDir)
                  throws IOException
Expand a zip file to a destination directory. The directory must exist. If an error occurs, the destination directory may contain a partially-extracted archive, so cleanup is up to the caller.

Parameters:
zipFile - the zip file
destDir - the destination directory
Throws:
IOException - if an error occurs

getMountSource

public static File getMountSource(Closeable handle)
Return the mount source File for a given mount handle.

Parameters:
handle - The handle to get the source for
Returns:
The mount source file or null if the handle does not have a source, or is not a MountHandle

getGlobPattern

public static Pattern getGlobPattern(String glob)
Get a regular expression pattern which matches any path names which match the given glob. The glob patterns function similarly to ant file patterns. Valid meta-characters in the glob pattern include: In addition, like ant, if the pattern ends with a /, then an implicit "**" will be appended.

See also: "Patterns" in the Ant Manual

Parameters:
glob - the glob to match
Returns:
the pattern

JBoss VFS 3.0.1.GA

Copyright © 2011 JBoss, A division of Red Hat, Inc. All Rights Reserved.