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:mysynopsis.SystemInitializer.java

/**
 * @throws IOException/*from   w w  w. j ava2 s.  co  m*/
 */
public static void initSystem() throws IOException {
    try {
        FileInputStream fstream;
        fstream = new FileInputStream("data.json");
        fstream.close();
    } catch (FileNotFoundException e) {
        // TODO Auto-generated catch block
        setDefault();

    }
    try {
        FileInputStream fstream;
        fstream = new FileInputStream("template.html");
        fstream.close();
    } catch (FileNotFoundException e) {
        // TODO Auto-generated catch block
        setDefaultHTML();

    }
}

From source file:org.phpmaven.pear.library.impl.Helper.java

/**
 * Returns the binary file contents./*  w  ww.jav a  2  s  .c  o  m*/
 * @param uri URI of the resource.
 * @return the files content.
 * @throws IOException thrown on errors.
 */
public static byte[] getBinaryFileContents(String uri) throws IOException {
    // is it inside the local filesystem?
    if (uri.startsWith("file://")) {
        final File channelFile = new File(uri.substring(7));

        final byte[] result = new byte[(int) channelFile.length()];
        final FileInputStream fis = new FileInputStream(channelFile);
        fis.read(result);
        fis.close();
        return result;
    }

    // try http connection
    final HttpClient client = new DefaultHttpClient();
    final HttpGet httpget = new HttpGet(uri);
    final HttpResponse response = client.execute(httpget);
    if (response.getStatusLine().getStatusCode() != HttpStatus.SC_OK) {
        throw new IOException("Invalid http status: " + response.getStatusLine().getStatusCode() + " / "
                + response.getStatusLine().getReasonPhrase());
    }
    final HttpEntity entity = response.getEntity();
    if (entity == null) {
        throw new IOException("Empty response.");
    }
    return EntityUtils.toByteArray(entity);
}

From source file:fr.insalyon.creatis.vip.applicationimporter.server.business.TargzUtils.java

private static void addFileToTarGz(TarArchiveOutputStream tOut, File f, String dir) throws BusinessException {
    try {//  w w  w . j ava 2 s.  c  o  m
        TarArchiveEntry tarEntry;
        if (dir == null) {
            tarEntry = new TarArchiveEntry(f, f.getName());
        } else {
            tarEntry = new TarArchiveEntry(f, dir + "/" + f.getName());
        }
        tOut.putArchiveEntry(tarEntry);
        if (!f.isDirectory()) {
            FileInputStream in = new FileInputStream(f);
            IOUtils.copy(in, tOut);
            tOut.closeArchiveEntry();
            in.close();
        } else {
            tOut.closeArchiveEntry();
            String name = f.getName();
            File[] children = f.listFiles();
            if (children != null) {
                for (File child : children) {

                    addFileToTarGz(tOut, new File(child.getParent() + "/" + child.getName()), name);
                }
            }
        }
    } catch (IOException ex) {
        throw new BusinessException(ex);
    }
}

From source file:Main.java

