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 String objectToString(Serializable object) {
    ByteArrayOutputStream out = new ByteArrayOutputStream();
    try {//from w w  w .ja  v  a2 s .c  o  m
        new ObjectOutputStream(out).writeObject(object);
        byte[] data = out.toByteArray();
        out.close();

        out = new ByteArrayOutputStream();
        Base64OutputStream b64 = new Base64OutputStream(out, Base64.DEFAULT);
        b64.write(data);
        b64.close();
        out.close();

        return new String(out.toByteArray());
    } catch (IOException e) {
        e.printStackTrace();
    }
    return null;
}

From source file:Main.java

/**
 * Returns a byte array from the given object.
 * /*from  w  w  w . j a v  a  2 s .  c o  m*/
 * @param object
 *            to convert
 * @return byte array from the object
        
 */
public static byte[] objectToBytes(Object object) throws IOException {
    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    ObjectOutputStream os = new ObjectOutputStream(baos);
    os.writeObject(object);
    return baos.toByteArray();
}

From source file:Main.java

@Deprecated
public static void writeBitmapAttribute(XmlSerializer out, String name, Bitmap value) throws IOException {
    if (value != null) {
        final ByteArrayOutputStream os = new ByteArrayOutputStream();
        value.compress(CompressFormat.PNG, 90, os);
        writeByteArrayAttribute(out, name, os.toByteArray());
    }//  w  w w . j a va2s.co m
}

From source file:Main.java

/**
 * Write an XML document out to a byte array in UTF-8
 * @param document - the document to write.
 * @param indent - the indent level./*ww w .ja  v a  2 s .c o  m*/
 * @return The XML as a byte array
 */
public static byte[] writeXmlDocumentToByteArray(Document document, int indent) {
    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    writeXmlDocumentToStream(document, baos, indent);
    return baos.toByteArray();
}

From source file:Main.java

private static byte[] readFully(InputStream input) throws IOException {
    byte[] buffer = new byte[8192];
    int bytesRead;
    ByteArrayOutputStream output = new ByteArrayOutputStream();
    while ((bytesRead = input.read(buffer)) != -1) {
        output.write(buffer, 0, bytesRead);
    }/*from ww w.j a va2  s.c o  m*/
    return output.toByteArray();
}

From source file:Main.java

/**
 * Converts a bitmap into an inputstream
 * @param image the bitmap/*www.ja  va2s.c  o m*/
 * @return the inputstream
 */
public static InputStream convertBitmap(Bitmap image) {
    ByteArrayOutputStream bos = new ByteArrayOutputStream();
    image.compress(CompressFormat.PNG, 0 /* ignored for PNG */, bos);
    byte[] bitmapdata = bos.toByteArray();
    return new ByteArrayInputStream(bitmapdata);
}

From source file:Main.java

public static Bitmap compressBmpFromBmp(Bitmap image) {
    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    int options = 100;
    image.compress(Bitmap.CompressFormat.JPEG, 100, baos);
    while (baos.toByteArray().length / 1024 > 100) {
        baos.reset();//ww  w. ja v a  2 s.  c o  m
        options -= 10;
        image.compress(Bitmap.CompressFormat.JPEG, options, baos);
    }
    ByteArrayInputStream isBm = new ByteArrayInputStream(baos.toByteArray());
    Bitmap bitmap = BitmapFactory.decodeStream(isBm, null, null);
    return bitmap;
}

From source file:azkaban.utils.GZIPUtils.java

public static byte[] unGzipBytes(byte[] bytes) throws IOException {
    ByteArrayInputStream byteInputStream = new ByteArrayInputStream(bytes);
    GZIPInputStream gzipInputStream = new GZIPInputStream(byteInputStream);

    ByteArrayOutputStream byteOutputStream = new ByteArrayOutputStream();
    IOUtils.copy(gzipInputStream, byteOutputStream);

    return byteOutputStream.toByteArray();
}

From source file:Main.java

public static Bitmap compressImage(Bitmap image) {
    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    image.compress(Bitmap.CompressFormat.JPEG, 100, baos);
    int options = 100;
    while (baos.toByteArray().length / 1024 > 100) {
        options -= 10;/*from w  w  w .  j a  va  2  s .c o  m*/
        if (options > 0) {
            baos.reset();
            image.compress(Bitmap.CompressFormat.JPEG, options, baos);
        }
    }
    ByteArrayInputStream isBm = new ByteArrayInputStream(baos.toByteArray());
    return BitmapFactory.decodeStream(isBm, null, null);
}

From source file:Main.java

public static byte[] toByteArray(final Bitmap bmp, boolean recycle) {
    byte[] result = null;
    if (bmp != null) {
        ByteArrayOutputStream output = new ByteArrayOutputStream();
        bmp.compress(Bitmap.CompressFormat.JPEG, 85, output);
        result = output.toByteArray();
        if (recycle) {
            bmp.recycle();// w w  w.  j av  a 2 s  . c o m
        }
    }
    return result;
}