List of usage examples for java.io BufferedInputStream close
public void close() throws IOException
From source file:com.sparkhunter.activities.FbfriendsActivity.java
public Bitmap getBitmap(String url) { AndroidHttpClient httpclient = null; Bitmap bm = null;//from w w w .j a v a2 s . c o m try { URL aURL = new URL(url); URLConnection conn = aURL.openConnection(); conn.connect(); InputStream is = conn.getInputStream(); BufferedInputStream bis = new BufferedInputStream(is); bm = BitmapFactory.decodeStream(new FlushedInputStream(is)); bis.close(); is.close(); } catch (Exception e) { e.printStackTrace(); } finally { if (httpclient != null) { httpclient.close(); } } return bm; }
From source file:it.eng.spagobi.engines.exporters.KpiExporter.java
public File getKpiExportXML(List<KpiResourceBlock> kpiBlocks, BIObject obj, String userId) throws Exception { File tmpFile = null;// w w w. ja va 2 s. c o m logger.debug("IN"); try { // recover BiObject Name Object idObject = obj.getId(); if (idObject == null) { logger.error("Document id not found"); } Integer id = Integer.valueOf(idObject.toString()); BIObject document = DAOFactory.getBIObjectDAO().loadBIObjectById(id); String docName = document.getName(); //Recover user Id HashedMap parameters = new HashedMap(); BasicXmlBuilder basic = new BasicXmlBuilder(docName); String template = basic.buildTemplate(kpiBlocks); String dirS = System.getProperty("java.io.tmpdir"); File dir = new File(dirS); dir.mkdirs(); tmpFile = File.createTempFile("tempXmlExport", ".xml", dir); FileOutputStream stream = new FileOutputStream(tmpFile); stream.write(template.getBytes()); stream.flush(); stream.close(); BufferedInputStream in = new BufferedInputStream(new FileInputStream(tmpFile)); in.close(); logger.debug("OUT"); return tmpFile; } catch (Throwable e) { logger.error("An exception has occured", e); throw new Exception(e); } finally { //tmpFile.delete(); } }
From source file:com.kamosoft.flickr.model.Photo.java
private Bitmap getBitmapFromURL(String url) throws JSONException, IOException { Bitmap bm = null;/*from ww w . j a va2s. c o m*/ URL aURL = new URL(url); URLConnection conn = aURL.openConnection(); conn.connect(); InputStream is = conn.getInputStream(); BufferedInputStream bis = new BufferedInputStream(is); bm = BitmapFactory.decodeStream(bis); bis.close(); is.close(); return bm; }
From source file:com.oneops.cms.crypto.CmsCryptoDES.java
private KeyParameter getSecretKeyFromFile() throws IOException, GeneralSecurityException { BufferedInputStream keystream = new BufferedInputStream(new FileInputStream(secretKeyFile)); int len = keystream.available(); if (len < MIN_DES_FILE_LENGTH) { keystream.close(); throw new EOFException(">>>> Bad DES file length = " + len); }//from w w w.java 2s . c om byte[] keyhex = new byte[len]; keystream.read(keyhex, 0, len); keystream.close(); return new KeyParameter(Hex.decode(keyhex)); }
From source file:com.thruzero.common.core.fs.walker.visitor.ZipCompressingVisitor.java
/** * Compress the given file and add it to the archive. *///from w ww . jav a 2s .c om @Override public void visitFile(final File file) throws IOException { logHelper.logZippingFile(file); // zip the file String relativePath = getRelativePath(file); ZipEntry zipEntry = new ZipEntry(relativePath); zipOut.putNextEntry(zipEntry); BufferedInputStream bis = new BufferedInputStream(new FileInputStream(file)); IOUtils.copy(bis, zipOut); zipOut.flush(); zipOut.closeEntry(); bis.close(); getStatus().incNumProcessed(); }
From source file:org.broadleafcommerce.admin.util.controllers.FileUploadController.java
protected ModelAndView onSubmit(HttpServletRequest request, HttpServletResponse response, Object command, BindException errors) throws ServletException, IOException { // cast the bean FileUploadBean bean = (FileUploadBean) command; // let's see if there's content there MultipartFile file = bean.getFile(); if (file == null) { // hmm, that's strange, the user did not upload anything }/*from w w w . j av a 2s . c om*/ try { String basepath = request.getPathTranslated().substring(0, request.getPathTranslated().indexOf(File.separator + "upload")); String absoluteFilename = basepath + File.separator + bean.getDirectory() + File.separator + file.getOriginalFilename(); FileSystemResource fileResource = new FileSystemResource(absoluteFilename); checkDirectory(basepath + File.separator + bean.getDirectory()); backupExistingFile(fileResource, basepath + bean.getDirectory()); FileOutputStream fout = new FileOutputStream(new FileSystemResource( basepath + File.separator + bean.getDirectory() + File.separator + file.getOriginalFilename()) .getFile()); BufferedOutputStream bout = new BufferedOutputStream(fout); BufferedInputStream bin = new BufferedInputStream(file.getInputStream()); int x; while ((x = bin.read()) != -1) { bout.write(x); } bout.flush(); bout.close(); bin.close(); return super.onSubmit(request, response, command, errors); } catch (Exception e) { //Exception occured; e.printStackTrace(); throw new RuntimeException(e); // return null; } }
From source file:org.caboclo.clients.ApiClient.java
protected void downloadURL(String url, ByteArrayOutputStream bos, Map<String, String> headers) throws IllegalStateException, IOException { HttpClient httpclient = new DefaultHttpClient(); HttpGet httpget = new HttpGet(url); for (String key : headers.keySet()) { String value = headers.get(key); httpget.setHeader(key, value);// w w w.ja v a 2 s. co m } HttpResponse response = httpclient.execute(httpget); HttpEntity entity = response.getEntity(); if (entity != null) { try { InputStream instream = entity.getContent(); BufferedInputStream bis = new BufferedInputStream(instream); int inByte; while ((inByte = bis.read()) != -1) { bos.write(inByte); } bis.close(); bos.close(); } catch (IOException ex) { ex.printStackTrace(); throw ex; } catch (IllegalStateException ex) { ex.printStackTrace(); httpget.abort(); throw ex; } httpclient.getConnectionManager().shutdown(); } }
From source file:fr.gcastel.freeboxV6GeekIncDownloader.services.GeekIncLogoDownloadService.java
public void downloadAndResize(int requiredSize) throws Exception { // Si le logo existe depuis moins d'une semaine, on ne le retlcharge // pas//from w w w .j ava 2s . c om if (outFile.exists()) { // Moins une semaine long uneSemainePlusTot = System.currentTimeMillis() - (7 * 24 * 60 * 60 * 1000); if (outFile.lastModified() > uneSemainePlusTot) { Log.i("GeekIncLogoDownloadService", "Le logo a dj t tlcharg il y a moins d'une semaine."); return; } } URLConnection ucon = toDownload.openConnection(); InputStream is = ucon.getInputStream(); BufferedInputStream bis = new BufferedInputStream(is); ByteArrayBuffer baf = new ByteArrayBuffer(50); int current = 0; while ((current = bis.read()) != -1) { baf.append((byte) current); } bis.close(); is.close(); // Fichier temporaire pour redimensionnement File tmpFile = new File(outFile.getAbsolutePath() + "tmp"); FileOutputStream fos = new FileOutputStream(tmpFile); fos.write(baf.toByteArray()); fos.close(); saveAndResizeFile(tmpFile, outFile, requiredSize); // Suppression du fichier temporaire tmpFile.delete(); }
From source file:org.jorge.lolin1.io.net.HTTPServices.java
public static void downloadFile(final String whatToDownload, final File whereToSaveIt) throws IOException { logString("debug", "Downloading url " + whatToDownload); AsyncTask<Void, Void, Object> imageDownloadAsyncTask = new AsyncTask<Void, Void, Object>() { @Override//from ww w . java 2 s . co m protected Object doInBackground(Void... params) { Object ret = null; BufferedInputStream bufferedInputStream = null; FileOutputStream fileOutputStream = null; try { logString("debug", "Opening stream for " + whatToDownload); bufferedInputStream = new BufferedInputStream( new URL(URLDecoder.decode(whatToDownload, "UTF-8").replaceAll(" ", "%20")) .openStream()); logString("debug", "Opened stream for " + whatToDownload); fileOutputStream = new FileOutputStream(whereToSaveIt); final byte data[] = new byte[1024]; int count; logString("debug", "Loop-writing " + whatToDownload); while ((count = bufferedInputStream.read(data, 0, 1024)) != -1) { fileOutputStream.write(data, 0, count); } logString("debug", "Loop-written " + whatToDownload); } catch (IOException e) { return e; } finally { if (bufferedInputStream != null) { try { bufferedInputStream.close(); } catch (IOException e) { ret = e; } } if (fileOutputStream != null) { try { fileOutputStream.close(); } catch (IOException e) { ret = e; } } } return ret; } }; imageDownloadAsyncTask.executeOnExecutor(fileDownloadExecutor); Object returned = null; try { returned = imageDownloadAsyncTask.get(); } catch (ExecutionException | InterruptedException e) { Crashlytics.logException(e); } if (returned != null) { throw (IOException) returned; } }
From source file:backend.translator.GTranslator.java
private File downloadFile(String url) throws IOException { HttpClient client = HttpClientBuilder.create().build(); HttpGet request = new HttpGet(url); request.addHeader("User-Agent", "Mozilla/5.0"); HttpResponse response = client.execute(request); HttpEntity entity = response.getEntity(); BufferedInputStream bis = new BufferedInputStream(entity.getContent()); File f = new File(R.TRANS_RESULT_PATH); BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream(f)); int inByte;//w w w.j a v a 2s .co m while ((inByte = bis.read()) != -1) bos.write(inByte); bis.close(); bos.close(); return f; }