Example usage for java.io ByteArrayOutputStream flush

List of usage examples for java.io ByteArrayOutputStream flush

Introduction

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

Prototype

public void flush() throws IOException 

Source Link

Document

Flushes this output stream and forces any buffered output bytes to be written out.

Usage

From source file:Main.java

public static void compressFileIfNeeded(String filePath) {
    File f = new File(filePath);

    Bitmap bitmap;//  w  w  w  . ja va2 s.c o m
    bitmap = BitmapFactory.decodeFile(filePath);
    int MAX_IMAGE_SIZE = 1000 * 1024;
    int streamLength = (int) f.length();
    if (streamLength > MAX_IMAGE_SIZE) {
        int compressQuality = 105;
        ByteArrayOutputStream bmpStream = new ByteArrayOutputStream();
        while (streamLength >= MAX_IMAGE_SIZE && compressQuality > 5) {
            try {
                bmpStream.flush();//to avoid out of memory error
                bmpStream.reset();
            } catch (IOException e) {
                e.printStackTrace();
            }
            compressQuality -= 5;
            bitmap.compress(Bitmap.CompressFormat.JPEG, compressQuality, bmpStream);
            byte[] bmpPicByteArray = bmpStream.toByteArray();
            streamLength = bmpPicByteArray.length;
            Log.d("test upload", "Quality: " + compressQuality);
            Log.d("test upload", "Size: " + streamLength);
        }

        FileOutputStream fo;

        try {
            f.delete();
            f = new File(filePath);
            fo = new FileOutputStream(f);
            fo.write(bmpStream.toByteArray());
            fo.flush();
            fo.close();
            ExifInterface exif = new ExifInterface(f.getAbsolutePath());
            exif.setAttribute(ExifInterface.TAG_ORIENTATION, "6");
            exif.saveAttributes();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}

From source file:Main.java

public static byte[] extract(InputStream inputStream) throws IOException {
    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    byte[] buffer = new byte[1024];
    int read = 0;
    while ((read = inputStream.read(buffer, 0, buffer.length)) != -1) {
        baos.write(buffer, 0, read);//from   ww  w. jav  a2  s . c  o  m
    }
    baos.flush();
    return baos.toByteArray();
}

From source file:Main.java

public static byte[] readFileToByteArray2(File file) {
    try {/*from   w  w w.  j av a2 s.  c  om*/
        FileInputStream fis = new FileInputStream(file);
        ByteArrayOutputStream bos = new ByteArrayOutputStream();
        byte[] buf = new byte[1024];

        for (int readNum; (readNum = fis.read(buf)) != -1;) {
            bos.write(buf, 0, readNum);
        }
        bos.flush();
        byte[] bytes = bos.toByteArray();
        fis.close();
        bos.close();
        return bytes;

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

From source file:Main.java

/**
 * Write the contents of an input stream into a byte array.
 * @param inputStream The stream to convert into a byte array.
 * @return A byte array containing the contents of the input stream.
 * @throws IOException//w  w  w .  j av a 2  s. c o  m
 */
private static byte[] convertInputStreamToByteArray(InputStream inputStream) throws IOException {
    byte[] bytes = null;
    ByteArrayOutputStream bos = new ByteArrayOutputStream();
    byte data[] = new byte[1024];
    int count;
    while ((count = inputStream.read(data)) != -1) {
        bos.write(data, 0, count);
    }
    bos.flush();
    bos.close();
    inputStream.close();
    bytes = bos.toByteArray();
    return bytes;
}

From source file:Main.java

static private byte[] readBytesFromStream(InputStream input) throws IOException {

    ByteArrayOutputStream buffer = new ByteArrayOutputStream();

    int nRead;/*from   ww w  .  j a  va 2s  .  c o  m*/
    byte[] data = new byte[16384];

    while ((nRead = input.read(data, 0, data.length)) != -1) {
        buffer.write(data, 0, nRead);
    }

    buffer.flush();
    input.close();
    return buffer.toByteArray();
}

From source file:net.ripe.rpki.commons.crypto.util.KeyPairUtil.java

public static String base64UrlEncode(byte[] data) {
    ByteArrayOutputStream out = new ByteArrayOutputStream();
    try {//from w w  w .jav a  2  s  . com
        new FileSystemSafeBase64UrlEncoder().encode(data, 0, data.length, out);
        out.flush();
        return out.toString();
    } catch (IOException e) {
        throw new IllegalArgumentException("Exception when base64url encoding data", e);
    }
}

From source file:Main.java

public static byte[] readFile(File f) throws IOException {
    FileInputStream fis = new FileInputStream(f);
    try {/*from w  w w .  ja v  a 2  s  .  co m*/
        ByteArrayOutputStream baos = new ByteArrayOutputStream();
        BufferedInputStream bis = new BufferedInputStream(fis);
        int ch;
        while ((ch = bis.read()) >= 0) {
            baos.write(ch);
        }
        baos.flush();
        return baos.toByteArray();
    } finally {
        fis.close();
    }
}

From source file:Main.java

/**
 * Serialize an object.// w w  w.  j  a  v a  2  s.c  om
 * @param obj a serializable object.
 * @return a byte array of the serialized object or null if an error occurs.
 */
public static byte[] serialize(Object obj) {
    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    ObjectOutputStream oos = null;
    try {
        oos = new ObjectOutputStream(baos);
        oos.writeObject(obj);
        oos.flush();
        baos.flush();
        return baos.toByteArray();
    } catch (IOException e) {
        return null;
    } finally {
        try {
            if (oos != null)
                oos.close();
        } catch (IOException e) {
        }
    }
}

From source file:Main.java

public static String BitmapToBase64(Bitmap bitmap) {

    String result = null;//from   w w  w .j  a v  a2 s .c o m
    ByteArrayOutputStream baos = null;
    try {
        if (bitmap != null) {
            baos = new ByteArrayOutputStream();
            bitmap.compress(Bitmap.CompressFormat.JPEG, 60, baos);

            baos.flush();
            baos.close();

            byte[] bitmapBytes = baos.toByteArray();
            result = Base64.encodeToString(bitmapBytes, Base64.DEFAULT);
        }
    } catch (IOException e) {
        e.printStackTrace();
    } finally {
        try {
            if (baos != null) {
                baos.flush();
                baos.close();
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
    return result;
}

From source file:Main.java

public static String convertIconToString(Bitmap bitmap) {
    String result = null;//from www . ja  va  2 s.  c o m
    ByteArrayOutputStream baos = null;
    try {
        if (bitmap != null) {
            baos = new ByteArrayOutputStream();
            bitmap.compress(Bitmap.CompressFormat.JPEG, 100, baos);

            baos.flush();
            baos.close();

            byte[] bitmapBytes = baos.toByteArray();
            result = Base64.encodeToString(bitmapBytes, Base64.DEFAULT);
        }
    } catch (IOException e) {
        e.printStackTrace();
    } finally {
        try {
            if (baos != null) {
                baos.flush();
                baos.close();
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
    return result;

}