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 toCompressedJpeg(Bitmap bitmap) {
    ByteArrayOutputStream thumbnailBytes = new ByteArrayOutputStream();
    bitmap.compress(CompressFormat.JPEG, 85, thumbnailBytes);
    return new ByteArrayInputStream(thumbnailBytes.toByteArray());
}

From source file:Main.java

/**
 * TODO doc/*w  ww  .j  a  va 2  s  .c  o m*/
 *
 * @param photoBitmap
 * @return
 */
public static byte[] toByteArray(Bitmap photoBitmap) throws IOException {
    ByteArrayOutputStream blob = new ByteArrayOutputStream();
    photoBitmap.compress(Bitmap.CompressFormat.PNG, 0 /*ignored for PNG*/, blob);
    byte[] photoByteArray = blob.toByteArray();
    blob.close();
    return photoByteArray;
}

From source file:ch.ledcom.jpreseed.InitrdRepackerTest.java

private static CpioArchiveInputStream gzippedCpio(ByteArrayOutputStream stream) throws IOException {
    return new CpioArchiveInputStream(new GZIPInputStream(new ByteArrayInputStream(stream.toByteArray())));
}

From source file:Main.java

public static byte[] toBytes(Object obj) throws Exception {
    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    ObjectOutputStream oos = new ObjectOutputStream(baos);
    oos.writeObject(obj);//www . ja  v  a 2  s .co m
    byte[] arr = baos.toByteArray();
    return arr;
}

From source file:net.bpelunit.util.FileUtil.java

public static byte[] readFile(File f) throws IOException {
    FileInputStream bprInputStream = null;
    try {//from   w w w  .  j  av  a 2 s .c  o  m
        bprInputStream = new FileInputStream(f);
        ByteArrayOutputStream bytesOut = new ByteArrayOutputStream((int) f.length());
        IOUtils.copy(bprInputStream, bytesOut);

        return bytesOut.toByteArray();
    } finally {
        IOUtils.closeQuietly(bprInputStream);
    }
}

From source file:Main.java

public static byte[] compressBmpToBytes(Bitmap bitmap, int maxSize) {
    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    int options = 100;
    bitmap.compress(Bitmap.CompressFormat.JPEG, options, baos);
    while (baos.toByteArray().length / 1024 >= maxSize) {
        baos.reset();//from  w w w  .  ja v a2s.c om
        options -= 5;
        bitmap.compress(Bitmap.CompressFormat.JPEG, options, baos);
    }

    return baos.toByteArray();
}

From source file:Main.java

public static byte[] toByte(InputStream input) throws IOException {
    byte[] buf = new byte[1024];
    int len = -1;
    ByteArrayOutputStream output = new ByteArrayOutputStream();
    while ((len = input.read(buf)) != -1) {
        output.write(buf, 0, len);/*from ww  w.  ja  v a 2 s.  co  m*/
    }
    byte[] data = output.toByteArray();
    output.close();
    input.close();
    return data;
}

From source file:Main.java

public static String bitmapChangeString(Bitmap bitmap) {
    if (bitmap != null) {
        ByteArrayOutputStream baos = new ByteArrayOutputStream();
        bitmap.compress(Bitmap.CompressFormat.PNG, 100, baos);
        String str = new String(Base64.encodeToString(baos.toByteArray(), Base64.DEFAULT));
        return str;
    }//from   w w w. ja v a 2 s .c  om
    return null;
}

From source file:Main.java

public static String imgsToBase64(Bitmap bitmap) {
    ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
    bitmap.compress(Bitmap.CompressFormat.JPEG, 100, outputStream);

    return Base64.encodeToString(outputStream.toByteArray(), Base64.DEFAULT);
}

From source file:Main.java

/**
 * Encodes an ImageView into a base-64 byte array representation. Encoding compression depends
 * on the quality parameter passed. 0 is the most compressed (lowest quality) and 100 is the
 * least compressed (highest quality).//  ww w  . ja  v a2s . c  om
 *
 * @param context
 * @param res     the resource id of an ImageView
 * @param quality The amount of compression, where 0 is the lowest quality and 100 is the
 *                highest
 * @return the raw base-64 image data compressed accordingly
 */
public static byte[] encodeBase64Image(Context context, int res, int quality) {
    // ensure quality is between 0 and 100 (inclusive)
    quality = Math.max(LOWEST_QUALITY, Math.min(HIGHEST_QUALITY, quality));

    Bitmap bitmap = BitmapFactory.decodeResource(context.getResources(), res);
    ByteArrayOutputStream stream = new ByteArrayOutputStream();
    bitmap.compress(Bitmap.CompressFormat.PNG, quality, stream);
    return stream.toByteArray();
}