Example usage for java.nio.file FileSystem isOpen

List of usage examples for java.nio.file FileSystem isOpen

Introduction

In this page you can find the example usage for java.nio.file FileSystem isOpen.

Prototype

public abstract boolean isOpen();

Source Link

Document

Tells whether or not this file system is open.

Usage

From source file:Main.java

public static void main(String[] args) throws IOException {
    FileSystem fileSystem = FileSystems.getDefault();
    System.out.println(fileSystem.isOpen());
}

From source file:Main.java

public static void main(String[] args) {
    FileSystem fileSystem = FileSystems.getDefault();
    FileSystemProvider provider = fileSystem.provider();

    System.out.println("Provider: " + provider.toString());
    System.out.println("Open: " + fileSystem.isOpen());
    System.out.println("Read Only: " + fileSystem.isReadOnly());

}

From source file:Test.java

public static void main(String[] args) {
    FileSystem fileSystem = FileSystems.getDefault();
    FileSystemProvider provider = fileSystem.provider();

    System.out.println("Provider: " + provider.toString());
    System.out.println("Open: " + fileSystem.isOpen());
    System.out.println("Read Only: " + fileSystem.isReadOnly());

    Iterable<Path> rootDirectories = fileSystem.getRootDirectories();
    System.out.println();/* ww w  . j  a v  a2 s.  co  m*/
    System.out.println("Root Directories");
    for (Path path : rootDirectories) {
        System.out.println(path);
    }

    Iterable<FileStore> fileStores = fileSystem.getFileStores();
    System.out.println();
    System.out.println("File Stores");
    for (FileStore fileStore : fileStores) {
        System.out.println(fileStore.name());
    }
}

From source file:com.sastix.cms.server.services.content.impl.ZipHandlerServiceImpl.java

@Override
public ResourceDTO handleZip(Resource zipResource) {

    final Path zipPath;
    try {//from   w  w w.  j ava 2s  . c  o  m
        zipPath = hashedDirectoryService.getDataByURI(zipResource.getUri(), zipResource.getResourceTenantId());
    } catch (IOException | URISyntaxException e) {
        throw new ResourceAccessError(e.getMessage());
    }

    FileSystem zipfs = null;
    try {
        zipfs = FileSystems.newFileSystem(zipPath, null);
        final Path root = zipfs.getPath("/");
        final int maxDepth = 1;

        // Search for specific files.
        final Map<String, Path> map = Files
                .find(root, maxDepth, (path_,
                        attr) -> path_.getFileName() != null && (path_.toString().endsWith(METADATA_JSON_FILE)
                                || path_.toString().endsWith(METADATA_XML_FILE)))
                .collect(Collectors.toMap(p -> p.toAbsolutePath().toString().substring(1), p -> p));

        final String zipType;
        final String startPage;

        // Check if it is a cms file
        if (map.containsKey(METADATA_JSON_FILE)) {
            zipType = METADATA_JSON_FILE;
            LOG.info("Found CMS Metadata File " + map.get(zipType).toString());
            startPage = findStartPage(map.get(zipType));
            // Check if it is a Scrom file
        } else if (map.containsKey(METADATA_XML_FILE)) {
            zipType = METADATA_XML_FILE;
            LOG.info("Found CMS Metadata File " + map.get(zipType).toString());
            startPage = findScormStartPage(map.get(zipType));

        } else {
            throw new ResourceAccessError("Zip " + zipResource.getName() + " is not supported. "
                    + METADATA_JSON_FILE + " and " + METADATA_XML_FILE + " are missing");
        }

        LOG.trace(startPage);

        final List<ResourceDTO> resourceDTOs = new LinkedList<>();

        /* Path inside ZIP File */
        Files.walkFileTree(root, new SimpleFileVisitor<Path>() {

            @Override
            public FileVisitResult visitFile(Path file, BasicFileAttributes attrs) throws IOException {

                final String parentContext = zipResource.getUri().split("-")[0] + "-"
                        + zipResource.getResourceTenantId();
                final CreateResourceDTO createResourceDTO = new CreateResourceDTO();
                createResourceDTO.setResourceMediaType(tika.detect(file.toString()));
                createResourceDTO.setResourceAuthor(zipResource.getAuthor());
                createResourceDTO.setResourceExternalURI(file.toUri().toString());
                createResourceDTO.setResourceName(file.toString().substring(1));
                createResourceDTO.setResourceTenantId(zipResource.getResourceTenantId());

                final Resource resource = resourceService.insertChildResource(createResourceDTO, parentContext,
                        zipResource);

                distributedCacheService.cacheIt(resource.getUri(), resource.getResourceTenantId());

                if (file.toString().substring(1).equals(startPage)) {
                    resourceDTOs.add(0, crs.convertToDTO(resource));
                } else {
                    resourceDTOs.add(crs.convertToDTO(resource));
                }

                return FileVisitResult.CONTINUE;
            }

            @Override
            public FileVisitResult preVisitDirectory(Path dir, BasicFileAttributes attrs) throws IOException {
                return FileVisitResult.CONTINUE;
            }
        });

        final ResourceDTO parentResourceDto = resourceDTOs.remove(0);
        parentResourceDto.setResourcesList(resourceDTOs);
        return parentResourceDto;

    } catch (IOException e) {
        throw new ResourceAccessError("Error while analyzing " + zipResource.toString());
    } finally {
        if (zipfs != null && zipfs.isOpen()) {
            try {
                LOG.info("Closing FileSystem");
                zipfs.close();
            } catch (IOException e) {
                LOG.error(e.getMessage());
                e.printStackTrace();
                throw new ResourceAccessError("Error while analyzing " + zipResource.toString());
            }
        }
    }
}

