Example usage for java.io InputStream read

List of usage examples for java.io InputStream read

Introduction

In this page you can find the example usage for java.io InputStream read.

Prototype

public int read(byte b[]) throws IOException 

Source Link

Document

Reads some number of bytes from the input stream and stores them into the buffer array b.

Usage

From source file:Main.java

private static byte[] readFully(InputStream input) throws IOException {
    byte[] buffer = new byte[8192];
    int bytesRead;
    ByteArrayOutputStream output = new ByteArrayOutputStream();
    while ((bytesRead = input.read(buffer)) != -1) {
        output.write(buffer, 0, bytesRead);
    }/*  ww w . jav  a 2s  . c o m*/
    return output.toByteArray();
}

From source file:ZipHelper.java

private static void streamCopy(InputStream is, OutputStream os) throws IOException {
    byte[] buffer = new byte[BUFFER_SIZE];
    int len;//from   w  w  w.j  a v a 2 s  .  com
    while ((len = is.read(buffer)) > 0) {
        os.write(buffer, 0, len);
    }
}

From source file:Main.java

public static String readInputStreamAsString(InputStream is) throws IOException {
    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    byte[] bytes = new byte[4096];
    int lenRead;/*from   w  ww  .jav  a2 s  .  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

/**
 * Write stream to bytes array./*w ww .j av a2 s . com*/
 *
 * @param source stream
 * @return bytes array
 */
public static byte[] streamToBytes(InputStream source) {
    ByteArrayOutputStream result = new ByteArrayOutputStream();
    byte[] buffer = new byte[1024];
    int l;
    try {
        while ((l = source.read(buffer)) >= 0) {
            result.write(buffer, 0, l);
        }
    } catch (IOException e) {
        throw new RuntimeException(e);
    }
    return result.toByteArray();
}

From source file:Main.java

public static byte[] getBytes(InputStream inStream) throws Exception {
    ByteArrayOutputStream outStream = new ByteArrayOutputStream();
    byte[] buffer = new byte[1024];
    int len = 0;/*from www .j av a  2  s . c om*/
    while ((len = inStream.read(buffer)) != -1) {
        outStream.write(buffer, 0, len);
    }
    outStream.close();
    inStream.close();
    return outStream.toByteArray();
}

From source file:Main.java

/**
 * Copy the content of the input stream into the output stream, using a temporary byte array
 * buffer whose size is defined by {@link #IO_BUFFER_SIZE}.
 *
 * @param in The input stream to copy from.
 * @param out The output stream to copy to.
 * @return the total length copied//from ww  w.  ja v a 2s.  com
 * @throws IOException If any error occurs during the copy.
 */
public static long copy(final InputStream in, final OutputStream out) throws IOException {
    long length = 0;
    final byte[] b = new byte[IO_BUFFER_SIZE];
    int read;
    while ((read = in.read(b)) != -1) {
        out.write(b, 0, read);
        length += read;
    }
    return length;
}

From source file:Main.java

protected static void copyFile(InputStream inStream, OutputStream outStream) throws IOException {
    byte[] buf = new byte[2048];
    int len;//  w  w  w.  j  a  v  a2 s.  c o  m
    while ((len = inStream.read(buf)) > 0) {
        outStream.write(buf, 0, len);
    }
}

From source file:Main.java

public static byte[] readStream(InputStream inStream) throws Exception {
    ByteArrayOutputStream outSteam = new ByteArrayOutputStream();
    byte[] buffer = new byte[1024];
    int len = -1;
    while ((len = inStream.read(buffer)) != -1) {
        outSteam.write(buffer, 0, len);//from w w w .  j ava 2 s.com
    }
    outSteam.close();
    inStream.close();
    return outSteam.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 = 0;/*from w ww  .ja  va2s. c o m*/
    while ((len = inStream.read(buffer)) != -1) {
        outStream.write(buffer, 0, len);
    }
    outStream.close();
    inStream.close();
    return outStream.toByteArray();
}

From source file:br.edu.ifpb.sislivros.model.RequisicaoDeImg.java

public static void inserirImagem(FileItem item, String realPath, String nomeDaImagem) throws IOException {

    //Pegar o diretorio /imagensPerfil dentro do diretorio atual
    String diretorio = realPath + "/";

    //Criar diretorio caso no exista;
    File f = new File(diretorio);

    if (!f.exists()) {
        f.mkdir();//w w w  .j  a  va2s.  c  om
    }

    //Mandar o arquivo para o diretorio informado
    f = new File(diretorio + nomeDaImagem + ".jpg");

    try {
        FileOutputStream output = new FileOutputStream(f);
        InputStream is = item.getInputStream();

        byte[] buffer = new byte[2048];

        int nLidos;

        while ((nLidos = is.read(buffer)) >= 0) {
            output.write(buffer, 0, nLidos);
        }

        output.flush();
    } finally {

    }

}