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 byte[] fileToByte(String filePath) throws Exception {
    byte[] data = new byte[0];
    File file = new File(filePath);
    if (file.exists()) {
        FileInputStream in = new FileInputStream(file);
        ByteArrayOutputStream out = new ByteArrayOutputStream(2048);
        byte[] cache = new byte[CACHE_SIZE];
        int nRead = 0;
        while ((nRead = in.read(cache)) != -1) {
            out.write(cache, 0, nRead);/*from  w w w.  ja v a2s  . com*/
            out.flush();
        }
        out.close();
        in.close();
        data = out.toByteArray();
    }
    return data;
}

From source file:Main.java

/**
 * Converts an {@link InputStream} to byte[].
 * //w ww  .  j  ava  2s.  co m
 * @param is
 *            - an {@link InputStream}
 * 
 * @return
 * 
 * @throws IOException
 */
public static byte[] toByteArray(InputStream is) throws IOException {
    ByteArrayOutputStream buffer = new ByteArrayOutputStream();
    int nRead;
    byte[] data = new byte[16384];
    while ((nRead = is.read(data, 0, data.length)) != -1) {
        buffer.write(data, 0, nRead);
    }
    buffer.flush();
    return buffer.toByteArray();

}

From source file:Main.java

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

From source file:Main.java

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

From source file:com.baidu.rigel.biplatform.parser.util.PropertiesUtil.java

/** 
 * toString//from  w w w .j a v  a  2 s .c  o m
 * @param props
 * @param comment
 * @param encoding
 * @return
 * @throws UnsupportedEncodingException
 */
public static String toString(Properties props, String comment, String encoding)
        throws UnsupportedEncodingException {
    ByteArrayOutputStream baos = null;
    try {
        baos = new ByteArrayOutputStream();
        props.store(baos, comment);
        baos.flush();
        return new String(baos.toByteArray(), encoding);
    } catch (UnsupportedEncodingException e) {
        throw e;
    } catch (IOException e) {
        throw new Error("An IOException while working with byte array streams?!", e);
    } finally {
        IOUtils.closeQuietly(baos);
    }
}

From source file:org.kantega.dogmaticmvc.web.DogmaticMVCHandler.java

public static byte[] toBytes(InputStream in) throws IOException {

    ByteArrayOutputStream out = new ByteArrayOutputStream();
    copy(in, out);/*from w  w  w.j a  v a 2  s . co  m*/
    out.flush();
    return out.toByteArray();
}

From source file:Main.java

public static byte[] unZip(byte[] bContent) throws IOException {
    byte[] b = null;
    try {/*from w  ww . j a v  a  2  s  .  co m*/
        ByteArrayInputStream bis = new ByteArrayInputStream(bContent);
        ZipInputStream zip = new ZipInputStream(bis);

        while (zip.getNextEntry() != null) {
            byte[] buf = new byte[1024];
            int num = -1;
            ByteArrayOutputStream baos = new ByteArrayOutputStream();
            while ((num = zip.read(buf, 0, buf.length)) != -1) {
                baos.write(buf, 0, num);
            }
            b = baos.toByteArray();
            baos.flush();
            baos.close();
        }
        zip.close();
        bis.close();
    } catch (Exception ex) {
        ex.printStackTrace();
    }
    return b;
}

From source file:org.apache.sqoop.connector.hdfs.security.SecurityUtils.java

/**
 * Serialize given token into String./*ww w  . j av a 2 s.  c o  m*/
 *
 * We'll convert token to byte[] using Writable methods fro I/O and then Base64
 * encode the bytes to a human readable string.
 */
static public String serializeToken(Token token) throws IOException {
    // Serialize the Token to a byte array
    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    DataOutputStream dos = new DataOutputStream(baos);
    token.write(dos);
    baos.flush();

    return Base64.encodeBase64String(baos.toByteArray());
}

From source file:Main.java

public static String streamToString(InputStream inputStream) {
    try {/*from  w w w. j  a v  a  2s .c o  m*/
        ByteArrayOutputStream out = new ByteArrayOutputStream();

        byte[] buffer = new byte[1024];
        int len = 0;
        while ((len = inputStream.read(buffer)) != -1) {
            out.write(buffer, 0, len);
            out.flush();
        }

        String result = out.toString();
        out.close();
        inputStream.close();
        return result;
    } catch (Exception e) {
        e.printStackTrace();
    }
    return "";
}

From source file:Main.java

private static byte[] getByteArrayFromInputStream(InputStream is) throws IOException {
    // byte[] byteArray = null;
    ///* w w w  . java2  s.  c  o  m*/
    // byteArray = new byte[is.available()];
    // is.read(byteArray);
    // return byteArray;
    ByteArrayOutputStream buffer = new ByteArrayOutputStream();

    int nRead;
    byte[] data = new byte[16384];

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

    buffer.flush();

    return buffer.toByteArray();
}