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.envirocar.app.util.Util.java

/**
 * Android devices up to version 2.3.7 have a bug in the native
 * Zip archive creation, making the archive unreadable with some
 * programs.//from w  w w.j a  va  2  s  .  com
 * 
 * @param files
 *            the list of files of the target archive
 * @param target
 *            the target filename
 * @throws IOException
 */
public static void zipInteroperable(List<File> files, String target) throws IOException {
    ZipArchiveOutputStream aos = null;
    OutputStream out = null;
    try {
        File tarFile = new File(target);
        out = new FileOutputStream(tarFile);

        try {
            aos = (ZipArchiveOutputStream) new ArchiveStreamFactory()
                    .createArchiveOutputStream(ArchiveStreamFactory.ZIP, out);
        } catch (ArchiveException e) {
            throw new IOException(e);
        }

        for (File file : files) {
            ZipArchiveEntry entry = new ZipArchiveEntry(file, file.getName());
            entry.setSize(file.length());
            aos.putArchiveEntry(entry);
            IOUtils.copy(new FileInputStream(file), aos);
            aos.closeArchiveEntry();
        }

    } catch (IOException e) {
        throw e;
    } finally {
        aos.finish();
        out.close();
    }
}

From source file:be.roots.taconic.pricingguide.service.PDFServiceTest.java

