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 byte[] getByteArrayFromInputStream(InputStream is) throws IOException {
    ByteArrayOutputStream baos = new ByteArrayOutputStream(10000);
    byte[] buffer = new byte[10000];
    int bytes;//from  ww w  . j av a  2s. c  om
    while ((bytes = is.read(buffer)) != -1) {
        baos.write(buffer, 0, bytes);
    }
    is.close();
    return baos.toByteArray();
}

From source file:StreamsUtils.java

public static byte[] readBytes(InputStream stream) throws IOException {
    ByteArrayOutputStream b = new ByteArrayOutputStream();
    int readedBytes;
    byte[] buf = new byte[1024];
    while ((readedBytes = stream.read(buf)) > 0) {
        b.write(buf, 0, readedBytes);
    }/*from   ww w.  ja v  a 2s. c o  m*/
    b.close();
    return b.toByteArray();
}

From source file:StreamsUtils.java

public static String readString(InputStream stream) throws IOException {
    ByteArrayOutputStream b = new ByteArrayOutputStream();
    int readedBytes;
    byte[] buf = new byte[1024];
    while ((readedBytes = stream.read(buf)) > 0) {
        b.write(buf, 0, readedBytes);
    }/*from w  w  w. j  a  va  2s  . c o  m*/
    b.close();
    return b.toString();
}

From source file:Main.java

public static byte[] stream2ByteArray(InputStream inStream) throws IOException {
    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    int len = 0;/* w ww  . j  a v a  2s.  c  o  m*/
    byte[] buffer = new byte[1024 * 10];
    while ((len = inStream.read(buffer)) != -1) {
        baos.write(buffer, 0, len);
    }
    byte[] result = baos.toByteArray();
    baos.close();
    inStream.close();
    return result;
}

From source file:Main.java

private static String readFully(InputStream inputStream) throws IOException {
    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    byte[] buffer = new byte[1024];
    int length = 0;
    while ((length = inputStream.read(buffer)) != -1) {
        baos.write(buffer, 0, length);
    }/* w w w  .j  a v a2s.c om*/
    return new String(baos.toByteArray());
}

From source file:Main.java

public static String readStream(InputStream inStream) throws Exception {
    ByteArrayOutputStream outStream = new ByteArrayOutputStream();
    byte[] buffer = new byte[1024];
    int len = -1;
    while ((len = inStream.read(buffer)) != -1) {
        outStream.write(buffer, 0, len);
    }/*from  w  w  w  .j av a2 s. c  o m*/
    outStream.close();
    inStream.close();
    return outStream.toString();
}

From source file:Main.java

private static String inputStream2String(InputStream is) {
    try {/*from   w w w  . ja v  a2s  .  co  m*/
        ByteArrayOutputStream baos = new ByteArrayOutputStream();
        byte[] array = new byte[1024];
        int len;
        while ((len = is.read(array, 0, array.length)) != -1) {
            baos.write(array, 0, len);
        }
        return baos.toString();
    } catch (IOException e) {
        e.printStackTrace();
    }
    return null;
}

From source file:Main.java

public static byte[] InputStreamTOByteArray(InputStream in) throws IOException {
    ByteArrayOutputStream outStream = new ByteArrayOutputStream();
    int count = -1;
    byte[] data = new byte[BUFFER_SIZE];
    while ((count = in.read(data, 0, BUFFER_SIZE)) != -1) {
        outStream.write(data, 0, count);
    }//  w w  w. j a  v  a  2  s .  co  m
    data = null;
    outStream.close();
    return outStream.toByteArray();

}

From source file:Main.java

public static byte[] readFully(File file) throws IOException {
    final InputStream in = new BufferedInputStream(new FileInputStream(file));
    try {/*from   ww  w.j a va 2s.com*/
        ByteArrayOutputStream bytes = new ByteArrayOutputStream();
        byte[] buffer = new byte[1 << 20];
        int count;
        while ((count = in.read(buffer)) != -1) {
            bytes.write(buffer, 0, count);
        }
        return bytes.toByteArray();
    } finally {
        in.close();
    }
}

From source file:Main.java

public static String imgCacheRead(Context context, String cacheImgFileName) {
    int len = 1024;
    byte[] buffer = new byte[len];
    try {/*from   www. j a  v a 2s.  com*/
        FileInputStream fis = context.openFileInput(cacheImgFileName);
        ByteArrayOutputStream baos = new ByteArrayOutputStream();
        int nrb = fis.read(buffer, 0, len); // read up to len bytes
        while (nrb != -1) {
            baos.write(buffer, 0, nrb);
            nrb = fis.read(buffer, 0, len);
        }
        buffer = baos.toByteArray();
        fis.close();
    } catch (FileNotFoundException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    }

    try {
        return new String(buffer, "utf-8");
    } catch (UnsupportedEncodingException e) {

        return null;
    }
}