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[] readInputStream(InputStream inStream) throws Exception {
    ByteArrayOutputStream outSteam = new ByteArrayOutputStream();
    byte[] buffer = new byte[4096];
    int len = 0;/*from   www  .  jav a  2s. c om*/
    while ((len = inStream.read(buffer)) != -1) {
        outSteam.write(buffer, 0, len);
    }
    outSteam.close();
    inStream.close();
    return outSteam.toByteArray();
}

From source file:Main.java

public static byte[] unGzip(byte[] data) {
    byte[] b = null;
    try {//  w ww  .ja  v a 2  s .  c  o  m
        ByteArrayInputStream bis = new ByteArrayInputStream(data);
        GZIPInputStream gzip = new GZIPInputStream(bis);
        byte[] buf = new byte[1024];
        int num = -1;
        ByteArrayOutputStream baos = new ByteArrayOutputStream();
        while ((num = gzip.read(buf, 0, buf.length)) != -1) {
            baos.write(buf, 0, num);
        }
        b = baos.toByteArray();
        baos.flush();
        baos.close();
        gzip.close();
        bis.close();
    } catch (Exception ex) {
        ex.printStackTrace();
    }
    return b;
}

From source file:Main.java

private static byte[] getFileBytes(String filePath) {
    byte[] buffer = null;
    try {//from   w w  w  .ja  v  a  2  s . c  o  m
        File file = new File(filePath);
        FileInputStream fis = new FileInputStream(file);
        ByteArrayOutputStream bos = new ByteArrayOutputStream(1024);
        byte[] b = new byte[1024];
        int n;
        while ((n = fis.read(b)) != -1) {
            bos.write(b, 0, n);
        }
        fis.close();
        bos.close();
        buffer = bos.toByteArray();
    } catch (FileNotFoundException e) {

    } catch (IOException e) {

    }
    return buffer;
}

From source file:Main.java

public static byte[] getBytesFromFile(File f) {
    if (f == null) {
        return null;
    }//from ww w  . j a v  a 2  s  .  com
    try {
        FileInputStream stream = new FileInputStream(f);
        ByteArrayOutputStream out = new ByteArrayOutputStream(1000);
        byte[] b = new byte[1000];
        for (int n; (n = stream.read(b)) != -1;) {
            out.write(b, 0, n);
        }
        stream.close();
        out.close();
        return out.toByteArray();
    } 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//from   ww  w . ja va  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

public static byte[] toByteArray(Reader input, String encoding) throws IOException {
    ByteArrayOutputStream output = new ByteArrayOutputStream();
    write(input, output, encoding);/*  w  w w .  j a va2 s . com*/
    output.close();
    return output.toByteArray();
}

From source file:Main.java

public static String readTextFile(InputStream inputStream) {
    ByteArrayOutputStream outputStream = new ByteArrayOutputStream();

    byte buf[] = new byte[1024];
    int len;/*  w ww  . j  a  va2s . c  om*/
    try {
        while ((len = inputStream.read(buf)) != -1) {
            outputStream.write(buf, 0, len);
        }
        outputStream.close();
        inputStream.close();
    } catch (IOException e) {

    }
    return outputStream.toString();
}

From source file:Main.java

public static byte[] getBytes(Serializable obj) throws IOException {
    ByteArrayOutputStream bout = new ByteArrayOutputStream();
    ObjectOutputStream out = new ObjectOutputStream(bout);
    out.writeObject(obj);//from www.j  a  v a 2 s.com
    out.flush();
    byte[] bytes = bout.toByteArray();
    bout.close();
    out.close();
    return bytes;
}

From source file:Main.java

public static byte[] getBytes(String filePath) {
    byte[] buffer = null;
    try {/*  w ww . j av a2  s  .c  om*/
        File file = new File(filePath);
        FileInputStream fis = new FileInputStream(file);
        ByteArrayOutputStream bos = new ByteArrayOutputStream(1000);
        byte[] b = new byte[1000];
        int n;
        while ((n = fis.read(b)) != -1) {
            bos.write(b, 0, n);
        }
        fis.close();
        bos.close();
        buffer = bos.toByteArray();
    } catch (FileNotFoundException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    }
    return buffer;
}

From source file:Main.java

public static byte[] inputStreamToByte(InputStream is) {
    try {/* w  w w .  jav  a 2 s .  c  o m*/
        ByteArrayOutputStream bytestream = new ByteArrayOutputStream();
        int ch;
        while ((ch = is.read()) != -1) {
            bytestream.write(ch);
        }
        byte imgdata[] = bytestream.toByteArray();
        bytestream.close();
        return imgdata;
    } catch (Exception e) {
        e.printStackTrace();
    }
    return null;
}