Example usage for java.io ByteArrayOutputStream close

List of usage examples for java.io ByteArrayOutputStream close

Introduction

In this page you can find the example usage for java.io ByteArrayOutputStream close.

Prototype

public void close() throws IOException 

Source Link

Document

Closing a ByteArrayOutputStream has no effect.

Usage

From source file:Main.java

public static byte[] bitmapToByteArray(Bitmap bitmap) {
    ByteArrayOutputStream stream = new ByteArrayOutputStream();
    bitmap.compress(Bitmap.CompressFormat.PNG, 100, stream);
    byte[] array = stream.toByteArray();
    try {/*from  w  ww . ja va2 s  .c o m*/
        stream.close();
    } catch (IOException e) {
        e.printStackTrace();
    }
    return array;

}

From source file:Main.java

public static byte[] convertToByteArray(Bitmap bmp) {
    ByteArrayOutputStream stream = new ByteArrayOutputStream();
    bmp.compress(Bitmap.CompressFormat.JPEG, 80, stream);
    byte[] byteArray = stream.toByteArray();
    try {/*from   w  ww.j a  v a2  s.  c o m*/
        stream.close();
    } catch (IOException e) {
        e.printStackTrace();
    }
    return byteArray;
}

From source file:Main.java

public static byte[] bmpToByteArray(Bitmap bmp) {
    if (bmp == null)
        return null;
    ByteArrayOutputStream output = new ByteArrayOutputStream();
    bmp.compress(Bitmap.CompressFormat.PNG, 80, output);

    byte[] result = output.toByteArray();
    try {/*w w w . ja  va2  s  .  c  o  m*/
        output.close();
    } catch (Exception e) {
        e.printStackTrace();
    }
    return result;
}

From source file:Main.java

public static String convertToBase64(Bitmap bmp) {
    ByteArrayOutputStream stream = new ByteArrayOutputStream();
    bmp.compress(Bitmap.CompressFormat.JPEG, 80, stream);
    byte[] byteArray = stream.toByteArray();
    try {/*w w w  . j  a v  a 2 s. c o  m*/
        stream.close();
    } catch (IOException e) {
        e.printStackTrace();
    }
    return Base64.encodeToString(byteArray, Base64.DEFAULT);
}

From source file:Main.java

/**
 * Method to convert a Bitmap into a byte[].
 * Attention: this method is very expensive!!!
 *
 * @param source The Bitmap to convert.//from w w w  . ja v  a  2s.  com
 * @return The byte[] that represents the source Bitmap.
 */
public static byte[] toByteArray(Bitmap source) throws IOException {
    ByteArrayOutputStream blob = new ByteArrayOutputStream();
    source.compress(Bitmap.CompressFormat.PNG, 0 /*ignored for PNG*/, blob);
    byte[] photoByteArray = blob.toByteArray();
    blob.close();
    return photoByteArray;
}

From source file:net.dv8tion.jda.utils.AvatarUtil.java

public static Avatar getAvatar(BufferedImage img) {
    try {/*  w  w  w. ja  v  a2 s .c o  m*/
        //resizing
        img = resize(img);
        //writing + converting to jpg if necessary
        ByteArrayOutputStream bout = new ByteArrayOutputStream();
        ImageIO.write(img, "jpg", bout);
        bout.close();

        return new Avatar("data:image/jpeg;base64,"
                + StringUtils.newStringUtf8(Base64.getEncoder().encode(bout.toByteArray())));
    } catch (IOException e) {
        JDAImpl.LOG.log(e);
    }
    return null;
}

From source file:Main.java

/**
 * Convert a bitmap to a byte array//from   www  .  ja  v a 2  s .  c  o  m
 * @param path the path to the picture on the phone
 * @return the image in a byte array
 */
public static byte[] picToByte(Bitmap bitmap) {
    if (bitmap == null) {
        return null;
    }
    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    bitmap.compress(Bitmap.CompressFormat.JPEG, 10, baos);
    byte[] ba = baos.toByteArray();
    try {
        baos.close();
    } catch (IOException e) {
        Log.e("Error", "Error closing output stream" + e.getMessage());
        return null;
    }
    return ba;

}

From source file:Main.java

/**
 * TODO doc//w w  w .  jav a2  s  . c om
 *
 * @param photoBitmap
 * @return
 */
public static byte[] toByteArray(Bitmap photoBitmap) throws IOException {
    ByteArrayOutputStream blob = new ByteArrayOutputStream();
    photoBitmap.compress(Bitmap.CompressFormat.PNG, 0 /*ignored for PNG*/, blob);
    byte[] photoByteArray = blob.toByteArray();
    blob.close();
    return photoByteArray;
}

From source file:Main.java

public static byte[] readInputStream(InputStream is) throws IOException {
    byte[] buffer = new byte[1024];
    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    int len;// w ww  . j a  va 2  s.c  om
    while ((len = is.read(buffer)) != -1) {
        baos.write(buffer, 0, len);
    }
    baos.flush();
    baos.close();
    is.close();
    return baos.toByteArray();
}

From source file:Main.java

public static byte[] load(InputStream is) throws IOException {
    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    byte[] buffer = new byte[1024];
    int len;/*from   w w  w  .  j a v a 2  s  . com*/
    while ((len = is.read(buffer)) != -1)
        baos.write(buffer, 0, len);
    baos.close();
    is.close();
    return baos.toByteArray();
}