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[], int off, int len) throws IOException 

Source Link

Document

Reads up to len bytes of data from the input stream into an array of bytes.

Usage

From source file:software.uncharted.util.HTTPUtil.java

public static String post(String urlToRead, String postData) {
    URL url;/*from ww w  .jav  a  2s  . com*/
    HttpURLConnection conn;
    try {
        url = new URL(urlToRead);
        conn = (HttpURLConnection) url.openConnection();

        conn.setRequestMethod("POST");
        conn.setDoOutput(true);
        conn.setRequestProperty("Content-Type", "application/json");

        DataOutputStream wr = new DataOutputStream(conn.getOutputStream());
        wr.writeBytes(postData);
        wr.flush();
        wr.close();

        ByteArrayOutputStream buffer = new ByteArrayOutputStream();
        int nRead;
        byte[] data = new byte[16384];
        InputStream is = conn.getInputStream();
        while ((nRead = is.read(data, 0, data.length)) != -1) {
            buffer.write(data, 0, nRead);
        }
        buffer.flush();
        return buffer.toString();
    } catch (Exception e) {
        e.printStackTrace();
        System.err.println("Failed to read URL: " + urlToRead + " <" + e.getMessage() + ">");
    }
    return null;
}

From source file:Main.java

public static int readSCSocketBytesFully(InputStream is, byte[] buffer, int len) throws Exception {
    int ret = 0, curRead = 0;

    while (true) {
        curRead = (len - ret);//  ww w .  ja va2  s  . co  m
        curRead = is.read(buffer, ret, curRead);
        ret += curRead;
        if (ret >= len) {
            break;
        }
        Thread.sleep(20);
    }
    return ret;
}

From source file:Main.java

public static int stmTryRead(InputStream in, byte[] bb, int off, int len) throws IOException {
    int r;//from  ww w  .  j  ava  2  s.co  m
    int l = 0;
    int t = off;
    while ((r = in.read(bb, t, len - l)) >= 0) {
        t += r;
        l += r;
        if (l == len) {
            break;
        }
    }
    return l;
}

From source file:Main.java

public static byte[] getBytesFromInputStream(InputStream in) throws Exception {
    ByteArrayOutputStream outStream = new ByteArrayOutputStream();
    byte[] data = new byte[4096];
    int count;//  ww  w .j a va 2s  .c  o m
    while ((count = in.read(data, 0, 4096)) != -1)
        outStream.write(data, 0, count);
    return outStream.toByteArray();
}

From source file:com.intel.cryptostream.utils.IOUtils.java

public static void readFully(InputStream in, byte buf[], int off, int len) throws IOException {
    int toRead = len;
    while (toRead > 0) {
        int ret = in.read(buf, off, toRead);
        if (ret < 0) {
            throw new IOException("Premature EOF from inputStream");
        }//from   w  w  w  .  j a  v  a 2  s  . com
        toRead -= ret;
        off += ret;
    }
}

From source file:Main.java

public static File getFileFromInputStream(File file, InputStream ins) throws IOException {
    OutputStream os = new FileOutputStream(file);
    int bytesRead = 0;
    byte[] buffer = new byte[8192];
    while ((bytesRead = ins.read(buffer, 0, 8192)) != -1) {
        os.write(buffer, 0, bytesRead);//from  w w  w . j  a v a2 s.  com
    }
    os.close();
    ins.close();
    return file;
}

From source file:Main.java

public static byte[] getByteArray(InputStream is) throws IOException {
    ByteArrayOutputStream buffer = new ByteArrayOutputStream();
    int nRead;// ww 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

private static byte[] getBytes(InputStream is) {
    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    byte[] b = new byte[2048];
    int len = 0;/*from   w w w .  j  a  va2s.  c  om*/
    try {
        while ((len = is.read(b, 0, 2048)) != -1) {
            baos.write(b, 0, len);
            baos.flush();
        }
    } catch (IOException e) {
        e.printStackTrace();
    }
    byte[] bytes = baos.toByteArray();
    return bytes;
}

From source file:Main.java

public static void CopyStream(InputStream is, OutputStream os) {
    final int buffer_size = 1024;
    try {//from   ww  w . j  ava2  s  .co  m
        byte[] bytes = new byte[buffer_size];
        for (;;) {
            int count = is.read(bytes, 0, buffer_size);
            if (count == -1)
                break;
            os.write(bytes, 0, count);
        }
    } catch (Exception ex) {
    }
}

From source file:Main.java

public static void copyStream(InputStream is, OutputStream os) {
    final int buffer_size = 1024;
    try {/*from w  w  w . jav  a 2  s  .com*/
        byte[] bytes = new byte[buffer_size];
        for (;;) {
            int count = is.read(bytes, 0, buffer_size);
            if (count == -1)
                break;
            os.write(bytes, 0, count);
        }
    } catch (Exception ex) {
    }
}