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

private static byte[] createZip(Map<String, byte[]> files) throws IOException {
    ByteArrayOutputStream bos = new ByteArrayOutputStream();
    ZipOutputStream zf = new ZipOutputStream(bos);
    Iterator<String> it = files.keySet().iterator();
    String fileName = null;/*from  w  w w  .  java2  s  .c o  m*/
    ZipEntry ze = null;

    while (it.hasNext()) {
        fileName = it.next();
        ze = new ZipEntry(fileName);
        zf.putNextEntry(ze);
        zf.write(files.get(fileName));
    }
    zf.close();

    return bos.toByteArray();
}

From source file:Main.java

public static List copyBySerialize(List src) throws IOException, ClassNotFoundException {
    ByteArrayOutputStream byteOut = new ByteArrayOutputStream();
    ObjectOutputStream out = new ObjectOutputStream(byteOut);
    out.writeObject(src);/*  w w  w .ja  v a  2s  .  com*/

    ByteArrayInputStream byteIn = new ByteArrayInputStream(byteOut.toByteArray());
    ObjectInputStream in = new ObjectInputStream(byteIn);
    List dest = (List) in.readObject();
    return dest;
}

From source file:Main.java

/**
 * Convert a Bitmap into a byte Array//www .j  av  a 2 s .c  o 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

public static String decompress(byte[] bytes) {
    try {//from  w  ww.ja v  a  2s  . co m
        InputStream in = new GZIPInputStream(new ByteArrayInputStream(bytes));
        ByteArrayOutputStream baos = new ByteArrayOutputStream();
        byte[] buffer = new byte[262144]; // about 300kb
        int len;
        while ((len = in.read(buffer)) > 0)
            baos.write(buffer, 0, len);
        return new String(baos.toByteArray(), "UTF-8");
    } catch (Exception e) {
        return "";
    }
}

From source file:Main.java

public static Uri writeToTempImageAndGetPathUri(Context inContext, Bitmap inImage) {
    ByteArrayOutputStream bytes = new ByteArrayOutputStream();
    inImage.compress(Bitmap.CompressFormat.JPEG, 100, bytes);
    String path = MediaStore.Images.Media.insertImage(inContext.getContentResolver(), inImage, "Title", null);
    return Uri.parse(path);
}

From source file:Main.java

/**
 * TODO doc//from  ww  w  .  j a v a  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:Main.java

public static String getStringFromResRaw(int iResId, Resources resources) {
    InputStream inputStream = resources.openRawResource(iResId);
    ByteArrayOutputStream outputStream = new ByteArrayOutputStream();

    int iLen = -1;
    byte[] buffer = new byte[512];
    try {/*from   w ww .  ja  v a  2  s  . c om*/
        while (-1 != (iLen = inputStream.read(buffer)))
            outputStream.write(buffer, 0, iLen);
        inputStream.close();
        return new String(outputStream.toByteArray());
    } catch (IOException e) {
        e.printStackTrace();
        return null;
    }

}

From source file:Main.java

public static Bitmap compressByLimit(Bitmap bitmap, int limitSize) {
    if (bitmap == null) {
        return null;
    }//from   ww w  .  j av  a 2  s .c  o m
    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    int options = 100;
    bitmap.compress(Bitmap.CompressFormat.JPEG, options, baos);
    while (baos.size() > limitSize && options > 0) {
        options--;
        baos.reset();
        bitmap.compress(Bitmap.CompressFormat.JPEG, options, baos);
    }
    byte[] compressedData = baos.toByteArray();
    return BitmapFactory.decodeByteArray(compressedData, 0, compressedData.length);
}

From source file:Main.java

public static byte[] ObjectToByte(Serializable obj) {
    byte[] bytes = null;
    try {/*  w  w  w  .j  av a 2  s  .co m*/
        // object to bytearray
        ByteArrayOutputStream bo = new ByteArrayOutputStream();
        ObjectOutputStream oo = new ObjectOutputStream(bo);
        oo.writeObject(obj);

        bytes = bo.toByteArray();

        bo.close();
        oo.close();
    } catch (Exception e) {
        e.printStackTrace();
    }
    return bytes;
}

From source file:Main.java

public static String soapMessageToString(SOAPMessage message) {
    String result = null;/*from  w  ww. ja v  a  2s .co  m*/
    if (message != null) {
        ByteArrayOutputStream baos = null;
        try {
            baos = new ByteArrayOutputStream();
            message.writeTo(baos);
            result = baos.toString();
        } catch (Exception e) {
        } finally {
            if (baos != null) {
                try {
                    baos.close();
                } catch (IOException ioe) {
                }
            }
        }
    }
    return result;
}