List of usage examples for java.io BufferedInputStream close
public void close() throws IOException
From source file:Main.java
/** * Parses the input stream of a URL and generates an XML document. * @param xmlUrl The URL of the XML document. * @return The parsed document.//from w w w .j a v a 2s . com */ public static Document parseXML(URL xmlUrl) { InputStream is = null; BufferedInputStream bis = null; try { is = xmlUrl.openConnection().getInputStream(); bis = new BufferedInputStream(is); return parseXML(bis); } catch (Exception e) { throw new RuntimeException("Failed to read XML URL:" + xmlUrl, e); } finally { try { bis.close(); } catch (Exception e) { } try { is.close(); } catch (Exception e) { } } }
From source file:Main.java
public static void copyFile(File in, File out) throws IOException { // avoids copying a file to itself if (in.equals(out)) { return;/*w w w .j a va 2 s . c om*/ } // ensure the output file location exists out.getCanonicalFile().getParentFile().mkdirs(); BufferedInputStream fis = new BufferedInputStream(new FileInputStream(in)); BufferedOutputStream fos = new BufferedOutputStream(new FileOutputStream(out)); // a temporary buffer to read into byte[] tmpBuffer = new byte[8192]; int len = 0; while ((len = fis.read(tmpBuffer)) != -1) { // add the temp data to the output fos.write(tmpBuffer, 0, len); } // close the input stream fis.close(); // close the output stream fos.flush(); fos.close(); }
From source file:Main.java
/** * Method read.//from ww w .jav a 2s . c om * * @param filepath * String * @return Properties * @throws FileNotFoundException */ public static Properties read(String filepath) throws FileNotFoundException { Properties rval; BufferedInputStream bis = null; try { File file = new File(filepath); bis = new BufferedInputStream(new FileInputStream(file)); rval = new Properties(); rval.load(bis); return rval; } catch (IOException e) { } finally { if (bis != null) { try { bis.close(); } catch (IOException e) { } bis = null; } } // try relative to java.home try { File file = new File(System.getProperty("java.home") + File.separator + filepath); bis = new BufferedInputStream(new FileInputStream(file)); rval = new Properties(); rval.load(bis); return rval; } catch (IOException e) { } finally { if (bis != null) { try { bis.close(); } catch (IOException e) { } bis = null; } } throw new FileNotFoundException("Property file " + filepath + " not found"); }
From source file:com.codemarvels.ant.aptrepotask.utils.Utils.java
/** * Compute the given message digest for a file. * //w ww.java 2 s. c om * @param hashType algorithm to be used (as {@code String}) * @param file File to compute the digest for (as {@code File}). * @return A {@code String} for the hex encoded digest. * @throws MojoExecutionException */ public static String getDigest(String hashType, File file) { try { FileInputStream fis = new FileInputStream(file); BufferedInputStream bis = new BufferedInputStream(fis); MessageDigest digest = MessageDigest.getInstance(hashType); DigestInputStream dis = new DigestInputStream(bis, digest); @SuppressWarnings("unused") int ch; while ((ch = dis.read()) != -1) ; String hex = new String(Hex.encodeHex(digest.digest())); fis.close(); bis.close(); dis.close(); return hex; } catch (NoSuchAlgorithmException e) { throw new RuntimeException("could not create digest", e); } catch (FileNotFoundException e) { throw new RuntimeException("could not create digest", e); } catch (IOException e) { throw new RuntimeException("could not create digest", e); } }
From source file:Main.java
@SuppressWarnings("SameParameterValue") public static Bitmap fetchAndRescaleBitmap(String uri, int width, int height) throws IOException { URL url = new URL(uri); BufferedInputStream is = null; try {//from w w w . java 2 s. co m HttpURLConnection urlConnection = (HttpURLConnection) url.openConnection(); is = new BufferedInputStream(urlConnection.getInputStream()); is.mark(MAX_READ_LIMIT_PER_IMG); int scaleFactor = findScaleFactor(width, height, is); is.reset(); return scaleBitmap(scaleFactor, is); } finally { if (is != null) { is.close(); } } }
From source file:edu.du.penrose.systems.util.HttpClientUtils.java
/** * Get file from URL, directories are created and files overwritten. * /*from w w w . j av a 2 s . c o m*/ * * @deprecated use org.apache.commons.io.FileUtils.copyURLToFile(URL, File) * @param requestUrl * @param outputPathAndFileName * * @return int request status code OR -1 if an exception occurred */ static public int getToFile(String requestUrl, String outputPathAndFileName) { int resultStatus = -1; File outputFile = new File(outputPathAndFileName); String outputPath = outputFile.getAbsolutePath().replace(outputFile.getName(), ""); File outputDir = new File(outputPath); if (!outputDir.exists()) { outputDir.mkdir(); } HttpClient client = new HttpClient(); // client.getState().setCredentials( // new AuthScope("localhost", 7080, null ), // new UsernamePasswordCredentials("nation", "nationPW") // ); // client.getParams().setAuthenticationPreemptive(true); HttpMethod method = new GetMethod(requestUrl); // method.setDoAuthentication( true ); // client.getParams().setAuthenticationPreemptive(true); // Execute and print response try { OutputStream os = new FileOutputStream(outputFile); client.executeMethod(method); InputStream is = method.getResponseBodyAsStream(); BufferedInputStream bis = new BufferedInputStream(is); byte[] bytes = new byte[8192]; // reading as chunk of 8192 bytes int count = bis.read(bytes); while (count != -1 && count <= 8192) { os.write(bytes, 0, count); count = bis.read(bytes); } bis.close(); os.close(); resultStatus = method.getStatusCode(); } catch (Exception e) { e.printStackTrace(); } finally { method.releaseConnection(); } return resultStatus; }
From source file:Main.java
public static void downLoadFile(final String strUrl, final String fileURL, final int bufferLength) { BufferedInputStream in = null; BufferedOutputStream out = null; try {/* w ww . j a va 2 s .com*/ in = new BufferedInputStream(new URL(strUrl).openStream()); File img = new File(fileURL); out = new BufferedOutputStream(new FileOutputStream(img)); byte[] buf = new byte[bufferLength]; int count = in.read(buf); while (count != -1) { out.write(buf, 0, count); count = in.read(buf); } in.close(); out.close(); } catch (Exception e) { e.printStackTrace(); } }
From source file:com.amalto.workbench.utils.ResourcesUtil.java
private static String readFileAsString(String fileName) throws Exception { FileInputStream fis = new FileInputStream(fileName); BufferedInputStream in = new BufferedInputStream(fis); byte buffer[] = new byte[256]; StringBuffer picStr = new StringBuffer(); Encoder encoder = Base64.getEncoder(); while (in.read(buffer) >= 0) { picStr.append(encoder.encodeToString(buffer)); }/*ww w.jav a 2 s . c om*/ fis.close(); fis = null; in.close(); in = null; buffer = null; return picStr.toString(); }
From source file:Main.java
public static File getFileFromCacheOrURL(File cacheDir, URL url) throws IOException { Log.i(TAG, "getFileFromCacheOrURL(): url = " + url.toExternalForm()); String filename = url.getFile(); int lastSlashPos = filename.lastIndexOf('/'); String fileNameNoPath = new String(lastSlashPos == -1 ? filename : filename.substring(lastSlashPos + 1)); File file = new File(cacheDir, fileNameNoPath); if (file.exists()) { if (file.length() > 0) { Log.i(TAG, "File exists in cache as: " + file.getAbsolutePath()); return file; } else {//from w ww .j a va2s . c o m Log.i(TAG, "Deleting zero length file " + file.getAbsolutePath()); file.delete(); } } Log.i(TAG, "File " + file.getAbsolutePath() + " does not exists."); URLConnection ucon = url.openConnection(); ucon.setReadTimeout(5000); ucon.setConnectTimeout(30000); InputStream is = ucon.getInputStream(); BufferedInputStream inStream = new BufferedInputStream(is, 1024 * 5); FileOutputStream outStream = new FileOutputStream(file); byte[] buff = new byte[5 * 1024]; // Read bytes (and store them) until there is nothing more to read(-1) int len; while ((len = inStream.read(buff)) != -1) { outStream.write(buff, 0, len); } // Clean up outStream.flush(); outStream.close(); inStream.close(); return file; }
From source file:com.bahmanm.karun.Utils.java
/** * Extracts a gzip'ed tar archive./*from www .j av a 2s . c om*/ * * @param archivePath Path to archive * @param destDir Destination directory * @throws IOException */ public synchronized static void extractTarGz(String archivePath, File destDir) throws IOException, ArchiveException { // copy File tarGzFile = File.createTempFile("karuntargz", "", destDir); copyFile(archivePath, tarGzFile.getAbsolutePath()); // decompress File tarFile = File.createTempFile("karuntar", "", destDir); FileInputStream fin = new FileInputStream(tarGzFile); BufferedInputStream bin = new BufferedInputStream(fin); FileOutputStream fout = new FileOutputStream(tarFile); GzipCompressorInputStream gzIn = new GzipCompressorInputStream(bin); final byte[] buffer = new byte[1024]; int n = 0; while (-1 != (n = gzIn.read(buffer))) { fout.write(buffer, 0, n); } bin.close(); fin.close(); gzIn.close(); fout.close(); // extract final InputStream is = new FileInputStream(tarFile); ArchiveInputStream ain = new ArchiveStreamFactory().createArchiveInputStream("tar", is); TarArchiveEntry entry = null; while ((entry = (TarArchiveEntry) ain.getNextEntry()) != null) { OutputStream out; if (entry.isDirectory()) { File f = new File(destDir, entry.getName()); f.mkdirs(); continue; } else out = new FileOutputStream(new File(destDir, entry.getName())); IOUtils.copy(ain, out); out.close(); } ain.close(); is.close(); }