Coverage Report - org.webslinger.commons.vfs.VFSUtil
 
Classes in this File Line Coverage Branch Coverage Complexity
VFSUtil
47%
80/171
64%
25/39
0
VFSUtil$1
0%
0/2
N/A
0
 
 1  
 package org.webslinger.commons.vfs;
 2  
 
 3  
 import java.io.ByteArrayInputStream;
 4  
 import java.io.ByteArrayOutputStream;
 5  
 import java.io.File;
 6  
 import java.io.InputStream;
 7  
 import java.io.IOException;
 8  
 import java.io.OutputStream;
 9  
 import java.io.PrintStream;
 10  
 import java.lang.reflect.InvocationTargetException;
 11  
 import java.lang.reflect.Method;
 12  
 import java.nio.charset.Charset;
 13  
 import java.util.ArrayList;
 14  
 import java.util.Arrays;
 15  
 import java.util.List;
 16  
 import java.util.TreeMap;
 17  
 import org.apache.commons.io.input.NullInputStream;
 18  
 import org.apache.commons.vfs.CacheStrategy;
 19  
 import org.apache.commons.vfs.FileContent;
 20  
 import org.apache.commons.vfs.FileFilter;
 21  
 import org.apache.commons.vfs.FileName;
 22  
 import org.apache.commons.vfs.FileObject;
 23  
 import org.apache.commons.vfs.FileSelectInfo;
 24  
 import org.apache.commons.vfs.FileSystem;
 25  
 import org.apache.commons.vfs.FileSystemException;
 26  
 import org.apache.commons.vfs.FileSystemManager;
 27  
 import org.apache.commons.vfs.FileSystemOptions;
 28  
 import org.apache.commons.vfs.FileType;
 29  
 import org.apache.commons.vfs.impl.StandardFileSystemManager;
 30  
 import org.apache.commons.vfs.operations.FileOperation;
 31  
 import org.apache.commons.vfs.operations.FileOperations;
 32  
 import org.apache.commons.vfs.provider.local.LocalFile;
 33  
 import org.apache.commons.vfs.provider.DelegateFileObject;
 34  
 import org.webslinger.commons.vfs.cow.COWFileObject;
 35  
 import org.webslinger.commons.vfs.local.WebslingerLocalFileProvider;
 36  
 import org.webslinger.commons.vfs.virtual.VirtualFileObject;
 37  
 import org.webslinger.io.Charsets;
 38  
 import org.webslinger.io.IOUtil;
 39  
 
 40  0
 public class VFSUtil {
 41  
     static Method getLocalFile;
 42  
     static {
 43  
         try {
 44  1
             getLocalFile = LocalFile.class.getDeclaredMethod("getLocalFile", (Class[]) null);
 45  1
             getLocalFile.setAccessible(true);
 46  0
         } catch (RuntimeException e) {
 47  0
             throw e;
 48  0
         } catch (Exception e) {
 49  0
             throw (InternalError) new InternalError(e.getMessage()).initCause(e);
 50  1
         }
 51  
 
 52  1
     }
 53  
     public static void touch(FileObject file) throws FileSystemException, IOException {
 54  21
         touch(file, System.currentTimeMillis());
 55  21
     }
 56  
 
 57  
     public static void touch(FileObject file, long time) throws FileSystemException, IOException {
 58  21
         if (!file.exists()) {
 59  21
             OutputStream out = file.getContent().getOutputStream();
 60  21
             out.close();
 61  
         }
 62  21
         file.getContent().setLastModifiedTime(time);
 63  21
     }
 64  
 
 65  
     public static boolean cmp(FileObject left, FileObject right) throws IOException {
 66  0
         FileContent leftContent = left.getContent();
 67  0
         FileContent rightContent = right.getContent();
 68  0
         if (leftContent.getSize() != rightContent.getSize()) return true;
 69  0
         return IOUtil.cmp(leftContent.getInputStream(), rightContent.getInputStream());
 70  
     }
 71  
 
 72  
     public static byte[] getBytes(FileObject file) throws IOException {
 73  0
         ByteArrayOutputStream baos = new ByteArrayOutputStream();
 74  0
         IOUtil.copy(file.getContent().getInputStream(), true, baos, true);
 75  0
         return baos.toByteArray();
 76  
     }
 77  
 
 78  
     public static void setBytes(FileObject file, byte[] bytes) throws IOException {
 79  0
         ByteArrayInputStream bais = new ByteArrayInputStream(bytes);
 80  0
         IOUtil.copy(bais, true, file.getContent().getOutputStream(), true);
 81  0
     }
 82  
 
 83  
     public static InputStream getInputStream(FileObject root, String fileName) throws FileSystemException {
 84  0
         return root.resolveFile(fileName).getContent().getInputStream();
 85  
     }
 86  
 
 87  
     public static OutputStream getOutputStream(FileObject root, String fileName) throws FileSystemException {
 88  0
         return root.resolveFile(fileName).getContent().getOutputStream();
 89  
     }
 90  
 
 91  
     public static Object getAttribute(FileObject root, String fileName, String attrName) throws FileSystemException {
 92  0
         return getAttribute(root.resolveFile(fileName), attrName);
 93  
     }
 94  
 
 95  
     public static Object getAttribute(FileObject file, String attrName) throws FileSystemException {
 96  0
         return file.getContent().getAttribute(attrName);
 97  
     }
 98  
 
 99  
     public static void setAttribute(FileObject root, String fileName, String attrName, Object value) throws FileSystemException {
 100  0
         setAttribute(root.resolveFile(fileName), attrName, value);
 101  0
     }
 102  
 
 103  
     public static void setAttribute(FileObject file, String attrName, Object value) throws FileSystemException {
 104  0
         file.getContent().setAttribute(attrName, value);
 105  0
     }
 106  
 
 107  
     public static String getString(FileObject file) throws FileSystemException, IOException {
 108  22
         return getStringEncoding(file, Charsets.UTF8);
 109  
     }
 110  
 
 111  
     public static String getString(FileObject root, String fileName) throws FileSystemException, IOException {
 112  16
         return getStringEncoding(root, fileName, Charsets.UTF8);
 113  
     }
 114  
 
 115  
     public static String getStringEncoding(FileObject root, String fileName, String encoding) throws FileSystemException, IOException {
 116  0
         return getStringEncoding(root.resolveFile(fileName), encoding);
 117  
     }
 118  
 
 119  
     public static String getStringEncoding(FileObject root, String fileName, Charset charset) throws FileSystemException, IOException {
 120  16
         return getStringEncoding(root.resolveFile(fileName), charset);
 121  
     }
 122  
 
 123  
     public static String getStringEncoding(FileObject file, String encoding) throws FileSystemException, IOException {
 124  0
         return IOUtil.readString(file.getContent().getInputStream(), encoding);
 125  
     }
 126  
 
 127  
     public static String getStringEncoding(FileObject file, Charset charset) throws FileSystemException, IOException {
 128  38
         return IOUtil.readString(file.getContent().getInputStream(), charset);
 129  
     }
 130  
 
 131  
     public static void setString(FileObject file, String value) throws FileSystemException, IOException {
 132  0
         setStringEncoding(file, Charsets.UTF8, value);
 133  0
     }
 134  
 
 135  
     public static void setString(FileObject root, String fileName, String value) throws FileSystemException, IOException {
 136  25
         setStringEncoding(root, fileName, Charsets.UTF8, value);
 137  25
     }
 138  
 
 139  
     public static void setStringEncoding(FileObject root, String fileName, String encoding, String value) throws FileSystemException, IOException {
 140  0
         setStringEncoding(root.resolveFile(fileName), encoding, value);
 141  0
     }
 142  
 
 143  
     public static void setStringEncoding(FileObject root, String fileName, Charset charset, String value) throws FileSystemException, IOException {
 144  25
         setStringEncoding(root.resolveFile(fileName), charset, value);
 145  25
     }
 146  
 
 147  
     public static void setStringEncoding(FileObject file, String encoding, String value) throws FileSystemException, IOException {
 148  0
         IOUtil.writeString(file.getContent().getOutputStream(), encoding, value);
 149  0
     }
 150  
 
 151  
     public static void setStringEncoding(FileObject file, Charset charset, String value) throws FileSystemException, IOException {
 152  25
         IOUtil.writeString(file.getContent().getOutputStream(), charset, value);
 153  25
     }
 154  
 
 155  
     public static File getLocalFile(LocalFile file) throws FileSystemException {
 156  0
         file.getType();
 157  
         try {
 158  
             try {
 159  0
                 return (File) getLocalFile.invoke(file, (Object[]) null);
 160  0
             } catch (InvocationTargetException e) {
 161  0
                 throw (InternalError) new InternalError("SNO").initCause(e.getCause());
 162  
             }
 163  0
         } catch (IllegalAccessException e) {
 164  0
             throw (InternalError) new InternalError("SNO").initCause(e);
 165  
         }
 166  
     }
 167  
 
 168  
     private static File resolveLocalFile(FileObject file, List<FileObject> queue) throws FileSystemException, IOException {
 169  0
         FileObject ptr = queue.get(0);
 170  0
         if (ptr instanceof DelegateFileObject) {
 171  0
             queue.set(0, ((DelegateFileObject) ptr).getDelegateFile());
 172  0
         } else if (ptr instanceof FilteringFileObject) {
 173  0
             queue.set(0, ((FilteringFileObject) ptr).getRealFile());
 174  0
         } else if (ptr instanceof COWFileObject) {
 175  0
             queue.remove(0);
 176  0
             queue.addAll(0, Arrays.asList(((COWFileObject) ptr).getAllFiles()));
 177  0
         } else if (ptr instanceof VirtualFileObject) {
 178  0
             queue.set(0, ((VirtualFileObject) ptr).getFile());
 179  0
         } else if (!ptr.exists()) {
 180  0
             queue.remove(0);
 181  0
         } else if (ptr instanceof LocalFile) {
 182  0
             queue.remove(0);
 183  0
             return new File(ptr.getName().getPath());
 184  
         } else {
 185  0
             throw new IOException("not a local file(" + file + "->(" + ptr.getClass().getName() + ")" + ptr + ")");
 186  
         }
 187  0
         return null;
 188  
     }
 189  
 
 190  
     public static void refresh(FileObject file) throws FileSystemException {
 191  5195
         file.close();
 192  5195
     }
 193  
 
 194  
     public static File findLocalFile(FileObject file) throws FileSystemException, IOException {
 195  0
         ArrayList<FileObject> queue = new ArrayList<FileObject>();
 196  0
         queue.add(file);
 197  0
         while (!queue.isEmpty()) {
 198  0
             File localFile = resolveLocalFile(file, queue);
 199  0
             if (localFile != null) return localFile;
 200  0
         }
 201  0
         throw new IOException("no local file found(" + file + ")");
 202  
     }
 203  
 
 204  
     public static File[] findAllLocalFiles(FileObject file) throws FileSystemException, IOException {
 205  0
         ArrayList<File> files = new ArrayList<File>();
 206  0
         ArrayList<FileObject> queue = new ArrayList<FileObject>();
 207  0
         queue.add(file);
 208  0
         while (!queue.isEmpty()) {
 209  0
             File localFile = resolveLocalFile(file, queue);
 210  0
             if (localFile != null) files.add(localFile);
 211  0
         }
 212  0
         return files.toArray(new File[files.size()]);
 213  
     }
 214  
 
 215  
     public static void truncate(FileObject file, long size) throws FileSystemException, IOException {
 216  0
         FileContent fromContent = file.getContent();
 217  0
         FileObject tmp = file.getFileSystem().getFileSystemManager().resolveFile("tmp:/VFSUtil.tmp");
 218  0
         OutputStream tmpOut = tmp.getContent().getOutputStream();
 219  0
         if (fromContent.getSize() > size) {
 220  0
             IOUtil.copy(fromContent.getInputStream(), true, tmpOut, true, size);
 221  
         } else {
 222  0
             if (fromContent.getSize() > 0) {
 223  0
                 IOUtil.copy(fromContent.getInputStream(), true, tmpOut, false);
 224  0
                 size -= fromContent.getSize();
 225  
             }
 226  0
             IOUtil.copy(new NullInputStream(size), true, tmpOut, true);
 227  
         }
 228  0
         tmp.moveTo(file);
 229  0
     }
 230  
 
 231  
     public static FileObject[] sortFiles(FileObject[] children) {
 232  0
         TreeMap<String, FileObject> sortMap = new TreeMap<String, FileObject>();
 233  0
         for (FileObject child: children) {
 234  0
             sortMap.put(child.getName().getPath(), child);
 235  
         }
 236  0
         return sortMap.values().toArray(new FileObject[sortMap.size()]);
 237  
     }
 238  
 
 239  
     public static FileFilter getFileFilterPattern(final String pattern) {
 240  0
         return new FileFilter() {
 241  
             public boolean accept(FileSelectInfo info) {
 242  0
                 return info.getFile().getName().getBaseName().matches(pattern);
 243  
             }
 244  
         };
 245  
     }
 246  
 
 247  
     public static void tree(FileObject file, PrintStream out) throws IOException {
 248  10
         tree(file, out, false);
 249  10
     }
 250  
 
 251  
     public static void tree(FileObject file, PrintStream out, boolean showHidden) throws IOException {
 252  10
         if (file.isHidden() && !showHidden) return;
 253  10
         printFileName(true, file, out);
 254  10
         if (out != null) out.println();
 255  10
         tree(new StringBuilder(), file, out, showHidden);
 256  10
     }
 257  
 
 258  
     protected static void tree(StringBuilder prefix, FileObject file, PrintStream out, boolean showHidden) throws IOException {
 259  46
         if (!file.getType().hasChildren()) return;
 260  20
         FileObject[] children = file.getChildren();
 261  20
         if (children == null || children.length == 0) return;
 262  20
         prefix.append(' ');
 263  56
         for (FileObject child: children) {
 264  36
             if (child.isHidden() && !showHidden) continue;
 265  36
             if (out != null) out.print(prefix);
 266  36
             printFileName(false, child, out);
 267  36
             if (out != null) out.println();
 268  36
             tree(prefix, child, out, showHidden);
 269  
         }
 270  20
         prefix.setLength(prefix.length() - 1);
 271  20
     }
 272  
 
 273  
     public static void printFileName(boolean full, FileObject file, PrintStream out) throws IOException {
 274  46
         FileName name = file.getName();
 275  46
         out.print(full ? name.toString() : name.getBaseName());
 276  46
         FileType type = file.getType();
 277  46
         if (type == FileType.IMAGINARY) {
 278  0
             out.print('!');
 279  
         } else {
 280  46
             if (type.hasChildren()) out.print('/');
 281  46
             if (type.hasContent()) {
 282  26
                 out.print('|');
 283  26
                 out.print(file.getContent().getSize());
 284  
             }
 285  
         }
 286  46
         out.print(' ');
 287  46
         out.print(file.isReadable() ? 'R' : '-');
 288  46
         out.print(file.isWriteable() ? 'W' : '-');
 289  46
         out.print(file.isHidden() ? 'H' : '-');
 290  46
     }
 291  
 
 292  
     public static FileSystemException makeFileSystemException(Throwable t) throws FileSystemException {
 293  4
         if (t instanceof Error) throw (Error) t;
 294  4
         if (t instanceof RuntimeException) throw (RuntimeException) t;
 295  4
         if (t instanceof FileSystemException) throw (FileSystemException) t;
 296  2
         return (FileSystemException) new FileSystemException(t.getMessage()).initCause(t);
 297  
     }
 298  
 
 299  
     public static <T extends FileOperation> T getOperation(FileObject file, Class<T> operationClass) throws FileSystemException {
 300  208
         FileOperations operations = file.getFileOperations();
 301  208
         if (!operations.hasOperation(operationClass)) return null;
 302  206
         return operationClass.cast(operations.getOperation(operationClass));
 303  
     }
 304  
 
 305  
     public static <T extends FileOperation> T getOperation(FileSystem fs, String path, Class<T> operationClass) throws FileSystemException {
 306  162
         return getOperation(fs.resolveFile(path), operationClass);
 307  
     }
 308  
 
 309  
     public static FileSystemOptions findFileSystemOptions(FileObject file, FileSystemOptions options, boolean create) {
 310  53
         if (options != null) return options;
 311  2
         while (options == null && file != null) {
 312  1
             options = file.getFileSystem().getFileSystemOptions();
 313  1
             if (file instanceof DelegateFileObject) {
 314  0
                 file = ((DelegateFileObject) file).getDelegateFile();
 315  
             } else {
 316  1
                 file = null;
 317  
             }
 318  
         }
 319  1
         if (options == null && create) options = new FileSystemOptions();
 320  1
         return options;
 321  
     }
 322  
 
 323  
     public static StandardFileSystemManager createStandardFileSystemManager() throws FileSystemException {
 324  46
         StandardFileSystemManager sfsm = new StandardFileSystemManager();
 325  46
         sfsm.setCacheStrategy(CacheStrategy.MANUAL);
 326  
         //sfsm.setCacheStrategy(CacheStrategy.ON_CALL);
 327  46
         sfsm.addProvider(new String[] {"wsfile"}, new WebslingerLocalFileProvider());
 328  46
         sfsm.init();
 329  46
         return sfsm;
 330  
     }
 331  
 
 332  
     public static FileObject toFileObject(FileSystemManager fsm, File file, FileSystemOptions options) throws FileSystemException {
 333  
         try {
 334  39
             return toFileObject(fsm, file.toURL().toString(), options);
 335  0
         } catch (Exception e) {
 336  0
             throw makeFileSystemException(e);
 337  
         }
 338  
     }
 339  
 
 340  
     public static FileObject toFileObject(FileSystemManager fsm, String url, FileSystemOptions options) throws FileSystemException {
 341  
         try {
 342  91
             String newUrl = url.toString().replaceAll("^file:", "wsfile:");
 343  91
             FileObject file = fsm.resolveFile(url, options);
 344  91
             if (!file.getName().getScheme().equals("file")) return file;
 345  39
             return fsm.resolveFile(file.getURL().toString().replaceAll("^file:", "wsfile:"), options);
 346  0
         } catch (Exception e) {
 347  0
             throw makeFileSystemException(e);
 348  
         }
 349  
     }
 350  
 }