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

/**
 * convert (encode) image to string//from   w  w  w . j  a va 2  s  .c o m
 * **/
public static String encodeToBase64(Bitmap image, Bitmap.CompressFormat compressFormat, int quality) {
    ByteArrayOutputStream byteArrayOS = new ByteArrayOutputStream();
    image.compress(compressFormat, quality, byteArrayOS);
    return Base64.encodeToString(byteArrayOS.toByteArray(), Base64.DEFAULT);
}

From source file:Main.java

public static byte[] serializeObject(Object o) {
    ByteArrayOutputStream bos = new ByteArrayOutputStream();

    try {//from  w ww. ja va2  s. c  o m
        ObjectOutput out = new ObjectOutputStream(bos);
        out.writeObject(o);
        out.close();

        byte[] buf = bos.toByteArray();

        return buf;
    } catch (IOException ioe) {
        Log.e("serializeObject", "error", ioe);

        return null;
    }
}

From source file:Main.java

public static String Base64Pic(String path) throws Exception {
    Bitmap bm = getSmallBitmap(path, 300, 300);
    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    bm.compress(Bitmap.CompressFormat.JPEG, 100, baos);
    byte[] b = baos.toByteArray();
    return Base64.encodeToString(b, Base64.DEFAULT);
}

From source file:Main.java

public static InputStream bitmap2Ins(Bitmap bm) {
    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    bm.compress(CompressFormat.JPEG, 100, baos);
    InputStream sbs = new ByteArrayInputStream(baos.toByteArray());
    return sbs;/*from   www  .ja v  a 2  s.co m*/
}

From source file:Main.java

public static InputStream bitmap2InputStream(Bitmap bm, Bitmap.CompressFormat format, int quality) {
    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    bm.compress(format, quality, baos);/*from w  w w . j  ava2 s. com*/
    InputStream is = new ByteArrayInputStream(baos.toByteArray());
    return is;
}

From source file:Main.java

public static String imgsToBase64(String filePath) {
    Bitmap bitmap = BitmapFactory.decodeFile(filePath);
    ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
    String lastName = filePath.substring(filePath.lastIndexOf(".") + 1);
    if (lastName.equals(Bitmap.CompressFormat.JPEG)) {
        bitmap.compress(Bitmap.CompressFormat.JPEG, 100, outputStream);
    } else {/*from  w  ww.  j a v a2 s .c o m*/
        bitmap.compress(Bitmap.CompressFormat.JPEG, 100, outputStream);
    }
    return Base64.encodeToString(outputStream.toByteArray(), Base64.DEFAULT);
}

From source file:Main.java

public static <T> String toXmlString(T obj, Class<T> type) {
    try {/*  w w w . j a v a  2s  .c  om*/
        JAXBContext jaxbContext = JAXBContext.newInstance(type);
        ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
        jaxbContext.createMarshaller().marshal(obj, byteArrayOutputStream);
        return byteArrayOutputStream.toString();
    } catch (JAXBException e) {
        throw new RuntimeException(e);
    }
}

From source file:Main.java

public static List<ByteArrayOutputStream> cutby(int value, byte[] content) {
    List<ByteArrayOutputStream> bytes = new ArrayList<ByteArrayOutputStream>();
    ByteArrayOutputStream out = new ByteArrayOutputStream();
    boolean preisSame = false; //
    if (content.length > 0) {
        for (int i = 0; i < content.length; i++) {
            if (content[i] != value) {
                out.write(content[i]);//from  w  w w . java 2 s . co  m
                preisSame = false;
            } else if (!preisSame) {
                if (out.size() > 0) {
                    bytes.add(out);
                    out = new ByteArrayOutputStream();
                }
                preisSame = true;
            }
        }
    }
    if (out.size() > 0) {
        bytes.add(out);
    }
    return bytes;
}

From source file:Main.java

private static ByteArrayOutputStream readFileToStream(String file) {
    FileInputStream fileInputStream = null;
    try {/*  w  ww.  j ava 2 s  .  c o m*/
        ByteArrayOutputStream bos = new ByteArrayOutputStream();
        fileInputStream = new FileInputStream(file);
        int readedBytes;
        byte[] buf = new byte[1024];
        while ((readedBytes = fileInputStream.read(buf)) > 0) {
            bos.write(buf, 0, readedBytes);
        }
        return bos;
    } catch (Exception e) {
        return null;
    } finally {
        try {
            if (fileInputStream != null)
                fileInputStream.close();
        } catch (Exception ignored) {

        }
    }
}

From source file:Main.java

/**
 * Bitmap -> byte// w  ww .j a v a 2s  .  c o m
 * 
 * @param bitmap
 * @return
 */
public static byte[] bitmapToByte(Bitmap bitmap) {
    try {
        ByteArrayOutputStream out = new ByteArrayOutputStream();
        bitmap.compress(Bitmap.CompressFormat.PNG, 100, out);
        bitmap.recycle();
        bitmap = null;
        byte[] array = out.toByteArray();
        return array;
    } catch (Exception e) {
        e.printStackTrace();
    }
    return null;
}