Example usage for java.io ByteArrayOutputStream close

List of usage examples for java.io ByteArrayOutputStream close

Introduction

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

Prototype

public void close() throws IOException 

Source Link

Document

Closing a ByteArrayOutputStream has no effect.

Usage

From source file:Main.java

public static byte[] decompress(byte[] data) {
    byte[] output = new byte[0];

    Inflater decompresser = new Inflater();
    decompresser.reset();//from   w  ww .  jav a2  s.  c om
    decompresser.setInput(data);

    ByteArrayOutputStream bos = new ByteArrayOutputStream(2 * data.length);
    try {
        byte[] buf = new byte[1024];
        while (!decompresser.finished()) {
            int length = decompresser.inflate(buf);
            bos.write(buf, 0, length);
        }
        output = bos.toByteArray();
        bos.close();
    } catch (Exception e) {
        output = data;
        e.printStackTrace();
    }

    decompresser.end();
    return output;
}

From source file:Main.java

public static byte[] bmpToByteArray2(final Bitmap bmp, final boolean needRecycle) {

    int i;/*from w  w w  . j  a  v  a 2s.  com*/
    int j;
    if (bmp.getHeight() > bmp.getWidth()) {
        i = bmp.getWidth();
        j = bmp.getWidth();
    } else {
        i = bmp.getHeight();
        j = bmp.getHeight();
    }

    Bitmap localBitmap = Bitmap.createBitmap(i, j, Bitmap.Config.RGB_565);
    Canvas localCanvas = new Canvas(localBitmap);

    while (true) {
        localCanvas.drawBitmap(bmp, new Rect(0, 0, i, j), new Rect(0, 0, i, j), null);
        if (needRecycle)
            bmp.recycle();
        ByteArrayOutputStream localByteArrayOutputStream = new ByteArrayOutputStream();
        localBitmap.compress(Bitmap.CompressFormat.JPEG, 100, localByteArrayOutputStream);
        localBitmap.recycle();
        byte[] arrayOfByte = localByteArrayOutputStream.toByteArray();
        try {
            localByteArrayOutputStream.close();
            return arrayOfByte;
        } catch (Exception e) {
            //F.out(e);
        }
        i = bmp.getHeight();
        j = bmp.getHeight();
    }
}

From source file:Main.java

public static byte[] compress(byte[] data) {
    Deflater deflater = new Deflater();
    deflater.setInput(data);/*from  w ww .  ja  v a2 s .  co m*/
    try {
        ByteArrayOutputStream outputStream = new ByteArrayOutputStream(data.length);

        deflater.finish();
        byte[] buffer = new byte[1024];
        while (!deflater.finished()) {
            int count = deflater.deflate(buffer);
            outputStream.write(buffer, 0, count);
        }
        outputStream.close();
        return outputStream.toByteArray();
    } catch (IOException e) {
        throw new RuntimeException(e);
    } finally {
        deflater.end();
    }

}

From source file:Main.java

/**
 * Download the avatar image from the server.
 *
 * @param avatarUrl the URL pointing to the avatar image
 * @return a byte array with the raw JPEG avatar image
 *///from  w  w  w  .j  a va2s.  c o m
public static byte[] downloadAvatar(final String avatarUrl) {
    // If there is no avatar, we're done
    if (TextUtils.isEmpty(avatarUrl)) {
        return null;
    }

    try {
        Log.i(TAG, "Downloading avatar: " + avatarUrl);
        // Request the avatar image from the server, and create a bitmap
        // object from the stream we get back.
        URL url = new URL(avatarUrl);
        HttpURLConnection connection = (HttpURLConnection) url.openConnection();
        connection.connect();
        try {
            final BitmapFactory.Options options = new BitmapFactory.Options();
            final Bitmap avatar = BitmapFactory.decodeStream(connection.getInputStream(), null, options);

            // Take the image we received from the server, whatever format it
            // happens to be in, and convert it to a JPEG image. Note: we're
            // not resizing the avatar - we assume that the image we get from
            // the server is a reasonable size...
            Log.i(TAG, "Converting avatar to JPEG");
            ByteArrayOutputStream convertStream = new ByteArrayOutputStream(
                    avatar.getWidth() * avatar.getHeight() * 4);
            avatar.compress(Bitmap.CompressFormat.JPEG, 95, convertStream);
            convertStream.flush();
            convertStream.close();
            // On pre-Honeycomb systems, it's important to call recycle on bitmaps
            avatar.recycle();
            return convertStream.toByteArray();
        } finally {
            connection.disconnect();
        }
    } catch (MalformedURLException muex) {
        // A bad URL - nothing we can really do about it here...
        Log.e(TAG, "Malformed avatar URL: " + avatarUrl);
    } catch (IOException ioex) {
        // If we're unable to download the avatar, it's a bummer but not the
        // end of the world. We'll try to get it next time we sync.
        Log.e(TAG, "Failed to download user avatar: " + avatarUrl);
    }
    return null;
}

