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

private static String readInStream(FileInputStream inStream) {
    try {/*  ww  w  . j a  v a2s  .c  o  m*/
        ByteArrayOutputStream outStream = new ByteArrayOutputStream();
        byte[] buffer = new byte[512];
        int length = -1;
        while ((length = inStream.read(buffer)) != -1) {
            outStream.write(buffer, 0, length);
        }

        outStream.close();
        inStream.close();
        return outStream.toString();
    } catch (IOException e) {
        // UIHelper.Log("e", "", "FileReadError", true);
    }
    return null;
}

From source file:LDAPTest.java

/**
     * Gets a context from the properties specified in the file ldapserver.properties
     * @return the directory context/*from www  .j  av  a  2 s . co  m*/
     */
    public static DirContext getContext() throws NamingException, IOException {
        Properties props = new Properties();
        FileInputStream in = new FileInputStream("ldapserver.properties");
        props.load(in);
        in.close();

        String url = props.getProperty("ldap.url");
        String username = props.getProperty("ldap.username");
        String password = props.getProperty("ldap.password");

        Hashtable<String, String> env = new Hashtable<String, String>();
        env.put(Context.SECURITY_PRINCIPAL, username);
        env.put(Context.SECURITY_CREDENTIALS, password);
        DirContext initial = new InitialDirContext(env);
        DirContext context = (DirContext) initial.lookup(url);

        return context;
    }

From source file:com.wisc.cs407project.ImageLoader.ImageLoader.java

public static Bitmap decodeFile(File f) {
    try {//from   w ww  .j  a  v a  2s . c o m
        //decode image size
        BitmapFactory.Options o = new BitmapFactory.Options();
        o.inJustDecodeBounds = true;
        FileInputStream stream1 = new FileInputStream(f);
        BitmapFactory.decodeStream(stream1, null, o);
        stream1.close();

        //Find the correct scale value. It should be the power of 2.
        int width_tmp = o.outWidth, height_tmp = o.outHeight;
        int scale = 1;
        while (true) {
            if (width_tmp / 2 < REQUIRED_WIDTH && height_tmp / 2 < REQUIRED_HEIGHT)
                break;
            width_tmp /= 2;
            height_tmp /= 2;
            scale *= 2;
        }

        //decode with inSampleSize
        BitmapFactory.Options o2 = new BitmapFactory.Options();
        o2.inSampleSize = scale;
        FileInputStream stream2 = new FileInputStream(f);
        Bitmap bitmap = BitmapFactory.decodeStream(stream2, null, o2);
        stream2.close();
        return bitmap;
    } catch (FileNotFoundException e) {
    } catch (IOException e) {
        e.printStackTrace();
    }
    return null;
}

From source file:com.huotu.mallduobao.common.thirdparty.ClientCustomSSL.java

public static String doRefund(String url, String data, String celPath, String celPassword) throws Exception {
    /**// w  w w  . ja  v a 2  s  .com
     * ?PKCS12? ?-- API 
     */

    KeyStore keyStore = KeyStore.getInstance("PKCS12");
    FileInputStream instream = new FileInputStream(new File(celPath));//P12
    try {
        /**
         * ?
         * */
        keyStore.load(instream, celPassword.toCharArray());//?..MCHID
    } finally {
        instream.close();
    }

    // Trust own CA and all self-signed certs
    /**
    * ?
    * */
    SSLContext sslcontext = SSLContexts.custom().loadKeyMaterial(keyStore, celPassword.toCharArray())//?
            .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();
    try {
        HttpPost httpost = new HttpPost(url); // ??
        httpost.addHeader("Connection", "keep-alive");
        httpost.addHeader("Accept", "*/*");
        httpost.addHeader("Content-Type", "application/x-www-form-urlencoded; charset=UTF-8");
        httpost.addHeader("Host", "api.mch.weixin.qq.com");
        httpost.addHeader("X-Requested-With", "XMLHttpRequest");
        httpost.addHeader("Cache-Control", "max-age=0");
        httpost.addHeader("User-Agent", "Mozilla/4.0 (compatible; MSIE 8.0; Windows NT 6.0) ");
        httpost.setEntity(new StringEntity(data, "UTF-8"));
        CloseableHttpResponse response = httpclient.execute(httpost);
        try {
            HttpEntity entity = response.getEntity();

            String jsonStr = EntityUtils.toString(response.getEntity(), "UTF-8");
            EntityUtils.consume(entity);
            return jsonStr;
        } finally {
            response.close();
        }
    } finally {
        httpclient.close();
    }
}

From source file:com.simple.weixin.refund.ClientCustomSSL.java

public static String doRefund(String password, String keyStrore, String url, String data) throws Exception {
    /**//from  w w w.j  a va2 s.  c om
     * ?PKCS12? ?-- API 
     */

    KeyStore keyStore = KeyStore.getInstance("PKCS12");
    FileInputStream instream = new FileInputStream(new File(keyStrore));//P12
    try {
        /**
         * ?
         * */
        keyStore.load(instream, password.toCharArray());//?..MCHID
    } finally {
        instream.close();
    }

    // Trust own CA and all self-signed certs
    /**
    * ?
    * */
    SSLContext sslcontext = SSLContexts.custom().loadKeyMaterial(keyStore, password.toCharArray())//?  
            .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();
    try {
        HttpPost httpost = new HttpPost(url); // ??
        httpost.addHeader("Connection", "keep-alive");
        httpost.addHeader("Accept", "*/*");
        httpost.addHeader("Content-Type", "application/x-www-form-urlencoded; charset=UTF-8");
        httpost.addHeader("Host", "api.mch.weixin.qq.com");
        httpost.addHeader("X-Requested-With", "XMLHttpRequest");
        httpost.addHeader("Cache-Control", "max-age=0");
        httpost.addHeader("User-Agent", "Mozilla/4.0 (compatible; MSIE 8.0; Windows NT 6.0) ");
        httpost.setEntity(new StringEntity(data, "UTF-8"));
        CloseableHttpResponse response = httpclient.execute(httpost);
        try {
            HttpEntity entity = response.getEntity();

            String jsonStr = EntityUtils.toString(response.getEntity(), "UTF-8");
            EntityUtils.consume(entity);
            return jsonStr;
        } finally {
            response.close();
        }
    } finally {
        httpclient.close();
    }
}

