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.n52.sos.XmlToExiConverter.java

protected void decode(String fileName) {
    try (InputStream exiIS = FileUtils.openInputStream(getFile(fileName, EXI_EXTENSION));
            OutputStream os = FileUtils.openOutputStream(getFile(fileName, XML_EXTENSION_2))) {

        Reader reader = new InputStreamReader(exiIS, "ISO-8859-1");
        InputSource is = new InputSource(reader);
        is.setEncoding("ISO-8859-1");

        TransformerFactory tf = TransformerFactory.newInstance();
        Transformer transformer = tf.newTransformer();

        EXISource exiSource = new EXISource();
        XMLReader exiReader = exiSource.getXMLReader();
        SAXSource saxSource = new SAXSource(is);
        //            SAXSource saxSource = new SAXSource(new InputSource(exiIS));
        exiSource.setXMLReader(exiReader);
        transformer.transform(saxSource, new StreamResult(os));
    } catch (Exception e) {
        System.out.println(e.getStackTrace());
    }/*from www .  jav  a  2 s  .  c om*/
}

From source file:org.nd4j.linalg.util.SerializationUtils.java

@SuppressWarnings("unchecked")
public static <T> T readObject(File file) {
    try {/*from  w w  w . jav  a2s . c om*/
        ObjectInputStream ois = new ObjectInputStream(FileUtils.openInputStream(file));
        T ret = (T) ois.readObject();
        ois.close();
        return ret;
    } catch (Exception e) {
        throw new RuntimeException(e);
    }

}

From source file:org.okj.commons.web.fileupload.FtpFileUploadToolkit.java

/** 
 * @see org.storevm.commons.web.fileupload.FileUploadToolkit#upload(org.storevm.commons.model.UploadFile, javax.servlet.http.HttpServletRequest)
 *//*w  ww  . jav a 2  s .  c om*/
@Override
public void upload(UploadFile file, HttpServletRequest request) {
    //
    File storeFile = getStoreUploadFile(file, request);

    //
    file.setMode(UploadMode.FTP);
    file.setPath(storeFile.getPath());

    //
    if (fileConverter != null) {
        Object value = fileConverter.convert(file);
        if (value != null) {
            request.setAttribute(CommonsKeys.CONVERT_RESULT, value);
        }
    }

    FtpUtils ftpUtils = FtpUtils.getInstance();

    //
    ftpUtils.connect();

    //
    ftpUtils.login();

    //
    try {
        InputStream in = FileUtils.openInputStream(file.getTmpFile());
        ftpUtils.uploadFile(in, file.getPath());
    } catch (IOException ex) {
        throw new FileUploadException(FileUploadErrorCode.SAVE_UPLOAD_FILE_FAIL);
    }

}

From source file:org.okj.im.core.service.QQHttpService.java

/**
 * /*w  w w  . j  av a 2  s  . c o m*/
 * @param filename
 * @param os
 */
protected void uploadFile(String filename, OutputStream os) {
    FileInputStream is = null;
    try {
        if (StringUtils.isNotBlank(filename)) {
            is = FileUtils.openInputStream(new File(filename)); //??

            // POST
            // Read bytes until EOF to write
            byte[] buffer = new byte[4096];
            int bytes_read; // How many bytes in buffer
            while ((bytes_read = is.read(buffer)) != -1) {
                os.write(buffer, 0, bytes_read);
            }
            os.flush(); //
        }
    } catch (IOException ex) {
        LogUtils.error(LOGGER, "?", ex);
    } finally {
        //?
        IOUtils.closeQuietly(is);
        IOUtils.closeQuietly(os);
    }
}

From source file:org.onesec.raven.ivr.actions.SayAmountActionTest.java

