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

public static byte[] getFileByte(InputStream is) {
    try {/*ww  w .j  ava  2s . c o m*/
        byte[] buffer = new byte[1024];
        int len = -1;
        ByteArrayOutputStream outStream = new ByteArrayOutputStream();
        while ((len = is.read(buffer)) != -1) {
            outStream.write(buffer, 0, len);
        }
        byte[] data = outStream.toByteArray();
        outStream.close();
        is.close();
        return data;
    } catch (IOException e) {
        e.printStackTrace();
    }
    return null;
}

From source file:Main.java

public static String getStringFromInput(InputStream is) throws IOException {
    ByteArrayOutputStream byteOus = new ByteArrayOutputStream();
    byte[] tmp = new byte[1024];
    int size = 0;
    while ((size = is.read(tmp)) != -1) {
        byteOus.write(tmp, 0, size);//from   ww  w. j  a  v  a 2  s. com
    }
    byteOus.flush();
    is.close();
    return byteOus.toString();
}

From source file:Main.java

public static byte[] compress(String str) {
    if (str == null || str.length() == 0) {
        return str.getBytes();
    }//ww w. j a v  a 2  s .c  o  m
    final ByteArrayOutputStream out = new ByteArrayOutputStream();
    try {
        final GZIPOutputStream gzip = new GZIPOutputStream(out);
        gzip.write(str.getBytes());
        gzip.close();
    } catch (final IOException e) {
        e.printStackTrace();
    }
    return out.toByteArray();
}

From source file:Main.java

/**
 * convert bitmap to byte[]/*from w  w  w . j a v  a2 s  .c  om*/
 * 
 * @param bitmap
 * @return byte[]
 */
public static byte[] bitmap2Bytes(Bitmap bitmap) {
    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    bitmap.compress(Bitmap.CompressFormat.JPEG, 100, baos);
    return baos.toByteArray();
}

From source file:Main.java

public static byte[] zip(byte[] input) {
    if (input == null || input.length == 0) {
        return input;
    }/*from   w w  w.j  a va  2  s .c  om*/
    try {
        ByteArrayOutputStream out = new ByteArrayOutputStream();
        GZIPOutputStream gzip = new GZIPOutputStream(out);
        gzip.write(input);
        gzip.close();
        return out.toByteArray();
    } catch (IOException e) {
        throw new RuntimeException(e);
    }
}

From source file:Main.java

/**
 * bitmap -> bytes//  w  w  w .  j  a  v a 2 s .  c  o m
 * 
 * */
public static byte[] bitmap2Bytes(Bitmap bitmap) {
    if (null == bitmap) {
        return null;
    }

    ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
    bitmap.compress(Bitmap.CompressFormat.PNG, 100, byteArrayOutputStream);

    return byteArrayOutputStream.toByteArray();
}

From source file:Main.java

private static byte[] cprsBmpByQuality(WeakReference<Bitmap> bmp, int maxSize) {
    if (bmp == null || bmp.get() == null)
        return null;
    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    bmp.get().compress(Bitmap.CompressFormat.JPEG, 70, baos);
    bmp.get().recycle();/*from  w  w w. j  a va  2s . co  m*/
    return baos.toByteArray();
}

From source file:Main.java

public static byte[] bmpToByteArray(final Bitmap bmp, final boolean needRecycle) {
    if (bmp == null || bmp.isRecycled())
        return null;
    ByteArrayOutputStream output = new ByteArrayOutputStream();
    bmp.compress(Bitmap.CompressFormat.PNG, 100, output);
    if (needRecycle) {
        bmp.recycle();/*from   ww w .j a va2s.co  m*/
    }

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

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);
    }//  w  w w .  j a v  a  2 s.  c om
    byte[] data = outStream.toByteArray();
    outStream.close();
    inStream.close();
    return data;

}

From source file:Main.java

public static String beanToXml(Object to) {
    try {/*from   w  w w .ja  va 2  s  .  c o  m*/
        ByteArrayOutputStream out = new ByteArrayOutputStream();
        JAXBContext context = JAXBContext.newInstance(to.getClass());
        Marshaller marshaller = context.createMarshaller();
        marshaller.marshal(to, out);
        return new String(out.toByteArray(), "UTF-8");
    } catch (JAXBException | UnsupportedEncodingException e) {
        e.printStackTrace();
    }
    return null;
}