List of usage examples for java.io BufferedInputStream close
public void close() throws IOException
From source file:com.sim2dial.dialer.ChatFragment.java
public static Bitmap downloadImage(String stringUrl) { URL url;/*from ww w .ja v a 2 s. co m*/ Bitmap bm = null; try { url = new URL(stringUrl); URLConnection ucon = url.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); } byte[] rawImage = baf.toByteArray(); bm = BitmapFactory.decodeByteArray(rawImage, 0, rawImage.length); bis.close(); } catch (Exception e) { e.printStackTrace(); } return bm; }
From source file:org.ametro.util.WebUtil.java
public static void downloadFile(Object context, URI uri, File file, boolean reuse, IDownloadListener listener) { BufferedInputStream strm = null; if (listener != null) { listener.onBegin(context, file); }// w ww. j a va2 s. c o m try { HttpClient client = ApplicationEx.getInstance().getHttpClient(); HttpGet request = new HttpGet(); request.setURI(uri); HttpResponse response = client.execute(request); StatusLine status = response.getStatusLine(); if (status.getStatusCode() == 200) { HttpEntity entity = response.getEntity(); long total = (int) entity.getContentLength(); long position = 0; if (!(file.exists() && reuse && file.length() == total)) { if (file.exists()) { file.delete(); } BufferedInputStream in = null; BufferedOutputStream out = null; try { in = new BufferedInputStream(entity.getContent()); out = new BufferedOutputStream(new FileOutputStream(file)); byte[] bytes = new byte[2048]; for (int c = in.read(bytes); c != -1; c = in.read(bytes)) { out.write(bytes, 0, c); position += c; if (listener != null) { if (!listener.onUpdate(context, position, total)) { throw new DownloadCanceledException(); } } } } finally { if (in != null) { try { in.close(); } catch (Exception e) { } } if (out != null) { try { out.close(); } catch (Exception e) { } } } } if (listener != null) { listener.onDone(context, file); } } else { String message = "Failed to download URL " + uri.toString() + " due error " + status.getStatusCode() + " " + status.getReasonPhrase(); throw new Exception(message); } } catch (DownloadCanceledException ex) { if (listener != null) { listener.onCanceled(context, file); } } catch (Exception ex) { if (listener != null) { listener.onFailed(context, file, ex); } } finally { if (strm != null) { try { strm.close(); } catch (IOException ex) { } } } }
From source file:com.streamsets.datacollector.cluster.TarFileCreator.java
/** * Copied from https://raw.githubusercontent.com/kamranzafar/jtar/master/src/test/java/org/kamranzafar/jtar/JTarTest.java *//*from ww w .jav a 2 s.c o m*/ private static void tarFolder(String parent, String path, TarOutputStream out) throws IOException { BufferedInputStream src = null; File f = new File(path); String files[] = f.list(); // is file if (files == null) { files = new String[1]; files[0] = f.getName(); } parent = ((parent == null) ? (f.isFile()) ? "" : f.getName() + "/" : parent + f.getName() + "/"); for (int i = 0; i < files.length; i++) { File fe = f; if (f.isDirectory()) { fe = new File(f, files[i]); } if (fe.isDirectory()) { String[] fl = fe.list(); if (fl != null && fl.length != 0) { tarFolder(parent, fe.getPath(), out); } else { TarEntry entry = new TarEntry(fe, parent + files[i] + "/"); out.putNextEntry(entry); } continue; } FileInputStream fi = new FileInputStream(fe); src = new BufferedInputStream(fi); TarEntry entry = new TarEntry(fe, parent + files[i]); out.putNextEntry(entry); IOUtils.copy(src, out); src.close(); out.flush(); } }
From source file:com.phonegap.plugins.ftpclient.FtpClient.java
/** * Uploads a file to a ftp server.//from w w w. j av a 2s . c o m * @param filename the name of the local file to send to the server * @param url the url of the server * @throws IOException */ private void put(String filename, URL url) throws IOException { FTPClient f = setup(url); BufferedInputStream buffIn = null; buffIn = new BufferedInputStream(new FileInputStream(filename)); f.storeFile(extractFileName(url), buffIn); buffIn.close(); teardown(f); }
From source file:de.thorstenberger.examServer.dao.xml.AbstractJAXBDao.java
/** * Unmarshall the given xml file and return the corresponding object. Will wrap any exception into a * {@link RuntimeException}.//from w ww.ja v a2 s . c o m * * @return the deserialized object * @throws RuntimeException * wrapping {@link JAXBException} of {@link FileNotFoundException} */ protected Object load() { log.debug(String.format("Trying to load xml package from file '%s'", workingPath + "/" + xmlFileName)); // start new file transaction Unmarshaller unmarshaller = null; try { // deserialize the xml unmarshaller = JAXBUtils.getJAXBUnmarshaller(jc); final BufferedInputStream bis = new BufferedInputStream(getFRM().readResource(xmlFileName)); final Object obj = unmarshaller.unmarshal(bis); bis.close(); // finish the transaction return obj; } catch (final JAXBException e) { throw new RuntimeException(e); } catch (final ResourceManagerException e) { throw new RuntimeException(e); } catch (final IOException e) { throw new RuntimeException(e); } finally { if (unmarshaller != null) JAXBUtils.releaseJAXBUnmarshaller(jc, unmarshaller); } }
From source file:com.servoy.extensions.plugins.http.Response.java
/** * Get the content of response as binary data. It also supports gzip-ed content. * * @sample/* ww w. ja v a 2s . c o m*/ * var mediaData = response.getMediaData(); */ public byte[] js_getMediaData() { if (response_body == null) { try { ByteArrayOutputStream sb = new ByteArrayOutputStream(); InputStream is = null; Header contentEncoding = res.getFirstHeader("Content-Encoding"); boolean gziped = contentEncoding == null ? false : "gzip".equalsIgnoreCase(contentEncoding.getValue()); is = res.getEntity().getContent(); if (gziped) { is = new GZIPInputStream(is); } BufferedInputStream bis = new BufferedInputStream(is); Utils.streamCopy(bis, sb); bis.close(); is.close(); response_body = sb.toByteArray(); } catch (IOException e) { Debug.error(e); } } return response_body instanceof byte[] ? (byte[]) response_body : null; }
From source file:de.tudarmstadt.ukp.teaching.uima.nounDecompounding.ranking.MutualInformationBased.java
public MutualInformationBased(Finder aFinder) { super(aFinder); try {// www . ja v a 2 s . com Properties properties = new Properties(); BufferedInputStream stream = new BufferedInputStream( new FileInputStream("src/main/resources/index.properties")); properties.load(stream); stream.close(); FREQUENCY = new BigInteger(properties.getProperty("frequency")); } catch (Exception e) { FREQUENCY = new BigInteger("143782944956"); } }
From source file:com.sina.stock.SinaStockClient.java
private Bitmap getBitmapFromUrl(String url) throws HttpException, IOException { HttpMethod method = new GetMethod(url); int statusCode = mHttpClient.executeMethod(method); if (statusCode != HttpStatus.SC_OK) { method.releaseConnection();//from w w w . ja v a2 s . c om return null; } InputStream in = method.getResponseBodyAsStream(); BufferedInputStream bis = new BufferedInputStream(in); Bitmap bm = BitmapFactory.decodeStream(bis); bis.close(); method.releaseConnection(); return bm; }
From source file:com.ibuildapp.romanblack.FanWallPlugin.data.Statics.java
/** * download file url and save it//from w ww . j a v a2 s . c om * * @param url */ public static String downloadFile(String url) { int BYTE_ARRAY_SIZE = 1024; int CONNECTION_TIMEOUT = 30000; int READ_TIMEOUT = 30000; // downloading cover image and saving it into file try { URL imageUrl = new URL(URLDecoder.decode(url)); URLConnection conn = imageUrl.openConnection(); conn.setConnectTimeout(CONNECTION_TIMEOUT); conn.setReadTimeout(READ_TIMEOUT); BufferedInputStream bis = new BufferedInputStream(conn.getInputStream()); File resFile = new File(cachePath + File.separator + Utils.md5(url)); if (!resFile.exists()) { resFile.createNewFile(); } FileOutputStream fos = new FileOutputStream(resFile); int current = 0; byte[] buf = new byte[BYTE_ARRAY_SIZE]; Arrays.fill(buf, (byte) 0); while ((current = bis.read(buf, 0, BYTE_ARRAY_SIZE)) != -1) { fos.write(buf, 0, current); Arrays.fill(buf, (byte) 0); } bis.close(); fos.flush(); fos.close(); Log.d("", ""); return resFile.getAbsolutePath(); } catch (SocketTimeoutException e) { return null; } catch (IllegalArgumentException e) { return null; } catch (Exception e) { return null; } }
From source file:de.uni_koeln.spinfo.maalr.webapp.controller.EditorController.java
@RequestMapping("/editor/download/{fileName}.html") public void export(@PathVariable("fileName") String fileName, HttpServletResponse response) throws IOException { File dir = new File("exports"); File file = new File(dir, fileName); ServletOutputStream out = response.getOutputStream(); if (!file.exists()) { OutputStreamWriter writer = new OutputStreamWriter(out); writer.write("This link has expired. Please re-export the data and try again."); writer.flush();//ww w.java2s . c o m return; } response.setContentType("application/zip"); response.setHeader("Content-Disposition", "attachment; filename=" + fileName); BufferedInputStream in = new BufferedInputStream(new FileInputStream(file)); IOUtils.copy(in, out); in.close(); out.close(); file.delete(); }