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[] decompressZLIB(byte[] input) throws IOException {
    Inflater decompressor = new Inflater();
    decompressor.setInput(input);//ww  w. jav a  2s .  c  o  m
    // Create an expandable byte array to hold the decompressed data
    ByteArrayOutputStream bos = new ByteArrayOutputStream(input.length);

    // Decompress the data
    byte[] buf = new byte[1024];
    while (!decompressor.finished()) {
        try {
            int count = decompressor.inflate(buf);
            bos.write(buf, 0, count);
        } catch (DataFormatException e) {
            throw new IOException(e.toString());
        }
    }
    bos.close();

    // Get the decompressed data
    byte[] decompressedData = bos.toByteArray();
    return decompressedData;
}

From source file:com.company.project.core.connector.HttpClientHelper.java

public static byte[] getByteaArrayFromConn(HttpURLConnection conn, boolean isSuccess) throws IOException {

    InputStream is = conn.getInputStream();
    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    byte[] buff = new byte[1024];
    int bytesRead = 0;

    while ((bytesRead = is.read(buff, 0, buff.length)) != -1) {
        baos.write(buff, 0, bytesRead);/*from  w  w w  .  j  a v  a 2s . c  om*/
    }

    byte[] bytes = baos.toByteArray();
    baos.close();
    return bytes;
}

From source file:com.qatickets.common.ZIPHelper.java

public static byte[] compress(byte[] data) {

    // Create the compressor with highest level of compression
    final Deflater compressor = new Deflater();
    compressor.setLevel(Deflater.BEST_COMPRESSION);

    // Give the compressor the data to compress
    compressor.setInput(data);//from ww  w . j  a v  a  2  s  .  c o  m
    compressor.finish();

    // Create an expandable byte array to hold the compressed data.
    // You cannot use an array that's the same size as the orginal because
    // there is no guarantee that the compressed data will be smaller than
    // the uncompressed data.
    final ByteArrayOutputStream bos = new ByteArrayOutputStream(data.length);

    // Compress the data
    byte[] buf = new byte[1024];
    while (!compressor.finished()) {
        final int count = compressor.deflate(buf);
        bos.write(buf, 0, count);
    }
    try {
        bos.close();
    } catch (IOException e) {
    }

    // Get the compressed data
    final byte[] compressedData = bos.toByteArray();

    compressor.end();

    return compressedData;
}

From source file:Serialization.java

/**
 * Gets an array of bytes corresponding to the given object.
 * @param object the object to serialize
 * @return an array of bytes./*from   ww w  .ja  v  a 2  s.co  m*/
 * @throws IOException if the object can't be turned into an array of bytes.
 */
public static byte[] storeObject(final Serializable object) throws IOException {
    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    ObjectOutputStream oos = null;
    try {
        oos = new ObjectOutputStream(baos);
        oos.writeObject(object);
        oos.flush();
        return baos.toByteArray();
    } finally {
        if (oos != null) {
            oos.close();
        }
        if (baos != null) {
            baos.close();
        }
    }
}

From source file:Main.java

public static byte[] getBytesFromFile(File f) {

    if (f == null) {
        return null;
    }//  www  .  j a  va  2 s.c  o  m

    FileInputStream stream = null;
    ByteArrayOutputStream out = null;

    try {
        stream = new FileInputStream(f);
        out = new ByteArrayOutputStream(1024);
        byte[] b = new byte[1024];
        int n;
        while ((n = stream.read(b)) != -1)
            out.write(b, 0, n);

    } catch (Exception e) {
    }

    try {
        if (out != null)
            out.close();

        if (stream != null)
            stream.close();
    } catch (Exception e) {
    }

    System.gc();

    if (out == null)
        return null;

    return out.toByteArray();
}

From source file:Main.java

