Example usage for java.io FileInputStream close

List of usage examples for java.io FileInputStream close

Introduction

In this page you can find the example usage for java.io FileInputStream close.

Prototype

public void close() throws IOException 

Source Link

Document

Closes this file input stream and releases any system resources associated with the stream.

Usage

From source file:gov.pnnl.cloud.KinesisApplication.java

/**
 * @param propertiesFile//  w ww . ja v a 2s  . c  o  m
 * @throws IOException Thrown when we run into issues reading properties
 */
private static void loadProperties(String propertiesFile) throws IOException {
    FileInputStream inputStream = new FileInputStream(propertiesFile);
    Properties properties = new Properties();
    try {
        properties.load(inputStream);
    } finally {
        inputStream.close();
    }

    String appNameOverride = properties.getProperty(ConfigKeys.APPLICATION_NAME_KEY);
    if (appNameOverride != null) {
        applicationName = appNameOverride;
    }
    LOG.info("Using application name " + applicationName);

    String streamNameOverride = properties.getProperty(ConfigKeys.STREAM_NAME_KEY);
    if (streamNameOverride != null) {
        streamName = streamNameOverride;
    }
    LOG.info("Using stream name " + streamName);

    String kinesisEndpointOverride = properties.getProperty(ConfigKeys.KINESIS_ENDPOINT_KEY);
    if (kinesisEndpointOverride != null) {
        kinesisEndpoint = kinesisEndpointOverride;
    }
    LOG.info("Using Kinesis endpoint " + kinesisEndpoint);

    String initialPositionOverride = properties.getProperty(ConfigKeys.INITIAL_POSITION_IN_STREAM_KEY);
    if (initialPositionOverride != null) {
        initialPositionInStream = InitialPositionInStream.valueOf(initialPositionOverride);
    }
    LOG.info("Using initial position " + initialPositionInStream.toString()
            + " (if a checkpoint is not found).");

    String kafkaBrokerListProperty = properties.getProperty(ConfigKeys.KAFKA_BROKER_LIST);
    if (kafkaBrokerListProperty == null) {
        throw new IOException("kafkaBrokerList cannot be null");
    }
    kafkaBrokerList = kafkaBrokerListProperty;
    LOG.info("Using Kafka broker list " + kafkaBrokerList);

}

From source file:Main.java

public static String getFileMD5(File file) {
    if (!file.isFile()) {
        return null;
    }/*from w ww .j av a 2s.  c om*/
    MessageDigest digest = null;
    FileInputStream in = null;
    byte buffer[] = new byte[1024];
    int len;
    try {
        digest = MessageDigest.getInstance("MD5");
        in = new FileInputStream(file);
        while ((len = in.read(buffer, 0, 1024)) != -1) {
            digest.update(buffer, 0, len);
        }
        in.close();
    } catch (Exception e) {
        e.printStackTrace();
        return null;
    }
    BigInteger bigInt = new BigInteger(1, digest.digest());
    return bigInt.toString(16);
}

From source file:com.data2semantics.yasgui.server.db.ConnectionFactory.java

private static void applyDeltas(Connection connect, File configDir)
        throws UnsupportedEncodingException, IOException, SQLException {
    @SuppressWarnings("unchecked")
    ArrayList<File> listFiles = new ArrayList<File>(
            FileUtils.listFiles(new File(configDir.getAbsolutePath() + "/config"),
                    FileFilterUtils.prefixFileFilter("delta_"), null));

    TreeMap<Integer, File> files = new TreeMap<Integer, File>();
    for (File file : listFiles) {
        String basename = file.getName();
        basename = basename.substring("delta_".length());
        basename = basename.substring(0, basename.length() - ".sql".length());
        int index = Integer.parseInt(basename);
        files.put(index, file);//from w ww  .  j  a v  a 2s. com
    }
    ArrayList<Integer> currentDeltas = getDeltas(connect);
    //treemap is naturally sorted, so just iterate through them
    for (Entry<Integer, File> entry : files.entrySet()) {
        if (!currentDeltas.contains(entry.getKey())) {
            ScriptRunner runner = new ScriptRunner(connect, false, true);
            FileInputStream fileStream = new FileInputStream(entry.getValue());
            runner.runScript(new BufferedReader(new InputStreamReader(fileStream, "UTF-8")));
            fileStream.close();
            setDeltaApplied(connect, entry.getKey());
        }

    }
}

From source file:Main.java

private static final void zip(File directory, File base, ZipOutputStream zos) throws IOException {
    File[] files = directory.listFiles();
    byte[] buffer = new byte[8192];
    int read = 0;
    for (int i = 0, n = files.length; i < n; i++) {
        if (files[i].isDirectory()) {
            zip(files[i], base, zos);//w  w w.j a v  a2 s.  co  m
        } else {
            FileInputStream in = new FileInputStream(files[i]);
            ZipEntry entry = new ZipEntry(files[i].getPath().substring(base.getPath().length() + 1));
            zos.putNextEntry(entry);
            while (-1 != (read = in.read(buffer))) {
                zos.write(buffer, 0, read);
            }
            in.close();
        }
    }
}

