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:com.cenrise.test.azkaban.GZIPUtils.java

public static byte[] unGzipBytes(final byte[] bytes) throws IOException {
    final ByteArrayInputStream byteInputStream = new ByteArrayInputStream(bytes);
    final GZIPInputStream gzipInputStream = new GZIPInputStream(byteInputStream);

    final ByteArrayOutputStream byteOutputStream = new ByteArrayOutputStream();
    IOUtils.copy(gzipInputStream, byteOutputStream);

    return byteOutputStream.toByteArray();
}

From source file:Main.java

public static byte[] toByteArray(Bitmap bitmap) {
    if (bitmap == null)
        return null;

    ByteArrayOutputStream stream = new ByteArrayOutputStream();
    // Compress image to lower quality scale 1 - 100
    bitmap.compress(Bitmap.CompressFormat.PNG, 100, stream);
    byte[] image = stream.toByteArray();
    return image;
}

From source file:Main.java

public static byte[] zip(byte[] input) {
    if (input == null || input.length == 0) {
        return input;
    }/*  w w  w .jav  a2 s. com*/
    try {
        ByteArrayOutputStream out = new ByteArrayOutputStream();
        GZIPOutputStream gzip = new GZIPOutputStream(out);
        gzip.write(input);
        gzip.close();
        return out.toByteArray();
    } catch (IOException e) {
        throw new RuntimeException(e);
    }
}

From source file:Main.java

public static byte[] serialize(Object obj) {
    ByteArrayOutputStream out = new ByteArrayOutputStream();
    ObjectOutputStream os;//from  w  ww .  j  a v  a  2s . c om
    try {
        os = new ObjectOutputStream(out);
        os.writeObject(obj);
        return out.toByteArray();
    } catch (Exception e) {

        e.printStackTrace();
    }
    return null;
}

From source file:com.eschava.forevernote.EvernoteUtil.java

public static Resource createResource(InputStream stream, String contentType) throws IOException {
    try {//from  w  w  w  .  j  av  a  2  s  .c om
        ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
        FileCopyUtils.copy(stream, outputStream);
        byte[] bytes = outputStream.toByteArray();

        MessageDigest messageDigest = MessageDigest.getInstance("MD5");
        byte[] hash = messageDigest.digest(bytes);

        Data data = new Data();
        data.setBody(bytes);
        data.setBodyHash(hash);

        Resource resource = new Resource();
        resource.setMime(contentType);
        resource.setData(data);

        return resource;
    } catch (NoSuchAlgorithmException e) {
        throw new RuntimeException(e); // is not possible
    }
}

From source file:Main.java

public static byte[] bitmapToByteArray(Bitmap bitmap, int imageQuality) {
    ByteArrayOutputStream bos = null;
    try {/*  ww  w. jav a 2 s .  c o m*/
        bos = new ByteArrayOutputStream();
        bitmap.compress(Bitmap.CompressFormat.JPEG, imageQuality, bos);
        return bos.toByteArray();
    } finally {
        if (bos != null) {
            try {
                bos.close();
            } catch (IOException e) {
                Log.w(TAG, "Failed to close ByteArrayOutputStream", e);
                // Ignore exception
            }
        }
    }
}

From source file:Main.java

public static String write(Element e) throws Exception {
    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    write(e, baos);//  ww w  .j ava2s . co m
    return new String(baos.toByteArray(), "UTF-8");
}

From source file:com.github.carlomicieli.nerdmovies.utility.ImageUtils.java

private static byte[] convertToArray(BufferedImage image, String contentType) throws IOException {
    byte[] imageInByte;

    String typeName = "jpg";
    if (contentType.equals(MediaType.IMAGE_PNG))
        typeName = "png";

    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    ImageIO.write(image, typeName, baos);
    baos.flush();//w w w. j  a  v  a2 s .  c  o m
    imageInByte = baos.toByteArray();
    baos.close();

    return imageInByte;
}

From source file:Main.java

/**
 * @param maxSize unit kb/* w  w  w .j a  v a2 s  . c o m*/
 */
public static boolean compressSizeToFile(Bitmap bitmap, int maxSize, File outFile) {
    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    int quality = 100;
    bitmap.compress(Bitmap.CompressFormat.JPEG, quality, baos);
    while (baos.toByteArray().length / 1024.0 > maxSize) {
        if (quality < 30) {
            break;
        }
        quality -= 20;
        baos.reset();
        bitmap.compress(Bitmap.CompressFormat.JPEG, quality, baos);
    }
    try {
        FileOutputStream fileOutputStream = new FileOutputStream(outFile);
        fileOutputStream.write(baos.toByteArray());
        fileOutputStream.close();
    } catch (Exception e) {
        return false;
    }
    return true;
}

From source file:Main.java

private static void saveBitmap2TempFile(File file, Bitmap bitmap) {

    ByteArrayOutputStream bos = new ByteArrayOutputStream();
    bitmap.compress(CompressFormat.JPEG, 70 /* ignored for PNG */, bos);
    byte[] bitmapdata = bos.toByteArray();

    // write the bytes in file
    FileOutputStream fos = null;/*from w ww .j  a  va2s .  c om*/
    try {

        fos = new FileOutputStream(file);

        fos.write(bitmapdata);
        fos.flush();
    } catch (FileNotFoundException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    } finally {

        try {
            fos.close();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

    }

}