Example usage for org.apache.commons.io FileUtils openInputStream

List of usage examples for org.apache.commons.io FileUtils openInputStream

Introduction

In this page you can find the example usage for org.apache.commons.io FileUtils openInputStream.

Prototype

public static FileInputStream openInputStream(File file) throws IOException 

Source Link

Document

Opens a FileInputStream for the specified file, providing better error messages than simply calling new FileInputStream(file).

Usage

From source file:org.craftercms.studio.impl.v1.repository.disk.DiskContentRepository.java

@Override
public InputStream getContent(String path) throws ContentNotFoundException {
    InputStream retStream = null;

    try {/*from www  . j a  va2s . com*/
        File file = constructRepoPath(path).toFile();
        retStream = new BufferedInputStream(FileUtils.openInputStream(file));
    }

    catch (Exception err) {
        throw new ContentNotFoundException("error while opening file", err);
    }

    return retStream;
}

From source file:org.crs4.entando.innomanager.aps.system.services.layer.LayerManager.java

@Override
public File unzipShapeFile(String layername, File zipFile, String zipFileName) {
    int page = 0;
    InputStream fis;/*from w  ww.jav a2  s.c o  m*/
    ZipInputStream zis;
    FileOutputStream fos;
    byte[] buffer = new byte[1024];
    String type;
    File newFile;
    String path = "";
    WorkLayer layer;
    try {
        layer = this.getWorkLayer(layername);
        path = getShapeFileDiskFolder(layername);
        if (layer == null)
            return null;
    } catch (Throwable t) {
        ApsSystemUtils.logThrowable(t, this, "UnZipShapeFile");
        return null;
    }
    if (!zipFileName.substring(zipFileName.length() - 4).equals(".zip"))
        return null;
    String fileName = null;
    String shapefileName = null;
    //try to unzip 
    boolean reset = false;
    try {
        fis = FileUtils.openInputStream(zipFile);
        zis = new ZipInputStream(fis);
        //get the zipped file list entry
        ZipEntry ze = zis.getNextEntry();

        boolean[] ext = { true, true, true };
        // controllo contenuto zip ( only the first 3 files: .shx, .shp, .dbf with the same name)
        while (ze != null) {
            type = null;
            ApsSystemUtils.getLogger().info("parse file: " + ze.getName());
            if (ze.getName().substring(ze.getName().length() - 4).equals(".shp")
                    && (shapefileName == null
                            || ze.getName().substring(0, ze.getName().length() - 4).equals(shapefileName))
                    && ext[0]) {
                type = ".shp";
                ext[0] = false;
            } else if (ze.getName().substring(ze.getName().length() - 4).equals(".dbf")
                    && (shapefileName == null
                            || ze.getName().substring(0, ze.getName().length() - 4).equals(shapefileName))
                    && ext[1]) {
                type = ".dbf";
                ext[1] = false;
            } else if (ze.getName().substring(ze.getName().length() - 4).equals(".shx")
                    && (shapefileName == null
                            || ze.getName().substring(0, ze.getName().length() - 4).equals(shapefileName))
                    && ext[2]) {
                type = ".shx";
                ext[2] = false;
            }
            // else the shapefiles haven't good ext or name 
            ApsSystemUtils.getLogger().info("type: " + type);

            // set the correct name of the shapefiles  (the first valid)

            if (type != null) {
                if (fileName == null) {
                    shapefileName = ze.getName().substring(0, ze.getName().length() - 4);
                    fileName = zipFileName.substring(0, zipFileName.length() - 4);
                    boolean found = false;
                    /// if exist changename
                    while (!found) {
                        newFile = new File(path + fileName + ".shp");
                        if (newFile.exists())
                            fileName += "0";
                        else
                            found = true;
                    }
                }
                ApsSystemUtils.getLogger().info("file write: " + path + fileName + type);
                newFile = new File(path + fileName + type);
                if (newFile.exists())
                    FileUtils.forceDelete(newFile);
                newFile = new File(path + fileName + type);
                {
                    fos = new FileOutputStream(newFile);
                    int len;
                    while ((len = zis.read(buffer)) > 0) {
                        fos.write(buffer, 0, len);
                    }
                    fos.close();
                    zis.closeEntry();
                }
            }
            ze = zis.getNextEntry();
        }
        zis.close();
    } catch (Throwable t) {
        ApsSystemUtils.logThrowable(t, this, "UnZippingShapeFile");
        reset = true;
    }
    if (fileName != null) {
        if (reset) {
            removeShapeFile(layername, fileName + ".shp");
        } else {
            newFile = new File(path + fileName + ".shp");
            if (newFile.exists())
                return newFile;
        }
    }
    return null;
}

From source file:org.cubictest.common.settings.CubicTestProjectSettings.java

/**
 * Looks up a resource named test-project.properties in the classpath.
 * Caches the result.//  ww w . ja  v  a  2 s. co m
 */
private void loadProperties(File propsFile) {
    InputStream in = null;

    try {
        in = FileUtils.openInputStream(propsFile);
        properties = new Properties();
        properties.load(in);
    } catch (Exception e) {
        ErrorHandler.logAndRethrow("Error loading properties", e);
    } finally {
        if (in != null) {
            try {
                in.close();
            } catch (Exception e2) {
                Logger.warn("Error closing input stream from property file.");
            }
        }
    }
}

