List of usage examples for java.io BufferedInputStream read
public int read(byte b[]) throws IOException
b.length
bytes of data from this input stream into an array of bytes. From source file:edu.du.penrose.systems.util.HttpClientUtils.java
/** * Get file from URL, directories are created and files overwritten. * /*from w w w. j a v a2 s. c o m*/ * * @deprecated use org.apache.commons.io.FileUtils.copyURLToFile(URL, File) * @param requestUrl * @param outputPathAndFileName * * @return int request status code OR -1 if an exception occurred */ static public int getToFile(String requestUrl, String outputPathAndFileName) { int resultStatus = -1; File outputFile = new File(outputPathAndFileName); String outputPath = outputFile.getAbsolutePath().replace(outputFile.getName(), ""); File outputDir = new File(outputPath); if (!outputDir.exists()) { outputDir.mkdir(); } HttpClient client = new HttpClient(); // client.getState().setCredentials( // new AuthScope("localhost", 7080, null ), // new UsernamePasswordCredentials("nation", "nationPW") // ); // client.getParams().setAuthenticationPreemptive(true); HttpMethod method = new GetMethod(requestUrl); // method.setDoAuthentication( true ); // client.getParams().setAuthenticationPreemptive(true); // Execute and print response try { OutputStream os = new FileOutputStream(outputFile); client.executeMethod(method); InputStream is = method.getResponseBodyAsStream(); BufferedInputStream bis = new BufferedInputStream(is); byte[] bytes = new byte[8192]; // reading as chunk of 8192 bytes int count = bis.read(bytes); while (count != -1 && count <= 8192) { os.write(bytes, 0, count); count = bis.read(bytes); } bis.close(); os.close(); resultStatus = method.getStatusCode(); } catch (Exception e) { e.printStackTrace(); } finally { method.releaseConnection(); } return resultStatus; }
From source file:eu.scape_project.arc2warc.utils.ArcUtils.java
/** * Read the ARC record content into a byte array. Note that the record * content can be only read once, it is "consumed" afterwards. * * @param arcRecord ARC record./*from ww w. j a va 2 s .com*/ * @return Content byte array. * @throws IOException If content is too large to be stored in a byte array. */ public static byte[] arcRecordPayloadToByteArray(ARCRecord arcRecord) throws IOException { // Byte point where the content of the ARC record begins int contentBegin = (int) arcRecord.getMetaData().getContentBegin(); ByteArrayOutputStream baos = new ByteArrayOutputStream(); BufferedInputStream buffis = new BufferedInputStream(arcRecord); BufferedOutputStream buffos = new BufferedOutputStream(baos); byte[] tempBuffer = new byte[BUFFER_SIZE]; int bytesRead; // skip header content buffis.skip(contentBegin); while ((bytesRead = buffis.read(tempBuffer)) != -1) { buffos.write(tempBuffer, 0, bytesRead); } buffis.close(); buffos.flush(); buffos.close(); return baos.toByteArray(); }
From source file:net.heroicefforts.viable.android.rep.it.auth.Authenticate.java
private static String readResponse(HttpResponse response) throws IOException { InputStream instream = response.getEntity().getContent(); Header contentEncoding = response.getFirstHeader("Content-Encoding"); if (Config.LOGV) //NOPMD if (contentEncoding != null) //NOPMD Log.v(TAG, "Response content encoding was '" + contentEncoding.getValue() + "'"); if (contentEncoding != null && contentEncoding.getValue().equalsIgnoreCase("gzip")) { if (Config.LOGD) Log.d(TAG, "Handling GZIP response."); instream = new GZIPInputStream(instream); }/* ww w .j av a2 s .co m*/ BufferedInputStream bis = new BufferedInputStream(instream); ByteArrayOutputStream baos = new ByteArrayOutputStream(); byte[] buf = new byte[1024]; int read = 0; while ((read = bis.read(buf)) > 0) baos.write(buf, 0, read); String body = baos.toString(); if (Config.LOGV) Log.v(TAG, "Response: " + body); return body; }
From source file:edu.du.penrose.systems.util.HttpClientUtils.java
/** * Appends response form URL to a StringBuffer * // w w w.j av a 2s . co m * @param requestUrl * @param resultStringBuffer * @return int request status code OR -1 if an exception occurred */ static public int getAsString(String requestUrl, StringBuffer resultStringBuffer) { HttpClient client = new HttpClient(); // client.getState().setCredentials( // new AuthScope("localhost", 7080, null ), // new UsernamePasswordCredentials("nation", "nationPW") // ); // client.getParams().setAuthenticationPreemptive(true); HttpMethod method = new GetMethod(requestUrl); // method.setDoAuthentication( true ); // client.getParams().setAuthenticationPreemptive(true); // Execute and print response try { client.executeMethod(method); InputStream is = method.getResponseBodyAsStream(); BufferedInputStream bis = new BufferedInputStream(is); String datastr = null; byte[] bytes = new byte[8192]; // reading as chunk of 8192 bytes int count = bis.read(bytes); while (count != -1 && count <= 8192) { datastr = new String(bytes, 0, count); resultStringBuffer.append(datastr); count = bis.read(bytes); } bis.close(); } catch (Exception e) { e.printStackTrace(); } finally { method.releaseConnection(); } return method.getStatusCode(); }
From source file:Main.java
public static File getFileFromCacheOrURL(File cacheDir, URL url) throws IOException { Log.i(TAG, "getFileFromCacheOrURL(): url = " + url.toExternalForm()); String filename = url.getFile(); int lastSlashPos = filename.lastIndexOf('/'); String fileNameNoPath = new String(lastSlashPos == -1 ? filename : filename.substring(lastSlashPos + 1)); File file = new File(cacheDir, fileNameNoPath); if (file.exists()) { if (file.length() > 0) { Log.i(TAG, "File exists in cache as: " + file.getAbsolutePath()); return file; } else {//from www .ja v a 2 s .c o m Log.i(TAG, "Deleting zero length file " + file.getAbsolutePath()); file.delete(); } } Log.i(TAG, "File " + file.getAbsolutePath() + " does not exists."); URLConnection ucon = url.openConnection(); ucon.setReadTimeout(5000); ucon.setConnectTimeout(30000); InputStream is = ucon.getInputStream(); BufferedInputStream inStream = new BufferedInputStream(is, 1024 * 5); FileOutputStream outStream = new FileOutputStream(file); byte[] buff = new byte[5 * 1024]; // Read bytes (and store them) until there is nothing more to read(-1) int len; while ((len = inStream.read(buff)) != -1) { outStream.write(buff, 0, len); } // Clean up outStream.flush(); outStream.close(); inStream.close(); return file; }
From source file:gov.nih.nci.ncicb.tcga.dcc.qclive.common.logging.ArchiveLoggerImplDBUnitSlowTest.java
private static String readFileAsString(String filePath) throws java.io.IOException { String result = null;// w w w . j a v a2 s .c om BufferedInputStream f = null; try { byte[] buffer = new byte[(int) new File(filePath).length()]; //noinspection IOResourceOpenedButNotSafelyClosed f = new BufferedInputStream(new FileInputStream(filePath)); f.read(buffer); result = new String(buffer); } finally { IOUtils.closeQuietly(f); } return result; }
From source file:com.useekm.types.AbstractGeo.java
public static byte[] gunzip(byte[] bytes) { try {//ww w . ja v a 2s . c o m ByteArrayInputStream bis = new ByteArrayInputStream(bytes); BufferedInputStream bufis = new BufferedInputStream(new GZIPInputStream(bis)); ByteArrayOutputStream bos = new ByteArrayOutputStream(); byte[] buf = new byte[1024]; int len; while ((len = bufis.read(buf)) > 0) bos.write(buf, 0, len); byte[] result = bos.toByteArray(); bufis.close(); bos.close(); return result; } catch (IOException e) { throw new IllegalStateException("Unexpected IOException on inmemory gunzip", e); } }
From source file:Main.java
public static void downLoadFile(final String strUrl, final String fileURL, final int bufferLength) { BufferedInputStream in = null; BufferedOutputStream out = null; try {/*from ww w. ja v a2 s .c o m*/ in = new BufferedInputStream(new URL(strUrl).openStream()); File img = new File(fileURL); out = new BufferedOutputStream(new FileOutputStream(img)); byte[] buf = new byte[bufferLength]; int count = in.read(buf); while (count != -1) { out.write(buf, 0, count); count = in.read(buf); } in.close(); out.close(); } catch (Exception e) { e.printStackTrace(); } }
From source file:Main.java
public static void copyFile(File sourceFile, File targetFile) throws IOException { BufferedInputStream inBuff = null; BufferedOutputStream outBuff = null; try {/*from w w w. j a va2 s. c om*/ inBuff = new BufferedInputStream(new FileInputStream(sourceFile)); outBuff = new BufferedOutputStream(new FileOutputStream(targetFile)); byte[] buffer = new byte[BUFFER]; int length; while ((length = inBuff.read(buffer)) != -1) { outBuff.write(buffer, 0, length); } outBuff.flush(); } finally { if (inBuff != null) { inBuff.close(); } if (outBuff != null) { outBuff.close(); } } }
From source file:Main.java
public static String getBinaryHash(File apk, String algo) { FileInputStream fis = null;/*w w w . j a v a2 s .com*/ BufferedInputStream bis = null; try { MessageDigest md = MessageDigest.getInstance(algo); fis = new FileInputStream(apk); bis = new BufferedInputStream(fis); byte[] dataBytes = new byte[524288]; int nread = 0; while ((nread = bis.read(dataBytes)) != -1) md.update(dataBytes, 0, nread); byte[] mdbytes = md.digest(); return toHexString(mdbytes); } catch (IOException e) { Log.e("FDroid", "Error reading \"" + apk.getAbsolutePath() + "\" to compute " + algo + " hash."); return null; } catch (NoSuchAlgorithmException e) { Log.e("FDroid", "Device does not support " + algo + " MessageDisgest algorithm"); return null; } finally { closeQuietly(fis); } }