From source file:Main.java

public static byte[] readStream(InputStream inStream) {
    try {//from w  w  w  . j  a v  a2 s.co m
        ByteArrayOutputStream outStream = new ByteArrayOutputStream();
        byte[] buffer = new byte[1024];
        int len = 0;
        while ((len = inStream.read(buffer)) != -1) {
            outStream.write(buffer, 0, len);
        }

        byte[] data = outStream.toByteArray();
        outStream.flush();
        outStream.close();
        inStream.close();

        return data;
    } catch (OutOfMemoryError e) {
        return null;
    } catch (Exception e) {
        return null;
    }
}

From source file:Main.java

@TargetApi(Build.VERSION_CODES.JELLY_BEAN_MR2)
private static String decryptStringImpl(Context context, final String encryptedText) {
    String plainText = null;/*  ww  w.ja va2s . c om*/
    try {
        final KeyStore keyStore = getKeyStore(context);

        PrivateKey privateKey = (PrivateKey) keyStore.getKey(KEY_ALIAS, null);

        String algorithm = ALGORITHM_OLD;
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
            algorithm = ALGORITHM;
        }
        Cipher cipher = Cipher.getInstance(algorithm);
        cipher.init(Cipher.DECRYPT_MODE, privateKey);

        CipherInputStream cipherInputStream = new CipherInputStream(
                new ByteArrayInputStream(Base64.decode(encryptedText, Base64.DEFAULT)), cipher);

        ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
        int b;
        while ((b = cipherInputStream.read()) != -1) {
            outputStream.write(b);
        }
        outputStream.close();
        plainText = outputStream.toString("UTF-8");
    } catch (Exception e) {
        e.printStackTrace();
    }
    return plainText;
}

From source file:com.giacomodrago.immediatecrypt.messagecipher.Compression.java

public static byte[] decompress(byte[] plaintext) {
    try {/* w  w w.  ja  v  a  2  s.  c om*/
        ByteArrayOutputStream writer = new ByteArrayOutputStream();
        ByteArrayInputStream reader = new ByteArrayInputStream(plaintext);
        GZIPInputStream gzipStream = new GZIPInputStream(reader);
        IOUtils.copy(gzipStream, writer);
        gzipStream.close();
        writer.flush();
        writer.close();
        return writer.toByteArray();
    } catch (IOException ex) {
        return null; // Invalid source data
    }
}

From source file:Main.java

public static byte[] inflate(InputStream in, int bufferSize) throws IOException {
    ByteArrayOutputStream outStream = new ByteArrayOutputStream(bufferSize);

    InflaterInputStream inStream = new InflaterInputStream(in);

    byte[] buf = new byte[4096];
    while (true) {
        int size = inStream.read(buf);
        if (size <= 0)
            break;
        outStream.write(buf, 0, size);//from  w w  w  .  j  a  va 2 s .co m
    }
    outStream.close();

    return outStream.toByteArray();
}

From source file:ch.fork.AdHocRailway.ui.utils.ImageTools.java

/**
 * Encode image to string//from   w  w w  .  j ava2  s. c o m
 *
 * @param image The image to encode
 * @param type  jpeg, bmp, ...
 * @return encoded string
 */
public static String encodeToString(BufferedImage image, String type) {
    String imageString = null;
    ByteArrayOutputStream bos = new ByteArrayOutputStream();

    try {
        ImageIO.write(image, type, bos);
        byte[] imageBytes = bos.toByteArray();

        imageString = Base64.encodeBase64String(imageBytes);

        bos.close();
    } catch (IOException e) {
        e.printStackTrace();
    }
    return imageString;
}

From source file:Main.java

public static String loadImageFromUrl(Context context, String imageURL, File file) {
    try {//from   www.  ja  v a  2  s.  c o  m
        URL url = new URL(imageURL);
        HttpURLConnection con = (HttpURLConnection) url.openConnection();
        con.setConnectTimeout(5 * 1000);
        con.setDoInput(true);
        con.connect();
        if (con.getResponseCode() == 200) {
            InputStream inputStream = con.getInputStream();

            ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
            byte[] buffer = new byte[1024 * 20];
            int length = -1;
            while ((length = inputStream.read(buffer)) != -1) {
                byteArrayOutputStream.write(buffer, 0, length);
            }
            byteArrayOutputStream.close();
            inputStream.close();

            FileOutputStream outputStream = new FileOutputStream(file);
            outputStream.write(byteArrayOutputStream.toByteArray());
            outputStream.close();
            return file.getPath();
        }
    } catch (MalformedURLException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    }
    return "";
}