List of usage examples for java.io BufferedInputStream read
public synchronized int read() throws IOException
read
method of InputStream
. From source file:com.android.tradefed.util.FileUtil.java
/** * Utility method to do byte-wise content comparison of two files. * * @return <code>true</code> if file contents are identical *//*from w w w .ja v a2 s .c o m*/ public static boolean compareFileContents(File file1, File file2) throws IOException { BufferedInputStream stream1 = null; BufferedInputStream stream2 = null; boolean result = true; try { stream1 = new BufferedInputStream(new FileInputStream(file1)); stream2 = new BufferedInputStream(new FileInputStream(file2)); boolean eof = false; while (!eof) { int byte1 = stream1.read(); int byte2 = stream2.read(); if (byte1 != byte2) { result = false; break; } eof = byte1 == -1; } } finally { StreamUtil.close(stream1); StreamUtil.close(stream2); } return result; }
From source file:com.athena.chameleon.web.common.controller.FileController.java
@RequestMapping("/download.do") public void download(HttpServletRequest request, HttpServletResponse response, @RequestParam("path") String path) throws Exception { Assert.notNull(path, "name must not be null."); File file = new File(path); Assert.isTrue(file.exists(), path + " does not exist."); String name = path.replaceAll("\\\\", "/"); name = name.substring(name.lastIndexOf("/") + 1, name.length()); long fileSize = file.length(); if (fileSize > 0L) { response.setHeader("Content-Length", Long.toString(fileSize)); }/*from www .ja v a 2s .c o m*/ String fileExt = name.substring(name.lastIndexOf(".") + 1); response.reset(); if (fileExt.equals("pdf")) { response.setContentType("application/pdf"); } else if (fileExt.equals("zip")) { response.setContentType("application/zip"); } else if (fileExt.equals("ear")) { response.setContentType("application/zip"); } else if (fileExt.equals("war")) { response.setContentType("application/zip"); } else if (fileExt.equals("jar")) { response.setContentType("application/zip"); } else { response.setContentType("application/octet-stream"); } if (request.getHeader("User-Agent").toLowerCase().contains("firefox") || request.getHeader("User-Agent").toLowerCase().contains("safari")) { response.setHeader("Content-Disposition", "attachment; filename=\"" + new String(name.getBytes("UTF-8"), "ISO-8859-1") + "\""); response.setHeader("Content-Transfer-Encoding", "binary"); } else { response.setHeader("Content-Disposition", "attachment;filename=" + URLEncoder.encode(name, "UTF-8")); } try { BufferedInputStream fin = new BufferedInputStream(new FileInputStream(file)); BufferedOutputStream outs = new BufferedOutputStream(response.getOutputStream()); int read = 0; while ((read = fin.read()) != -1) { outs.write(read); } IOUtils.closeQuietly(fin); IOUtils.closeQuietly(outs); } catch (Exception e) { e.printStackTrace(); } }
From source file:com.panoskrt.dbadapter.DBAdapter.java
public void downloadDB(Context context, String dbName, String urlLink) { try {// ww w.j av a2s . c om URL url = new URL(urlLink); 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); } FileOutputStream fos = context.openFileOutput(dbName, Context.MODE_PRIVATE); fos.write(baf.toByteArray()); fos.close(); File dbFile = new File(context.getFilesDir() + "/" + dbName); InputStream in = new FileInputStream(dbFile); OutputStream out = new FileOutputStream(dbPath + dbName); bufCopy(in, out); dbFile.delete(); } catch (Exception ex) { ex.printStackTrace(); } }
From source file:com.att.voice.TTS.java
public void say(String text, String file) { text = text.replace("\"", ""); try {// w w w. j a va2 s . co m HttpPost httpPost = new HttpPost("https://api.att.com/speech/v3/textToSpeech"); httpPost.setHeader("Authorization", "Bearer " + mAuthToken); httpPost.setHeader("Accept", "audio/x-wav"); httpPost.setHeader("Content-Type", "text/plain"); httpPost.setHeader("Tempo", "-16"); HttpEntity entity = new StringEntity(text, "UTF-8"); httpPost.setEntity(entity); HttpResponse response = httpclient.execute(httpPost); //String result = EntityUtils.toString(response.getEntity()); HttpEntity result = response.getEntity(); BufferedInputStream bis = new BufferedInputStream(result.getContent()); String filePath = System.getProperty("user.dir") + tempFile; BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream(new File(filePath))); int inByte; while ((inByte = bis.read()) != -1) { bos.write(inByte); } bis.close(); bos.close(); executeOnCommandLine("afplay " + System.getProperty("user.dir") + "/" + tempFile); } catch (Exception ex) { System.err.println(ex.getMessage()); } }
From source file:com.comcast.cmb.test.tools.CNSTestingUtils.java
public static String sendHttpMessage(String endpoint, String message) throws Exception { if ((message == null) || (endpoint == null)) { throw new Exception("Message and Endpoint must both be set"); }/*from w w w . ja v a 2 s. c o m*/ String newPostBody = message; byte newPostBodyBytes[] = newPostBody.getBytes(); URL url = new URL(endpoint); logger.info(">> " + url.toString()); HttpURLConnection con = (HttpURLConnection) url.openConnection(); con.setRequestMethod("GET"); // POST no matter what con.setDoOutput(true); con.setDoInput(true); con.setFollowRedirects(false); con.setUseCaches(true); logger.info(">> " + "GET"); //con.setRequestProperty("content-length", newPostBody.length() + ""); con.setRequestProperty("host", url.getHost()); con.connect(); logger.info(">> " + newPostBody); int statusCode = con.getResponseCode(); BufferedInputStream responseStream; logger.info("StatusCode:" + statusCode); if (statusCode != 200 && statusCode != 201) { responseStream = new BufferedInputStream(con.getErrorStream()); } else { responseStream = new BufferedInputStream(con.getInputStream()); } int b; String response = ""; while ((b = responseStream.read()) != -1) { response += (char) b; } logger.info("response:" + response); return response; }
From source file:ch.ethz.twimight.net.opportunistic.ScanningService.java
public static byte[] toByteArray(InputStream in) throws IOException { BufferedInputStream bis = new BufferedInputStream(in); ByteArrayBuffer baf = new ByteArrayBuffer(2048); // get the bytes one by one int current = 0; while ((current = bis.read()) != -1) { baf.append((byte) current); }/*from ww w. j ava 2 s . c o m*/ return baf.toByteArray(); }
From source file:eu.esdihumboldt.hale.io.geoserver.AbstractResource.java
/** * @see eu.esdihumboldt.hale.io.geoserver.Resource#write(java.io.OutputStream) */// ww w. j a v a2 s .com @Override public void write(OutputStream out) throws IOException { // unset unspecified variables by setting their value to null for (String var : this.allowedAttributes) { if (!this.attributes.containsKey(var)) { this.attributes.put(var, null); } } InputStream resourceStream = locateResource(); if (resourceStream != null) { BufferedInputStream input = new BufferedInputStream(resourceStream); BufferedOutputStream output = new BufferedOutputStream(out); try { for (int b = input.read(); b >= 0; b = input.read()) { output.write(b); } } finally { try { input.close(); } catch (IOException e) { // ignore exception on close } try { output.close(); } catch (IOException e) { // ignore exception on close } } } }
From source file:it.unicaradio.android.gcm.GcmServerRpcCall.java
/** * @param conn//ww w .j a va 2s . co m * @throws IOException */ private String getResult(HttpURLConnection conn) throws IOException { InputStream is = conn.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); } String result = new String(baf.toByteArray()); Log.d(TAG, result); return result; }
From source file:fr.shywim.antoinedaniel.sync.AppState.java
/** * Load AppState from file.// www . j a v a 2 s . co 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:com.zoffcc.applications.aagtl.ImageManager.java
public void DownloadFromUrl(String imageURL, String fileName) { // this is // the downloader method try {/*from w ww . ja va2s . c om*/ URL url = new URL(imageURL); File file = new File(fileName); //long startTime = System.currentTimeMillis(); //Log.d("ImageManager", "download begining"); //Log.d("ImageManager", "download url:" + url); //Log.d("ImageManager", "downloaded file name:" + fileName); /* Open a connection to that URL. */ URLConnection ucon = url.openConnection(); ucon.setConnectTimeout(10000); ucon.setReadTimeout(7000); /* * Define InputStreams to read from the URLConnection. */ InputStream is = ucon.getInputStream(); BufferedInputStream bis = new BufferedInputStream(is, HTMLDownloader.large_buffer_size); /* * Read bytes to the Buffer until there is nothing more to read(-1). */ ByteArrayBuffer baf = new ByteArrayBuffer(HTMLDownloader.default_buffer_size); 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(); //Log.d("ImageManager", "download ready in" // + ((System.currentTimeMillis() - startTime) / 1000) // + " sec"); } catch (SocketTimeoutException e2) { Log.d("ImageManager", "Connectiont timout: " + e2); } catch (IOException e) { Log.d("ImageManager", "Error: " + e); } catch (Exception e) { Log.d("ImageManager", "Error: " + e); } }