List of usage examples for java.io ByteArrayOutputStream toByteArray
public synchronized byte[] toByteArray()
From source file:com.github.khandroid.http.misc.FileDownloader.java
public static byte[] download(HttpClient httpClient, URI source) throws ClientProtocolException, IOException { byte[] ret;//from w ww. j a v a 2s . c om KhandroidLog.v("Downloading " + source.toString()); HttpGet req = new HttpGet(source); HttpResponse response = httpClient.execute(req); StatusLine statusLine = response.getStatusLine(); int statusCode = statusLine.getStatusCode(); KhandroidLog.v("Status code:" + statusCode); if (statusCode == 200) { HttpEntity entity = response.getEntity(); ByteArrayOutputStream output = new ByteArrayOutputStream(); entity.writeTo(output); output.close(); ret = output.toByteArray(); } else { throw new IOException( "Download failed, HTTP response code " + statusCode + " - " + statusLine.getReasonPhrase()); } req.releaseConnection(); return ret; }
From source file:Main.java
public static byte[] bmpToByteArray(final Bitmap bmp, final boolean needRecycle) { ByteArrayOutputStream output = new ByteArrayOutputStream(); bmp.compress(CompressFormat.PNG, 100, output); if (needRecycle) { bmp.recycle();/*w ww . ja va 2 s .com*/ } byte[] result = output.toByteArray(); try { output.close(); } catch (Exception e) { e.printStackTrace(); } return result; }
From source file:com.mebigfatguy.polycasso.URLFetcher.java
/** * retrieve arbitrary data found at a specific url * - either http or file urls/* ww w .jav a 2 s . c om*/ * for http requests, sets the user-agent to mozilla to avoid * sites being cranky about a java sniffer * * @param url the url to retrieve * @param proxyHost the host to use for the proxy * @param proxyPort the port to use for the proxy * @return a byte array of the content * * @throws IOException the site fails to respond */ public static byte[] fetchURLData(String url, String proxyHost, int proxyPort) throws IOException { HttpURLConnection con = null; InputStream is = null; try { URL u = new URL(url); if (url.startsWith("file://")) { is = new BufferedInputStream(u.openStream()); } else { Proxy proxy; if (proxyHost != null) { proxy = new Proxy(Type.HTTP, new InetSocketAddress(proxyHost, proxyPort)); } else { proxy = Proxy.NO_PROXY; } con = (HttpURLConnection) u.openConnection(proxy); con.addRequestProperty("User-Agent", "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/536.6 (KHTML, like Gecko) Chrome/20.0.1092.0 Safari/536.6"); con.addRequestProperty("Accept-Charset", "UTF-8"); con.addRequestProperty("Accept-Language", "en-US,en"); con.addRequestProperty("Accept", "text/html,image/*"); con.setDoInput(true); con.setDoOutput(false); con.connect(); is = new BufferedInputStream(con.getInputStream()); } ByteArrayOutputStream baos = new ByteArrayOutputStream(); IOUtils.copy(is, baos); return baos.toByteArray(); } finally { IOUtils.closeQuietly(is); if (con != null) { con.disconnect(); } } }
From source file:Main.java
public static byte[] compressToBytes(Bitmap bitmap, int quality) { ByteArrayOutputStream baos = null; try {/* w ww. j a va 2 s. co m*/ baos = new ByteArrayOutputStream(65536); bitmap.compress(CompressFormat.PNG, quality, baos); return baos.toByteArray(); } finally { try { baos.flush(); baos.close(); } catch (IOException e) { e.printStackTrace(); } } }
From source file:Main.java
public static byte[] bmpToByteArray(final Bitmap bmp, final boolean needRecycle) { ByteArrayOutputStream output = new ByteArrayOutputStream(); bmp.compress(Bitmap.CompressFormat.JPEG, 80, output); if (needRecycle) { bmp.recycle();/* w w w . j a v a 2 s . com*/ } byte[] result = output.toByteArray(); try { output.close(); } catch (Exception e) { e.printStackTrace(); } return result; }
From source file:Main.java
public static byte[] getFileByte(InputStream is) { try {/*from w w w.j av a2 s .c o m*/ byte[] buffer = new byte[1024]; int len = -1; ByteArrayOutputStream outStream = new ByteArrayOutputStream(); while ((len = is.read(buffer)) != -1) { outStream.write(buffer, 0, len); } byte[] data = outStream.toByteArray(); outStream.close(); is.close(); return data; } catch (IOException e) { e.printStackTrace(); } return null; }
From source file:Main.java
/** * Bitmap -> byte/*from ww w. j av a2 s. co m*/ * * @param bitmap * @return */ public static byte[] bitmapToByte(Bitmap bitmap) { try { ByteArrayOutputStream out = new ByteArrayOutputStream(); bitmap.compress(Bitmap.CompressFormat.PNG, 100, out); bitmap.recycle(); bitmap = null; byte[] array = out.toByteArray(); return array; } catch (Exception e) { e.printStackTrace(); } return null; }
From source file:Main.java
public static byte[] bitmapToBytes(Bitmap bitmap, Bitmap.CompressFormat format) { ByteArrayOutputStream stream = new ByteArrayOutputStream(); if (format == null) { format = Bitmap.CompressFormat.PNG; }//from ww w . j a v a 2s . c o m bitmap.compress(format, 100, stream); byte[] byteArray = stream.toByteArray(); return byteArray; }
From source file:com.github.harmanpa.jrecon.utils.Compression.java
public static byte[] compress(byte[] data) throws IOException { ByteArrayOutputStream baos = new ByteArrayOutputStream(128); OutputStream os = new BZip2CompressorOutputStream(baos); os.write(data);//from w w w. ja v a 2 s . c o m os.close(); return baos.toByteArray(); }
From source file:com.lewie9021.videothumbnail.VideoThumbnail.java
public static String encodeTobase64(Bitmap image) { Bitmap bmImage = image;//from w w w. j a v a 2 s . c om ByteArrayOutputStream byteArrayData = new ByteArrayOutputStream(); bmImage.compress(Bitmap.CompressFormat.JPEG, 100, byteArrayData); byte[] byteData = byteArrayData.toByteArray(); String encodedImage = Base64.encodeToString(byteData, Base64.DEFAULT); return encodedImage; }