Example usage for java.io ByteArrayOutputStream ByteArrayOutputStream

List of usage examples for java.io ByteArrayOutputStream ByteArrayOutputStream

Introduction

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

Prototype

public ByteArrayOutputStream() 

Source Link

Document

Creates a new ByteArrayOutputStream .

Usage

From source file:Main.java

public static byte[] bitampToByteArray(Bitmap bitmap) {
    byte[] array = null;
    try {/*w w  w.  ja  v  a2 s  .co  m*/
        if (null != bitmap) {
            ByteArrayOutputStream os = new ByteArrayOutputStream();
            bitmap.compress(Bitmap.CompressFormat.PNG, 100, os);
            array = os.toByteArray();
            os.close();
        }
    } catch (IOException e) {
        e.printStackTrace();
    }

    return array;
}

From source file:Main.java

public static byte[] Bitmap2Bytes(Bitmap bm, Bitmap.CompressFormat format) {
    if (bm == null) {
        return null;
    }//from www  .j av a 2s  . c  om
    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    bm.compress(format, 100, baos);
    return baos.toByteArray();
}

From source file:Main.java

/**
 * Serialize an object into bytes.// w w  w .j a  v  a2  s  . c  o  m
 * @param o Object to be serialized.
 * @return Serialized stream of bytes.
 * @throws IOException
 */
public static byte[] objectToBytes(Serializable o) throws IOException {
    ByteArrayOutputStream out = new ByteArrayOutputStream();
    ObjectOutputStream oo = new ObjectOutputStream(out);
    oo.writeObject(o);
    oo.close();
    return out.toByteArray();
}

From source file:Main.java

public static byte[] bitmapToBytes(Bitmap source) {
    if (source == null) {
        return null;
    } else {//from w w w  .  j a v  a  2  s . co  m
        ByteArrayOutputStream returnByte = new ByteArrayOutputStream();
        source.compress(Bitmap.CompressFormat.JPEG, DEFAULT_QUALITY, returnByte);
        return returnByte.toByteArray();
    }
}

From source file:Main.java

public static String compress(String str) throws IOException {
    if (str == null || str.length() == 0) {
        return str;
    }/*  w ww .j  a  v  a 2 s.c  om*/
    ByteArrayOutputStream out = new ByteArrayOutputStream();
    GZIPOutputStream gzip = new GZIPOutputStream(out);
    gzip.write(str.getBytes());
    gzip.close();
    return out.toString("ISO-8859-1");
}

From source file:Main.java

public static byte[] convertToBytes(Bitmap bitmap, CompressFormat format, int quality) {
    ByteArrayOutputStream stream = new ByteArrayOutputStream();
    bitmap.compress(format, quality, stream);
    byte[] data = stream.toByteArray();
    try {//w  w w . ja v a 2s. co m
        stream.close();
    } catch (Exception e) {
        // nothing
    }
    return data;
}

From source file:Main.java

public static byte[] serialize(Object o) {
    ByteArrayOutputStream bos = new ByteArrayOutputStream();
    ObjectOutput out = null;//from  w w w  .j av a  2s  . c  o  m
    byte[] bytes = null;
    try {
        out = new ObjectOutputStream(bos);
        out.writeObject(o);
        bytes = bos.toByteArray();
    } catch (IOException e) {
        e.printStackTrace();
    } finally {
        try {
            if (out != null) {
                out.close();
            }
        } catch (IOException ex) {
            // ignore close exception
        }
        try {
            bos.close();
        } catch (IOException ex) {
            // ignore close exception
        }
    }
    return bytes;
}

From source file:Main.java

/**
 * Converts bitmap to byte array//from w w w .  ja  va 2s  .c o m
 *
 * @param bitmap
 * @return
 */
public static byte[] getBytes(Bitmap bitmap) {
    ByteArrayOutputStream stream = new ByteArrayOutputStream();
    bitmap.compress(Bitmap.CompressFormat.PNG, 0, stream);
    return stream.toByteArray();
}

From source file:SerializationUtils.java

public static byte[] serialize(Object obj) {
    try {//from   w  w  w  .  j av a 2  s  . com
        ByteArrayOutputStream buffer = new ByteArrayOutputStream();
        ObjectOutputStream oos = new ObjectOutputStream(buffer);
        oos.writeObject(obj);
        oos.close();
        return buffer.toByteArray();
    } catch (IOException e) {
        e.printStackTrace();
        throw new RuntimeException("error writing to byte-array!");
    }
}

From source file:Main.java

public static byte[] readInputStream(InputStream is) throws IOException {
    byte[] buffer = new byte[1024];
    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    int len;/*from   w w  w .  j  a  v a 2 s . c  om*/
    while ((len = is.read(buffer)) != -1) {
        baos.write(buffer, 0, len);
    }
    baos.flush();
    baos.close();
    is.close();
    return baos.toByteArray();
}