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

/**
 * Create a byte array out of drawable/*w  w w .j  a  v  a 2  s.c o m*/
 *
 * @param drawable The source drawable
 * @return The byte array of source drawable
 */
public static byte[] getByteArrayFromDrawable(Drawable drawable) {
    if (drawable == null) {
        return null;
    }

    Bitmap bitmap = getBitmapFromDrawable(drawable);
    if (bitmap == null) {
        return null;
    }
    ByteArrayOutputStream stream = new ByteArrayOutputStream();
    bitmap.compress(Bitmap.CompressFormat.PNG, 100, stream);
    return stream.toByteArray();
}

From source file:apiserver.services.images.FileHelper.java

public static byte[] fileBytes(Object uploadedFile) throws IOException {

    if (uploadedFile instanceof CommonsMultipartFile) {
        return ((CommonsMultipartFile) uploadedFile).getBytes();
    } else if (uploadedFile instanceof BufferedImage) {
        ByteArrayOutputStream baos = new ByteArrayOutputStream();
        ImageIO.write((BufferedImage) uploadedFile, "jpg", baos);
        return baos.toByteArray();
    }//from  ww  w .j a v  a2 s.  c  om

    return null;
}

From source file:Main.java

public static byte[] bitmap2Bytes(Bitmap bitmap) {
    ByteArrayOutputStream baos;
    try {/*from  ww w. j a v  a2  s  .c om*/
        baos = new ByteArrayOutputStream();
        bitmap.compress(Bitmap.CompressFormat.PNG, 100, baos);
        byte[] b = baos.toByteArray();
        baos.flush();
        baos.close();
        return b;
    } catch (Exception e) {
        e.printStackTrace();
    }
    return null;
}

From source file:Main.java

public static File saveBitmap2file(Bitmap bmp) {
    ByteArrayOutputStream stream = new ByteArrayOutputStream();
    bmp.compress(Bitmap.CompressFormat.JPEG, 100, stream);
    byte[] byteArray = stream.toByteArray();

    File imageFile = null;/*from  w  w w.j  av  a 2s . c om*/
    try {
        imageFile = File.createTempFile("tempImage" + System.currentTimeMillis(), ".png");
    } catch (IOException e) {
        e.printStackTrace();
    }

    FileOutputStream fstream = null;
    try {
        fstream = new FileOutputStream(imageFile);
        BufferedOutputStream bStream = new BufferedOutputStream(fstream);
        bStream.write(byteArray);
        if (bStream != null) {
            bStream.close();
        }
    } catch (IOException e) {
        e.printStackTrace();
    }
    return imageFile;

}

From source file:Main.java

public static byte[] convertBitmapToByteArray(Bitmap bitmap) {
    if (bitmap != null) {
        ByteArrayOutputStream baos = new ByteArrayOutputStream();
        // TODO see if it raise problems
        bitmap.compress(Bitmap.CompressFormat.JPEG, 80, baos);
        return baos.toByteArray();
    } else {/* w  ww.  j av  a2  s. c  o m*/
        return null;
    }
}

From source file:com.cisco.oss.foundation.directory.utils.JsonSerializer.java

/**
 * Serialize the Object to JSON String./*  w  w  w .  jav  a 2  s.c o m*/
 *
 * @param instance
 *            the Object instance.
 * @return the JSON String byte array.
 * @throws JsonGenerationException
 * @throws JsonMappingException
 * @throws IOException
 */
public static byte[] serialize(Object instance)
        throws JsonGenerationException, JsonMappingException, IOException {
    ByteArrayOutputStream out = new ByteArrayOutputStream();
    mapper.writeValue(out, instance);
    return out.toByteArray();
}

From source file:Main.java

public static String bitmapToBase64(Bitmap bitmap) {

    try {//  w  w w . ja v a2s . co m
        ByteArrayOutputStream baos = new ByteArrayOutputStream();
        bitmap.compress(Bitmap.CompressFormat.JPEG, 100, baos);
        baos.close();
        byte[] buffer = baos.toByteArray();
        String photo = Base64.encodeToString(buffer, 0, buffer.length, Base64.DEFAULT);
        return photo;
    } catch (Exception e) {
        e.printStackTrace();
    }
    return null;
}

From source file:Main.java

public static byte[] ObjectToByte(Serializable obj) {
    byte[] bytes = null;
    try {/*from ww w. j  av a2 s .  c  om*/
        // object to bytearray
        ByteArrayOutputStream bo = new ByteArrayOutputStream();
        ObjectOutputStream oo = new ObjectOutputStream(bo);
        oo.writeObject(obj);

        bytes = bo.toByteArray();

        bo.close();
        oo.close();
    } catch (Exception e) {
        e.printStackTrace();
    }
    return bytes;
}

From source file:com.janoz.usenet.support.LogUtil.java

public static InputStream dumpStreamAndReOffer(InputStream is) throws IOException {
    FileOutputStream fos = null;//  w w w. j  a v a 2 s  .c  o m
    try {
        ByteArrayOutputStream os = new ByteArrayOutputStream();
        copyStream(is, os);
        byte[] data = os.toByteArray();
        fos = new FileOutputStream(File.createTempFile("dump", ".bin", new File(".")));
        fos.write(data);
        return new ByteArrayInputStream(data);
    } finally {
        if (fos != null) {
            fos.close();
        }
    }

}

From source file:net.sf.sripathi.ws.mock.util.WebServiceutil.java

/**
 * Invokes the web service using SAAJ API.
 * /*w  w  w.  j  a v  a 2 s. c  o  m*/
 * @param request soap request string.
 * @param url end point URL.
 * @param user user name for authentication.
 * @param password password for authentication.
 * 
 * @return response soap string.
 */
public static String callWebService(String request, String url, String user, String password) {

    if (request == null)
        request = "";
    try {
        SOAPConnection conn = SOAPConnectionFactory.newInstance().createConnection();

        MimeHeaders hd = new MimeHeaders();
        hd.addHeader("Content-Type", "text/xml");
        if (StringUtil.isValid(user) && StringUtil.isValid(password)) {
            String authorization = new String(Base64.encodeBase64((user + ":" + password).getBytes()));
            hd.addHeader("Authorization", "Basic " + authorization);
        }

        SOAPMessage msg = MessageFactory.newInstance().createMessage(hd,
                new ByteArrayInputStream(request.getBytes()));
        SOAPMessage resp = conn.call(msg, url);
        ByteArrayOutputStream baos = new ByteArrayOutputStream();
        resp.writeTo(baos);
        return new String(baos.toByteArray());
    } catch (Exception e) {
        StringWriter sw = new StringWriter();
        e.printStackTrace(new PrintWriter(sw));
        return sw.toString();
    }
}