/** * @param rootName The root file name. * @param fileSystemOptions The FileSystem options. */ protected RamFileSystem(final FileName rootName, final FileSystemOptions fileSystemOptions) { super(rootName, null, fileSystemOptions); this.cache = Collections.synchronizedMap(new HashMap<FileName, RamFileData>()); // create root final RamFileData rootData = new RamFileData(rootName); rootData.setType(FileType.FOLDER); rootData.setLastModified(System.currentTimeMillis()); this.cache.put(rootName, rootData); }
/** * Constructor. * * @param name The file name. */ public RamFileData(final FileName name) { super(); this.children = Collections.synchronizedCollection(new ArrayList<RamFileData>()); this.clear(); if (name == null) { throw new IllegalArgumentException("name can not be null"); } this.name = name; }
@Override public void write(final byte[] b, final int off, final int len) throws IOException { final RamFileData data = this.file.getData(); final int size = data.size(); final int newSize = size + len; // Store the Exception in order to notify the client again on close() try { this.file.resize(newSize); } catch (final IOException e) { this.exception = e; throw e; } System.arraycopy(b, off, data.getContent(), size, len); }
/** * @param from The original file. * @param to The new file. * @throws FileSystemException if an error occurs. */ void rename(final RamFileObject from, final RamFileObject to) throws FileSystemException { if (!this.cache.containsKey(from.getName())) { throw new FileSystemException("File does not exist: " + from.getName()); } // Copy data to.getData().setContent(from.getData().getContent()); to.getData().setLastModified(from.getData().getLastModified()); to.getData().setType(from.getData().getType()); this.save(to); this.delete(from); }
/** * Saves a file * * @param file * @throws FileSystemException */ void save(final RamFileObject file) throws FileSystemException { // Validate name if (file.getData().getName() == null) { throw new FileSystemException(new IllegalStateException("The data has no name. " + file)); } // Add to the parent if (file.getName().getDepth() > 0) { final RamFileData parentData = this.cache.get(file.getParent().getName()); // Only if not already added if (!parentData.hasChildren(file.getData())) { final RamFileObject parent = (RamFileObject) file.getParent(); parent.getData().addChild(file.getData()); parent.close(); } } // Store in cache cache.put(file.getName(), file.getData()); file.getData().updateLastModified(); file.close(); }
/** * @param name The name of the file. * @return children The names of the children. */ String[] listChildren(final FileName name) { final RamFileData data = this.cache.get(name); if (data == null || !data.getType().hasChildren()) { return null; } final Collection<RamFileData> children = data.getChildren(); String[] names; synchronized (children) { names = new String[children.size()]; int pos = 0; final Iterator<RamFileData> iter = children.iterator(); while (iter.hasNext()) { final RamFileData childData = iter.next(); names[pos] = childData.getName().getBaseName(); pos++; } } return names; }
/** * Remove a child. * * @param data The file data. * @throws FileSystemException if an error occurs. */ void removeChild(final RamFileData data) throws FileSystemException { if (!this.getType().hasChildren()) { throw new FileSystemException("A child can only be removed from a folder"); } if (!this.children.contains(data)) { throw new FileSystemException("Child not found. " + data); } this.children.remove(data); updateLastModified(); }
@Override public void setLength(final long newLength) throws IOException { this.file.resize(newLength); this.buf = this.file.getData().getContent(); } }
/** * Delete a file * * @param file * @throws FileSystemException */ void delete(final RamFileObject file) throws FileSystemException { // root is read only check FileSystemException.requireNonNull(file.getParent(), "unable to delete root"); // Remove reference from cache this.cache.remove(file.getName()); // Notify the parent final RamFileObject parent = (RamFileObject) this.resolveFile(file.getParent().getName()); parent.getData().removeChild(file.getData()); parent.close(); // Close the file file.getData().clear(); file.close(); }
public void attach(final RamFileObject fo) { if (fo.getName() == null) { throw new IllegalArgumentException("Null argument"); } RamFileData data = this.cache.get(fo.getName()); if (data == null) { data = new RamFileData(fo.getName()); } fo.setData(data); }
@Override protected long doGetLastModifiedTime() throws Exception { return data.getLastModified(); }
@Override protected FileType doGetType() throws Exception { return data.getType(); }
/** * @param from The original file. * @param to The new file. * @throws FileSystemException if an error occurs. */ void rename(final RamFileObject from, final RamFileObject to) throws FileSystemException { if (!this.cache.containsKey(from.getName())) { throw new FileSystemException("File does not exist: " + from.getName()); } // Copy data to.getData().setContent(from.getData().getContent()); to.getData().setLastModified(from.getData().getLastModified()); to.getData().setType(from.getData().getType()); this.save(to); this.delete(from); }
/** * Saves a file * * @param file * @throws FileSystemException */ void save(final RamFileObject file) throws FileSystemException { // Validate name if (file.getData().getName() == null) { throw new FileSystemException(new IllegalStateException("The data has no name. " + file)); } // Add to the parent if (file.getName().getDepth() > 0) { final RamFileData parentData = this.cache.get(file.getParent().getName()); // Only if not already added if (!parentData.hasChildren(file.getData())) { final RamFileObject parent = (RamFileObject) file.getParent(); parent.getData().addChild(file.getData()); parent.close(); } } // Store in cache cache.put(file.getName(), file.getData()); file.getData().updateLastModified(); file.close(); }
/** * @param name The name of the file. * @return children The names of the children. */ String[] listChildren(final FileName name) { final RamFileData data = this.cache.get(name); if (data == null || !data.getType().hasChildren()) { return null; } final Collection<RamFileData> children = data.getChildren(); String[] names; synchronized (children) { names = new String[children.size()]; int pos = 0; final Iterator<RamFileData> iter = children.iterator(); while (iter.hasNext()) { final RamFileData childData = iter.next(); names[pos] = childData.getName().getBaseName(); pos++; } } return names; }
/** * Remove a child. * * @param data The file data. * @throws FileSystemException if an error occurs. */ void removeChild(final RamFileData data) throws FileSystemException { if (!this.getType().hasChildren()) { throw new FileSystemException("A child can only be removed from a folder"); } if (!this.children.contains(data)) { throw new FileSystemException("Child not found. " + data); } this.children.remove(data); updateLastModified(); }
@Override public void setLength(final long newLength) throws IOException { this.file.resize(newLength); this.buf = this.file.getData().getContent(); } }
/** * Delete a file * * @param file * @throws FileSystemException */ void delete(final RamFileObject file) throws FileSystemException { // root is read only check if (file.getParent() == null) { throw new FileSystemException("unable to delete root"); } // Remove reference from cache this.cache.remove(file.getName()); // Notify the parent final RamFileObject parent = (RamFileObject) this.resolveFile(file.getParent().getName()); parent.getData().removeChild(file.getData()); parent.close(); // Close the file file.getData().clear(); file.close(); }