Example usage for java.io ByteArrayOutputStream toByteArray

List of usage examples for java.io ByteArrayOutputStream toByteArray

Introduction

In this page you can find the example usage for java.io ByteArrayOutputStream toByteArray.

Prototype

public synchronized byte[] toByteArray() 

Source Link

Document

Creates a newly allocated byte array.

Usage

From source file:Main.java

public static InputStream Bitmap2InputStream(Bitmap bm, int quality) {
    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    bm.compress(Bitmap.CompressFormat.PNG, quality, baos);
    InputStream is = new ByteArrayInputStream(baos.toByteArray());
    return is;/*from  w  ww  .  ja v a2s . c  o  m*/
}

From source file:Main.java

public static String readAsString(File source, String encoding) throws IOException {
    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    loadFromFile(source, baos);/*  w  w  w  .  j  a  v a 2s  .c o  m*/
    byte[] bytes = baos.toByteArray();
    return new String(bytes, encoding);
}

From source file:Main.java

public static byte[] bitmapToBytes(Bitmap source) {
    if (source == null) {
        return null;
    } else {/* w w  w  . ja v a2s. c om*/
        ByteArrayOutputStream returnByte = new ByteArrayOutputStream();
        source.compress(Bitmap.CompressFormat.JPEG, DEFAULT_QUALITY, returnByte);
        return returnByte.toByteArray();
    }
}

From source file:Main.java

/**
 * Reads all bytes from an input stream into a byte array.
 * Does not close the stream./*from   w w w  . ja  va  2s.co  m*/
 *
 * @param in the input stream to read from
 * @return a byte array containing all the bytes from the stream
 * @throws IOException if an I/O error occurs
 */
public static byte[] toByteArray(InputStream in) throws IOException {
    // Presize the ByteArrayOutputStream since we know how large it will need
    // to be, unless that value is less than the default ByteArrayOutputStream
    // size (32).
    ByteArrayOutputStream out = new ByteArrayOutputStream(Math.max(32, in.available()));
    copy(in, out);
    return out.toByteArray();
}

From source file:Main.java

/**
 * Convert a Bitmap into a byte Array//ww w . j av  a  2s. co  m
 * @param bitmap
 * @return
 */
public static byte[] convertBitmapToByte(Bitmap bitmap) {
    try {
        ByteArrayOutputStream stream = new ByteArrayOutputStream();
        bitmap.compress(Bitmap.CompressFormat.PNG, 100, stream);
        byte[] byteArray = stream.toByteArray();
        return byteArray;
    } catch (Exception e) {
        return null;
    }
}

From source file:Main.java

private static byte[] cprsBmpByQuality(WeakReference<Bitmap> bmp, int maxSize) {
    if (bmp == null || bmp.get() == null)
        return null;
    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    bmp.get().compress(Bitmap.CompressFormat.JPEG, 70, baos);
    bmp.get().recycle();//from  w w w .jav a  2  s  .  c o  m
    return baos.toByteArray();
}

From source file:Main.java

public static String encodeImageBase64(Bitmap bitmap) {
    ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
    bitmap.compress(Bitmap.CompressFormat.PNG, 100, outputStream);
    return Base64.encodeToString(outputStream.toByteArray(), Base64.DEFAULT);
}

From source file:Main.java

public static InputStream getInputStreamFromBitmap(Bitmap bitmap) {
    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    bitmap.compress(Bitmap.CompressFormat.PNG, 100, baos);
    InputStream isBm = new ByteArrayInputStream(baos.toByteArray());
    return isBm;/*  w  ww. ja v a 2  s  .  co  m*/
}

From source file:Main.java

public static byte[] readStream(InputStream is) throws Exception {
    byte[] bytes = new byte[1024];
    int leng;/*from w ww  .  j a  v  a 2  s.  c  o  m*/
    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    while ((leng = is.read(bytes)) != -1) {
        baos.write(bytes, 0, leng);
    }
    return baos.toByteArray();
}

From source file:Main.java

public static InputStream getInputStreamValue(Bitmap bitmap) {
    ByteArrayOutputStream stream = new ByteArrayOutputStream();
    bitmap.compress(Bitmap.CompressFormat.PNG, 100, stream);
    InputStream in = new ByteArrayInputStream(stream.toByteArray());
    return in;//  ww w  .j av  a 2 s.  com
}