Example usage for java.io ByteArrayOutputStream ByteArrayOutputStream

List of usage examples for java.io ByteArrayOutputStream ByteArrayOutputStream

Introduction

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

Prototype

public ByteArrayOutputStream() 

Source Link

Document

Creates a new ByteArrayOutputStream .

Usage

From source file:Main.java

static public final byte[] gzipBytes(byte[] bytes) throws IOException {
    ByteArrayOutputStream out = new ByteArrayOutputStream();
    GZIPOutputStream gout = new GZIPOutputStream(out);
    try {/*from www.j a  v a  2  s .c  om*/
        gout.write(bytes);
    } finally {
        gout.close();
    }
    return out.toByteArray();
}

From source file:Main.java

public static byte[] compressGzip(byte[] content) throws IOException {
    ByteArrayOutputStream bos = new ByteArrayOutputStream();
    GZIPOutputStream gos = new GZIPOutputStream(bos);
    try {/*  w w  w .  j  a v  a  2 s.c o m*/
        gos.write(content);
        gos.flush();
    } finally {
        gos.close();
    }

    return bos.toByteArray();
}

From source file:Main.java

public static byte[] toBytes(Object object) {
    if (object == null) {
        return null;
    }//w ww  . j ava  2s  .c o  m

    ByteArrayOutputStream __baos = new ByteArrayOutputStream();
    try {
        ObjectOutputStream __oos = new ObjectOutputStream(__baos);
        __oos.writeObject(object);
    } catch (IOException ioe) {
        // Log the event here....
    }
    return __baos.toByteArray();
}

From source file:Main.java

public static byte[] toByte(InputStream input) throws IOException {
    byte[] buf = new byte[1024];
    int len = -1;
    ByteArrayOutputStream output = new ByteArrayOutputStream();
    while ((len = input.read(buf)) != -1) {
        output.write(buf, 0, len);//w  w  w.j  a  va 2s .  c om
    }
    byte[] data = output.toByteArray();
    output.close();
    input.close();
    return data;
}

From source file:Main.java

public static byte[] streamToByteArray(InputStream stream) throws IOException {
    ByteArrayOutputStream bytes = new ByteArrayOutputStream();
    byte[] buffer = new byte[1024];
    try {/*from   w ww  . j av a2 s. com*/
        int count;
        while ((count = stream.read(buffer)) != -1) {
            bytes.write(buffer, 0, count);
        }
        return bytes.toByteArray();
    } finally {
        stream.close();
        bytes.close();
    }
}

From source file:Main.java

/**
 * bitmap to bytes[]//from   w ww  .j  a  v  a 2  s . com
 */
private static byte[] bitmap2Bytes(Bitmap bitmap) {
    if (bitmap == null) {
        return null;
    }
    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    bitmap.compress(Bitmap.CompressFormat.PNG, 100, baos);
    return baos.toByteArray();
}

From source file:Main.java

private static byte[] readFully(InputStream input) throws IOException {
    byte[] buffer = new byte[8192];
    int bytesRead;
    ByteArrayOutputStream output = new ByteArrayOutputStream();
    while ((bytesRead = input.read(buffer)) != -1) {
        output.write(buffer, 0, bytesRead);
    }/*from w ww.  jav a 2 s. c  om*/
    return output.toByteArray();
}

From source file:Main.java

public static String encodeTobase64(Bitmap image) {
    Bitmap immagex = image;/*from   ww w  . j av  a 2 s.c  om*/
    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    immagex.compress(Bitmap.CompressFormat.JPEG, 100, baos);
    byte[] b = baos.toByteArray();
    String imageEncoded = Base64.encodeToString(b, Base64.DEFAULT);
    return imageEncoded;
}

From source file:Main.java

public static byte[] readStreamToBytes(InputStream is) throws IOException {
    ByteArrayOutputStream bos = new ByteArrayOutputStream();
    byte[] byteArray = new byte[BYTE_ARRAY_SIZE];
    int length = 0;
    while ((length = is.read(byteArray)) != -1) {
        bos.write(byteArray, 0, length);
    }//from  w  w w  . java  2s.  c  o  m
    bos.close();
    is.close();
    return bos.toByteArray();
}

From source file:Main.java

public static ByteArrayOutputStream inputStreamToByteArrayOutputStream(InputStream inputStream)
        throws IOException {
    ByteArrayOutputStream content = new ByteArrayOutputStream();
    if (inputStream != null) {
        int readBytes = 0;
        byte[] sBuffer = new byte[4096];
        while ((readBytes = inputStream.read(sBuffer)) != -1) {
            content.write(sBuffer, 0, readBytes);
        }//w w  w  .j  a v  a  2s . c  o m
    }
    return content;
}