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

public static byte[] bmpToByteArray(final Bitmap bmp, final boolean needRecycle) {
    ByteArrayOutputStream output = new ByteArrayOutputStream();
    bmp.compress(Bitmap.CompressFormat.PNG, 100, output);
    if (needRecycle) {
        bmp.recycle();//from w  ww .j a  va  2  s  .c o m
    }

    byte[] result = output.toByteArray();
    try {
        output.close();
    } catch (Exception e) {
        e.printStackTrace();
    }

    return result;
}

From source file:MainClass.java

public static byte[] makeBytes(long t, double q) {
    try {/* w ww  . j  a  va2 s  . c om*/
        ByteArrayOutputStream byteOut = new ByteArrayOutputStream();
        DataOutputStream dataOut = new DataOutputStream(byteOut);
        dataOut.writeLong(t);
        dataOut.writeDouble(q);
        return byteOut.toByteArray();
    } catch (IOException e) {
        return new byte[0];
    }
}

From source file:Main.java

/**
 * Converts image to byte array./*www  .j av a 2s  . co  m*/
 * <br /><br />
 * <p/>
 * <b>ATTENTION</b>: image conversion is done by compression, witch is very long running operation.
 *
 * @param bmp     original bitmap
 * @param format  compress format
 * @param quality compress quality
 * @return compress image as byte array
 * @see android.graphics.Bitmap#compress(android.graphics.Bitmap.CompressFormat, int, java.io.OutputStream)
 */
public static byte[] bmpToByte(Bitmap bmp, Bitmap.CompressFormat format, int quality) {
    final ByteArrayOutputStream stream = new ByteArrayOutputStream();
    bmp.compress(format, quality, stream);

    try {
        stream.flush();
        byte[] data = stream.toByteArray();
        stream.close();

        return data;
    } catch (IOException e) {
        return null;
    }
}

From source file:Main.java

private static byte[] bmpToByteArray(final Bitmap bmp, final boolean needRecycle) {
    ByteArrayOutputStream output = new ByteArrayOutputStream();
    bmp.compress(Bitmap.CompressFormat.PNG, 100, output);
    if (needRecycle) {
        bmp.recycle();/*from w  w w  . j a  va2s.c om*/
    }

    byte[] result = output.toByteArray();
    try {
        output.close();
    } catch (Exception e) {
    }

    return result;
}

From source file:com.facebook.buck.testutil.integration.TarInspector.java

private static ImmutableMap<String, byte[]> readTar(Optional<String> compressorType, Path tar)
        throws IOException, CompressorException {

    HashMap<String, byte[]> result = new HashMap<>();

    try (TarArchiveInputStream archiveStream = getArchiveInputStream(compressorType, tar)) {
        TarArchiveEntry entry;/*from w  w  w .ja  v  a  2  s  .c o  m*/
        while ((entry = archiveStream.getNextTarEntry()) != null) {
            ByteArrayOutputStream buffer = new ByteArrayOutputStream();
            ByteStreams.copy(archiveStream, buffer);
            result.put(entry.getName(), buffer.toByteArray());
        }
    }

    return ImmutableMap.copyOf(result);
}

From source file:dualcontrol.DualControlDigest.java

public static byte[] getBytes(char[] chars) throws IOException {
    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    Writer writer = new OutputStreamWriter(baos);
    writer.write(chars);//from   w ww  .  j  ava  2s. com
    writer.close();
    return baos.toByteArray();
}

From source file:Main.java

public static byte[] createFromNV21(@NonNull final byte[] data, final int width, final int height, int rotation,
        final Rect croppingRect, final boolean flipHorizontal) throws IOException {
    byte[] rotated = rotateNV21(data, width, height, rotation, flipHorizontal);
    final int rotatedWidth = rotation % 180 > 0 ? height : width;
    final int rotatedHeight = rotation % 180 > 0 ? width : height;
    YuvImage previewImage = new YuvImage(rotated, ImageFormat.NV21, rotatedWidth, rotatedHeight, null);

    ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
    previewImage.compressToJpeg(croppingRect, 80, outputStream);
    byte[] bytes = outputStream.toByteArray();
    outputStream.close();/*  ww  w  .  j av  a  2  s .  c  o  m*/
    return bytes;
}

From source file:Main.java

public static byte[] Bitmap2Bytes(Bitmap bm) {
    if (bm == null) {
        return null;
    }//from w w  w.  java 2  s.c o m
    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    bm.compress(CompressFormat.PNG, 100, baos);

    if (bm != null) {
        bm.recycle();
        bm = null;
    }

    return baos.toByteArray();
}

From source file:Main.java

public static byte[] readToEndAsArray(InputStream input) throws IOException {
    DataInputStream dis = new DataInputStream(input);
    byte[] stuff = new byte[1024];
    ByteArrayOutputStream buff = new ByteArrayOutputStream();
    int read = 0;
    while ((read = dis.read(stuff)) != -1) {
        buff.write(stuff, 0, read);//ww w.  j a v  a2 s .  c o m
    }
    dis.close();
    return buff.toByteArray();
}

From source file:Main.java

public static List copyBySerialize(List src) throws IOException, ClassNotFoundException {
    ByteArrayOutputStream byteOut = new ByteArrayOutputStream();
    ObjectOutputStream out = new ObjectOutputStream(byteOut);
    out.writeObject(src);/* w  w  w  .  ja  v a 2 s.co m*/

    ByteArrayInputStream byteIn = new ByteArrayInputStream(byteOut.toByteArray());
    ObjectInputStream in = new ObjectInputStream(byteIn);
    List dest = (List) in.readObject();
    return dest;
}