List of usage examples for org.apache.http.util ByteArrayBuffer ByteArrayBuffer
public ByteArrayBuffer(int i)
From source file:bala.padio.Settings.java
public static String DownloadFromUrl(String u) { try {//from w w w .j ava 2 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:nu.nethome.home.items.web.proxy.HomeCloudConnection.java
private HttpResponse performLocalRequest(HttpRequest request) throws IOException { HttpResponse httpResponse;/*from w w w . j a v a 2 s. c o m*/ HttpURLConnection connection = (HttpURLConnection) new URL(localURL + request.url).openConnection(); for (String header : request.headers) { String parts[] = header.split(":"); connection.setRequestProperty(parts[0].trim(), parts[1].trim()); } ByteArrayBuffer baf = new ByteArrayBuffer(50); try (InputStream response = connection.getInputStream()) { BufferedInputStream bis = new BufferedInputStream(response); int read; int bufSize = 512; byte[] buffer = new byte[bufSize]; while (true) { read = bis.read(buffer); if (read == -1) { break; } baf.append(buffer, 0, read); } } catch (IOException e) { return new HttpResponse(systemId, "", new String[0], CHALLENGE); } Map<String, List<String>> map = connection.getHeaderFields(); String headers[] = new String[map.size()]; int i = 0; for (Map.Entry<String, List<String>> entry : map.entrySet()) { System.out.println("Key : " + entry.getKey() + " ,Value : " + entry.getValue()); headers[i++] = entry.getKey() + ":" + entry.getValue().get(0); } httpResponse = new HttpResponse(systemId, new String(Base64.encodeBase64(baf.toByteArray())), headers, CHALLENGE); return httpResponse; }
From source file:com.emobc.android.activities.generators.PdfActivityGenerator.java
/** * Download a pdf file to web.//from w w w. j av a 2s . com * @param file * @param urlString * @return */ protected boolean downloadPdfToFile(File file, String urlString) { try { URL url = new URL(urlString); long startTime = System.currentTimeMillis(); Log.d("ImageManager", "download begining"); Log.d("ImageManager", "download url:" + url); Log.d("ImageManager", "downloaded file name:" + file.getAbsolutePath()); // 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(); Log.d("ImageManager", "download ready in" + ((System.currentTimeMillis() - startTime) / 1000) + " sec"); return true; } catch (IOException e) { Log.d("ImageManager", "Error: " + e); } return false; }
From source file:com.development.androrb.listfolders.java
private String loaddata(String Urli) { try {/* ww w. j a v a 2 s.c om*/ URLConnection conn; conn = new URL(Urli).openConnection(); InputStream is = conn.getInputStream(); BufferedInputStream bis = new BufferedInputStream(is); ByteArrayBuffer baf = new ByteArrayBuffer(8192); // loading part int current = 0; while ((current = bis.read()) != -1) { baf.append((byte) current); } //Log.i(TAG, " *------ after while -----*: "+current); html = EncodingUtils.getString(baf.toByteArray(), "UTF-8"); } catch (Exception e) { TextUpdate = "Ups, check your Connection ..."; runOnUiThread(showTextUpdate); //Toast.makeText(this, "Shit, Loading Error!", Toast.LENGTH_SHORT).show(); } return html; }
From source file:riddimon.android.asianetautologin.HttpUtils.java
public static String readStreamIntoString(InputStream in) { StringBuilder builder = new StringBuilder(); int buf_size = 50 * 1024; // read in chunks of 50 KB ByteArrayBuffer bab = new ByteArrayBuffer(buf_size); int read = 0; BufferedInputStream bis = new BufferedInputStream(in); if (bis != null) { byte buffer[] = new byte[buf_size]; try {// w w w . j a v a2 s . c om while ((read = bis.read(buffer, 0, buf_size)) != -1) { //builder.append(new String(buffer, "utf-8")); bab.append(buffer, 0, read); } builder.append(new String(bab.toByteArray(), "UTF-8")); } catch (UnsupportedEncodingException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } finally { try { bis.close(); } catch (IOException e) { e.printStackTrace(); } } } return builder.toString(); }
From source file:com.chuannuo.tangguo.net.TGHttpResponseHandler.java
byte[] getResponseData(HttpEntity entity) throws IOException { byte[] responseBody = null; if (entity != null) { InputStream instream = entity.getContent(); if (instream != null) { long contentLength = entity.getContentLength(); if (contentLength > Integer.MAX_VALUE) { throw new IllegalArgumentException("HTTP entity too large to be buffered in memory"); }/*from w ww . j a va 2 s. co m*/ int buffersize = (contentLength <= 0) ? BUFFER_SIZE : (int) contentLength; try { ByteArrayBuffer buffer = new ByteArrayBuffer(buffersize); try { byte[] tmp = new byte[BUFFER_SIZE]; long count = 0; int l; // do not send messages if request has been cancelled while ((l = instream.read(tmp)) != -1 && !Thread.currentThread().isInterrupted()) { count += l; buffer.append(tmp, 0, l); sendProgressMessage(count, (contentLength <= 0 ? 1 : contentLength)); } } finally { TGHttpClient.silentCloseInputStream(instream); TGHttpClient.endEntityViaReflection(entity); } responseBody = buffer.toByteArray(); } catch (OutOfMemoryError e) { System.gc(); throw new IOException("File too large to fit into available memory"); } } } return responseBody; }
From source file:com.oakesville.mythling.util.HttpHelper.java
private byte[] extractResponseBytes(InputStream is, long requestStartTime) throws IOException { BufferedInputStream bis = null; BufferedReader br = null;// w w w .j ava 2 s .co m try { ByteArrayBuffer baf = new ByteArrayBuffer(1024); bis = new BufferedInputStream(is); int b = 0; while ((b = bis.read()) != -1) baf.append((byte) b); long requestEndTime = System.currentTimeMillis(); byte[] bytes = baf.toByteArray(); if (BuildConfig.DEBUG) { Log.d(TAG, " -> (" + url + ") http request time: " + (requestEndTime - requestStartTime) + " ms"); // how much memory are we using Debug.MemoryInfo memoryInfo = new Debug.MemoryInfo(); Debug.getMemoryInfo(memoryInfo); Log.d(TAG, " -> response byte array size: " + bytes.length / 1024 + " kb (Pss = " + memoryInfo.getTotalPss() + " kb)"); } if (!binary) { // detect character set UniversalDetector detector = new UniversalDetector(null); detector.handleData(bytes, 0, bytes.length); detector.dataEnd(); String detected = detector.getDetectedCharset(); if (BuildConfig.DEBUG) Log.d(TAG, " -> charset: " + detected + " - detect time: " + (System.currentTimeMillis() - requestEndTime) + " ms"); if (detected != null && !detected.equals(charset)) { try { Charset.forName(detected); charset = detected; } catch (UnsupportedCharsetException ex) { // not supported -- stick with UTF-8 } } } return bytes; } finally { try { if (bis != null) bis.close(); if (br != null) br.close(); } catch (IOException ex) { Log.e(TAG, ex.getMessage(), ex); } } }
From source file:com.development.androrb.Orband.java
private String loaddata(String Urli) { try {//from ww w. ja v a 2s .com //Log.i(TAG, " *------ Load Data: "+Urli); URLConnection conn; conn = new URL(Urli).openConnection(); InputStream is = conn.getInputStream(); BufferedInputStream bis = new BufferedInputStream(is); ByteArrayBuffer baf = new ByteArrayBuffer(8192); // loading part int current = 0; int countix = 0; int kbread = 0; while ((current = bis.read()) != -1) { countix++; baf.append((byte) current); if (countix >= 1000) { countix = 0; kbread++; TextUpdate = loadingmediadata + " (" + kbread + " kb)"; runOnUiThread(showTextUpdate); } } // Log.i(TAG, " *------ Load Data done -----*: "); html = EncodingUtils.getString(baf.toByteArray(), "UTF-8"); // ------ } catch (Exception e) { TextUpdate = "Ups, check your Connection ..."; runOnUiThread(showTextUpdate); } return html; }
From source file:com.example.heya.couchdb.ConnectionHandler.java
/** * Get a File object from an URL//w w w.ja va 2s . c o m * * @param url * @param path * @return * @throws IOException * @throws ClientProtocolException * @throws SpikaException * @throws JSONException * @throws IllegalStateException * @throws SpikaForbiddenException */ public static void getFile(String url, File file, String userId, String token) throws ClientProtocolException, IOException, SpikaException, IllegalStateException, JSONException, SpikaForbiddenException { File mFile = file; //URL mUrl = new URL(url); // you can write here any link InputStream is = httpGetRequest(url, userId); BufferedInputStream bis = new BufferedInputStream(is); ByteArrayBuffer baf = new ByteArrayBuffer(20000); int current = 0; while ((current = bis.read()) != -1) { baf.append((byte) current); } /* Convert the Bytes read to a String. */ FileOutputStream fos = new FileOutputStream(mFile); fos.write(baf.toByteArray()); fos.flush(); fos.close(); is.close(); }