Example usage for java.io InputStream close

List of usage examples for java.io InputStream close

Introduction

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

Prototype

public void close() throws IOException 

Source Link

Document

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

Usage

From source file:Main.java

private static boolean loadAssetImage(ImageView view, String filename) {

    InputStream input = null;
    try {/*from ww  w.  jav  a 2  s.  c o  m*/
        input = view.getContext().getAssets().open(filename);
        Bitmap icon = BitmapFactory.decodeStream(input);
        view.setImageBitmap(icon);
        input.close();
        return true;
    } catch (Exception error) {
    } finally {
        silentClose(input);
    }
    return false;
}

From source file:com.splout.db.common.CompressorUtil.java

public static void createZip(File dir, File out, IOFileFilter filefilter, IOFileFilter dirFilter)
        throws IOException {
    Collection<File> files = FileUtils.listFiles(dir, filefilter, dirFilter);

    out.delete();//from ww w .j av a 2 s  . co m
    ZipOutputStream zos = new ZipOutputStream(new FileOutputStream(out));
    byte[] buf = new byte[1024];
    for (File f : files) {
        ZipEntry ze = new ZipEntry(getRelativePath(f, dir));
        zos.putNextEntry(ze);
        InputStream is = new FileInputStream(f);
        int cnt;
        while ((cnt = is.read(buf)) >= 0) {
            zos.write(buf, 0, cnt);
        }
        is.close();
        zos.flush();
        zos.closeEntry();
    }
    zos.close();
}

From source file:net.idlesoft.android.apps.github.utils.GravatarCache.java

private static Bitmap downloadGravatar(final String id) throws IOException {
    final URL aURL = new URL("http://www.gravatar.com/avatar/" + URLEncoder.encode(id) + "?size=100&d=mm");
    final HttpURLConnection conn = (HttpURLConnection) aURL.openConnection();
    conn.setDoInput(true);/* ww  w. jav  a 2 s  .  c  o m*/
    conn.connect();
    final InputStream is = conn.getInputStream();
    final Bitmap bm = BitmapFactory.decodeStream(is);
    is.close();
    return bm;
}

From source file:keel.Algorithms.Genetic_Rule_Learning.Bojarczuk_GP.Main.java

/**
 * <p>/*from  w  w w . j  av  a2  s. c om*/
 * Configure the execution of the algorithm.
 * 
 * @param jobFilename Name of the KEEL file with properties of the execution
 *  </p>                  
 */

private static void configureJob(String jobFilename) {

    Properties props = new Properties();

    try {
        InputStream paramsFile = new FileInputStream(jobFilename);
        props.load(paramsFile);
        paramsFile.close();
    } catch (IOException ioe) {
        ioe.printStackTrace();
        System.exit(0);
    }

    // Files training and test
    String trainFile;
    String testFile;
    StringTokenizer tokenizer = new StringTokenizer(props.getProperty("inputData"));
    tokenizer.nextToken();
    trainFile = tokenizer.nextToken();
    trainFile = trainFile.substring(1, trainFile.length() - 1);
    testFile = tokenizer.nextToken();
    testFile = testFile.substring(1, testFile.length() - 1);

    tokenizer = new StringTokenizer(props.getProperty("outputData"));
    String reportTrainFile = tokenizer.nextToken();
    reportTrainFile = reportTrainFile.substring(1, reportTrainFile.length() - 1);
    String reportTestFile = tokenizer.nextToken();
    reportTestFile = reportTestFile.substring(1, reportTestFile.length() - 1);
    String reportRulesFile = tokenizer.nextToken();
    reportRulesFile = reportRulesFile.substring(1, reportRulesFile.length() - 1);

    // Algorithm auxiliar configuration
    XMLConfiguration algConf = new XMLConfiguration();
    algConf.setRootElementName("experiment");
    algConf.addProperty("process[@algorithm-type]",
            "net.sourceforge.jclec.problem.classification.freitas.FreitasAlgorithm");
    algConf.addProperty("process.rand-gen-factory[@type]", "net.sourceforge.jclec.util.random.RanecuFactory");
    algConf.addProperty("process.rand-gen-factory[@seed]", Integer.parseInt(props.getProperty("seed")));
    algConf.addProperty("process.population-size", Integer.parseInt(props.getProperty("population-size")));
    algConf.addProperty("process.max-of-generations", Integer.parseInt(props.getProperty("max-generations")));
    algConf.addProperty("process.max-deriv-size", Integer.parseInt(props.getProperty("max-deriv-size")));
    algConf.addProperty("process.dataset[@type]", "net.sourceforge.jclec.util.dataset.KeelDataSet");
    algConf.addProperty("process.dataset.train-data.file-name", trainFile);
    algConf.addProperty("process.dataset.test-data.file-name", testFile);
    algConf.addProperty("process.species[@type]",
            "net.sourceforge.jclec.problem.classification.freitas.FreitasSyntaxTreeSpecies");
    algConf.addProperty("process.evaluator[@type]",
            "net.sourceforge.jclec.problem.classification.freitas.FreitasEvaluator");
    algConf.addProperty("process.provider[@type]", "net.sourceforge.jclec.syntaxtree.SyntaxTreeCreator");
    algConf.addProperty("process.parents-selector[@type]", "net.sourceforge.jclec.selector.RouletteSelector");
    algConf.addProperty("process.recombinator[@type]",
            "net.sourceforge.jclec.syntaxtree.SyntaxTreeRecombinator");
    algConf.addProperty("process.recombinator[@rec-prob]", Double.parseDouble(props.getProperty("rec-prob")));
    algConf.addProperty("process.recombinator.base-op[@type]",
            "net.sourceforge.jclec.problem.classification.freitas.FreitasCrossover");
    algConf.addProperty("process.copy-prob", Double.parseDouble(props.getProperty("copy-prob")));
    algConf.addProperty("process.listener[@type]",
            "net.sourceforge.jclec.problem.classification.freitas.KeelFreitasPopulationReport");
    algConf.addProperty("process.listener.report-dir-name", "./");
    algConf.addProperty("process.listener.train-report-file", reportTrainFile);
    algConf.addProperty("process.listener.test-report-file", reportTestFile);
    algConf.addProperty("process.listener.rules-report-file", reportRulesFile);
    algConf.addProperty("process.listener.global-report-name", "resumen");
    algConf.addProperty("process.listener.report-frequency", 50);

    try {
        algConf.save(new File("configure.txt"));
    } catch (ConfigurationException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }

    net.sourceforge.jclec.RunExperiment.main(new String[] { "configure.txt" });
}

