| 1 | |
package org.webslinger.ext.quercus; |
| 2 | |
|
| 3 | |
import java.io.InputStream; |
| 4 | |
import java.io.IOException; |
| 5 | |
import java.io.OutputStream; |
| 6 | |
import java.util.EnumSet; |
| 7 | |
import java.util.Iterator; |
| 8 | |
import java.util.Map; |
| 9 | |
|
| 10 | |
import com.caucho.vfs.FilesystemPath; |
| 11 | |
import com.caucho.vfs.Path; |
| 12 | |
import com.caucho.vfs.RandomAccessStream; |
| 13 | |
import com.caucho.vfs.StreamImpl; |
| 14 | |
import com.caucho.vfs.VfsStream; |
| 15 | |
import com.caucho.vfs.WriteStream; |
| 16 | |
import com.caucho.vfs.WriterStreamImpl; |
| 17 | |
|
| 18 | |
import org.webslinger.vfs.VFSDelegate; |
| 19 | |
import org.webslinger.vfs.RandomAccess; |
| 20 | |
|
| 21 | |
public class VFSDelegatePath extends FilesystemPath { |
| 22 | |
private final VFSDelegate<?, Object, ?> vfsDelegate; |
| 23 | |
private final Object id; |
| 24 | |
|
| 25 | |
public VFSDelegatePath(VFSDelegate<?, Object, ?> vfsDelegate, Object id) throws Exception { |
| 26 | 24 | super(getVFSDelegatePathParent(vfsDelegate, id), null, vfsDelegate.absolutePath(id)); |
| 27 | 24 | this.vfsDelegate = vfsDelegate; |
| 28 | 24 | this.id = id; |
| 29 | 24 | } |
| 30 | |
|
| 31 | |
public VFSDelegate<?, Object, ?> getVFSDelegate() { |
| 32 | 48 | return vfsDelegate; |
| 33 | |
} |
| 34 | |
|
| 35 | |
public Object getId() { |
| 36 | 45 | return id; |
| 37 | |
} |
| 38 | |
|
| 39 | |
public String getScheme() { |
| 40 | 18 | return "vfs"; |
| 41 | |
} |
| 42 | |
|
| 43 | |
public String getFullPath() { |
| 44 | 18 | return getPath(); |
| 45 | |
} |
| 46 | |
|
| 47 | |
public String getPath() { |
| 48 | |
try { |
| 49 | 24 | return getVFSDelegate().absolutePath(getId()); |
| 50 | 0 | } catch (IOException e) { |
| 51 | 0 | return null; |
| 52 | |
} |
| 53 | |
} |
| 54 | |
|
| 55 | |
public boolean exists() { |
| 56 | |
try { |
| 57 | 0 | return getVFSDelegate().getType(getId()) != VFSDelegate.Type.IMAGINARY; |
| 58 | 0 | } catch (IOException e) { |
| 59 | 0 | return false; |
| 60 | |
} |
| 61 | |
} |
| 62 | |
|
| 63 | |
public boolean isDirectory() { |
| 64 | |
try { |
| 65 | 0 | return getVFSDelegate().getType(getId()) == VFSDelegate.Type.DIRECTORY; |
| 66 | 0 | } catch (IOException e) { |
| 67 | 0 | return false; |
| 68 | |
} |
| 69 | |
} |
| 70 | |
|
| 71 | |
public boolean isFile() { |
| 72 | 0 | return !isDirectory(); |
| 73 | |
} |
| 74 | |
|
| 75 | |
public String getContentType() { |
| 76 | |
try { |
| 77 | 0 | return getVFSDelegate().getContentType(getId()); |
| 78 | 0 | } catch (IOException e) { |
| 79 | 0 | return null; |
| 80 | |
} |
| 81 | |
} |
| 82 | |
|
| 83 | |
public long getLength() { |
| 84 | |
try { |
| 85 | 6 | return getVFSDelegate().getLength(getId()); |
| 86 | 0 | } catch (IOException e) { |
| 87 | 0 | return -1; |
| 88 | |
} |
| 89 | |
} |
| 90 | |
|
| 91 | |
public long getLastModified() { |
| 92 | |
try { |
| 93 | 6 | return getVFSDelegate().getLastModifiedTime(getId()); |
| 94 | 0 | } catch (IOException e) { |
| 95 | 0 | return -1; |
| 96 | |
} |
| 97 | |
} |
| 98 | |
|
| 99 | |
public boolean canRead() { |
| 100 | |
try { |
| 101 | 0 | return getVFSDelegate().getPermissions(getId()).contains(VFSDelegate.Permission.READABLE); |
| 102 | 0 | } catch (IOException e) { |
| 103 | 0 | return false; |
| 104 | |
} |
| 105 | |
} |
| 106 | |
|
| 107 | |
public boolean canWrite() { |
| 108 | |
try { |
| 109 | 0 | return getVFSDelegate().getPermissions(getId()).contains(VFSDelegate.Permission.WRITEABLE); |
| 110 | 0 | } catch (IOException e) { |
| 111 | 0 | return false; |
| 112 | |
} |
| 113 | |
} |
| 114 | |
|
| 115 | |
public String[] list() throws IOException { |
| 116 | 0 | Object[] children = getVFSDelegate().getChildren(getId()); |
| 117 | 0 | String[] names = new String[children.length]; |
| 118 | 0 | for (int i = 0; i < names.length; i++) { |
| 119 | 0 | names[i] = getVFSDelegate().getBaseName(children[i]); |
| 120 | |
} |
| 121 | 0 | return names; |
| 122 | |
} |
| 123 | |
|
| 124 | |
public boolean mkdir() throws IOException { |
| 125 | 0 | throw new UnsupportedOperationException(); |
| 126 | |
} |
| 127 | |
|
| 128 | |
public boolean mkdirs() throws IOException { |
| 129 | 0 | throw new UnsupportedOperationException(); |
| 130 | |
} |
| 131 | |
|
| 132 | |
public boolean remove() throws IOException { |
| 133 | 0 | throw new UnsupportedOperationException(); |
| 134 | |
} |
| 135 | |
|
| 136 | |
public Object getAttribute(String name) throws IOException { |
| 137 | 0 | return getVFSDelegate().getAttribute(getId(), name); |
| 138 | |
} |
| 139 | |
|
| 140 | |
public Iterator getAttributeNames() throws IOException { |
| 141 | 0 | throw new UnsupportedOperationException(); |
| 142 | |
} |
| 143 | |
|
| 144 | |
public Path fsWalk(String userPath, Map newAttributes, String newPath) { |
| 145 | |
try { |
| 146 | 3 | return new VFSDelegatePath(getVFSDelegate(), getVFSDelegate().resolve(getId(), newPath)); |
| 147 | 0 | } catch (Exception e) { |
| 148 | 0 | e.printStackTrace(); |
| 149 | 0 | return null; |
| 150 | |
} |
| 151 | |
} |
| 152 | |
|
| 153 | |
protected InputStream getInputStream() throws IOException { |
| 154 | 6 | return getVFSDelegate().openInput(getId()); |
| 155 | |
} |
| 156 | |
|
| 157 | |
protected OutputStream getOutputStream(boolean append) throws IOException { |
| 158 | 0 | return getVFSDelegate().openOutput(getId(), append); |
| 159 | |
} |
| 160 | |
|
| 161 | |
public StreamImpl openReadImpl() throws IOException { |
| 162 | 6 | return new VfsStream(getInputStream(), null); |
| 163 | |
} |
| 164 | |
|
| 165 | |
public StreamImpl openAppendImpl() throws IOException { |
| 166 | 0 | return new VfsStream(null, getOutputStream(true)); |
| 167 | |
} |
| 168 | |
|
| 169 | |
public StreamImpl openWriteImpl() throws IOException { |
| 170 | 0 | return new VfsStream(null, getOutputStream(false)); |
| 171 | |
} |
| 172 | |
|
| 173 | |
public RandomAccessStream openRandomAccess() throws IOException { |
| 174 | 0 | final RandomAccess ra = getVFSDelegate().openRandom(getId(), false); |
| 175 | 0 | return new RandomAccessStream() { |
| 176 | |
public long getFilePointer() throws IOException { |
| 177 | 0 | return ra.getFilePointer(); |
| 178 | |
} |
| 179 | |
|
| 180 | |
public void write(int b) throws IOException { |
| 181 | 0 | ra.write(b); |
| 182 | 0 | } |
| 183 | |
|
| 184 | |
public int read() throws IOException { |
| 185 | 0 | return ra.read(); |
| 186 | |
} |
| 187 | |
|
| 188 | |
public void close() throws IOException { |
| 189 | 0 | ra.close(); |
| 190 | 0 | } |
| 191 | |
|
| 192 | |
public long getLength() throws IOException { |
| 193 | 0 | return ra.getLength(); |
| 194 | |
} |
| 195 | |
|
| 196 | |
public InputStream getInputStream() throws IOException { |
| 197 | 0 | return ra.getInputStream(); |
| 198 | |
} |
| 199 | |
|
| 200 | |
public OutputStream getOutputStream() throws IOException { |
| 201 | 0 | return ra.getOutputStream(); |
| 202 | |
} |
| 203 | |
|
| 204 | |
public boolean seek(long pointer) { |
| 205 | 0 | return ra.seek(pointer); |
| 206 | |
} |
| 207 | |
|
| 208 | |
public void write(long pointer, byte[] buffer, int offset, int length) throws IOException { |
| 209 | 0 | ra.write(pointer, buffer, offset, length); |
| 210 | 0 | } |
| 211 | |
|
| 212 | |
public int read(long pointer, byte[] buffer, int offset, int length) throws IOException { |
| 213 | 0 | return ra.read(pointer, buffer, offset, length); |
| 214 | |
} |
| 215 | |
|
| 216 | |
public void write(byte[] buffer, int offset, int length) throws IOException { |
| 217 | 0 | ra.write(buffer, offset, length); |
| 218 | 0 | } |
| 219 | |
|
| 220 | |
public int read(byte[] buffer, int offset, int length) throws IOException { |
| 221 | 0 | return ra.read(buffer, offset, length); |
| 222 | |
} |
| 223 | |
}; |
| 224 | |
} |
| 225 | |
|
| 226 | |
protected static VFSDelegatePath getVFSDelegatePathParent(VFSDelegate<?, Object, ?> vfsDelegate, Object id) throws Exception { |
| 227 | 24 | Object parent = vfsDelegate.getParent(id); |
| 228 | 24 | if (parent == null) return null; |
| 229 | 12 | return new VFSDelegatePath(vfsDelegate, parent); |
| 230 | |
} |
| 231 | |
} |