Example usage for java.io File length

List of usage examples for java.io File length

Introduction

In this page you can find the example usage for java.io File length.

Prototype

public long length() 

Source Link

Document

Returns the length of the file denoted by this abstract pathname.

Usage

From source file:org.mycontroller.standalone.model.McTemplate.java

@JsonIgnore
public static McTemplate get(String fileName) throws IllegalAccessException, IOException {
    File templateFile = FileUtils.getFile(AppProperties.getInstance().getTemplatesLocation() + fileName);
    return McTemplate.builder().extension(FilenameUtils.getExtension(templateFile.getCanonicalPath()))
            .canonicalPath(templateFile.getCanonicalPath()).name(templateFile.getCanonicalPath())
            .lastModified(templateFile.lastModified()).size(templateFile.length()).build();
}

From source file:com.vmware.identity.openidconnect.sample.RelyingPartyInstaller.java

static PublicKey loadPublicKey(String file, String algorithm)
        throws IOException, NoSuchAlgorithmException, InvalidKeySpecException {
    // Read Public Key.
    File filePublicKey = new File(file);
    FileInputStream fis = new FileInputStream(file);
    byte[] encodedPublicKey = new byte[(int) filePublicKey.length()];
    fis.read(encodedPublicKey);//  w  w  w  .j a v  a2  s  .  c  o m
    fis.close();

    // Generate Public Key.
    KeyFactory keyFactory = KeyFactory.getInstance(algorithm);
    X509EncodedKeySpec publicKeySpec = new X509EncodedKeySpec(encodedPublicKey);
    PublicKey publicKey = keyFactory.generatePublic(publicKeySpec);

    return publicKey;
}

From source file:com.jeet.cli.Admin.java

private static void checkIntegrity(Map fileMap) {
    System.out.print("Enter File Index(0 to cancel): ");
    String choise = null;//  ww  w.j  a v  a 2 s. co m
    try {
        choise = bufferRead.readLine();
    } catch (IOException ioe) {
        System.err.println("Error in reading option.");
        System.err.println("Please try again.");
        checkIntegrity(fileMap);
    }
    if (choise != null && NumberUtils.isDigits(choise)) {
        Integer choiseInt = Integer.parseInt(choise);
        if (fileMap.containsKey(choiseInt)) {
            try {
                String key = fileMap.get(choiseInt).toString();
                File file = FileUtil.downloadAndDecryptFile(key);
                Long fileLength = file.length();
                if (FileUtil.getHash(key.split("/")[1]).equals(HashUtil.generateFileHash(file))) {
                    Map userMetadata = S3Connect.getUserMetadata(key);
                    //                        System.out.println(userMetadata.get(Constants.LAST_MODIFIED_KEY));
                    //                        System.out.println(S3Connect.getLastModified(key).getTime());
                    //check last access time
                    if (userMetadata.containsKey(Constants.LAST_MODIFIED_KEY)) {
                        Long millisFromMettaData = Long
                                .valueOf(userMetadata.get(Constants.LAST_MODIFIED_KEY).toString());
                        Long millisFromS3 = S3Connect.getLastModified(key).getTime();
                        Seconds difference = Seconds.secondsBetween(new DateTime(millisFromMettaData),
                                new DateTime(millisFromS3));
                        if (difference.getSeconds() < Constants.LAST_MODIFIED_VARIANT) {
                            //check file length
                            if (userMetadata.containsKey(Constants.FILE_LENGTH_KEY) && fileLength.toString()
                                    .equals(userMetadata.get(Constants.FILE_LENGTH_KEY))) {
                                //check hash from user data
                                if (userMetadata.containsKey(Constants.HASH_KEY) && userMetadata
                                        .get(Constants.HASH_KEY).equals(FileUtil.getHash(key.split("/")[1]))) {
                                    System.out.println(ANSI_GREEN + "Data integrity is preserved.");
                                } else {
                                    System.out.println(ANSI_RED + "Data integrity is not preserved.");
                                }
                            } else {
                                System.out.println(ANSI_RED + "File is length does not matched.");
                            }
                        } else {
                            System.out.println(ANSI_RED + "File is modified outside the system.");
                        }
                    } else {
                        System.out.println(ANSI_RED + "File is modified outside the system.");
                    }
                } else {
                    System.out.println(ANSI_RED + "Data integrity is not preserved.");
                }

            } catch (Exception ex) {
                ex.printStackTrace();
                System.err.println("Error in downlaoding file.");
            }
            askFileListInputs(fileMap);
        } else if (choiseInt.equals(0)) {
            System.out.println("Check Integrity file canceled.");
            askFileListInputs(fileMap);
        } else {
            System.err.println("Please select from provided options only.");
            checkIntegrity(fileMap);
        }
    } else {
        System.err.println("Please enter digits only.");
        checkIntegrity(fileMap);
    }
}

From source file:com.vexsoftware.votifier.util.rsa.RSAIO.java

/**
 * Loads an RSA key pair from a directory. The directory must have the files
 * "public.key" and "private.key"./*from w ww .  j a  v  a  2  s.  com*/
 * 
 * @param directory
 *            The directory to load from
 * @return The key pair
 * @throws Exception
 *             If an error occurs
 */
