Example usage for java.nio.file FileSystemException FileSystemException

List of usage examples for java.nio.file FileSystemException FileSystemException

Introduction

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

Prototype

public FileSystemException(String file) 

Source Link

Document

Constructs an instance of this class.

Usage

From source file:io.github.retz.executor.FileManager.java

static void fetchPersistentFiles(List<String> files, String destination, boolean trustPVFiles)
        throws IOException {
    for (String file : files) {
        java.nio.file.Path path = Paths.get(file).getFileName();
        if (path == null) {
            throw new FileSystemException(destination);
        }/*from   w  w  w  .  j a  v  a2 s. co  m*/
        File f = new File(FilenameUtils.concat(destination, path.toString()));
        LOG.info("Downloading: {} as {}", file, f);
        if (f.exists()) {
            LOG.debug("File already exists: {}", f);
            if (!trustPVFiles) {
                try {
                    boolean needsDecompression = needsDecompression(f, destination);
                    if (needsDecompression) {
                        decompress(f, destination);
                    } else {
                        LOG.info("File {} was correctly decompressed before. Skipping decompression.", file);
                    }
                } catch (ArchiveException e) {
                    LOG.error("ArchiveException on {}: {}", f, e.getMessage());
                    e.printStackTrace();
                }
            }
        } else if (file.startsWith("http")) {
            fetchHTTPFile(file, destination);
            decompress(f, destination);
        } else if (file.startsWith("hdfs://")) {
            fetchHDFSFile(file, destination);
            decompress(f, destination);
        } else if (file.startsWith("maprfs://")) {
            fetchHDFSFile(file, destination);
            decompress(f, destination);
        } else {
            LOG.error("Invalid URL scheme: {}", file);
        }
    }
}

From source file:com.bbytes.jfilesync.sync.JFileSyncListenerClientThread.java

