memoryfilesystem.java

来自「jsr170接口的java实现。是个apache的开源项目。」· Java 代码 · 共 244 行

JAVA
244
字号
/* * Licensed to the Apache Software Foundation (ASF) under one or more * contributor license agreements.  See the NOTICE file distributed with * this work for additional information regarding copyright ownership. * The ASF licenses this file to You under the Apache License, Version 2.0 * (the "License"); you may not use this file except in compliance with * the License.  You may obtain a copy of the License at * *      http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */package org.apache.jackrabbit.core.fs.mem;import java.io.ByteArrayInputStream;import java.io.ByteArrayOutputStream;import java.io.FilterOutputStream;import java.io.IOException;import java.io.InputStream;import java.io.OutputStream;import java.util.HashMap;import java.util.HashSet;import java.util.Iterator;import java.util.Map;import java.util.Set;import org.apache.jackrabbit.core.fs.FileSystem;import org.apache.jackrabbit.core.fs.FileSystemException;import org.apache.jackrabbit.core.fs.RandomAccessOutputStream;public class MemoryFileSystem implements FileSystem {    private Map entries = new HashMap();    public void close() {    }    public void copy(String srcPath, String destPath)            throws FileSystemException {        assertExistence(srcPath);        MemoryFile srcFile = getFile(srcPath);        OutputStream destinationOutputStream = getOutputStream(destPath);        try {            destinationOutputStream.write(srcFile.getData());        } catch (IOException e) {            throw new FileSystemException(e.getMessage(), e);        } finally {            try {                destinationOutputStream.close();            } catch (IOException e) {                // ignore            }        }    }    private MemoryFile getFile(String filePath) throws FileSystemException {        MemoryFileSystemEntry entry = getEntry(filePath);        assertIsFile(filePath);        return (MemoryFile) entry;    }    public void createFolder(String folderPath) throws FileSystemException {        if (exists(folderPath)) {            throw new FileSystemException("Folder or file " + folderPath                    + " already exists");        }        if (!exists(FileSystem.SEPARATOR)) {            createFolderInternal("/");        }        String relativePath = folderPath.substring(1);        String[] pathElements = relativePath.split(FileSystem.SEPARATOR);        String currentFolderPath = "";        for (int i = 0; i < pathElements.length; i++) {            String pathElement = pathElements[i];            currentFolderPath += "/" + pathElement;            createFolderInternal(currentFolderPath);        }    }    private void createFolderInternal(String folderPath) {        MemoryFolder folder = new MemoryFolder();        entries.put(folderPath, folder);    }    public void deleteFile(String filePath) throws FileSystemException {        assertExistence(filePath);        entries.remove(filePath);    }    public void deleteFolder(String folderPath) throws FileSystemException {        assertExistence(folderPath);        if (hasChildren(folderPath)) {            throw new FileSystemException(folderPath + " not empty");        }        entries.remove(folderPath);    }    public boolean exists(String path) throws FileSystemException {        return entries.containsKey(path);    }    public InputStream getInputStream(String filePath)            throws FileSystemException {        assertExistence(filePath);        assertIsFile(filePath);        MemoryFile file = getFile(filePath);        return new ByteArrayInputStream(file.getData());    }    private void assertIsFolder(String folderPath) throws FileSystemException {        assertExistence(folderPath);        if (!getEntry(folderPath).isFolder()) {            throw new FileSystemException("Folder " + folderPath                    + " does not exist");        }    }    private void assertIsFile(String filePath) throws FileSystemException {        if (!isFile(filePath)) {            throw new FileSystemException(filePath + " is a folder");        }    }    public OutputStream getOutputStream(String filePath)            throws FileSystemException {        String folderPath = filePath;        if (filePath.lastIndexOf(FileSystem.SEPARATOR) > 0) {            folderPath = filePath.substring(0, filePath.lastIndexOf("/"));        } else {            folderPath = "/";        }        assertIsFolder(folderPath);        final MemoryFile file = new MemoryFile();        entries.put(filePath, file);        return new FilterOutputStream(new ByteArrayOutputStream()) {            public void close() throws IOException {                out.close();                file.setData(((ByteArrayOutputStream) out).toByteArray());            }        };    }    public RandomAccessOutputStream getRandomAccessOutputStream(String filePath)            throws UnsupportedOperationException {        throw new UnsupportedOperationException(                "Random access is not implemented for the memory file system");    }    public boolean hasChildren(String path) throws FileSystemException {        assertIsFolder(path);        return list(path).length > 0;    }    public void init() {        createFolderInternal("/");    }    public boolean isFile(String path) throws FileSystemException {        assertExistence(path);        return !getEntry(path).isFolder();    }    private MemoryFileSystemEntry getEntry(String path) {        return ((MemoryFileSystemEntry) entries.get(path));    }    private void assertExistence(String path) throws FileSystemException {        if (!exists(path))            throw new FileSystemException("no such file " + path);    }    public boolean isFolder(String path) throws FileSystemException {        if (path.equals("/")) {            return true;        } else {            assertExistence(path);            return getEntry(path).isFolder();        }    }    public long lastModified(String path) throws FileSystemException {        assertExistence(path);        return getEntry(path).getLastModified();    }    public long length(String filePath) throws FileSystemException {        assertIsFile(filePath);        return getFile(filePath).getData().length;    }    public String[] list(String folderPath) {        if (folderPath.equals("/")) {            folderPath = "";        }        Set allNames = entries.keySet();        Set selectedNames = new HashSet();        for (Iterator iter = allNames.iterator(); iter.hasNext();) {            String name = (String) iter.next();            if (name.matches(folderPath + "/[^/]*") && !name.equals("/")) {                selectedNames.add(name);            }        }        return (String[]) selectedNames.toArray(new String[0]);    }    public String[] listFiles(String folderPath) {        return listInternal(folderPath, false);    }    public String[] listFolders(String folderPath) {        return listInternal(folderPath, true);    }    private String[] listInternal(String folderPath, boolean isFolder) {        String[] entryPaths = list(folderPath);        Set resultEntryPaths = new HashSet();        for (int i = 0; i < entryPaths.length; i++) {            String entryPath = entryPaths[i];            MemoryFileSystemEntry entry = getEntry(entryPath);            if (entry.isFolder() == isFolder) {                resultEntryPaths.add(entryPath);            }        }        return (String[]) resultEntryPaths.toArray(new String[0]);    }    public void move(String srcPath, String destPath) {        Object src = entries.remove(srcPath);        entries.put(destPath, src);    }    public void touch(String filePath) throws FileSystemException {        assertIsFile(filePath);        getEntry(filePath).touch();    }}

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?