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 byte[] toByteArray(Bitmap bitmap) {
    ByteArrayOutputStream stream = new ByteArrayOutputStream();
    bitmap.compress(Bitmap.CompressFormat.PNG, 100, stream);
    return stream.toByteArray();
}

From source file:Main.java

private static byte[] createByteArrayFromBitmap(Bitmap bitmap) {
    Log.e(LOG_TAG, "creating asset");
    final ByteArrayOutputStream byteStream = new ByteArrayOutputStream();
    bitmap.compress(Bitmap.CompressFormat.PNG, 100, byteStream);
    Log.e(LOG_TAG, "length : " + Integer.toString(byteStream.toByteArray().length));
    return byteStream.toByteArray();
}

From source file:Main.java

public static byte[] xmlToBytes(final Node body) throws TransformerException {
    final Transformer transformer = FACTORY.newTransformer();
    final Source source = new DOMSource(body);
    final ByteArrayOutputStream buffer = new ByteArrayOutputStream();
    final Result result = new StreamResult(buffer);
    transformer.transform(source, result);
    return buffer.toByteArray();
}

From source file:net.felixrabe.unleashthecouch.Utils.java

public static String getDocument(String couchDbDocUrl) {
    HttpClient httpClient = new DefaultHttpClient();
    HttpGet httpGet = new HttpGet(couchDbDocUrl);
    HttpResponse response = null;// w w  w .j  a v  a2s  .c o  m
    try {
        response = httpClient.execute(httpGet);
        HttpEntity entity = response.getEntity();
        if (entity != null) {
            ByteArrayOutputStream baos = new ByteArrayOutputStream();
            entity.writeTo(baos);
            return new String(baos.toByteArray(), "UTF-8");
        }
    } catch (ClientProtocolException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    }

    return null;
}

From source file:Main.java

static public byte[] getJPEGRepresentationFromBitmap(Bitmap bitmap) {
    ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
    bitmap.compress(Bitmap.CompressFormat.JPEG, 80, outputStream);
    return outputStream.toByteArray();
}

From source file:Main.java

public static String getStringFromAssets(String strFileName, Resources resources) {
    String result = null;/* w ww. j ava 2s .  c  o  m*/
    try {
        InputStream in = resources.getAssets().open(strFileName);
        int ch = 0;
        ByteArrayOutputStream baos = new ByteArrayOutputStream();
        while ((ch = in.read()) != -1) {
            baos.write(ch);
        }
        byte[] buff = baos.toByteArray();
        baos.close();
        in.close();
        result = new String(buff);
    } catch (Exception e) {
        e.printStackTrace();
    }
    return result;
}

From source file:Main.java

public static byte[] convertIntToByteArray(int my_int) throws IOException {
    ByteArrayOutputStream bos = new ByteArrayOutputStream();
    DataOutputStream dos = new DataOutputStream(bos);
    dos.writeInt(my_int);
    dos.close();/* www  . j  ava2 s.  c  o m*/
    byte[] int_bytes = bos.toByteArray();
    bos.close();
    return int_bytes;
}

From source file:Streams.java

public static byte[] drain(InputStream r) throws IOException {
    ByteArrayOutputStream bytes = new ByteArrayOutputStream();
    drain(r, bytes);//from   w  ww  .  java2 s  .c  o m
    return bytes.toByteArray();
}

From source file:Main.java

public static byte[] compressBitmap(byte[] data, float size) {
    Bitmap bitmap = BitmapFactory.decodeByteArray(data, 0, data.length);
    if (bitmap == null || getSizeOfBitmap(bitmap) <= size) {
        return data;
    }//from   w w w. ja v  a  2s. co m
    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    bitmap.compress(Bitmap.CompressFormat.JPEG, 100, baos);
    int quality = 100;
    while ((baos.toByteArray().length / 1024f) > size) {
        quality -= 5;
        baos.reset();
        if (quality <= 0) {
            break;
        }
        bitmap.compress(Bitmap.CompressFormat.JPEG, quality, baos);
    }
    byte[] byteData = baos.toByteArray();
    return byteData;
}

From source file:Main.java

public static void bitmap2file(Bitmap bitmap, String path) {
    try {/*from   ww w.java2s  .  co m*/
        // create a file to write bitmap data
        File f = new File(path);
        if (f.exists()) {
            f.delete();
            f.createNewFile();
        }
        ByteArrayOutputStream bos = new ByteArrayOutputStream();
        bitmap.compress(CompressFormat.JPEG, 30 /* ignored for PNG */, bos);
        byte[] bitmapdata = bos.toByteArray();
        // write the bytes in file
        FileOutputStream fos = new FileOutputStream(f);
        fos.write(bitmapdata);
        fos.flush();
        fos.close();
    } catch (IOException e) {
        e.printStackTrace();
    }
}