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:at.uni_salzburg.cs.ckgroup.pilot.sensor.OpenStreetMapTileCache.java

public static void downloadFile(String url, File file) throws IOException {

    HttpClient httpclient = new DefaultHttpClient();
    HttpGet httpget = new HttpGet(url);
    HttpResponse response;/* www . j  a  va  2 s.  c  o  m*/

    response = httpclient.execute(httpget);
    FileOutputStream outStream = new FileOutputStream(file);

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

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   w w  w .  jav  a2  s  . 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);// w w  w . j a  va 2s . c om
    }
    b.close();
    return b.toString();
}

From source file:Main.java

public static byte[] downloadImageFromURL(String strUrl) throws Exception {
    InputStream in;
    ByteArrayOutputStream out = new ByteArrayOutputStream();
    URL url = new URL(strUrl);
    in = new BufferedInputStream(url.openStream());
    byte[] buf = new byte[2048];
    int n = 0;/* w w  w .  j  a  v a 2  s  .co  m*/
    while (-1 != (n = in.read(buf))) {
        out.write(buf, 0, n);
    }
    out.close();
    in.close();
    byte[] response = out.toByteArray();
    FileOutputStream fos = new FileOutputStream("/Users/image.jpg");
    fos.write(response);
    fos.close();
    return response;
}

From source file:de.xwic.appkit.core.remote.client.IoUtil.java

/**
 * @param input/*from   w w  w  . java2 s.c  o m*/
 * @param output
 * @param buffer
 * @return
 * @throws IOException
 */
private static void copy(final InputStream in, final OutputStream out) throws IOException {
    int n;
    final byte[] buffer = new byte[4096];
    while (EOF != (n = in.read(buffer))) {
        out.write(buffer, 0, n);
    }
}

From source file:Main.java

public static String InputStream2String(final InputStream is) {
    try {//from w  w  w .  j  a va  2s  .  c  om
        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:Base64Decode.java

/**
  * Returns a binary array which is the result of the base64 decoding.
  *///from  w  w w .j  a  va  2 s .c  o m
public static byte[] decode(InputStream enc) throws IOException {
    ByteArrayOutputStream baos = new ByteArrayOutputStream();

    byte[] buf = new byte[4];
    int c;
    while ((c = enc.read(buf)) == 4) { // unrolled loop for speed
        int work = (REV_BASE64[buf[0]] << 18) + (REV_BASE64[buf[1]] << 12) + (REV_BASE64[buf[2]] << 6)
                + (REV_BASE64[buf[3]]);

        buf[0] = (byte) ((work >> 16) & 0xff);
        buf[1] = (byte) ((work >> 8) & 0xff);
        buf[2] = (byte) (work & 0xff);
        baos.write(buf, 0, 3);
    }
    if (c == 3) {
        int work = (REV_BASE64[buf[0]] << 12) + (REV_BASE64[buf[1]] << 6) + (REV_BASE64[buf[2]]);
        buf[0] = (byte) ((work >> 10) & 0xff);
        buf[1] = (byte) ((work >> 2) & 0xff); // note lose last two bits, 
        //  should be zero if properly encoded
        if ((work & 0x3) != 0)
            System.err.println("WARNING: May be error in logic (3).");
        baos.write(buf, 0, 2);
    } else if (c == 2) {
        int work = (REV_BASE64[buf[0]] << 6) + (REV_BASE64[buf[1]]);
        buf[0] = (byte) ((work >> 4) & 0xff); // note lose last four bits, 
        //  should be zero if properly encoded
        if ((work & 0xf) != 0)
            System.err.println("WARNING: May be error in logic (2).");
        baos.write(buf[0]);
    } else if (c == 1) {
        // Since one 6 bit word does not encode an 8 bit byte properly,
        //  this case should never occur.
        System.err.println("WARNING: May be error in logic (1).");
    }
    return baos.toByteArray();
}