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

public static byte[] readStream(InputStream inputStream) throws Exception {
    ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
    byte[] buffer = new byte[1024];
    int len = 0;//from www  . jav a 2s  .  c o  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 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);/*from w w  w.  j  a v  a2  s .  com*/
    }
    byteOus.flush();
    is.close();
    return byteOus.toString();
}

From source file:Bzip2Uncompress.java

/**
 * Copy bytes from an <code>InputStream</code> to an <code>OutputStream</code>.
 *//*from w w  w  .  ja v a  2s.com*/
private static void copy(final InputStream input, final OutputStream output) throws IOException {
    final byte[] buffer = new byte[8024];
    int n = 0;
    while (-1 != (n = input.read(buffer))) {
        output.write(buffer, 0, n);
    }
}

From source file:Main.java

public static String responseAsString(HttpResponse response) throws IOException {

    StatusLine status = response.getStatusLine();
    if (status.getStatusCode() != HTTP_STATUS_OK) {
        throw new IOException("Invalid response from server! " + status.toString());
    }//w  w  w .j a  va 2 s  .  com

    //do the herdy gerdy and convert the response to a String
    InputStream ist = response.getEntity().getContent();

    ByteArrayOutputStream content = new ByteArrayOutputStream();
    int readCount = 0;
    while ((readCount = ist.read(buff)) != -1)
        content.write(buff, 0, readCount);

    String thePagesContent = EncodingUtils.getString(content.toByteArray(), "UTF-8");

    return thePagesContent;
}

From source file:Main.java

/**
 * /*from  w w w.ja  v a  2  s.c om*/
 * @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 inStream) throws Exception {
    byte[] buffer = new byte[1024];
    int len = -1;
    ByteArrayOutputStream outStream = new ByteArrayOutputStream();
    while ((len = inStream.read(buffer)) != -1) {
        outStream.write(buffer, 0, len);
    }/*from w  w  w.  java 2s .  c  o  m*/
    byte[] data = outStream.toByteArray();
    outStream.close();
    inStream.close();
    return data;

}

From source file:at.uni_salzburg.cs.ckgroup.cscpp.utils.HttpQueryUtils.java

public static String simpleQuery(String url) throws IOException {

    HttpClient httpclient = new DefaultHttpClient();
    HttpGet httpget = new HttpGet(url);
    HttpResponse response;/*from  w  ww . j a  v  a 2 s.co m*/

    response = httpclient.execute(httpget);
    ByteArrayOutputStream bo = new ByteArrayOutputStream();

    HttpEntity entity = response.getEntity();
    if (entity != null) {
        InputStream instream = entity.getContent();
        int l;
        byte[] tmp = new byte[2048];
        while ((l = instream.read(tmp)) != -1) {
            bo.write(tmp, 0, l);
        }
    }

    return bo.toString();
}

From source file:ispyb.common.util.IspybFileUtils.java

public static byte[] getFile(String filePath) throws IOException {
    File file = new File(filePath);
    if (file.exists()) {
        byte[] fileInBytes = new byte[(int) file.length()];
        InputStream inputStream = null;
        try {//w w w . ja va 2s.  c  o  m
            inputStream = new FileInputStream(file);
            inputStream.read(fileInBytes);
        } catch (FileNotFoundException e) {
            e.printStackTrace();
            throw e;
        } finally {
            inputStream.close();
        }
        return fileInBytes;
    }
    return null;
}

From source file:Main.java

public static byte[] getBytes(InputStream in) {

    try {//w w w. j a  v a2  s.co 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

/**
 * Transfers all bytes that can be read from <tt>in</tt> to <tt>out</tt>.
 *
 * @param in The InputStream to read data from.
 * @param out The OutputStream to write data to.
 * @return The total number of bytes transfered.
 *//* ww w  .j av a  2  s . c o m*/
public static final long transfer(InputStream in, OutputStream out) throws IOException {
    long totalBytes = 0;
    int bytesInBuf = 0;
    byte[] buf = new byte[4096];

    while ((bytesInBuf = in.read(buf)) != -1) {
        out.write(buf, 0, bytesInBuf);
        totalBytes += bytesInBuf;
    }

    return totalBytes;
}