From source file:Main.java

public static Bitmap load(File file, int minSideLen, int maxSize) throws IOException {

    FileInputStream is = null;
    try {//from  w  ww .  j  a va 2  s .c  om
        Options opts = new Options();
        opts.inJustDecodeBounds = true;

        is = new FileInputStream(file);
        BitmapFactory.decodeStream(is, null, opts);
        is.close();

        is = new FileInputStream(file);
        return load(is, computeSampleSize(opts, minSideLen, maxSize));
    } finally {
        if (is != null) {
            is.close();
        }
    }
}

From source file:Main.java

public static byte[] readFile(File f) throws IOException {
    FileInputStream fis = new FileInputStream(f);
    try {//  w w  w.  j a  va2s.c o  m
        ByteArrayOutputStream baos = new ByteArrayOutputStream();
        BufferedInputStream bis = new BufferedInputStream(fis);
        int ch;
        while ((ch = bis.read()) >= 0) {
            baos.write(ch);
        }
        baos.flush();
        return baos.toByteArray();
    } finally {
        fis.close();
    }
}

From source file:ZipHandler.java

/**
 * unzipps a zip file placed at <zipURL> to path <xmlURL>
 * @param zipURL//w  w  w  . j a  v a2 s  .  com
 * @param xmlURL
 */
public static void unStructZip(String zipURL, String xmlURL) throws IOException {
    FileInputStream fis = new FileInputStream(new File(zipURL));
    ZipInputStream zis = new ZipInputStream(fis);
    FileOutputStream fos = new FileOutputStream(xmlURL);
    ZipEntry ze = zis.getNextEntry();
    writeInOutputStream(zis, fos);
    fos.flush();
    fis.close();
    fos.close();
    zis.close();
}

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

/**
 * Update a database. Called on an empty database. This method executes an SQL script generating the needed db tables.
 * Also checks our delta's, to see whether we need to change our database.
 * /*from  w  w w.j  ava 2  s .  co  m*/
 * @param connect
 * @param config
 * @param configDir
 * @return
 * @throws FileNotFoundException
 * @throws IOException
 * @throws SQLException
 * @throws JSONException
 * @throws ClassNotFoundException
 */
private static Connection updateDatabase(Connection connect, JSONObject config, File configDir)
        throws FileNotFoundException, IOException, SQLException, JSONException, ClassNotFoundException {
    String dbName = config.getString(SettingKeys.MYSQL_DB);
    if (!databaseExists(connect, dbName)) {
        createDatabase(connect, dbName);
        connect.close();
        connect = connect(
                config.getString(SettingKeys.MYSQL_HOST) + "/" + config.getString(SettingKeys.MYSQL_DB),
                config.getString(SettingKeys.MYSQL_USERNAME), config.getString(SettingKeys.MYSQL_PASSWORD));

        System.out.println("connected to " + config.getString(SettingKeys.MYSQL_HOST) + "/"
                + config.getString(SettingKeys.MYSQL_DB));

        ScriptRunner runner = new ScriptRunner(connect, false, true);
        String filename = "create.sql";
        FileInputStream fileStream = new FileInputStream(
                configDir.getAbsolutePath() + "/" + ConfigFetcher.CONFIG_DIR + filename);
        runner.runScript(new BufferedReader(new InputStreamReader(fileStream, "UTF-8")));
        fileStream.close();
    }
    applyDeltas(connect, configDir);
    return connect;
}

From source file:com.linkedin.databus.bootstrap.common.BootstrapConfigBase.java

@SuppressWarnings("static-access")
public static Properties loadConfigProperties(String[] args) throws IOException {
    CommandLineParser cliParser = new GnuParser();

    Option dbOption = OptionBuilder.withLongOpt(BOOTSTRAP_DB_PROPS_OPT_LONG_NAME)
            .withDescription("Bootstrap producer properties to use").hasArg().withArgName("property_file")
            .create(BOOTSTRAP_DB_PROP_OPT_CHAR);
    Options options = new Options();
    options.addOption(dbOption);/*from w ww  .j  a  v  a  2 s . c om*/

    CommandLine cmd = null;
    try {
        cmd = cliParser.parse(options, args);
    } catch (ParseException pe) {
        throw new RuntimeException("BootstrapConfig: failed to parse command-line options.", pe);
    }

    Properties props = null;
    if (cmd.hasOption(BOOTSTRAP_DB_PROP_OPT_CHAR)) {
        String propFile = cmd.getOptionValue(BOOTSTRAP_DB_PROP_OPT_CHAR);
        LOG.info("Loading bootstrap DB config from properties file " + propFile);
        props = new Properties();
        FileInputStream f = new FileInputStream(propFile);
        try {
            props.load(f);
        } finally {
            if (null != f)
                f.close();
        }
    } else {
        LOG.info("Using system properties for bootstrap DB config");
    }

    return props;
}