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

private static String toString(InputStream is) throws IOException {

    byte[] bytes = new byte[1024];
    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    int lidos;/* www. j  a  va2  s. co  m*/
    while ((lidos = is.read(bytes)) > 0) {
        baos.write(bytes, 0, lidos);
    }
    return new String(baos.toByteArray());
}

From source file:Main.java

public static boolean saveDrawabletoFile(Context c, Drawable d, File file) {
    //create a file to write bitmap data
    try {/*from w ww  .  ja  v  a2  s  .c  o m*/
        file.createNewFile();
        //Convert bitmap to byte array
        Bitmap bitmap = ((BitmapDrawable) d).getBitmap();
        ByteArrayOutputStream bos = new ByteArrayOutputStream();
        bitmap.compress(CompressFormat.PNG, 0 /*ignored for PNG*/, bos);
        byte[] bitmapdata = bos.toByteArray();

        //write the bytes in file
        FileOutputStream fos = new FileOutputStream(file);
        fos.write(bitmapdata);
        fos.flush();
        fos.close();
        return true;
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }

    return false;
}

From source file:Main.java

private static byte[] readInputStream(InputStream inputStream) throws IOException {
    byte[] buffer = new byte[8192];
    int len = 0;/*from   w w w.j av  a  2s . c  o m*/
    ByteArrayOutputStream bos = new ByteArrayOutputStream();
    while ((len = inputStream.read(buffer)) != -1) {
        bos.write(buffer, 0, len);
    }
    bos.close();
    return bos.toByteArray();
}

From source file:Main.java

public static byte[] serialize(Object obj) throws Exception {
    //System.err.println(" ser ##"+obj);
    ByteArrayOutputStream bout = new ByteArrayOutputStream();
    ObjectOutputStream oout = new ObjectOutputStream(bout);
    oout.writeObject(obj);//from  w w w. ja v  a2  s  .c  o m
    oout.flush();
    byte array[] = bout.toByteArray();
    oout.close();
    bout.close();
    //System.err.println(" ser #"+new String(array));
    return array;
}

From source file:Main.java

public static byte[] readStream(InputStream inStream) throws Exception {
    byte[] buffer = new byte[1024];
    int len = -1;
    ByteArrayOutputStream outStream = new ByteArrayOutputStream();
    while ((len = inStream.read(buffer)) != -1) {
        outStream.write(buffer, 0, len);
    }//www.jav a2s .  c o  m
    byte[] data = outStream.toByteArray();
    outStream.close();
    inStream.close();
    return data;

}

From source file:com.vaadin.testbench.testutils.ImageLoader.java

public static String loadImageToString(String folder, String filename) throws IOException {
    BufferedImage img = loadImage(folder, filename);
    final ByteArrayOutputStream outStream = new ByteArrayOutputStream();

    ImageIO.write(img, "png", outStream);
    return new String(Base64.encodeBase64(outStream.toByteArray()));
}

From source file:Main.java

public static final byte[] serialize(Object o) throws IOException {
    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    ObjectOutputStream oos = new ObjectOutputStream(baos);
    oos.writeObject(o);/* www  .ja va  2s .c om*/
    oos.flush();
    oos.close();
    return baos.toByteArray();
}

From source file:Main.java

/**
 * @param context//from   w  ww. j a  va2 s  . c  o m
 * @param data
 * @return
 */
@Nullable
public static byte[] retrieveSelectedImage(@NonNull Context context, @NonNull Intent data) {
    InputStream inStream = null;
    Bitmap bitmap = null;
    try {
        inStream = context.getContentResolver().openInputStream(data.getData());
        bitmap = BitmapFactory.decodeStream(inStream);
        final ByteArrayOutputStream outStream = new ByteArrayOutputStream();
        bitmap.compress(Bitmap.CompressFormat.JPEG, 100, outStream);
        return outStream.toByteArray();
    } catch (FileNotFoundException e) {
        return null;
    } finally {
        if (inStream != null) {
            try {
                inStream.close();
            } catch (IOException ignored) {
            }
        }
        if (bitmap != null) {
            bitmap.recycle();
        }
    }
}

From source file:Main.java

public static byte[] doZip(byte[] data) throws IOException {
    ByteArrayOutputStream out = new ByteArrayOutputStream();
    java.util.zip.GZIPOutputStream gout = new GZIPOutputStream(out);
    gout.write(data);//from   w  ww . j a v  a  2s. c o m
    gout.close();
    return out.toByteArray();
}

From source file:org.jenkinsci.plugins.fabric8.support.JSONHelper.java

public static HttpResponse jsonResponse(final Object payload) {
    return new HttpResponse() {
        @Override/*  www. j  a v a2 s. c o  m*/
        public void generateResponse(StaplerRequest request, StaplerResponse response, Object node)
                throws IOException, ServletException {
            response.setContentType("application/json");
            ByteArrayOutputStream buffer = new ByteArrayOutputStream();
            objectMapper.writeValue(buffer, payload);
            response.getOutputStream().write(buffer.toByteArray());
        }
    };
}