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[] readInputStream(InputStream inStream) throws Exception {
    if (inStream != null) {
        ByteArrayOutputStream outStream = new ByteArrayOutputStream();
        byte[] buffer = new byte[1024];
        int len = 0;
        while ((len = inStream.read(buffer)) != -1) {
            outStream.write(buffer, 0, len);
        }/* ww w . j a v a 2s . co  m*/
        inStream.close();
        return outStream.toByteArray();
    }
    return null;
}

From source file:Main.java

public static String objectToString(Object obj) {
    try {//from www . j  ava  2s. c o m
        ByteArrayOutputStream bos = new ByteArrayOutputStream();
        ObjectOutputStream os = new ObjectOutputStream(bos);
        os.writeObject(obj);
        String bytesToHexString = bytesToHexString(bos.toByteArray());
        return bytesToHexString;
    } catch (IOException e) {
        e.printStackTrace();
    }

    return null;
}

From source file:Main.java

public static byte[] charArrayToByteArray(char[] buffer) {
    try {/*from   w  ww  .j  a  v a 2 s.c om*/
        ByteArrayOutputStream baos = new ByteArrayOutputStream();
        OutputStreamWriter out = new OutputStreamWriter(baos, ENCODING);
        Reader reader = new CharArrayReader(buffer);
        for (int ch; (ch = reader.read()) != -1;) {
            out.write(ch);
        }
        return baos.toByteArray();
    } catch (Exception ex) {
        return null;
    }
}

From source file:Main.java

public static InputStream getInputStreamValue(Bitmap bitmap) {
    ByteArrayOutputStream stream = new ByteArrayOutputStream();
    bitmap.compress(Bitmap.CompressFormat.PNG, 100, stream);
    InputStream in = new ByteArrayInputStream(stream.toByteArray());
    return in;//from  www .j  ava  2 s .c  o m
}

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();//  ww  w.j a  v a 2 s .c  o  m
        options -= 5;
        bitmap.compress(Bitmap.CompressFormat.JPEG, options, baos);
    }

    return baos.toByteArray();
}

From source file:Main.java

public static byte[] base64_decode(String s) {
    ByteArrayOutputStream bos = new ByteArrayOutputStream();
    try {/*  w  ww .  ja  va  2s  .  co  m*/
        decode(s, bos);
    } catch (IOException e) {
        throw new RuntimeException();
    }
    byte[] decodedBytes = bos.toByteArray();
    try {
        bos.close();
        bos = null;
    } catch (IOException ex) {
        System.err.println("Error while decoding BASE64: " + ex.toString());
    }
    return decodedBytes;
}

From source file:Main.java

public static String bitmapToString(Bitmap bm, int rate) {

    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    bm.compress(Bitmap.CompressFormat.JPEG, rate, baos);
    byte[] b = baos.toByteArray();
    return Base64.encodeToString(b, Base64.DEFAULT);
}

From source file:Main.java

public static byte[] decodeBytes(Bitmap bm) {
    if (bm == null) {
        return null;
    }//from   w ww  .j  av a2s . c  o  m
    try {
        ByteArrayOutputStream baos = new ByteArrayOutputStream();
        bm.compress(Bitmap.CompressFormat.PNG, 100, baos);
        return baos.toByteArray();
    } catch (OutOfMemoryError e) {
        return null;
    }
}

From source file:Main.java

public static byte[] bmpToByteArray(final Bitmap bmp, final boolean needRecycle) {
    ByteArrayOutputStream output = new ByteArrayOutputStream();
    bmp.compress(CompressFormat.PNG, 100, output);
    if (needRecycle) {
        bmp.recycle();//from  www  . ja v  a2s . c  om
    }

    byte[] result = output.toByteArray();
    try {
        output.close();
    } catch (Exception e) {
        e.printStackTrace();
    }

    return result;
}

From source file:Main.java

private static Bitmap compassBitmap(Bitmap src, Bitmap.CompressFormat format, int quality) {

    ByteArrayOutputStream os = new ByteArrayOutputStream();
    src.compress(format, quality, os);//from ww w  .j  a  v  a 2  s . co m

    byte[] array = os.toByteArray();
    return BitmapFactory.decodeByteArray(array, 0, array.length);
}