List of usage examples for java.io BufferedInputStream close
public void close() throws IOException
From source file:www.image.ImageManager.java
/** * Downloads a file/*from ww w . j a v a2 s . c o m*/ * @param url * @return * @throws IOException */ public Bitmap fetchImage(String url) throws IOException { Bitmap mbitmap = null; try { HttpGet get = new HttpGet(url); HttpConnectionParams.setConnectionTimeout(get.getParams(), CONNECTION_TIMEOUT_MS); HttpConnectionParams.setSoTimeout(get.getParams(), SOCKET_TIMEOUT_MS); HttpResponse response = null; response = mClient.execute(get); HttpEntity entity = response.getEntity(); BufferedInputStream bis = new BufferedInputStream(entity.getContent(), 8 * 1024); mbitmap = scaleBitmap(bis, 120, 120); bis.close(); if (response.getStatusLine().getStatusCode() != 200) { mbitmap = mFailBitmap; } } catch (ClientProtocolException e) { throw new IOException("Invalid client protocol."); } catch (Exception e) { e.printStackTrace(); } return mbitmap; }
From source file:com.pipinan.githubcrawler.GithubCrawler.java
/** * * @param username the owner name of respoitory * @param reponame the name of respoitory * @param path which folder would you like to save the zip file * @throws IOException//w ww .j av a2 s .c o m */ public void crawlRepoZip(String username, String reponame, String path) throws IOException { GHRepository repo = github.getRepository(username + "/" + reponame); HttpClient httpclient = getHttpClient(); //the url pattern is https://github.com/"USER_NAME"/"REPO_NAME"/archive/master.zip HttpGet httpget = new HttpGet("https://github.com/" + username + "/" + reponame + "/archive/master.zip"); HttpResponse response = httpclient.execute(httpget); try { System.out.println(response.getStatusLine()); if (response.getStatusLine().toString().contains("200 OK")) { //the header "Content-Disposition: attachment; filename=JSON-java-master.zip" can find the filename String filename = null; Header[] headers = response.getHeaders("Content-Disposition"); for (Header header : headers) { System.out.println("Key : " + header.getName() + " ,Value : " + header.getValue()); String tmp = header.getValue(); filename = tmp.substring(tmp.lastIndexOf("filename=") + 9); } if (filename == null) { System.err.println("Can not find the filename in the response."); System.exit(-1); } HttpEntity entity = response.getEntity(); BufferedInputStream bis = new BufferedInputStream(entity.getContent()); String filePath = path + filename; BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream(new File(filePath))); int inByte; while ((inByte = bis.read()) != -1) { bos.write(inByte); } bis.close(); bos.close(); EntityUtils.consume(entity); } } finally { } }
From source file:com.yahoo.yrlhaifa.haifa_utils.utils.FileUtils.java
/** * @param src source/*from w w w .j av a2 s . co m*/ * @param dst destination * @throws IOException io exception */ public static void copy(File src, File dst) throws IOException { BufferedInputStream reader = new BufferedInputStream(new FileInputStream(src)); BufferedOutputStream writer = new BufferedOutputStream(new FileOutputStream(dst)); copy(reader, writer); writer.close(); reader.close(); }
From source file:ezbake.frack.submitter.util.JarUtil.java
private static void add(File source, final String prefix, JarOutputStream target) throws IOException { BufferedInputStream in = null; log.debug("Adding file {} to jar", source.getName()); try {//from ww w . java2 s . c o m String entryPath = source.getPath().replace("\\", "/").replace(prefix, ""); if (entryPath.startsWith("/")) { entryPath = entryPath.substring(1); } if (source.isDirectory()) { if (!entryPath.isEmpty()) { if (!entryPath.endsWith("/")) { entryPath += "/"; } JarEntry entry = new JarEntry(entryPath); entry.setTime(source.lastModified()); target.putNextEntry(entry); target.closeEntry(); } for (File nestedFile : source.listFiles()) { add(nestedFile, prefix, target); } } else { JarEntry entry = new JarEntry(entryPath); entry.setTime(source.lastModified()); target.putNextEntry(entry); in = new BufferedInputStream(new FileInputStream(source)); byte[] buffer = new byte[BUFFER_SIZE]; int len; while ((len = in.read(buffer)) > 0) { target.write(buffer, 0, len); } target.closeEntry(); } } finally { if (in != null) { in.close(); } } }
From source file:gov.nih.nci.restgen.util.GeneratorUtil.java
private static void add(File source, JarOutputStream target) throws IOException { BufferedInputStream in = null; try {//from w w w . ja va 2s.co m if (source.isDirectory()) { String name = source.getPath().replace("\\", "/"); if (!name.isEmpty()) { if (!name.endsWith("/")) name += "/"; JarEntry entry = new JarEntry(name); entry.setTime(source.lastModified()); target.putNextEntry(entry); target.closeEntry(); } for (File nestedFile : source.listFiles()) add(nestedFile, target); return; } JarEntry entry = new JarEntry(source.getPath().replace("\\", "/")); entry.setTime(source.lastModified()); target.putNextEntry(entry); in = new BufferedInputStream(new FileInputStream(source)); byte[] buffer = new byte[1024]; while (true) { int count = in.read(buffer); if (count == -1) break; target.write(buffer, 0, count); } target.closeEntry(); } finally { if (in != null) in.close(); } }
From source file:fr.shywim.antoinedaniel.sync.AppState.java
/** * Load AppState from file.//from w w w .j a v a 2 s. c o m * * @param context a context. */ public void load(final Context context) { File privDir = context.getFilesDir(); final File appState = new File(privDir, FILE_NAME); new Thread(new Runnable() { @Override public void run() { try { if (appState.exists()) { BufferedInputStream bis = new BufferedInputStream(new FileInputStream(appState)); byte[] bytes = new byte[(int) appState.length()]; for (int i = 0, temp; (temp = bis.read()) != -1; i++) { bytes[i] = (byte) temp; } bis.close(); loadFromBytes(bytes, context, null); } } catch (IOException e) { e.printStackTrace(); } } }).start(); }
From source file:ru.develgame.jflickrorganizer.Threads.BackupRunnable.java
private void downloadPhoto(File newFile, Size size, PhotosInterface photoInt) throws FileNotFoundException, IOException, FlickrException { if (!newFile.exists()) { BufferedInputStream inStream = new BufferedInputStream(photoInt.getImageAsStream(size)); FileOutputStream fos = new FileOutputStream(newFile); byte buffer[] = IOUtils.toByteArray(inStream); fos.write(buffer);/*from www.ja va 2s . c o m*/ fos.flush(); fos.close(); inStream.close(); } }
From source file:com.cpp255.bookbarcode.DouBanBookInfoXmlParser.java
public Bitmap DownloadBitmap(String bitmapUrl) { Bitmap bitmap = null;/*from w w w . ja v a2s. c om*/ BufferedInputStream bis = null; try { URL url = new URL(bitmapUrl); URLConnection conn = url.openConnection(); bis = new BufferedInputStream(conn.getInputStream()); bitmap = BitmapFactory.decodeStream(bis); } catch (Exception e) { e.printStackTrace(); } finally { try { if (bis != null) bis.close(); } catch (Exception e) { e.printStackTrace(); } } return bitmap; }
From source file:net.sf.jasperreports.engine.xml.JRXmlTemplateLoader.java
/** * Parses a template XML file into a {@link JRTemplate template object}. * /* w ww . j ava2 s . co m*/ * @param file the template XML file * @return the template object */ public JRTemplate loadTemplate(File file) { BufferedInputStream fileIn; try { fileIn = new BufferedInputStream(new FileInputStream(file)); } catch (FileNotFoundException e) { throw new JRRuntimeException(EXCEPTION_MESSAGE_KEY_TEMPLATE_NOT_FOUND, (Object[]) null, e); } try { return load(fileIn); } finally { try { fileIn.close(); } catch (IOException e) { log.warn("Error closing XML file", e); } } }
From source file:com.lazerycode.ebselen.customhandlers.FileDownloader.java
public String downloader(WebElement element, String attribute) throws Exception { //Assuming that getAttribute does some magic to return a fully qualified URL String downloadLocation = element.getAttribute(attribute); if (downloadLocation.trim().equals("")) { throw new Exception("The element you have specified does not link to anything!"); }// www.j a v a2s .co m URL downloadURL = new URL(downloadLocation); HttpClient client = new HttpClient(); client.getParams().setCookiePolicy(CookiePolicy.RFC_2965); client.setHostConfiguration(mimicHostConfiguration(downloadURL.getHost(), downloadURL.getPort())); client.setState(mimicCookieState(driver.manage().getCookies())); HttpMethod getRequest = new GetMethod(downloadURL.getPath()); FileHandler downloadedFile = new FileHandler( downloadPath + downloadURL.getFile().replaceFirst("/|\\\\", ""), true); try { int status = client.executeMethod(getRequest); LOGGER.info("HTTP Status {} when getting '{}'", status, downloadURL.toExternalForm()); BufferedInputStream in = new BufferedInputStream(getRequest.getResponseBodyAsStream()); int offset = 0; int len = 4096; int bytes = 0; byte[] block = new byte[len]; while ((bytes = in.read(block, offset, len)) > -1) { downloadedFile.getWritableFileOutputStream().write(block, 0, bytes); } downloadedFile.close(); in.close(); LOGGER.info("File downloaded to '{}'", downloadedFile.getAbsoluteFile()); } catch (Exception Ex) { LOGGER.error("Download failed: {}", Ex); throw new Exception("Download failed!"); } finally { getRequest.releaseConnection(); } return downloadedFile.getAbsoluteFile(); }