public static void saveAndTestByteArray(byte[] fileAsByteArray, Integer numberOfPagesExpected)
        throws IOException, DocumentException {

    try {/*from w  ww.  j ava  2s . co m*/

        final byte[] clone = fileAsByteArray.clone();

        assertNotNull(clone);

        File file = File.createTempFile(
                "result-" + Thread.currentThread().getStackTrace()[2].getMethodName() + "-", ".pdf");
        IOUtils.write(fileAsByteArray, new FileOutputStream(file));

        if (MANUAL_MODE) {
            LOGGER.info("Wrote test file to " + file.getAbsolutePath());
        } else {
            assertTrue(file.exists());
            assertTrue(file.length() > 0);
            file.delete();
        }

        if (numberOfPagesExpected != null) {
            PdfReader reader = new PdfReader(clone);
            assertEquals(numberOfPagesExpected.intValue(), reader.getNumberOfPages());
        }

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

From source file:com.github.cat.yum.store.util.YumUtil.java

private static void yumXmlSave(Document doc, RepoModule repo) throws IOException, NoSuchAlgorithmException {
    File outfile = getXmlFile(repo);
    xmlToFile(doc, outfile);//ww w  . j  av  a 2s .com

    String xmlCode = HashFile.getsum(outfile, ALGORITHM);
    repo.setXmlCode(xmlCode);
    repo.setXmlSize(outfile.length());
    GZipUtils.compress(outfile);

    outfile = getXmlGzFile(repo);
    String xmlGzode = HashFile.getsum(outfile, ALGORITHM);
    repo.setXmlGzCode(xmlGzode);
    repo.setXmlGzSize(outfile.length());

    outfile.renameTo(getXmlGzFile(repo, xmlGzode));
}

From source file:net.naijatek.myalumni.util.utilities.FileUtil.java

public static long getDirLength(final File dir) throws IOException {
    if (dir.isFile()) {
        throw new IOException("BadInputException: not a directory.");
    }//from  ww  w .j  a v  a 2 s  .c o m
    long size = 0;
    File[] files = dir.listFiles();
    if (files != null) {
        for (File file : files) {
            long length = 0;
            if (file.isFile()) {
                length = file.length();
            } else {
                length = getDirLength(file);
            }
            size += length;
        } //for
    } //if
    return size;
}

From source file:jeeves.utils.BinaryFile.java

public static String getContentLength(Element response) {
    String path = response.getAttributeValue("path");
    if (path == null)
        return null;
    String length = "-1";
    if (!remoteFile) {
        File f = new File(path);
        length = f.length() + "";
    }//from   w  w  w  .j  a  va2 s .c o  m
    return length;
}

From source file:de.nrw.hbz.deepzoomer.fileUtil.FileUtil.java

/**
 * <p><em>Title: Loads File into an Base64 encoded Stream</em></p>
 * <p>Description: </p>/*from w w  w  . j  a  v  a  2  s  .c o  m*/
 * 
 * @param origPidfFile
 * @return 
 */
public static byte[] loadFileIntoStream(File origPidfFile) {
    FileInputStream fis = null;
    byte[] pdfStream = null;
    byte[] pdfRawStream = null;
    try {
        fis = new FileInputStream(origPidfFile);
        int i = (int) origPidfFile.length();
        byte[] b = new byte[i];
        fis.read(b);
        pdfRawStream = b;
        pdfStream = Base64.encodeBase64(pdfRawStream);
    } catch (IOException ioExc) {
        System.out.println(ioExc);
        log.error(ioExc);
    } finally {
        if (fis != null) {
            try {
                fis.close();
            } catch (IOException ioExc) {
                log.error(ioExc);
            }
        }
    }
    return pdfStream;
}

From source file:fm.smart.r1.activity.CreateSoundActivity.java

public static byte[] getBytesFromFile(File file) throws IOException {
    InputStream is = new FileInputStream(file);
    // Get the size of the file
    long length = file.length();
    // You cannot create an array using a long type.
    // It needs to be an int type.
    // Before converting to an int type, check
    // to ensure that file is not larger than Integer.MAX_VALUE.
    if (length > Integer.MAX_VALUE) {
        // File is too large
    }//from   ww  w  .  ja  v  a  2  s  .  c o m

    // Create the byte array to hold the data
    byte[] bytes = new byte[(int) length];
    // Read in the bytes
    int offset = 0;
    int numRead = 0;
    while (offset < bytes.length && (numRead = is.read(bytes, offset, bytes.length - offset)) >= 0) {
        offset += numRead;
    }

    // Ensure all the bytes have been read in
    if (offset < bytes.length) {
        throw new IOException("Could not completely read file " + file.getName());
    }

    // Close the input stream and return bytes
    is.close();
    return bytes;
}

From source file:com.aurel.track.admin.server.dbbackup.DatabaseBackupBL.java

public static void zipDatabase(String backupName, boolean includeAttachments, PropertiesConfiguration tcfg)
        throws DatabaseBackupBLException {
    String driver = tcfg.getString("torque.dsfactory.track.connection.driver");
    if (driver != null) {
        driver = driver.trim();/*ww w .  j a v  a2  s .co  m*/
    }
    String url = tcfg.getString("torque.dsfactory.track.connection.url");
    if (url != null) {
        url = url.trim();
    }
    String user = tcfg.getString("torque.dsfactory.track.connection.user");
    if (user != null) {
        user = user.trim();
    }
    String password = tcfg.getString("torque.dsfactory.track.connection.password");
    String backupRootDir = ApplicationBean.getInstance().getSiteBean().getBackupDir();
    String backupDirTMP = backupRootDir + File.separator + DB_BACKUP_TMP_DIR;

    File tmpDir = new File(backupDirTMP);
    FileUtil.deltree(tmpDir);
    tmpDir.mkdirs();

    DatabaseInfo databaseInfo = new DatabaseInfo(driver, url, user, password);

    try {
        DataReader.writeDataToSql(databaseInfo, tmpDir.getAbsolutePath());
    } catch (DDLException e) {
        e.printStackTrace();
    }
    String fileNameData = tmpDir.getAbsolutePath() + File.separator + DataReader.FILE_NAME_DATA;
    long size = 0;
    try {
        File file = new File(fileNameData);
        size = file.length();
    } catch (Exception ex) {
        LOGGER.error(ExceptionUtils.getStackTrace(ex), ex);
    }
    if (size <= 1024) {
        LOGGER.error("Can't write database data to file. The file size to small! FileName:" + fileNameData
                + ",size=" + size + " bytes.");
        DatabaseBackupBLException ex = new DatabaseBackupBLException(
                "Can't write database data to file(File size to small:\"" + fileNameData + "\")");
        ex.setLocalizedKey("admin.server.databaseBackup.err.cantWriteDatabaseDataToFile");
        ex.setLocalizedParams(new Object[] { fileNameData });
        throw ex;
    }

    exportSchema(tmpDir);

    zipPart2(includeAttachments, backupName, tmpDir);

}

From source file:com.almarsoft.GroundhogReader.lib.FSUtils.java

public static long writeByteArrayToDiskFileAndGetSize(byte[] data, String directory, String fileName)
        throws IOException {

    File outDir = new File(directory);
    if (!outDir.exists())
        outDir.mkdirs();/* ww w. j av  a 2 s  . c om*/

    File outFile = new File(directory, fileName);

    BufferedOutputStream fos = new BufferedOutputStream(new FileOutputStream(outFile));

    try {
        fos.write(data);
    } finally {
        fos.close();
    }

    return outFile.length();
}

From source file:net.naijatek.myalumni.util.utilities.FileUtil.java

public static long getDirLength_onDisk(final File dir) throws IOException {
    if (dir.isFile()) {
        throw new IOException("BadInputException: not a directory.");
    }/*from w ww.j  a v a2s  .  co  m*/
    long size = 0;
    File[] files = dir.listFiles();
    if (files != null) {
        for (File file : files) {
            long length = 0;
            if (file.isFile()) {
                length = file.length();
            } else {
                length = getDirLength_onDisk(file);
            }
            double mod = Math.ceil((double) length / 512);
            if (mod == 0) {
                mod = 1;
            }
            length = (long) mod * 512;
            size += length;
        }
    } //if
    return size;
}