public static String parseObj2Xml(Object object) throws Exception {
    XMLEncoder encoder = null;//w w w.ja  v  a  2  s. c o  m
    String str = null;
    BufferedOutputStream bufOut = null;
    ByteArrayOutputStream out = null;
    try {
        out = new ByteArrayOutputStream();
        bufOut = new BufferedOutputStream(out);
        encoder = new XMLEncoder(bufOut);
        encoder.writeObject(object);
        encoder.close();
        str = new String(out.toByteArray());
    } catch (Exception ex) {
        throw ex;
    } finally {
        if (out != null)
            out.close();
        if (bufOut != null)
            bufOut.close();
    }
    return str;
}

From source file:Main.java

/**
 * Convert an image to a byte array/*ww w .  ja  v a 2  s  .  com*/
 * @param uri
 * @param context
 * @return
 */
public static byte[] convertImageToByte(Uri uri, Context context) {
    byte[] data = null;
    try {
        ContentResolver cr = context.getContentResolver();
        InputStream inputStream = cr.openInputStream(uri);
        Bitmap bitmap = BitmapFactory.decodeStream(inputStream);
        ByteArrayOutputStream baos = new ByteArrayOutputStream();
        bitmap.compress(Bitmap.CompressFormat.PNG, 100, baos);
        data = baos.toByteArray();
        if (inputStream != null) {
            try {
                inputStream.close();
            } catch (Exception e2) {
            }
        }
        if (baos != null) {
            try {
                baos.close();
            } catch (Exception e2) {
            }
        }
    } catch (FileNotFoundException e) {
        e.printStackTrace();
    }
    return data;
}

From source file:de.alpharogroup.xml.json.JsonTransformer.java

/**
 * Creates from the given {@link List} to a json string.
 *
 * @param <T>/*from  w  w w. jav  a 2 s.c  o m*/
 *            the generic type
 * @param list
 *            the list
 * @return the json string.
 * @throws JsonGenerationException
 *             If an error occurs by writing json string
 * @throws JsonMappingException
 *             the If an error occurs when mapping the string into Object
 * @throws IOException
 *             Signals that an I/O exception has occurred.
 */
public static <T> String toJson(final List<T> list)
        throws JsonGenerationException, JsonMappingException, IOException {
    final ByteArrayOutputStream out = new ByteArrayOutputStream();
    final ObjectMapper mapper = new ObjectMapper();

    mapper.writeValue(out, list);

    final byte[] bytes = out.toByteArray();
    out.close();
    return new String(bytes);
}

From source file:Main.java

public static String readFile(Context context, String fileName) {
    if (!exists(context, fileName)) {
        return null;
    }//  w w  w. ja v a2s. co  m
    FileInputStream fis = null;
    String content = null;
    try {
        fis = context.openFileInput(fileName);
        if (fis != null) {

            byte[] buffer = new byte[1024];
            ByteArrayOutputStream arrayOutputStream = new ByteArrayOutputStream();
            while (true) {
                int readLength = fis.read(buffer);
                if (readLength == -1)
                    break;
                arrayOutputStream.write(buffer, 0, readLength);
            }
            fis.close();
            arrayOutputStream.close();
            content = new String(arrayOutputStream.toByteArray());

        }
    } catch (FileNotFoundException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
        content = null;
    } finally {
        try {
            if (fis != null)
                fis.close();
        } catch (IOException ioe) {
            ioe.printStackTrace();
        }
    }
    return content;
}

From source file:Main.java

public static String readFile(String filePath) {
    if (filePath == null || !new File(filePath).exists()) {
        return null;
    }//from   w w w .  j  ava2  s .  c om
    FileInputStream fis = null;
    String content = null;
    try {
        fis = new FileInputStream(filePath);
        if (fis != null) {

            byte[] buffer = new byte[1024];
            ByteArrayOutputStream arrayOutputStream = new ByteArrayOutputStream();
            while (true) {
                int readLength = fis.read(buffer);
                if (readLength == -1)
                    break;
                arrayOutputStream.write(buffer, 0, readLength);
            }
            fis.close();
            arrayOutputStream.close();
            content = new String(arrayOutputStream.toByteArray());

        }
    } catch (FileNotFoundException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
        content = null;
    } finally {
        try {
            if (fis != null)
                fis.close();
        } catch (IOException ioe) {
            ioe.printStackTrace();
        }
    }
    return content;
}