1 28 29 package com.caucho.vfs; 30 31 import java.io.IOException ; 32 import java.util.Map ; 33 34 class BindPath extends FilesystemPath { 35 private Node _node; 36 private Path _backing; 37 38 BindPath(Path backing) 39 { 40 this(null, "/", null, "/", null, backing); 41 42 _root = this; 43 if (backing instanceof FilesystemPath) 44 _separatorChar = ((FilesystemPath) backing)._separatorChar; 45 } 46 47 50 private BindPath(BindPath root, 51 String userPath, Map <String ,Object > attributes, 52 String path, Node node, Path backing) 53 { 54 super(root, userPath, path); 55 56 if (backing == null) 57 throw new IllegalArgumentException ("backing must not be null"); 58 59 if (node == null) 60 node = new Node("", backing); 61 62 if (backing == null) 63 backing = node._backing; 64 65 _node = node; 66 _backing = backing; 67 68 if (backing instanceof FilesystemPath) 69 _separatorChar = ((FilesystemPath) backing)._separatorChar; 70 } 71 72 public Path fsWalk(String userPath, 73 Map <String ,Object > attributes, 74 String path) 75 { 76 Node ptr = _node; 77 78 int offset = 0; 79 while (offset + 1 < path.length()) { 80 if (ptr.firstChild == null) 81 return ptr._backing.lookup(path.substring(offset), attributes); 82 83 int p = path.indexOf(_separatorChar, offset + 1); 84 String segment; 85 if (p == -1) 86 segment = path.substring(offset + 1); 87 else 88 segment = path.substring(offset + 1, p); 89 90 Node next = ptr.findChild(segment); 91 92 if (next == null) 93 return ptr._backing.lookup(path.substring(offset), attributes); 94 95 offset = p; 96 ptr = next; 97 } 98 99 return new BindPath(this, userPath, attributes, path, _node, null); 100 } 101 102 public String getScheme() 103 { 104 return _root.getScheme(); 105 } 106 107 public boolean exists() 108 { 109 return _backing.exists(); 110 } 111 112 public boolean isDirectory() 113 { 114 return _backing.isDirectory(); 115 } 116 117 public boolean isFile() 118 { 119 return _backing.isFile(); 120 } 121 122 public long getLength() 123 { 124 return _backing.getLength(); 125 } 126 127 public long getLastModified() 128 { 129 return _backing.getLastModified(); 130 } 131 132 public boolean canRead() 133 { 134 return _backing.canRead(); 135 } 136 137 public boolean canWrite() 138 { 139 return _backing.canWrite(); 140 } 141 142 public String []list() throws IOException 143 { 144 String []list = _backing.list(); 145 146 if (_node.firstChild == null) 147 return list; 148 149 String []newList = new String [list.length + _node.size()]; 150 151 int i = 0; 152 for (Node ptr = _node.firstChild; ptr != null; ptr = ptr.next) 153 newList[i++] = ptr.name; 154 155 for (int j = 0; j < list.length; j++) 156 newList[i++] = list[j++]; 157 158 return newList; 159 } 160 161 public boolean mkdir() 162 throws IOException 163 { 164 return _backing.mkdir(); 165 } 166 167 public boolean mkdirs() 168 throws IOException 169 { 170 return _backing.mkdirs(); 171 } 172 173 public boolean remove() 174 throws IOException 175 { 176 return _backing.remove(); 177 } 178 179 public boolean renameTo(Path path) 180 throws IOException 181 { 182 return _backing.renameTo(path); 183 } 184 185 public StreamImpl openReadImpl() throws IOException 186 { 187 return _backing.openReadImpl(); 188 } 189 190 public StreamImpl openWriteImpl() throws IOException 191 { 192 return _backing.openWriteImpl(); 193 } 194 195 public StreamImpl openReadWriteImpl() throws IOException 196 { 197 return _backing.openReadWriteImpl(); 198 } 199 200 public StreamImpl openAppendImpl() throws IOException 201 { 202 return _backing.openAppendImpl(); 203 } 204 205 void bind(String path, Path context) 206 { 207 Node ptr = _node; 208 209 int offset = 0; 210 while (offset + 1 < path.length()) { 211 int p = path.indexOf(_separatorChar, offset + 1); 212 String segment; 213 if (p == -1) 214 segment = path.substring(offset + 1); 215 else 216 segment = path.substring(offset + 1, p); 217 218 Node next = ptr.findChild(segment); 219 220 if (next == null) 221 next = ptr.addChild(segment, ptr._backing); 222 } 223 224 ptr._backing = context; 225 } 226 227 public int hashCode() 228 { 229 return _backing.hashCode(); 230 } 231 232 public boolean equals(Object b) 233 { 234 return _backing.equals(b); 235 } 236 237 public String toString() 238 { 239 return _backing.toString(); 240 } 241 242 static class Node { 243 Node parent; 244 Node firstChild; 245 Node next; 246 247 String name; 248 Path _backing; 249 250 Node(String name, Path backing) 251 { 252 this.name = name; 253 this._backing = backing; 254 } 255 256 int size() 257 { 258 int size = 0; 259 260 for (Node ptr = firstChild; ptr != null; ptr = ptr.next) 261 size++; 262 263 return size; 264 } 265 266 Node findChild(String name) 267 { 268 for (Node ptr = firstChild; ptr != null; ptr = ptr.next) { 269 if (ptr.name.equals(name)) 270 return ptr; 271 } 272 273 return null; 274 } 275 276 Node addChild(String name, Path backing) 277 { 278 for (Node ptr = firstChild; ptr != null; ptr = ptr.next) { 279 if (ptr.name.equals(name)) { 280 ptr._backing = backing; 281 return ptr; 282 } 283 } 284 285 Node node = new Node(name, backing); 286 node.next = firstChild; 287 node.parent = this; 288 firstChild = node; 289 290 return node; 291 } 292 } 293 } 294 295 | Popular Tags |