public static Node createNumbersNode(Tree tree) throws Exception {
    ContainerNode numbers = new ContainerNode("numbers");
    tree.getRootNode().addAndSaveChildren(numbers);
    assertTrue(numbers.start());//ww  w .j  av  a2  s .c o m

    File[] sounds = new File("src/test/wav/numbers").listFiles();
    for (File sound : sounds) {
        if (sound.isDirectory())
            continue;
        AudioFileNode audioNode = new AudioFileNode();
        audioNode.setName(FilenameUtils.getBaseName(sound.getName()));
        numbers.addAndSaveChildren(audioNode);
        audioNode.getAudioFile().setDataStream(FileUtils.openInputStream(sound));
        assertTrue(audioNode.start());
    }

    return numbers;
}

From source file:org.onesec.raven.ivr.impl.FileInputStreamSource.java

public InputStream getInputStream() {
    try {// w w  w .  j  av  a  2  s. c  o  m
        return FileUtils.openInputStream(file);
    } catch (IOException ex) {
        if (owner.isLogLevelEnabled(LogLevel.ERROR))
            owner.getLogger().error(ex.getMessage(), ex);
        return null;
    }
}

From source file:org.onesec.raven.ivr.queue.actions.SayNumberInQueueActionTest.java

@Before
public void prepare() throws Exception {
    resourceManager = registry.getService(ResourceManager.class);
    ResourcesNode resources = (ResourcesNode) tree.getRootNode().getChildren(ResourcesNode.NAME);
    resources.setDefaultLocale(new Locale("ru"));

    executor = new ExecutorServiceNode();
    executor.setName("executor");
    tree.getRootNode().addAndSaveChildren(executor);
    executor.setCorePoolSize(20);// w  w w.  j  av a2  s .c  om
    executor.setMaximumPoolSize(20);
    assertTrue(executor.start());

    conv = new TestEndpointConversationNode();
    conv.setName("endpoint");
    tree.getRootNode().addAndSaveChildren(conv);
    conv.setExecutorService(executor);
    conv.setFileName("target/number_in_queue.wav");
    //        assertTrue(conv.start());

    //        numbers = SayAmountActionTest.createNumbersNode(tree);
    numbers = resourceManager.getResource(Constants.NUMBERS_FEMALE_RESOURCE, null);
    assertNotNull(numbers);

    preamble = new AudioFileNode();
    preamble.setName("preamble");
    tree.getRootNode().addAndSaveChildren(preamble);
    preamble.getAudioFile()
            .setDataStream(FileUtils.openInputStream(new File("src/test/wav/number_in_queue.wav")));
    assertTrue(preamble.start());

    owner = new SayNumberInQueueActionNode();
    owner.setName("say number in queue");
    tree.getRootNode().addAndSaveChildren(owner);
    //        assertTrue(owner.start());
}

From source file:org.openhab.model.core.internal.folder.FolderObserver.java

private void checkFolder(String foldername) {
    File folder = getFolder(foldername);
    if (!folder.exists()) {
        return;/*from w  w w. j  a v a 2 s.  c om*/
    }
    String[] extensions = folderFileExtMap.get(foldername);

    // check current files and add or refresh them accordingly
    Set<String> currentFileNames = new HashSet<String>();
    for (File file : folder.listFiles()) {
        if (file.isDirectory())
            continue;
        if (!file.getName().contains("."))
            continue;
        if (file.getName().startsWith("."))
            continue;

        // if there is an extension filter defined, continue if the file has a different extension
        String fileExt = getExtension(file.getName());
        if (extensions != null && extensions.length > 0 && !ArrayUtils.contains(extensions, fileExt))
            continue;

        currentFileNames.add(file.getName());
        Long timeLastCheck = lastCheckedMap.get(file.getName());
        if (timeLastCheck == null)
            timeLastCheck = 0L;
        if (FileUtils.isFileNewer(file, timeLastCheck)) {
            if (modelRepo != null) {
                try {
                    if (modelRepo.addOrRefreshModel(file.getName(), FileUtils.openInputStream(file))) {
                        lastCheckedMap.put(file.getName(), new Date().getTime());
                    }
                } catch (IOException e) {
                    logger.warn("Cannot open file '" + file.getAbsolutePath() + "' for reading.", e);
                }
            }
        }
    }

    // check for files that have been deleted meanwhile
    if (lastFileNames.get(foldername) != null) {
        for (String fileName : lastFileNames.get(foldername)) {
            if (!currentFileNames.contains(fileName)) {
                logger.info("File '{}' has been deleted", fileName);
                if (modelRepo != null) {
                    modelRepo.removeModel(fileName);
                    lastCheckedMap.remove(fileName);
                }
            }
        }
    }
    lastFileNames.put(foldername, currentFileNames);
}