From source file:Main.java

public static void copyFile(File targetFile, File file) {
    if (targetFile.exists()) {
        return;/*from  w  w  w .  j  a  v a 2s .  co m*/
    } else {
        createFile(targetFile, true);
    }
    try {
        FileInputStream is = new FileInputStream(file);
        FileOutputStream fos = new FileOutputStream(targetFile);
        byte[] buffer = new byte[1024 * 5];
        int len;
        while ((len = is.read(buffer)) != -1) {
            fos.write(buffer, 0, len);
        }
        is.close();
        fos.flush();
        fos.close();
    } catch (FileNotFoundException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    }
}

From source file:eu.scape_project.tool.toolwrapper.data.components_spec.utils.Utils.java

/**
 * Method that creates a {@link Components} instance from the provided
 * component spec file, validating it against component spec XML Schema
 * //from   w ww  .  j  a v a2  s . co  m
 * @param componentSpecFilePath
 *            path to the component spec file
 */
public static Components createComponents(String componentSpecFilePath) {
    Components component = null;
    File schemaFile = null;
    try {
        JAXBContext context = JAXBContext.newInstance(Components.class);
        Unmarshaller unmarshaller = context.createUnmarshaller();

        // copy XML Schema from resources to a temporary location
        schemaFile = File.createTempFile("schema", null);
        FileUtils.copyInputStreamToFile(Utils.class.getResourceAsStream(COMPONENTS_SPEC_FILENAME_IN_RESOURCES),
                schemaFile);
        Schema schema = SchemaFactory.newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI).newSchema(schemaFile);

        // validate provided toolspec against XML Schema
        unmarshaller.setSchema(schema);

        // unmarshal it
        final FileInputStream stream = new FileInputStream(new File(componentSpecFilePath));
        try {
            component = unmarshaller.unmarshal(new StreamSource(stream), Components.class).getValue();
        } finally {
            stream.close();
        }
    } catch (JAXBException e) {
        log.error("The component spec provided doesn't validate against its schema!", e);
    } catch (SAXException e) {
        log.error("The XML Schema is not valid!", e);
    } catch (IOException e) {
        log.error("An error occured while copying the XML Schema from the resources to a temporary location!",
                e);
    } catch (Exception e) {
        log.error("An error occured!", e);
    } finally {
        if (schemaFile != null) {
            schemaFile.deleteOnExit();
        }
    }
    return component;
}

From source file:Main.java

public static List ObjectXmlDecoder(String objSource) throws FileNotFoundException, IOException, Exception {
    List objList = new ArrayList();
    File fin = new File(objSource);
    FileInputStream fis = new FileInputStream(fin);
    XMLDecoder decoder = new XMLDecoder(fis);
    Object obj = null;//from  w  w  w. j  av  a  2s.c  o m
    try {
        while ((obj = decoder.readObject()) != null) {
            objList.add(obj);
        }
    } catch (Exception e) {
        // TODO Auto-generated catch block
    }
    fis.close();
    decoder.close();
    return objList;
}

From source file:Main.java

public static void copyFile(File fromFile, File toFile, Boolean rewrite) {
    if (!fromFile.exists()) {
        return;//from  www.java  2  s  . co m
    }
    if (!fromFile.isFile()) {
        return;
    }
    if (!fromFile.canRead()) {
        return;
    }
    if (!toFile.getParentFile().exists()) {
        toFile.getParentFile().mkdirs();
    }
    if (toFile.exists() && rewrite) {
        toFile.delete();
    }

    try {
        java.io.FileInputStream fosfrom = new java.io.FileInputStream(fromFile);
        java.io.FileOutputStream fosto = new FileOutputStream(toFile);
        byte bt[] = new byte[1024];
        int c;
        while ((c = fosfrom.read(bt)) > 0) {
            fosto.write(bt, 0, c);
        }
        fosfrom.close();
        fosto.close();
    } catch (Exception ex) {
        ex.printStackTrace();
    }
}

From source file:info.papdt.blacklight.support.http.FeedbackUtility.java

private static String readLog() {
    String res = "";

    try {/* w  w  w.  j a  va 2  s  .  c  om*/
        FileInputStream fin = new FileInputStream(CrashHandler.CRASH_LOG);

        int length = fin.available();

        byte[] buffer = new byte[length];

        fin.read(buffer);

        res = EncodingUtils.getString(buffer, "UTF-8");

        fin.close();

    } catch (Exception e) {

        e.printStackTrace();
        return "";

    }
    return res;
}

From source file:com.nwn.NwnFileHandler.java

/**
 * Get md5 value of given file/*from  w ww  . j  a va 2s . co  m*/
 * @param file file to get md5 of
 * @return String representation of MD5 value
 */
public static String getMd5(Path file) {
    try {
        FileInputStream fis = new FileInputStream(file.toFile());
        String md5 = DigestUtils.md5Hex(fis);
        fis.close();

        return md5;
    } catch (IOException ex) {
        ex.printStackTrace();
    }
    return "";
}