public static KeyPair load(File directory) throws Exception {
    // Read the public key file.
    File publicKeyFile = new File(directory + "/public.key");
    FileInputStream in = null;
    byte[] encodedPublicKey;
    try {
        in = new FileInputStream(directory + "/public.key");
        encodedPublicKey = new byte[(int) publicKeyFile.length()];
        in.read(encodedPublicKey);
        encodedPublicKey = DatatypeConverter.parseBase64Binary(new String(encodedPublicKey));
    } finally {
        try {
            in.close();
        } catch (Exception exception) {
            // ignore
        }
    }

    // Read the private key file.
    File privateKeyFile = new File(directory + "/private.key");
    byte[] encodedPrivateKey;
    try {
        in = new FileInputStream(directory + "/private.key");
        encodedPrivateKey = new byte[(int) privateKeyFile.length()];
        in.read(encodedPrivateKey);
        encodedPrivateKey = DatatypeConverter.parseBase64Binary(new String(encodedPrivateKey));
    } finally {
        try {
            in.close();
        } catch (Exception exception) {
            // ignore
        }
    }

    // Instantiate and return the key pair.
    KeyFactory keyFactory = KeyFactory.getInstance("RSA");
    X509EncodedKeySpec publicKeySpec = new X509EncodedKeySpec(encodedPublicKey);
    PublicKey publicKey = keyFactory.generatePublic(publicKeySpec);
    PKCS8EncodedKeySpec privateKeySpec = new PKCS8EncodedKeySpec(encodedPrivateKey);
    PrivateKey privateKey = keyFactory.generatePrivate(privateKeySpec);
    return new KeyPair(publicKey, privateKey);
}

From source file:com.vmware.identity.openidconnect.sample.RelyingPartyInstaller.java

static PrivateKey loadPrivateKey(String file, String algorithm)
        throws IOException, NoSuchAlgorithmException, InvalidKeySpecException {
    // Read Private Key.
    File filePrivateKey = new File(file);
    FileInputStream fis = new FileInputStream(file);
    byte[] encodedPrivateKey = new byte[(int) filePrivateKey.length()];
    fis.read(encodedPrivateKey);//from  ww  w .  ja  v  a  2s .  c om
    fis.close();

    // Generate KeyPair.
    KeyFactory keyFactory = KeyFactory.getInstance(algorithm);
    PKCS8EncodedKeySpec privateKeySpec = new PKCS8EncodedKeySpec(encodedPrivateKey);
    PrivateKey privateKey = keyFactory.generatePrivate(privateKeySpec);

    return privateKey;
}

From source file:Main.java

/** Return true if two files are exactly equal.
 * This will call <code>zipEquals()</code> if both files
 * are zip files./*  w  w  w . ja v  a 2s  .co m*/
 */
public static boolean equals(File file1, File file2) throws IOException {
    if (isZip(file1) && isZip(file2)) {
        return zipEquals(file1, file2);
    }

    if (file1.length() != file2.length())
        return false;

    InputStream in1 = null;
    InputStream in2 = null;
    try {
        in1 = new FileInputStream(file1);
        in2 = new FileInputStream(file2);
        return equals(in1, in2);
    } finally {
        try {
            if (in1 != null)
                in1.close();
        } catch (IOException e) {
        }
        try {
            if (in2 != null)
                in2.close();
        } catch (IOException e) {
        }
    }
}

From source file:com.bc.util.io.FileUtils.java

/**
 * Retrieves the size of a file object. Three cases can be distinguished:
 * <ul>//from w w  w .jav a  2  s.  c  o m
 * <li>The file object denotes a file: returns the result of File.length()</li>
 * <li>The file object denotes a directory: returns the iteratively accumulated size of all files contained</li>
 * <li>The file object does not exist or is null: returns 0</li>
 * </ul>
 *
 * @param file the file to check
 * @return the size
 */
public static long getSizeInBytes(File file) {
    if ((file == null) || (!file.exists())) {
        return 0;
    }

    if (file.isFile()) {
        return file.length();
    }

    long size = 0;

    final File[] content = file.listFiles();
    if (content == null) {
        return 0;
    }
    for (int i = 0; i < content.length; i++) {
        size += getSizeInBytes(content[i]);
    }

    return size;
}

From source file:com.google.feedserver.util.BeanCliHelper.java

/**
 * Helper that loads a file into a string.
 * /* w w  w.  ja  v  a  2  s  .c o m*/
 * @param fileName properties file with rules configuration.
 * @returns a String representing the file.
 * @throws IOException if any errors are encountered reading the properties
 *         file
 */
static String readFileIntoString(final String fileName) throws IOException {
    if (testFileContents != null) {
        return testFileContents;
    }
    File file = new File(fileName);
    byte[] fileContents = new byte[(int) file.length()];
    new BufferedInputStream(new FileInputStream(fileName)).read(fileContents);
    return new String(fileContents);
}

From source file:com.hazelcast.example.mapreduce.downloader.ImageDownloaderMapper.java

public static Long saveImage(String sourceUrl, String destUrl) {

    File file = new File(destUrl);
    URL url;//from   w w  w  .j  av a 2 s  .c o m
    try {
        //url = new URL(URLEncoder.encode(sourceUrl, "UTF-8"));
        url = new URL(sourceUrl);
        FileUtils.copyURLToFile(url, file);
    } catch (MalformedURLException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    }

    return file.length();
}

From source file:$.FileUtils.java

public static void urlToPath(URL url, File fOut) throws IOException {
        URLConnection uc = url.openConnection();
        logger.info("ContentType: " + uc.getContentType());
        InputStream in = uc.getInputStream();
        org.apache.commons.io.FileUtils.copyInputStreamToFile(in, fOut);
        logger.info("File of length " + fOut.length() + " created from URL " + url.toString());
        in.close();/*from  w w  w .jav a2  s .co m*/
    }