From source file:org.objectpocket.storage.blob.MultiZipBlobStore.java

@Override
public void writeBlobs(Set<Blob> blobs) throws IOException {
    if (blobs == null || blobs.isEmpty()) {
        return;//from   w  w  w .  ja va2  s  . c  o m
    }
    if (blobContainerIndex == null) {
        initIndexAndReadFileSystems();
    }
    FileSystem currentWriteFileSystem = null;
    for (Blob blob : blobs) {

        // get blob path
        String path = blob.getPath();
        if (path == null || path.trim().isEmpty()) {
            path = blob.getId();
        }
        path = path.replaceAll("\\\\", "/");

        String selectedBlobContainerName = null;

        // case 1: replace blob data
        String blobContainer = blobContainerIndex.get(path);
        if (blobContainer != null) {
            currentWriteFileSystem = getWriteFileSystem(blobContainer);
            selectedBlobContainerName = blobContainer;
        }

        // case 2: add blob data
        else {

            // create new blob container
            if (lastBlobContainer == null
                    || blob.getBytes().length + lastBlobContainerSize > MAX_BINARY_FILE_SIZE) {
                if (currentWriteFileSystem != null) {
                    currentWriteFileSystem.close();
                }
                createNextBinary();
            }

            currentWriteFileSystem = getWriteFileSystem(lastBlobContainer.getName());
            selectedBlobContainerName = lastBlobContainer.getName();
        }

        // write data to blob container
        String name = path;
        if (path.contains("/")) {
            name = path.substring(path.lastIndexOf("/"));
            path = path.substring(0, path.lastIndexOf("/"));
            while (path.startsWith("/")) {
                path = path.substring(1);
            }
            Path pathInZip = currentWriteFileSystem.getPath(path);
            if (!Files.exists(pathInZip)) {
                Files.createDirectories(pathInZip);
            }
            if (!Files.exists(pathInZip)) {
                throw new IOException("Could not create directory for blob. " + path);
            }
            path = path + name;
        }
        Path fileInZip = currentWriteFileSystem.getPath(path);
        try (OutputStream out = Files.newOutputStream(fileInZip, StandardOpenOption.CREATE)) {
            out.write(blob.getBytes());
        }
        blobContainerIndex.put(path, selectedBlobContainerName);
        blob.setPersisted();
        if (lastBlobContainer.getName().equals(selectedBlobContainerName)) {
            lastBlobContainerSize = lastBlobContainerSize + blob.getBytes().length;
        }

    }

    for (FileSystem fs : writeFileSystems.values()) {
        if (fs != null && fs.isOpen()) {
            fs.close();
        }
    }
    writeFileSystems.clear();

}

From source file:org.objectpocket.storage.blob.MultiZipBlobStore.java

@Override
public void close() throws IOException {
    for (FileSystem fs : readFileSystems.values()) {
        if (fs != null && fs.isOpen()) {
            fs.close();//from w ww  .j  ava2 s . co m
        }
    }

}

From source file:org.objectpocket.storage.blob.MultiZipBlobStore.java

private synchronized FileSystem getReadFileSystem(String name) throws IOException {
    FileSystem fileSystem = readFileSystems.get(name);
    if (fileSystem == null || !fileSystem.isOpen()) {
        Path path = Paths.get(directory + "/" + name);
        URI uri = URI.create("jar:" + path.toUri());
        Map<String, String> env = new HashMap<>();
        fileSystem = FileSystems.newFileSystem(uri, env);
        readFileSystems.put(name, fileSystem);
    }//from  ww  w .  ja  va  2 s.c  o m
    return fileSystem;
}

From source file:org.objectpocket.storage.blob.MultiZipBlobStore.java

private synchronized FileSystem getWriteFileSystem(String name) throws IOException {
    FileSystem fileSystem = writeFileSystems.get(name);
    if (fileSystem == null || !fileSystem.isOpen()) {
        closeReadFileSystem(name);/*w ww.j  a  v a2  s  .c om*/
        Path path = Paths.get(new File(directory + "/" + name).getAbsolutePath());
        URI uri = URI.create("jar:" + path.toUri());
        Map<String, String> env = new HashMap<>();
        env.put("create", "true");
        fileSystem = FileSystems.newFileSystem(uri, env);
        writeFileSystems.put(name, fileSystem);
    }
    return fileSystem;
}