From source file:org.openhim.mediator.fhir.MediatorMain.java

private static MediatorConfig loadConfig(String configPath)
        throws IOException, RoutingTable.RouteAlreadyMappedException {
    MediatorConfig config = new MediatorConfig();

    if (configPath != null) {
        Properties props = new Properties();
        File conf = new File(configPath);
        InputStream in = FileUtils.openInputStream(conf);
        props.load(in);/* w  ww .  j a  va2  s.  co m*/
        IOUtils.closeQuietly(in);

        config.setProperties(props);
    } else {
        config.setProperties("mediator.properties");
    }

    config.setName(config.getProperty("mediator.name"));
    config.setServerHost(config.getProperty("mediator.host"));
    config.setServerPort(Integer.parseInt(config.getProperty("mediator.port")));
    config.setRootTimeout(Integer.parseInt(config.getProperty("mediator.timeout")));

    config.setCoreHost(config.getProperty("core.host"));
    config.setCoreAPIUsername(config.getProperty("core.api.user"));
    config.setCoreAPIPassword(config.getProperty("core.api.password"));
    if (config.getProperty("core.api.port") != null) {
        config.setCoreAPIPort(Integer.parseInt(config.getProperty("core.api.port")));
    }

    config.setRoutingTable(buildRoutingTable());
    config.setStartupActors(buildStartupActorsConfig());

    InputStream regInfo = MediatorMain.class.getClassLoader()
            .getResourceAsStream("mediator-registration-info.json");
    RegistrationConfig regConfig = new RegistrationConfig(regInfo);
    config.setRegistrationConfig(regConfig);

    if (config.getProperty("mediator.heartbeats") != null
            && "true".equalsIgnoreCase(config.getProperty("mediator.heartbeats"))) {
        config.setHeartbeatsEnabled(true);
    }

    return config;
}

From source file:org.openhim.mediator.MediatorMain.java

private static MediatorConfig loadConfig(String configPath)
        throws IOException, RoutingTable.RouteAlreadyMappedException {
    MediatorConfig config = new MediatorConfig();

    if (configPath != null) {
        Properties props = new Properties();
        File conf = new File(configPath);
        InputStream in = FileUtils.openInputStream(conf);
        props.load(in);//from w  ww  . jav a  2s  .  c o m
        IOUtils.closeQuietly(in);

        config.setProperties(props);
    } else {
        config.setProperties("mediator.properties");
    }

    config.setName(config.getProperty("mediator.name"));
    config.setServerHost(config.getProperty("mediator.host"));
    config.setServerPort(Integer.parseInt(config.getProperty("mediator.port")));
    config.setRootTimeout(Integer.parseInt(config.getProperty("mediator.timeout")));

    config.setCoreHost(config.getProperty("core.host"));
    config.setCoreAPIUsername(config.getProperty("core.api.user"));
    config.setCoreAPIPassword(config.getProperty("core.api.password"));
    if (config.getProperty("core.api.port") != null) {
        config.setCoreAPIPort(Integer.parseInt(config.getProperty("core.api.port")));
    }

    config.setRoutingTable(buildRoutingTable());

    InputStream regInfo = MediatorMain.class.getClassLoader()
            .getResourceAsStream("mediator-registration-info.json");
    RegistrationConfig regConfig = new RegistrationConfig(regInfo);
    config.setRegistrationConfig(regConfig);

    if (config.getProperty("mediator.heartbeats") != null
            && "true".equalsIgnoreCase(config.getProperty("mediator.heartbeats"))) {
        config.setHeartbeatsEnabled(true);
    }

    return config;
}