| 1 | |
package org.webslinger.vfs; |
| 2 | |
|
| 3 | |
import java.io.InputStream; |
| 4 | |
import java.io.IOException; |
| 5 | |
import java.io.OutputStream; |
| 6 | |
import java.util.Collection; |
| 7 | |
|
| 8 | |
import org.apache.commons.vfs.FileName; |
| 9 | |
import org.apache.commons.vfs.FileObject; |
| 10 | |
import org.apache.commons.vfs.FileType; |
| 11 | |
import org.apache.commons.vfs.RandomAccessContent; |
| 12 | |
import org.apache.commons.vfs.util.RandomAccessMode; |
| 13 | |
|
| 14 | |
import org.webslinger.io.Charsets; |
| 15 | |
import org.webslinger.io.IOUtil; |
| 16 | |
|
| 17 | 1705 | public class CommonsVfsFileObjectVFSDelegate extends VFSDelegate<FileName, FileObject, FileObject> { |
| 18 | |
public FileName getId(FileObject file) { |
| 19 | 0 | return file.getName(); |
| 20 | |
} |
| 21 | |
|
| 22 | |
public Type getType(FileObject file) throws IOException { |
| 23 | 0 | FileType type = file.getType(); |
| 24 | 0 | if (type.hasChildren()) return Type.DIRECTORY; |
| 25 | 0 | if (type.hasContent()) return Type.FILE; |
| 26 | 0 | return Type.IMAGINARY; |
| 27 | |
} |
| 28 | |
|
| 29 | |
public long getLastModifiedTime(FileObject file) throws IOException { |
| 30 | 854 | return file.exists() ? file.getContent().getLastModifiedTime() : -1; |
| 31 | |
} |
| 32 | |
|
| 33 | |
public Collection<Permission> getPermissions(FileObject file) throws IOException { |
| 34 | 0 | if (file.isReadable()) { |
| 35 | 0 | return file.isWriteable() ? Permission.ALL_SET : Permission.READABLE_SET; |
| 36 | |
} else { |
| 37 | 0 | return file.isWriteable() ? Permission.WRITEABLE_SET : Permission.NONE_SET; |
| 38 | |
} |
| 39 | |
} |
| 40 | |
|
| 41 | |
public String absolutePath(FileObject file) throws IOException { |
| 42 | 501 | return file.getName().getPath(); |
| 43 | |
} |
| 44 | |
|
| 45 | |
public String getBaseName(FileObject file) throws IOException { |
| 46 | 0 | return file.getName().getBaseName(); |
| 47 | |
} |
| 48 | |
|
| 49 | |
public FileObject getParent(FileObject file) throws IOException { |
| 50 | 24 | return file.getParent(); |
| 51 | |
} |
| 52 | |
|
| 53 | |
public FileObject resolve(FileObject file, String name) throws IOException { |
| 54 | 3 | return file.resolveFile(name); |
| 55 | |
} |
| 56 | |
|
| 57 | |
public FileObject[] getChildren(FileObject file) throws IOException { |
| 58 | 0 | return file.getChildren(); |
| 59 | |
} |
| 60 | |
|
| 61 | |
public boolean attributeExists(FileObject file, String name) throws IOException { |
| 62 | 0 | return file.exists() ? file.getContent().attributeExists(name) : false; |
| 63 | |
} |
| 64 | |
|
| 65 | |
public Object getAttribute(FileObject file, String name) throws IOException { |
| 66 | 0 | return file.exists() ? file.getContent().getAttribute(name) : null; |
| 67 | |
} |
| 68 | |
|
| 69 | |
public String getContentType(FileObject file) throws IOException { |
| 70 | 0 | return file.getContent().getContentInfo().getContentType(); |
| 71 | |
} |
| 72 | |
|
| 73 | |
public long getLength(FileObject file) throws IOException { |
| 74 | 6 | return file.getContent().getSize(); |
| 75 | |
} |
| 76 | |
|
| 77 | |
public String getString(FileObject file) throws IOException { |
| 78 | 284 | return IOUtil.readString(openInput(file), Charsets.UTF8); |
| 79 | |
} |
| 80 | |
|
| 81 | |
public InputStream openInput(FileObject file) throws IOException { |
| 82 | 290 | return file.exists() ? file.getContent().getInputStream() : null; |
| 83 | |
} |
| 84 | |
|
| 85 | |
public OutputStream openOutput(FileObject file, boolean append) throws IOException { |
| 86 | 0 | return file.getContent().getOutputStream(append); |
| 87 | |
} |
| 88 | |
|
| 89 | |
public RandomAccess openRandom(FileObject file, boolean read) throws IOException { |
| 90 | 0 | final RandomAccessContent rac = file.getContent().getRandomAccessContent(read ? RandomAccessMode.READ : RandomAccessMode.READWRITE); |
| 91 | 0 | return new RandomAccess() { |
| 92 | |
public long getLength() throws IOException { |
| 93 | 0 | return rac.length(); |
| 94 | |
} |
| 95 | |
|
| 96 | |
public boolean seek(long offset) { |
| 97 | 0 | synchronized (rac) { |
| 98 | |
try { |
| 99 | 0 | rac.seek(offset); |
| 100 | 0 | } catch (IOException e) { |
| 101 | 0 | throw (InternalError) new InternalError(e.getMessage()).initCause(e); |
| 102 | 0 | } |
| 103 | 0 | return true; |
| 104 | 0 | } |
| 105 | |
} |
| 106 | |
|
| 107 | |
public void write(int b) throws IOException { |
| 108 | 0 | synchronized (rac) { |
| 109 | 0 | rac.write(b); |
| 110 | 0 | } |
| 111 | 0 | } |
| 112 | |
|
| 113 | |
public int read() throws IOException { |
| 114 | 0 | synchronized (rac) { |
| 115 | 0 | return rac.readByte(); |
| 116 | 0 | } |
| 117 | |
} |
| 118 | |
|
| 119 | |
public void write(long pointer, byte[] buffer, int offset, int length) throws IOException { |
| 120 | 0 | synchronized (rac) { |
| 121 | 0 | rac.seek(pointer); |
| 122 | 0 | rac.write(buffer, offset, length); |
| 123 | 0 | } |
| 124 | 0 | } |
| 125 | |
|
| 126 | |
public int read(long pointer, byte[] buffer, int offset, int length) throws IOException { |
| 127 | 0 | synchronized (rac) { |
| 128 | 0 | rac.seek(pointer); |
| 129 | 0 | rac.readFully(buffer, offset, length); |
| 130 | 0 | return length; |
| 131 | 0 | } |
| 132 | |
} |
| 133 | |
|
| 134 | |
public void close() throws IOException { |
| 135 | 0 | rac.close(); |
| 136 | 0 | } |
| 137 | |
|
| 138 | |
public InputStream getInputStream() throws IOException { |
| 139 | 0 | return rac.getInputStream(); |
| 140 | |
} |
| 141 | |
|
| 142 | |
public OutputStream getOutputStream() throws IOException { |
| 143 | 0 | throw new UnsupportedOperationException(); |
| 144 | |
} |
| 145 | |
}; |
| 146 | |
} |
| 147 | |
} |