From source file:Main.java

public static long getMaxCpuFreq() {
    long longRet = 0;
    String result = "0";
    ProcessBuilder cmd;/*w  w w .j  av  a 2s . c om*/
    try {
        String[] args = { "/system/bin/cat", "/sys/devices/system/cpu/cpu0/cpufreq/cpuinfo_max_freq" };
        cmd = new ProcessBuilder(args);
        Process process = cmd.start();
        InputStream in = process.getInputStream();
        byte[] re = new byte[24];
        result = "";
        while (in.read(re) != -1) {
            result = result + new String(re);
        }
        in.close();
    } catch (IOException ex) {
        ex.printStackTrace();
        result = "0";
    }

    if (result.length() != 0) {
        try {
            longRet = Long.valueOf(result.trim());
        } catch (Exception e) {
            android.util.Log.e(TAG, "");
        }
    }
    return longRet;
}

From source file:Main.java

public static void copyFile(File src, File dst) throws IOException {
    InputStream in = new FileInputStream(src);
    OutputStream out = new FileOutputStream(dst);

    byte[] buf = new byte[1024];
    int len;/*from w  ww. j a v  a 2 s . com*/
    while ((len = in.read(buf)) > 0) {
        out.write(buf, 0, len);
    }
    in.close();
    out.close();
}

From source file:Main.java

public static byte[] stream2ByteArrayQuiet(InputStream inStream) {
    byte[] result = null;
    try {//from  w  w  w  . j  a va  2 s .  co m
        result = stream2ByteArray(inStream);
    } catch (IOException e) {
        // nothing to do
    } finally {
        try {
            inStream.close();
        } catch (IOException e) {
            // nothing to do
        } finally {
            inStream = null;
        }
    }
    return result;
}

From source file:Main.java

/**
 * Returns the lowercase string representation of the file's MD5 sum.
 *///ww  w . j  av a  2 s. com
public static String getMD5Sum(String file) throws IOException {
    try {
        MessageDigest digest = MessageDigest.getInstance("MD5");
        InputStream is = new FileInputStream(file);
        byte[] buffer = new byte[8192];
        int read = 0;
        while ((read = is.read(buffer)) > 0) {
            digest.update(buffer, 0, read);
        }
        is.close();
        byte[] md5sum = digest.digest();
        BigInteger bigInt = new BigInteger(1, md5sum);
        return bigInt.toString(16);
    } catch (NoSuchAlgorithmException e) {
        return "";
    }
}

From source file:Main.java

public static byte[] getAssetBytes(String path) {
    byte[] mBytes = null;
    if (sAssetManager != null) {
        InputStream input = null;
        try {//from  w ww. j a  v a2 s .  com
            input = sAssetManager.open(path);
            int length = input.available();
            mBytes = new byte[length];
            input.read(mBytes);
            input.close();
            if (!mFileTable.containsKey(path)) {
                mFileTable.put(path, true);
            }
        } catch (Exception e) {
            if (!mFileTable.containsKey(path)) {
                mFileTable.put(path, false);
            }
        }
    }
    return mBytes;
}

From source file:com.cats.version.utils.Utils.java

public static void closeRes(InputStream fis) {
    if (null != fis) {
        try {//w w w. ja  v a  2s.  com
            fis.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}