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 getStringFromInput(InputStream is) throws IOException {
    ByteArrayOutputStream byteOus = new ByteArrayOutputStream();
    byte[] tmp = new byte[1024];
    int size = 0;
    while ((size = is.read(tmp)) != -1) {
        byteOus.write(tmp, 0, size);
    }//w  w w.j  a v a2 s.c o  m
    byteOus.flush();
    is.close();
    return byteOus.toString();
}

From source file:Main.java

/** Lee un flujo de datos de entrada y los recupera en forma de array de
 * bytes. Este método consume pero no cierra el flujo de datos de
 * entrada./* ww w .  j  a  v a 2  s.  com*/
 * @param input
 *        Flujo de donde se toman los datos.
 * @return Los datos obtenidos del flujo.
 * @throws IOException
 *         Cuando ocurre un problema durante la lectura */
public static byte[] getDataFromInputStream(final InputStream input) throws IOException {
    if (input == null) {
        return new byte[0];
    }
    int nBytes = 0;
    final byte[] buffer = new byte[BUFFER_SIZE];
    final ByteArrayOutputStream baos = new ByteArrayOutputStream();
    while ((nBytes = input.read(buffer)) != -1) {
        baos.write(buffer, 0, nBytes);
    }
    return baos.toByteArray();
}

From source file:Main.java

public static byte[] 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);
    }// www.j ava 2s.c o  m
    outstream.close();
    inStream.close();

    return outstream.toByteArray();
}

From source file:Main.java

public static byte[] getByteArray(InputStream is) throws IOException {
    ByteArrayOutputStream buffer = new ByteArrayOutputStream();
    int nRead;//from w w  w  .j  a v  a  2  s .c o m
    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 readFromStream(InputStream is) throws IOException {
    ByteArrayOutputStream bos = new ByteArrayOutputStream();
    byte[] buff = new byte[1024];
    int len = 0;/* w  w w .  j a v a2 s.  c om*/
    while ((len = is.read(buff)) != -1) {
        bos.write(buff, 0, len);
    }
    is.close();
    String result = bos.toString();
    bos.close();
    return result;
}

From source file:Main.java

/**
 * //from ww  w . ja  v  a  2s.c o m
 * @param in
 * @return
 * @throws IOException
 */
public static String readFormStream(InputStream in) throws IOException {
    ByteArrayOutputStream out = new ByteArrayOutputStream();

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

    String result = out.toString();
    in.close();
    out.close();

    return result;
}

From source file:Main.java

public static byte[] readStream(InputStream inputStream) throws Exception {
    ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
    byte[] buffer = new byte[1024];
    int len = 0;/*from  w ww .j a v  a  2 s .co m*/
    while ((len = inputStream.read(buffer)) != -1) {
        outputStream.write(buffer, 0, len);
    }
    outputStream.close();
    inputStream.close();
    return outputStream.toByteArray();
}

From source file:Main.java

public static byte[] getBytes(InputStream in) {

    try {// w  w w. j  a  v  a 2  s. c  o m
        ByteArrayOutputStream baos = new ByteArrayOutputStream();
        byte[] buffer = new byte[8192];
        int len = 0;
        while ((len = in.read(buffer)) != -1)
            baos.write(buffer, 0, len);
        in.close();
        return baos.toByteArray();
    } catch (IOException e) {
        return null;
    }
}

From source file:Main.java

public static ByteArrayOutputStream toByteArrayOutputStream(InputStream is) throws IOException {
    final ByteArrayOutputStream baos = new ByteArrayOutputStream();
    final byte[] buffer = new byte[512];
    int length = 0;
    while ((length = is.read(buffer)) != -1) {
        baos.write(buffer, 0, length);
    }/*www  .  jav a  2 s  . c om*/

    return baos;
}

From source file:Main.java

public static byte[] streamToBytes(InputStream is) {
    ByteArrayOutputStream os = new ByteArrayOutputStream(1024);
    byte[] buffer = new byte[1024];
    int len;//  w w  w.  ja  v  a2s  .  c o  m
    try {
        while ((len = is.read(buffer)) >= 0) {
            os.write(buffer, 0, len);
        }
    } catch (java.io.IOException e) {
    }
    return os.toByteArray();
}