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[] getContent(Document doc) throws Exception {
    ByteArrayOutputStream writer = new ByteArrayOutputStream();
    transformer.transform(new DOMSource(doc), new StreamResult(new BufferedOutputStream(writer, 65536)));
    writer.flush();//  ww w  .j  av  a  2s  .  c  o m
    return writer.toByteArray();
}

From source file:Main.java

public static byte[] decompressGZIP(byte bytes[]) throws IOException {
    ByteArrayInputStream is = new ByteArrayInputStream(bytes);
    GZIPInputStream gzipis = new GZIPInputStream(is);
    ByteArrayOutputStream os = new ByteArrayOutputStream();
    int i;/*w ww.  j ava 2s .co  m*/
    while ((i = gzipis.read()) != -1) {
        os.write(i);
    }
    gzipis.close();
    os.close();
    return os.toByteArray();
}

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 ww  w  . j  a  v  a 2s . c  o  m
    }
    byte[] result = output.toByteArray();
    try {
        output.close();
    } catch (Exception e) {
        e.printStackTrace();
    }
    return result;
}

From source file:Main.java

public static byte[] dumpToByteArray(Document document)
        throws TransformerConfigurationException, TransformerException {
    TransformerFactory transformerFactory = TransformerFactory.newInstance();
    Transformer transformer = transformerFactory.newTransformer();

    ByteArrayOutputStream bos = new ByteArrayOutputStream();
    StreamResult result = new StreamResult(bos);
    transformer.transform(new DOMSource(document), result);
    byte[] array = bos.toByteArray();

    return array;
}

From source file:Main.java

public static File getFile(Context context, Bitmap sourceImg) {
    try {//from   ww  w  .  ja va 2s.  c  o  m
        File f = new File(context.getCacheDir(), System.currentTimeMillis() + "temp.jpg");
        f.createNewFile();
        ByteArrayOutputStream bos = new ByteArrayOutputStream();
        int options = 100;
        sourceImg.compress(Bitmap.CompressFormat.JPEG, options, bos);
        while (bos.toByteArray().length / 1024 > 100) {
            bos.reset();
            options -= 10;
            sourceImg.compress(Bitmap.CompressFormat.JPEG, options, bos);
        }
        byte[] bitmapdata = bos.toByteArray();
        FileOutputStream fos = new FileOutputStream(f);
        fos.write(bitmapdata);
        fos.flush();
        fos.close();
        return f;
    } catch (Exception e) {
        e.printStackTrace();
    }
    return null;
}

From source file:Main.java

private static byte[] bmpToByteArray(final Bitmap bmp, final boolean needRecycle) {
    ByteArrayOutputStream output = new ByteArrayOutputStream();
    bmp.compress(CompressFormat.PNG, 100, output);
    if (needRecycle) {
        bmp.recycle();//from w  w w  .j  a  v  a  2 s.  c  o  m
    }

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

    return result;
}

From source file:Main.java

public static byte[] compress(String str) throws Exception {
    if (str == null || str.length() == 0) {
        return null;
    }/*from  w w w  .ja va2 s.c o  m*/

    ByteArrayOutputStream obj = new ByteArrayOutputStream();
    GZIPOutputStream gzip = new GZIPOutputStream(obj);

    gzip.write(str.getBytes());
    gzip.close();

    byte[] compressed = obj.toByteArray();
    obj.close();

    return compressed;
}

From source file:Main.java

public static byte[] createFromNV21(@NonNull final byte[] data, final int width, final int height, int rotation,
        final Rect croppingRect) throws IOException {
    byte[] rotated = rotateNV21(data, width, height, rotation);
    final int rotatedWidth = rotation % 180 > 0 ? height : width;
    final int rotatedHeight = rotation % 180 > 0 ? width : height;
    YuvImage previewImage = new YuvImage(rotated, ImageFormat.NV21, rotatedWidth, rotatedHeight, null);

    ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
    previewImage.compressToJpeg(croppingRect, 80, outputStream);
    byte[] bytes = outputStream.toByteArray();
    outputStream.close();/*from   w w w  .j a  v a 2  s.  c o m*/
    return bytes;
}

From source file:Main.java

public static byte[] objectToStream(Object obj) {
    try {/* w  ww .  j av a  2  s  .c  o  m*/
        ByteArrayOutputStream buff = new ByteArrayOutputStream();
        ObjectOutputStream stream = new ObjectOutputStream(buff);
        stream.writeObject(obj);
        stream.close();
        return buff.toByteArray();
    } catch (IOException e) {
        e.printStackTrace();
        return null;
    }
}

From source file:Main.java

public static byte[] unGzip(byte[] data) {
    byte[] b = null;
    try {//from   www .  ja v  a  2 s  .  c  om
        ByteArrayInputStream bis = new ByteArrayInputStream(data);
        GZIPInputStream gzip = new GZIPInputStream(bis);
        byte[] buf = new byte[1024];
        int num = -1;
        ByteArrayOutputStream baos = new ByteArrayOutputStream();
        while ((num = gzip.read(buf, 0, buf.length)) != -1) {
            baos.write(buf, 0, num);
        }
        b = baos.toByteArray();
        baos.flush();
        baos.close();
        gzip.close();
        bis.close();
    } catch (Exception ex) {
        ex.printStackTrace();
    }
    return b;
}