public static KeyPair loadKeyPair(Context context, String name)
        throws IOException, NoSuchAlgorithmException, InvalidKeySpecException {
    // Read Public Key.
    File filePublicKey = new File(context.getFilesDir(), name + "_public.key");
    FileInputStream fis = new FileInputStream(filePublicKey);
    byte[] encodedPublicKey = new byte[(int) filePublicKey.length()];
    fis.read(encodedPublicKey);//from   w  w w . j  a  va  2  s .  co m
    fis.close();

    // Read Private Key.
    File filePrivateKey = new File(context.getFilesDir(), name + "_private.key");
    fis = new FileInputStream(filePrivateKey);
    byte[] encodedPrivateKey = new byte[(int) filePrivateKey.length()];
    fis.read(encodedPrivateKey);
    fis.close();

    // Generate KeyPair.
    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:Main.java

public static boolean isEncode(File f) {
    try {// www .  j a  va  2  s . co m
        FileInputStream input = new FileInputStream(f);
        //            input = new BufferedInputStream(input);
        byte[] ecodeBytes = new byte[ecodeTag.getBytes().length];
        input.read(ecodeBytes);
        input.close();
        return new String(ecodeBytes).equals(ecodeTag);
    } catch (IOException e) {
        e.printStackTrace();
    }
    return false;
}

From source file:Main.java

public static String readSDFile(String fileName) throws IOException {

    File file = new File(fileName);

    FileInputStream fis = new FileInputStream(file);

    int length = fis.available();

    byte[] buffer = new byte[length];
    fis.read(buffer);//w  w w.ja va  2s.co  m

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

    fis.close();
    return res;
}

From source file:com.weitaomi.systemconfig.wechat.ClientCustomSSL.java

public static String connectKeyStore(String url, String xml, String path, int flag) throws Exception {
    KeyStore keyStore = KeyStore.getInstance("PKCS12");
    File file = LoadFileFactory.getFile(path);
    char[] arr = null;
    if (flag == 0) {
        arr = WechatConfig.MCHID.toCharArray();
    }//from   w w w.j  a  va  2s . c o m
    if (flag == 1) {
        arr = WechatConfig.MCHID_OFFICIAL.toCharArray();
    }
    FileInputStream instream = new FileInputStream(file);
    try {
        keyStore.load(instream, arr);
    } finally {
        instream.close();
    }

    // Trust own CA and all self-signed certs
    SSLContext sslcontext = SSLContexts.custom().loadKeyMaterial(keyStore, arr).build();
    // Allow TLSv1 protocol only
    SSLConnectionSocketFactory sslsf = new SSLConnectionSocketFactory(sslcontext, new String[] { "TLSv1" },
            null, SSLConnectionSocketFactory.BROWSER_COMPATIBLE_HOSTNAME_VERIFIER);
    CloseableHttpClient httpclient = HttpClients.custom().setSSLSocketFactory(sslsf).build();

    StringEntity entityRequest = new StringEntity(xml, "utf-8");
    HttpPost httpPost = new HttpPost(url);
    httpPost.setEntity(entityRequest);
    //        httpPost.setHeader("Content-Type", "application/json");//; charset=utf-8
    HttpResponse response = httpclient.execute(httpPost);

    if (response.getStatusLine().getStatusCode() != HttpStatus.SC_OK) {
        throw new RuntimeException("");
    }
    HttpEntity resEntity = response.getEntity();
    InputStream inputStream = resEntity.getContent();
    return HttpRequestUtils.readInstream(inputStream, "UTF-8");
}

From source file:com.fizzed.stork.util.AssemblyUtils.java

static public void addFileToTGZStream(TarArchiveOutputStream tgzout, File f, String base, boolean appendName)
        throws IOException {
    //File f = new File(path);
    String entryName = base;/*from  w w w .j  a  v a  2 s .  c  o m*/
    if (appendName) {
        if (!entryName.equals("")) {
            if (!entryName.endsWith("/")) {
                entryName += "/" + f.getName();
            } else {
                entryName += f.getName();
            }
        } else {
            entryName += f.getName();
        }
    }
    TarArchiveEntry tarEntry = new TarArchiveEntry(f, entryName);

    if (f.isFile()) {
        if (f.canExecute()) {
            // -rwxr-xr-x
            tarEntry.setMode(493);
        } else {
            // keep default mode
        }
    }

    tgzout.putArchiveEntry(tarEntry);

    if (f.isFile()) {
        FileInputStream in = new FileInputStream(f);
        IOUtils.copy(in, tgzout);
        in.close();
        tgzout.closeArchiveEntry();

    } else {
        tgzout.closeArchiveEntry();
        File[] children = f.listFiles();
        if (children != null) {
            for (File child : children) {
                logger.info(" adding: " + entryName + "/" + child.getName());
                addFileToTGZStream(tgzout, child, entryName + "/", true);
            }
        }
    }
}

From source file:com.ipcglobal.fredimport.util.FredUtils.java

/**
 * Creates a tar entry for the path specified with a name built from the base passed in and the file/directory name. If the path is a directory, a recursive
 * call is made such that the full directory is added to the tar.
 *
 * @param tOut//from w ww . jav a 2 s .  c  o  m
 *            The tar file's output stream
 * @param path
 *            The filesystem path of the file/directory being added
 * @param base
 *            The base prefix to for the name of the tar file entry
 *
 * @throws IOException
 *             If anything goes wrong
 */
private static void addFileToTarGz(TarArchiveOutputStream tOut, String path, String base) throws IOException {
    try {
        File f = new File(path);
        String entryName = base + f.getName();
        TarArchiveEntry tarEntry = new TarArchiveEntry(f, entryName);
        tOut.putArchiveEntry(tarEntry);
        if (f.isFile()) {
            // had to do this in 3 steps due to memory leak as per http://stackoverflow.com/questions/13461393/compress-directory-to-tar-gz-with-commons-compress/23524963#23524963 
            FileInputStream in = new FileInputStream(f);
            IOUtils.copy(in, tOut);
            in.close();

            tOut.closeArchiveEntry();
        } else {
            tOut.closeArchiveEntry();
            File[] children = f.listFiles();
            if (children != null) {
                for (File child : children) {
                    addFileToTarGz(tOut, child.getAbsolutePath(), entryName + "/");
                }
            }
        }
    } catch (Exception e) {
        System.out.println(e);
        e.printStackTrace();
        throw e;
    }
}

From source file:controllers.ConfigurationController.java

public static DBConfig getConfig(HttpServletRequest request) {
    try {/*from  ww w  .j  a  v a2s .c  o  m*/
        String path = Utility.getPath("dbconfig", request);
        path += "config.properties";

        Properties p = new Properties();
        FileInputStream file = new FileInputStream(path);
        p.load(file);
        file.close();

        DBConfig config = new DBConfig();
        String db = p.getProperty("prop.db");

        if (db.equals("SQLServer"))
            config.setDb_type(DB_TYPE.SQLServer);
        if (db.equals("MySQL"))
            config.setDb_type(DB_TYPE.MySQL);
        if (db.equals("FirebirdSQL"))
            config.setDb_type(DB_TYPE.FirebirdSQL);
        if (db.equals("PostgreSQL"))
            config.setDb_type(DB_TYPE.PostgreSQL);
        if (db.equals("ORACLE"))
            config.setDb_type(DB_TYPE.ORACLE);

        config.setHost(p.getProperty("prop.server"));
        config.setPort(Integer.parseInt(p.getProperty("prop.port")));
        config.setUser(p.getProperty("prop.user"));
        config.setPassword(p.getProperty("prop.passwd"));
        config.setDatabase(p.getProperty("prop.database"));

        return config;
    } catch (Exception ex) {
        System.err.println(ex.getMessage());
    }
    return new DBConfig();
}