List of usage examples for java.io BufferedInputStream close
public void close() throws IOException
From source file:com.andrewshu.android.reddit.threads.BitmapManager.java
/** * http://ballardhack.wordpress.com/2010/04/10/loading-images-over-http-on-a-separate-thread-on-android/ * Convenience method to retrieve a bitmap image from * a URL over the network. The built-in methods do * not seem to work, as they return a FileNotFound * exception./*w w w . ja v a2 s . c om*/ * * Note that this does not perform any threading -- * it blocks the call while retrieving the data. * * @param url The URL to read the bitmap from. * @return A Bitmap image or null if an error occurs. */ public Bitmap readBitmapFromNetwork(String url) { InputStream is = null; BufferedInputStream bis = null; Bitmap bmp = null; try { is = fetch(url); bis = new BufferedInputStream(is); bmp = BitmapFactory.decodeStream(bis); } catch (MalformedURLException e) { Log.e(TAG, "Bad ad URL", e); } catch (IOException e) { Log.e(TAG, "Could not get remote ad image", e); } finally { try { if (is != null) is.close(); if (bis != null) bis.close(); } catch (IOException e) { Log.w(TAG, "Error closing stream."); } } return bmp; }
From source file:com.tesshu.subsonic.client.sample4_music_andmovie.StreamDownloadAndPlayWithThreadApplication.java
@Override public void start(Stage stage) throws Exception { Search2Controller search2 = context.getBean(Search2Controller.class); StreamController streamController = context.getBean(StreamController.class); SuccessObserver callback = context.getBean(SuccessObserver.class); SearchResult2 result2 = search2.get("e", null, null, null, null, 1, null, null); List<Child> songs = result2.getSongs(); File tmpDirectory = new File(tmpPath); tmpDirectory.mkdir();/*from w w w .jav a 2 s . c om*/ int maxBitRate = 256; Child song = songs.get(0); new Thread(new Runnable() { public void run() { try { streamController.stream(song, maxBitRate, format, null, null, null, null, (subject, inputStream, contentLength) -> { File dir = new File( tmpPath + "/" + song.getPath().replaceAll("([^/]+?)?$", StringUtils.EMPTY)); dir.mkdirs(); file = new File(tmpPath + "/" + song.getPath().replaceAll("([^.]+?)?$", StringUtils.EMPTY) + format); try { FileOutputStream fos = new FileOutputStream(file); BufferedInputStream reader = new BufferedInputStream(inputStream); byte buf[] = new byte[256]; int len; while ((len = reader.read(buf)) != -1) { fos.write(buf, 0, len); } fos.flush(); fos.close(); reader.close(); inputStream.close(); LOG.info("download finished"); } catch (IOException e) { e.printStackTrace(); } }, callback); } catch (Exception e) { e.printStackTrace(); } } }).start(); LOG.info("download thread start"); new Thread(new Runnable() { public void run() { while (file == null || file.getPath() == null) { LOG.info("wait file writing."); try { Thread.sleep(1000); } catch (InterruptedException e) { e.printStackTrace(); } String path = Paths.get(file.getPath()).toUri().toString(); Group root = new Group(); Scene scene = new Scene(root, 640, 480); Media media = new Media(path); MediaPlayer player = new MediaPlayer(media); new Thread(new Runnable() { public void run() { try { while (MediaPlayer.Status.READY != player.getStatus()) { LOG.info(player.getStatus() + " : " + path); LOG.info(media.errorProperty()); Thread.sleep(1000); if (MediaPlayer.Status.PLAYING == player.getStatus()) { LOG.info(player.getStatus() + " : " + path); break; } } } catch (Exception e) { e.printStackTrace(); } } }).start(); MediaView view = new MediaView(player); ((Group) scene.getRoot()).getChildren().add(view); Platform.runLater(() -> { stage.setScene(scene); stage.show(); player.play(); }); } } }).start(); }
From source file:de.dhbw_mannheim.cloudraid.dropbox.impl.net.connector.DropboxConnector.java
@Override public byte[] getMetadata(String resource, int size) { Response response = performGet(resource, "m"); if (response == null) { return null; }/*from w ww .jav a2 s . c o m*/ BufferedInputStream bis = new BufferedInputStream(response.getStream()); byte meta[] = new byte[size]; Arrays.fill(meta, (byte) 0); try { bis.read(meta, 0, size); } catch (IOException ignore) { meta = null; } finally { try { bis.close(); } catch (Exception ignore) { } } return meta; }
From source file:eu.scape_project.arc2warc.identification.PayloadContent.java
private byte[] inputStreamToByteArray() throws IOException { ByteArrayOutputStream baos = new ByteArrayOutputStream(); BufferedInputStream buffis = new BufferedInputStream(inputStream); BufferedOutputStream buffos = new BufferedOutputStream(baos); byte[] tempBuffer = new byte[BUFFER_SIZE]; int bytesRead; boolean firstByteArray = true; while ((bytesRead = buffis.read(tempBuffer)) != -1) { buffos.write(tempBuffer, 0, bytesRead); if (applyIdentification && firstByteArray && tempBuffer != null && bytesRead > 0) { identified = identifyPayloadType(tempBuffer); }//from w w w .j av a 2s .c om firstByteArray = false; } buffis.close(); buffos.flush(); buffos.close(); return baos.toByteArray(); }
From source file:net.sf.xmm.moviemanager.http.HttpUtil.java
public HTTPResult readData1(URL url) throws Exception { if (!isSetup()) setup();/*w w w. j ava 2 s . c o m*/ GetMethod method = new GetMethod(url.toString()); int statusCode = client.executeMethod(method); if (statusCode != HttpStatus.SC_OK) { log.debug("HTTP StatusCode not HttpStatus.SC_OK:(" + statusCode + "):" + method.getStatusLine()); } BufferedInputStream stream = new BufferedInputStream(method.getResponseBodyAsStream()); StringBuffer data = new StringBuffer(); // Saves the page data in a string buffer... int buffer; while ((buffer = stream.read()) != -1) { data.append((char) buffer); } stream.close(); return new HTTPResult(url, statusCode == HttpStatus.SC_OK ? data : null, method.getStatusLine()); }
From source file:net.sf.jmimemagic.detectors.OfficeXFileDetector.java
/** * DOCUMENT ME!/*w w w. j av a2s.c o m*/ * * @param file DOCUMENT ME! * @param offset DOCUMENT ME! * @param length DOCUMENT ME! * @param bitmask DOCUMENT ME! * @param comparator DOCUMENT ME! * @param mimeType DOCUMENT ME! * @param params DOCUMENT ME! * * @return DOCUMENT ME! */ public String[] process(File file, int offset, int length, long bitmask, char comparator, String mimeType, Map params) { log.debug("processing file data"); BufferedInputStream is = null; try { is = new BufferedInputStream(new FileInputStream(file)); byte[] b = new byte[length]; int n = is.read(b, offset, length); if (n > 0) { return process(b, offset, length, bitmask, comparator, mimeType, params); } } catch (IOException e) { log.error("error opening stream", e); } finally { if (is != null) { try { is.close(); } catch (IOException e) { log.error("error closing stream"); } } } return null; }
From source file:net.sf.jvifm.ui.ZipLister.java
private File extractToTemp(FileObject fileObject) { String basename = fileObject.getName().getBaseName(); File tempFile = null;/*from w w w . j a v a2 s. c om*/ try { String tmpPath = System.getProperty("java.io.tmpdir"); tempFile = new File(FilenameUtils.concat(tmpPath, basename)); byte[] buf = new byte[4096]; BufferedInputStream bin = new BufferedInputStream(fileObject.getContent().getInputStream()); BufferedOutputStream bout = new BufferedOutputStream(new FileOutputStream(tempFile)); while (bin.read(buf, 0, 1) != -1) { bout.write(buf, 0, 1); } bout.close(); bin.close(); // by barney } catch (Throwable e) { e.printStackTrace(); } return tempFile; }
From source file:jp.co.opentone.bsol.linkbinder.view.logo.ProjectLogoManagerTest.java
private ProjectLogo getExpectedData(String testFile) { BufferedInputStream bis = null; File f = new File(testFile); byte[] data = new byte[(int) f.length()]; try {/*from w w w . j av a 2s . com*/ bis = new BufferedInputStream(new FileInputStream(testFile)); bis.read(data); bis.close(); } catch (Exception e) { fail("?????"); } ProjectLogo expected = new ProjectLogo(); expected.setImage(data); expected.setLastModified(f.lastModified()); return expected; }
From source file:com.mousebird.maply.MaplyStarModel.java
public MaplyStarModel(String fileName, String imageName, Activity activity) throws IOException { AssetManager assetMgr = activity.getAssets(); InputStream inputStream = null; String[] paths = assetMgr.list("maplystarmodel"); for (String path : paths) { if (path.equals(imageName)) { //image BufferedInputStream bufferedInputStream = null; try { inputStream = assetMgr.open("maplystarmodel/" + path); bufferedInputStream = new BufferedInputStream(inputStream); image = BitmapFactory.decodeStream(bufferedInputStream); } finally { if (bufferedInputStream != null) { try { bufferedInputStream.close(); } catch (IOException e) { }/*from w ww . j a va 2s . c o m*/ } } } if (path.equals(fileName)) { //data Matcher m; try { inputStream = assetMgr.open("maplystarmodel/" + path); String stars = IOUtils.toString(inputStream, Charset.defaultCharset()); Pattern p = Pattern.compile("[-]?[0-9]*\\.?[0-9]+"); m = p.matcher(stars); } finally { if (inputStream != null) { try { inputStream.close(); } catch (IOException e) { } } } this.stars = new ArrayList<SingleStar>(); if (m.groupCount() % 3 == 0) { int i = 0; SingleStar s = null; while (m.find()) { switch (i) { case 0: s = new SingleStar(); s.ra = Float.valueOf(m.group()); i++; break; case 1: s.dec = Float.valueOf(m.group()); i++; break; case 2: s.mag = Float.valueOf(m.group()); this.stars.add(s); i = 0; break; default: break; } } } } } }
From source file:gov.nih.nci.caintegrator.application.analysis.grid.preprocess.PreprocessDatasetGridRunner.java
private void postUpload(PreprocessDatasetMAGEServiceContextClient analysis, PreprocessDatasetParameters parameters, File unprocessedGctFile) throws IOException, ConnectionException { TransferServiceContextReference up = analysis.submitData(parameters.createParameterList()); TransferServiceContextClient tClient = new TransferServiceContextClient(up.getEndpointReference()); BufferedInputStream bis = null; try {//from w w w . j ava 2 s . c o m long size = unprocessedGctFile.length(); bis = new BufferedInputStream(new FileInputStream(unprocessedGctFile)); TransferClientHelper.putData(bis, size, tClient.getDataTransferDescriptor()); } catch (Exception e) { // For some reason TransferClientHelper throws "Exception", going to rethrow a connection exception. throw new ConnectionException("Unable to transfer gct data to the server.", e); } finally { if (bis != null) { bis.close(); } } tClient.setStatus(Status.Staged); }