List of usage examples for java.io BufferedInputStream read
public synchronized int read() throws IOException
read
method of InputStream
. From source file:Main.java
public static boolean downloadUrlToStream(String urlString, OutputStream outputStream) { HttpURLConnection urlConnection = null; BufferedOutputStream out = null; BufferedInputStream in = null; try {/*from w ww. ja v a 2 s . c o m*/ final URL url = new URL(urlString); urlConnection = (HttpURLConnection) url.openConnection(); in = new BufferedInputStream(urlConnection.getInputStream(), 8 * 1024); out = new BufferedOutputStream(outputStream, 8 * 1024); int b; while ((b = in.read()) != -1) { out.write(b); } return true; } catch (final IOException e) { e.printStackTrace(); } finally { if (urlConnection != null) { urlConnection.disconnect(); } try { if (out != null) { out.close(); } if (in != null) { in.close(); } } catch (final IOException e) { e.printStackTrace(); } } return false; }
From source file:com.cyberway.issue.crawler.frontier.RecoveryJournal.java
/** * Read a line from the given bufferedinputstream into the MutableString. * Return true if a line was read; false if EOF. * //from w ww .j a v a2 s. c o m * @param is * @param read * @return True if we read a line. * @throws IOException */ private static boolean readLine(BufferedInputStream is, MutableString read) throws IOException { read.length(0); int c = is.read(); while ((c != -1) && c != '\n' && c != '\r') { read.append((char) c); c = is.read(); } if (c == -1 && read.length() == 0) { // EOF and none read; return false return false; } if (c == '\n') { // consume LF following CR, if present is.mark(1); if (is.read() != '\r') { is.reset(); } } // a line (possibly blank) was read return true; }
From source file:com.BibleQuote.utils.FsUtils.java
public static boolean loadContentFromURL(String fromURL, String toFile) { try {/*w ww . j av a 2 s . com*/ URL url = new URL("http://bible-desktop.com/xml" + fromURL); File file = new File(toFile); /* Open a connection to that URL. */ URLConnection ucon = url.openConnection(); /* Define InputStreams to read from the URLConnection */ InputStream is = ucon.getInputStream(); BufferedInputStream bis = new BufferedInputStream(is); /* Read bytes to the Buffer until there is nothing more to read(-1) */ ByteArrayBuffer baf = new ByteArrayBuffer(50); int current = 0; while ((current = bis.read()) != -1) { baf.append((byte) current); } /* Convert the Bytes read to a String. */ FileOutputStream fos = new FileOutputStream(file); fos.write(baf.toByteArray()); fos.close(); } catch (IOException e) { Log.e(TAG, String.format("loadContentFromURL(%1$s, %2$s)", fromURL, toFile), e); return false; } return true; }
From source file:oneDrive.OneDriveAPI.java
public static void uploadFile(String filePath) { URLConnection urlconnection = null; try {//from w ww . jav a 2 s . com File file = new File(UPLOAD_PATH + filePath); URL url = new URL(DRIVE_PATH + file.getName() + ":/content"); urlconnection = url.openConnection(); urlconnection.setDoOutput(true); urlconnection.setDoInput(true); if (urlconnection instanceof HttpURLConnection) { try { ((HttpURLConnection) urlconnection).setRequestMethod("PUT"); ((HttpURLConnection) urlconnection).setRequestProperty("Content-type", "application/octet-stream"); ((HttpURLConnection) urlconnection).addRequestProperty("Authorization", "Bearer " + ACCESS_TOKEN); ((HttpURLConnection) urlconnection).connect(); } catch (ProtocolException e) { e.printStackTrace(); } } BufferedOutputStream bos = new BufferedOutputStream(urlconnection.getOutputStream()); BufferedInputStream bis = new BufferedInputStream(new FileInputStream(file)); int i; // read byte by byte until end of stream while ((i = bis.read()) >= 0) { bos.write(i); } bos.flush(); bos.close(); } catch (Exception e) { e.printStackTrace(); } }
From source file:bala.padio.Settings.java
public static String DownloadFromUrl(String u) { try {// w ww . j a v a2 s. c o m URL url = new URL(u); 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); } return new String(baf.toByteArray()); } catch (Exception e) { Log.e(TAG, "Error: " + e); } return null; }
From source file:edu.harvard.i2b2.eclipse.plugins.metadataLoader.util.FileUtil.java
public static void download(String workingDir, String URL, String filename) throws IOException { workingDir = workingDir + filename;// ww w .j av a 2 s . co m DefaultHttpClient httpclient = new DefaultHttpClient(); HttpGet httpget = new HttpGet(URL); HttpResponse response = httpclient.execute(httpget); // System.out.println(response.getStatusLine()); HttpEntity entity = response.getEntity(); if (entity != null) { InputStream instream = entity.getContent(); try { BufferedInputStream bis = new BufferedInputStream(instream); BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream(new File(workingDir))); int inByte; while ((inByte = bis.read()) != -1) { bos.write(inByte); } bis.close(); bos.close(); // unzip("ICD10.zip"); } catch (IOException ex) { throw ex; } catch (RuntimeException ex) { httpget.abort(); throw ex; } finally { instream.close(); } httpclient.getConnectionManager().shutdown(); } }
From source file:org.ligi.android.dubwise_uavtalk.dashboard.DownloadDashboardImagesStatusAlertDialog.java
/** * //from w ww .j a va 2 s . c o m * @param activity * @param autoclose - if the alert should close when connection is established * */ public static void show(Context activity, boolean autoclose, Intent after_connection_intent) { LinearLayout lin = new LinearLayout(activity); lin.setOrientation(LinearLayout.VERTICAL); ScrollView sv = new ScrollView(activity); TextView details_text_view = new TextView(activity); LinearLayout lin_in_scrollview = new LinearLayout(activity); lin_in_scrollview.setOrientation(LinearLayout.VERTICAL); sv.addView(lin_in_scrollview); lin_in_scrollview.addView(details_text_view); details_text_view.setText("no text"); ProgressBar progress = new ProgressBar(activity, null, android.R.attr.progressBarStyleHorizontal); progress.setMax(img_lst.length); lin.addView(progress); lin.addView(sv); new AlertDialog.Builder(activity).setTitle("Download Status").setView(lin) .setPositiveButton("OK", new DialogDiscardingOnClickListener()).show(); class AlertDialogUpdater implements Runnable { private Handler h = new Handler(); private TextView myTextView; private ProgressBar myProgress; public AlertDialogUpdater(TextView ab, ProgressBar progress) { myTextView = ab; myProgress = progress; } public void run() { for (int i = 0; i < img_lst.length; i++) { class MsgUpdater implements Runnable { private int i; public MsgUpdater(int i) { this.i = i; } public void run() { myProgress.setProgress(i + 1); if (i != img_lst.length - 1) myTextView.setText("Downloading " + img_lst[i] + ".png"); else myTextView.setText("Ready - please restart DUBwise to apply changes!"); } } h.post(new MsgUpdater(i)); try { URLConnection ucon = new URL(url_lst[i]).openConnection(); BufferedInputStream bis = new BufferedInputStream(ucon.getInputStream()); ByteArrayBuffer baf = new ByteArrayBuffer(50); int current = 0; while ((current = bis.read()) != -1) baf.append((byte) current); File path = new File( Environment.getExternalStorageDirectory() + "/dubwise/images/dashboard"); path.mkdirs(); FileOutputStream fos = new FileOutputStream( new File(path.getAbsolutePath() + "/" + img_lst[i] + ".png")); fos.write(baf.toByteArray()); fos.close(); } catch (Exception e) { } try { Thread.sleep(199); } catch (InterruptedException e) { } } } } new Thread(new AlertDialogUpdater(details_text_view, progress)).start(); }
From source file:ota.otaupdates.utils.Utils.java
public static void DownloadFromUrl(String download_url, String fileName) { final String TAG = "Downloader"; if (!isMainThread()) { try {/*from w w w. ja v a 2 s .co m*/ URL url = new URL(download_url); if (!new File(DL_PATH).isDirectory()) { if (!new File(DL_PATH).mkdirs()) { Log.e(TAG, "Creating the directory " + DL_PATH + "failed"); } } File file = new File(DL_PATH + fileName); long startTine = System.currentTimeMillis(); Log.d(TAG, "Beginning download of " + url.getPath() + " to " + DL_PATH + fileName); /* * Open a connection and define Streams */ URLConnection ucon = url.openConnection(); InputStream is = ucon.getInputStream(); BufferedInputStream bis = new BufferedInputStream(is); /* * Read bytes until there is nothing left */ ByteArrayBuffer baf = new ByteArrayBuffer(50); int current = 0; while ((current = bis.read()) != -1) { baf.append((byte) current); } /* Convert Bytes to a String */ FileOutputStream fos = new FileOutputStream(file); fos.write(baf.toByteArray()); fos.close(); Log.d(TAG, "Download finished in " + ((System.currentTimeMillis() - startTine) / 1000) + " seconds"); } catch (Exception e) { Log.e(TAG, "Error: " + e); e.printStackTrace(); } } else { Log.e(TAG, "Tried to run in Main Thread. Aborting..."); } }
From source file:info.unyttig.helladroid.newzbin.NewzBinController.java
/** * Finds reports based on the paramaters given in searchOptions * /*from w w w . ja v a2 s . co m*/ * @param searchOptions * @return ArrayList<NewzBinReport> - list of result reports. */ public static ArrayList<NewzBinReport> findReport(final Handler messageHandler, final HashMap<String, String> searchOptions) { String url = NBAPIURL + "reportfind/"; ArrayList<NewzBinReport> searchRes = new ArrayList<NewzBinReport>(); try { HttpResponse response = doPost(url, searchOptions); checkReturnCode(response.getStatusLine().getStatusCode(), false); InputStream is = response.getEntity().getContent(); BufferedInputStream bis = new BufferedInputStream(is); ByteArrayBuffer baf = new ByteArrayBuffer(20); int current = 0; while ((current = bis.read()) != -1) { baf.append((byte) current); } /* Convert the Bytes read to a String. */ String text = new String(baf.toByteArray()); // Log.d(LOG_NAME, text); BufferedReader reader = new BufferedReader(new StringReader(text)); String str = reader.readLine(); totalRes = Integer.parseInt(str.substring(str.indexOf("=") + 1)); while ((str = reader.readLine()) != null) { String[] values = str.split(" "); NewzBinReport temp2 = new NewzBinReport(); temp2.setNzbId(Integer.parseInt(values[0])); temp2.setSize(Long.parseLong(values[1])); temp2.setTitle(values[2]); if (!reports.containsKey(temp2.getNzbId())) { reports.put(temp2.getNzbId(), temp2); searchRes.add(temp2); } else searchRes.add(reports.get(temp2.getNzbId())); } Object[] result = new Object[2]; result[0] = totalRes; result[1] = searchRes; return searchRes; // TODO message handling } catch (ClientProtocolException e) { Log.e(LOG_NAME, "ClientProtocol thrown: ", e); sendUserMsg(messageHandler, e.toString()); } catch (IOException e) { Log.e(LOG_NAME, "IOException thrown: ", e); sendUserMsg(messageHandler, e.toString()); } catch (NewzBinPostReturnCodeException e) { Log.e(LOG_NAME, "POST ReturnCode error: " + e.toString()); sendUserMsg(messageHandler, e.getMessage()); } return searchRes; }
From source file:Main.java
public static boolean bitmapToOutPutStream(final Bitmap bitmap, OutputStream outputStream) { BufferedOutputStream out = null; BufferedInputStream in = null; try {/*w w w. j a v a 2s.c o m*/ ByteArrayOutputStream baos = new ByteArrayOutputStream(); bitmap.compress(Bitmap.CompressFormat.PNG, 100, baos); InputStream inputStream = new ByteArrayInputStream(baos.toByteArray()); in = new BufferedInputStream(inputStream, 8 * 1024); out = new BufferedOutputStream(outputStream, 8 * 1024); int b; while ((b = in.read()) != -1) { out.write(b); } return true; } catch (IOException e) { e.printStackTrace(); } finally { try { if (out != null) { out.close(); } if (in != null) { in.close(); } } catch (final IOException e) { e.printStackTrace(); } } return false; }