Example usage for java.io ByteArrayOutputStream write

List of usage examples for java.io ByteArrayOutputStream write

Introduction

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

Prototype

public synchronized void write(byte b[], int off, int len) 

Source Link

Document

Writes len bytes from the specified byte array starting at offset off to this ByteArrayOutputStream .

Usage

From source file:Main.java

public static String getStringFromInputStream(InputStream is) {
    if (is == null) {
        return null;
    }/*from w  ww  . jav a 2s .  com*/
    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    byte[] buffer = new byte[4 * 1024];
    int len = 0;
    try {
        while ((len = is.read(buffer)) != -1) {
            baos.write(buffer, 0, len);
        }
        is.close();
    } catch (Exception e) {
    }
    return baos.toString();
}

From source file:Main.java

public static String InputStream2String(final InputStream is) {
    try {/*from w ww .j  a v  a  2  s  .c o m*/
        final ByteArrayOutputStream baos = new ByteArrayOutputStream();
        byte[] buffer = new byte[1024];
        int length = 0;
        while ((length = is.read(buffer)) != -1) {
            baos.write(buffer, 0, length);
        }
        return baos.toString();
    } catch (IOException e) {
        e.printStackTrace();
    }
    return null;
}

From source file:Main.java

public static String converStream2String(InputStream is, String charset) throws UnsupportedEncodingException {
    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    byte[] buffer = new byte[512];
    int len = 0;/*from  ww  w.  java2  s.  c o  m*/

    try {
        while ((len = is.read(buffer)) != -1) {
            baos.write(buffer, 0, len);
        }
        is.close();
        return new String(baos.toByteArray());
    } catch (Exception e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
    return new String(baos.toByteArray(), charset);
}

From source file:Main.java

/**
 * unzip some gzip compressed data/*from  w w w  .  ja  v  a 2  s.co m*/
 * @param bytes the data to uncompress
 * @return the uncompressed data
 */
public static String gzipDecompress(byte[] bytes) {
    ByteArrayInputStream stream = new ByteArrayInputStream(bytes);
    try {
        GZIPInputStream gs = new GZIPInputStream(stream);
        BufferedInputStream bufis = new BufferedInputStream(gs);
        ByteArrayOutputStream bos = new ByteArrayOutputStream();
        byte[] buf = new byte[1024];
        int len;
        while ((len = bufis.read(buf)) > 0) {
            bos.write(buf, 0, len);
        }
        return bos.toString();
    } catch (IOException e) {
        return null;
    }
}

From source file:Main.java

/**
 * Read a string content from a stream.//  w ww . j  a  v  a  2s. c  om
 * @param in the stream to read from.
 * @return the raw bytes extracted from the stream.
 * @throws Exception if any error occurs.
 */
static byte[] readBytes(InputStream in) throws Exception {
    ByteArrayOutputStream out = new ByteArrayOutputStream();
    byte[] buffer = new byte[2048];
    int n;
    while ((n = in.read(buffer)) > 0) {
        out.write(buffer, 0, n);
    }
    out.close();
    return out.toByteArray();
}

From source file:Main.java

public static byte[] streamToByteArray(InputStream stream) throws IOException {
    ByteArrayOutputStream bytes = new ByteArrayOutputStream();
    byte[] buffer = new byte[1024];
    try {//from  w ww.  j a  va  2  s  .c om
        int count;
        while ((count = stream.read(buffer)) != -1) {
            bytes.write(buffer, 0, count);
        }
        return bytes.toByteArray();
    } finally {
        stream.close();
        bytes.close();
    }
}

From source file:Main.java

/**
 * Converts an {@link InputStream} to byte[].
 * /*ww w .  j  a va2 s . c  o 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 String readInputStreamAsString(InputStream is) throws IOException {
    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    byte[] bytes = new byte[4096];
    int lenRead;/*ww  w  .  j  a v  a2s.co  m*/
    while ((lenRead = is.read(bytes)) != -1) {
        if (lenRead > 0)
            baos.write(bytes, 0, lenRead);
    }

    if (baos.size() > 0)
        return baos.toString("utf-8");
    return null;
}

From source file:Main.java

private static String isStreamString(InputStream is) {
    ByteArrayOutputStream bo = new ByteArrayOutputStream();
    byte[] buf = new byte[1024];
    int len = -1;
    try {/*from   w  w  w .  ja va2s  .  c o  m*/
        while ((len = is.read(buf)) != -1) {
            bo.write(buf, 0, len);
        }
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    } finally {
        if (is != null)
            try {
                is.close();
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
    }
    return replaceBr(new String(bo.toByteArray()));
}

From source file:Main.java

public static final byte[] input2byte(InputStream inStream) throws IOException {
    ByteArrayOutputStream swapStream = new ByteArrayOutputStream();
    byte[] buff = new byte[100];
    int rc = 0;/*from ww  w .j  a v  a 2 s.com*/
    while ((rc = inStream.read(buff, 0, 100)) > 0) {
        swapStream.write(buff, 0, rc);
    }
    byte[] in2b = swapStream.toByteArray();
    return in2b;
}