private void fileModified(FileSyncMessage fileSyncMessage) {

    if (!fileSyncMessage.isDirectory()) {
        File theDir = null;//  w  w  w. jav  a  2s  .c o m
        try {
            if (fileSyncMessage.getBaseFolderRelativePath() != null
                    & fileSyncMessage.getBaseFolderRelativePath().length() > 0) {
                theDir = new File(
                        new File(destinationFolder).getPath() + fileSyncMessage.getBaseFolderRelativePath());
                // if the directory does not exist, create it
                if (!theDir.exists()) {
                    boolean result = theDir.mkdirs();
                    if (!result) {
                        throw new FileSystemException(
                                "Directory creation failed : " + new File(destinationFolder).getPath()
                                        + fileSyncMessage.getBaseFolderRelativePath());
                    }
                }
            }
            if (theDir != null) {
                copyFileToDestinationFolder(theDir, fileSyncMessage);
            } else {
                copyFileToDestinationFolder(new File(destinationFolder), fileSyncMessage);
            }

        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}

From source file:info.schnatterer.songbirdDbTools.commands.playlist.ExportPlaylistsCommand.java

/**
 * Checks a path, if it exists, is a directory and if the application can write to it.
 * /*from  w  w  w. j  a  va  2  s .  c  om*/
 * @param destinationFolder
 *            the path to be checked.
 * @throws FileSystemException
 *             if any of the mentioned checks fails.
 */
private static void checkDirectory(final String destinationFolder) throws FileSystemException {
    File destinationFile = new File(destinationFolder);
    if (!destinationFile.exists()) {
        throw new FileSystemException(
                "Destination folder does not exist: " + destinationFile.getAbsolutePath());
    }
    if (!destinationFile.isDirectory()) {
        throw new FileSystemException(
                "Destination folder is not a directory: " + destinationFile.getAbsolutePath());
    }
    if (!destinationFile.canWrite()) {
        throw new FileSystemException("Destination folder is read only: " + destinationFile.getAbsolutePath());
    }
}

From source file:gov.noaa.pfel.erddap.dataset.EDDGridFromMergeIRFiles.java

/**
 * This gets source data from one file.//from  ww  w  . j  a v  a  2 s . c  o m
 *
 * @param fileDir
 * @param fileName
 * @param tDataVariables the desired data variables
 * @param tConstraints 
 *   For each axis variable, there will be 3 numbers (startIndex, stride, stopIndex).
 *   !!! If there is a special axis0, this will not include constraints for axis0.
 * @return a PrimitiveArray[] with an element for each tDataVariable with
 *   the dataValues.
 *   <br>The dataValues are straight from the source, not modified.
 *   <br>The primitiveArray dataTypes are usually the sourceDataTypeClass, but
 *   can be any type. EDDGridFromFiles will convert to the
 *   sourceDataTypeClass.
 *   <br>Note the lack of axisVariable values!
 * @throws Throwable if trouble (notably, WaitThenTryAgainException). If
 *   there is trouble, this doesn't call addBadFile or requestReloadASAP().
 */
public PrimitiveArray[] lowGetSourceDataFromFile(String fileDir, String fileName, EDV tDataVariables[],
        IntArray tConstraints) throws Throwable {

    if (verbose)
        String2.log("getSourceDataFromFile(" + fileDir + ", " + fileName + ", " + tDataVariables + ", "
                + tConstraints + ")");

    //make the selection spec  and get the axis values
    int nbAxisVariable = axisVariables.length;
    int nbDataVariable = tDataVariables.length;
    PrimitiveArray[] paa = new PrimitiveArray[nbDataVariable];
    StringBuilder selectionSB = new StringBuilder();

    int minTime = 0, maxTime = 0, strideTime = 0;
    int minLat = 0, maxLat = 0, strideLat = 0;
    int minLon = 0, maxLon = 0, strideLon = 0;

    for (int avi = 0; avi < nbAxisVariable; avi++) {
        switch (axisVariables[avi].sourceName()) {
        case "latitude":
            minLat = tConstraints.get(avi * 3);
            strideLat = tConstraints.get(avi * 3 + 1);
            maxLat = tConstraints.get(avi * 3 + 2);
            break;
        case "longitude":
            minLon = tConstraints.get(avi * 3);
            strideLon = tConstraints.get(avi * 3 + 1);
            maxLon = tConstraints.get(avi * 3 + 2);
            break;
        case "time":
            minTime = tConstraints.get(avi * 3);
            strideTime = tConstraints.get(avi * 3 + 1);
            maxTime = tConstraints.get(avi * 3 + 2);
            break;
        }
        selectionSB.append(
                (avi == 0 ? "" : ",") + axisVariables[avi].sourceName() + "{" + tConstraints.get(avi * 3) + ":"
                        + tConstraints.get(avi * 3 + 1) + ":" + tConstraints.get(avi * 3 + 2) + "}"); //start:stop:stride !
    }
    String selection = selectionSB.toString();

    int nbLat = DataHelper.strideWillFind(maxLat - minLat + 1, strideLat);
    int nbLon = DataHelper.strideWillFind(maxLon - minLon + 1, strideLon);
    int nbTime = DataHelper.strideWillFind(maxTime - minTime + 1, strideTime);
    int total = nbLat * nbLon * nbTime;//data length

    if (reallyVerbose)
        String2.log("constraints : " + selection + "\nnb: lat=" + nbLat + ", lon=" + nbLon + ", time=" + nbTime
                + ", total size=" + total);
    int indexOut = 0;//index in data array

    BufferedInputStream bis = new BufferedInputStream( //because it supports "marks"
            new FileInputStream(fileDir + fileName));//may throw exception
    InputStream inStream;
    String ext = File2.getExtension(fileName);
    if (ext.equals(""))
        inStream = bis;
    else
        inStream = new CompressorStreamFactory().createCompressorInputStream(bis); //This inputStream must support "marks".
    //was just for .gz: else inStream = new GZIPInputStream(bis);

    try {

        byte[] in = new byte[NLON * NLAT * 2];
        short[] out1 = new short[total];
        double[] out2 = new double[total];

        //entire read of the file
        long t0 = System.currentTimeMillis();
        int indexIn = 0;
        byte[] buff = new byte[NLAT];
        int n = -2;
        while ((n = inStream.read(buff, 0, buff.length)) != -1) {
            System.arraycopy(buff, 0, in, indexIn, n);
            indexIn += n;
        }
        if (indexIn != in.length) {
            throw new FileSystemException("Merge file seems to be corrupted because size is : " + indexIn
                    + " and should be " + in.length);
        }
        if (verbose)
            String2.log("read file in " + (System.currentTimeMillis() - t0) + "ms");
        if (reallyVerbose)
            String2.logNoNewline("Closing file...");
        inStream.close();//I care about this exception                        
        inStream = null; //indicate it closed successfully
        if (reallyVerbose)
            String2.log("Done");

        if (reallyVerbose)
            String2.logNoNewline("Copy filtered data...");
        t0 = System.currentTimeMillis();
        for (int t = minTime; t <= maxTime; t += strideTime) {//[0 - 1]
            int offsetTime = NLON * NLAT * t;
            for (int la = minLat; la <= maxLat; la += strideLat) {
                int offsetLat = NLON * la;
                for (int lo = minLon; lo <= maxLon; lo += strideLon) {
                    short value = (short) (in[offsetTime + offsetLat + lo] & 0xff);
                    value += 75;
                    out1[indexOut] = value;
                    out2[indexOut] = ((double) Math.round(T2F(value) * 10.)) / 10.;
                    indexOut++;
                }
            }
        }
        if (reallyVerbose) {
            String2.log("Done in " + (System.currentTimeMillis() - t0) + "ms");
            if (total >= 10) {
                String2.log("Log the 10 last values :");
                int i = 0;
                while (i != 10) {
                    int j = total - 1 - i;
                    String2.log("\tT[" + j + "] = " + out1[j] + "F[" + j + "] = " + out2[j]);
                    i++;
                }
            }
        }

        in = null;
        if (nbDataVariable == 2) {
            paa[0] = PrimitiveArray.factory(out1);
            paa[1] = PrimitiveArray.factory(out2);
        } else if (nbDataVariable == 1) {
            if (tDataVariables[0].sourceName().equalsIgnoreCase("ir")) {
                paa[0] = PrimitiveArray.factory(out1);
            } else {
                paa[0] = PrimitiveArray.factory(out2);
            }
        } //else 0

    } catch (Throwable t) {
        //make sure it is explicitly closed
        if (inStream != null) {
            try {
                inStream.close();
            } catch (Throwable t2) {
                if (verbose)
                    String2.log("2nd attempt to close also failed:\n" + MustBe.throwableToShortString(t2));
            }
        }
        if (verbose)
            String2.log("Error while reading " + fileDir + fileName);
        throw t;
    }
    return paa;
}