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:com.tremolosecurity.unison.u2f.util.U2fUtil.java

private static String inflate(String saml) throws Exception {
    byte[] compressedData = Base64.decodeBase64(saml);
    ByteArrayInputStream bin = new ByteArrayInputStream(compressedData);

    InflaterInputStream decompressor = new InflaterInputStream(bin, new Inflater(true));
    //decompressor.setInput(compressedData);

    // Create an expandable byte array to hold the decompressed data
    ByteArrayOutputStream bos = new ByteArrayOutputStream(compressedData.length);

    // Decompress the data
    byte[] buf = new byte[1024];
    int len;//from   ww w .j a v  a2s  .  c  o  m
    while ((len = decompressor.read(buf)) > 0) {

        bos.write(buf, 0, len);

    }
    try {
        bos.close();
    } catch (IOException e) {
    }

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

    String decoded = new String(decompressedData);

    return decoded;
}

From source file:Main.java

public static byte[] toByteArray(Object obj) {
    byte[] returnBytes = null;
    ByteArrayOutputStream byteArrayOutStream = new ByteArrayOutputStream();
    ObjectOutput objectOut = null;
    try {//  w ww .jav  a 2  s. co m
        objectOut = new ObjectOutputStream(byteArrayOutStream);
        objectOut.writeObject(obj);
        returnBytes = byteArrayOutStream.toByteArray();
    } catch (java.io.IOException ioe) {
        return null;
    } finally {
        try {
            objectOut.close();
            byteArrayOutStream.close();
        } catch (java.io.IOException ioe) {

        }
    }
    return returnBytes;
}

From source file:com.github.egonw.rrdf.RJenaHelper.java

public static String toString(Model model, String format) throws Exception {
    ByteArrayOutputStream output = new ByteArrayOutputStream();
    model.write(output, format);/*from   ww w  .  jav  a2s  .  c o  m*/
    output.close();
    String result = new String(output.toByteArray());
    return result;
}

From source file:BytesUtil.java

public static byte[] toByteArray(Object obj) throws IOException {
    byte[] bytes = null;
    ByteArrayOutputStream bos = null;
    ObjectOutputStream oos = null;
    try {/*w w w  .  j av  a  2s .  co m*/
        bos = new ByteArrayOutputStream();
        oos = new ObjectOutputStream(bos);
        oos.writeObject(obj);
        oos.flush();
        bytes = bos.toByteArray();
    } finally {
        if (oos != null) {
            oos.close();
        }
        if (bos != null) {
            bos.close();
        }
    }
    return bytes;
}

From source file:brainleg.app.util.AppWeb.java

private static String readFrom(InputStream is) throws IOException {
    ByteArrayOutputStream out = new ByteArrayOutputStream();
    int c;/*from w ww.  ja  v a  2s.c o  m*/
    while ((c = is.read()) != -1) {
        out.write(c);
    }
    String s = out.toString();
    out.close();
    return s;
}

From source file:Main.java

public static int getByteCount(Bitmap bitmap, Bitmap.CompressFormat mCompressFormat) {
    int size = 0;
    ByteArrayOutputStream output = null;
    try {//from www  .  j ava 2  s .co  m
        output = new ByteArrayOutputStream();
        bitmap.compress(mCompressFormat, 100, output);
        byte[] result = output.toByteArray();
        size = result.length;
        result = null;
    } catch (Exception e) {
        e.printStackTrace();
    } finally {
        if (output != null) {
            try {
                output.close();
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }
    return size;
}

From source file:Main.java

public static byte[] gzipDecodeByteArray(byte[] data) {
    GZIPInputStream gzipInputStream = null;
    try {//  ww w .  jav a2s  .  co m
        gzipInputStream = new GZIPInputStream(new ByteArrayInputStream(data));

        ByteArrayOutputStream outputStream = new ByteArrayOutputStream(data.length);
        byte[] buffer = new byte[1024];
        while (gzipInputStream.available() > 0) {
            int count = gzipInputStream.read(buffer, 0, 1024);
            if (count > 0) {
                //System.out.println("Read " + count + " bytes");
                outputStream.write(buffer, 0, count);
            }
        }

        outputStream.close();
        gzipInputStream.close();
        return outputStream.toByteArray();
    } catch (IOException e) {
        e.printStackTrace();
        return null;
    }
}

From source file:br.com.manish.ahy.kernel.util.JPAUtil.java

public static byte[] blobToBytes(Blob blob) {
    byte[] byteData = null;
    try {/* w w w  . j  a va 2  s  .  c  o m*/
        InputStream is = blob.getBinaryStream();
        ByteArrayOutputStream os = new ByteArrayOutputStream(1024);
        byte[] bytes = new byte[512];

        int readBytes;
        while ((readBytes = is.read(bytes)) > 0) {
            os.write(bytes, 0, readBytes);
        }

        byteData = os.toByteArray();

        is.close();
        os.close();
    } catch (Exception e) {
        throw new OopsException(e, "Error reading file data.");
    }
    return byteData;
}

From source file:Main.java

/**
 * Read a file from assets/* w  w w . jav  a  2s  .  c o m*/
 *
 * @return the string from assets
 */

public static String getfromAssets(Context ctx, String file_name) {

    AssetManager assetManager = ctx.getAssets();
    ByteArrayOutputStream outputStream = null;
    InputStream inputStream = null;
    try {
        inputStream = assetManager.open(file_name);
        outputStream = new ByteArrayOutputStream();
        byte buf[] = new byte[1024];
        int len;
        try {
            while ((len = inputStream.read(buf)) != -1) {
                outputStream.write(buf, 0, len);
            }
            outputStream.close();
            inputStream.close();
        } catch (IOException e) {
        }
    } catch (IOException e) {
    }
    return outputStream.toString();

}

From source file:Main.java

private static byte[] mergeBytes(byte[] in, ConcurrentLinkedQueue<byte[]> decompressUnfinishedDataQueue) {

    if (!decompressUnfinishedDataQueue.isEmpty()) {

        ByteArrayOutputStream out = new ByteArrayOutputStream();
        try {//from  w  w  w .j  a v a2 s.  c  o m
            while (!decompressUnfinishedDataQueue.isEmpty()) {
                out.write(decompressUnfinishedDataQueue.poll());
            }
            out.write(in);
            in = out.toByteArray();

        } catch (IOException e) {
            throw new RuntimeException(e);
        } finally {
            try {
                out.close();
            } catch (IOException e) {
            }
        }
    }
    return in;
}