From source file:org.cubictest.model.i18n.Language.java

private Properties getProperties() {
    if (properties == null) {
        try {// w  w w  .j  a  v a  2  s . c  o m
            properties = new Properties();
            properties.load(FileUtils.openInputStream(FileUtil.getFileFromWorkspaceRoot(fileName)));
        } catch (IOException e) {
            ErrorHandler.logAndShowErrorDialogAndRethrow(e);
        }
    }
    return properties;
}

From source file:org.cubictest.model.i18n.Language.java

public boolean updateLanguage() {
    boolean success = false;
    try {//w  w w .ja v  a2 s  . c o m
        properties = new Properties();
        properties.load(FileUtils.openInputStream(FileUtil.getFileFromWorkspaceRoot(fileName)));
        success = true;
    } catch (IOException e) {
        ErrorHandler.logAndShowErrorDialog(e);
    }
    return success;
}

From source file:org.dataconservancy.archive.impl.elm.fs.FsEntityStore.java

public InputStream get(String entityId) throws EntityNotFoundException {

    try {/*from w w w . java 2s .c om*/
        return FileUtils.openInputStream(getFile(entityId));
    } catch (FileNotFoundException e) {
        throw new EntityNotFoundException(entityId);
    } catch (IOException e) {
        throw new RuntimeException(e);
    }
}

From source file:org.dbflute.intro.app.web.document.DocumentAction.java

private StreamResponse createHtmlStreamResponse(File file) {
    // done deco dummy name or comment about it by jflute (2017/01/12)
    StreamResponse stream = asStream("schema-...html"); // dummy name because unused (not download)
    stream.headerContentDispositionInline();
    return stream.contentType("text/html; encoding=\"UTF-8\"").stream(out -> {
        try (InputStream ins = FileUtils.openInputStream(file)) {
            out.write(ins);/* www .  j  av  a  2  s  . co m*/
        }
    });
}

From source file:org.deegree.metadata.persistence.ebrim.eo.EbrimEOMDStore.java

/**
 * Creates a new {@link EbrimEOMDStore} instance.
 * //from   w  ww . ja v a 2 s  .com
 * @param connId
 *            id of the JDBC connection to use, must not be <code>null</code>
 * @param queriesDir
 *            directory containing individual AdhocQuery files (*.xml), can be <code>null</code>
 * @param profile
 *            RegistryPackage containing the profile informations, can be <code>null</code>
 * @param queryTimeout
 *            number of milliseconds to allow for queries, or <code>0</code> (unlimited)
 * @throws ResourceInitException
 */
public EbrimEOMDStore(String connId, File queriesDir, RegistryPackage profile, Date lastModified,
        long queryTimeout, ResourceMetadata<MetadataStore<? extends MetadataRecord>> metadata,
        Workspace workspace) throws ResourceInitException {
    this.connId = connId;
    this.profile = profile;
    this.lastModified = lastModified;
    this.queryTimeout = queryTimeout;
    this.metadata = metadata;
    this.workspace = workspace;
    if (queriesDir != null) {
        File[] listFiles = queriesDir.listFiles(new FilenameFilter() {
            @Override
            public boolean accept(File arg0, String arg1) {
                return arg1.endsWith(".xml");
            }
        });

        if (listFiles != null) {
            for (File file : listFiles) {
                FileInputStream is = null;
                try {
                    is = FileUtils.openInputStream(file);
                    XMLStreamReader xmlStream = XMLInputFactory.newInstance().createXMLStreamReader(is);
                    AdhocQuery query = new AdhocQuery(xmlStream);
                    idToQuery.put(query.getId(), query);
                    LOG.info("Found adhocQuery " + file + " with id " + query.getId());
                } catch (Throwable t) {
                    LOG.error(t.getMessage(), t);
                    throw new ResourceInitException(t.getMessage());
                } finally {
                    try {
                        if (is != null) {
                            is.close();
                        }
                    } catch (IOException e) {
                        LOG.info("Could not close InputStream");
                    }
                }
            }
        }
    }
}

From source file:org.deeplearning4j.base.MnistFetcher.java

private boolean checkMD5OfFile(String targetMD5, File file) throws IOException {
    InputStream in = FileUtils.openInputStream(file);
    String trueMd5 = DigestUtils.md5Hex(in);
    IOUtils.closeQuietly(in);//from   w ww  .  j ava2 s  . c o m
    return (targetMD5.equals(trueMd5));
}

From source file:org.deeplearning4j.util.SerializationUtils.java

@SuppressWarnings("unchecked")
public static <T> T readObject(File file) {
    ObjectInputStream ois = null;
    try {//  ww w. j a v  a2  s .  c  om
        ois = new ObjectInputStream(FileUtils.openInputStream(file));
        return (T) ois.readObject();
    } catch (Exception e) {
        throw new RuntimeException(e);
    } finally {
        if (ois != null)
            try {
                ois.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
    }

}