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 void copy(InputStream in, OutputStream out) throws IOException {
    byte[] buf = new byte[1024];
    int len;//from   w ww.  ja  v  a  2s.c  o m
    while ((len = in.read(buf)) > 0) {
        out.write(buf, 0, len);
    }
    out.flush();
    in.close();
    out.close();
}

From source file:Main.java

public static String loadJSONFromAsset(Context context, String fileName) {
    String json;/*from  ww w.j av  a2  s. co  m*/
    try {

        InputStream is = context.getAssets().open(fileName);
        int size = is.available();
        byte[] buffer = new byte[size];
        is.read(buffer);
        is.close();
        json = new String(buffer, "UTF-8");
    } catch (IOException ex) {
        ex.printStackTrace();
        return null;
    }
    return json;
}

From source file:Main.java

/**
 * A method to load x.json files from assets folder and convert them
 * into a string.//from   ww w . ja va2s.com
 *
 * @param c        A contect
 * @param fileName The json file in assets folder which to load json data from
 * @return
 */
public static String loadJSONFromAsset(Context c, String fileName) {
    String json = null;
    try {
        InputStream is = c.getAssets().open(fileName);
        int size = is.available();
        byte[] buffer = new byte[size];
        is.read(buffer);
        is.close();
        json = new String(buffer, "UTF-8");
    } catch (IOException ex) {
        ex.printStackTrace();
        return null;
    }
    return json;
}

From source file:Main.java

public final static long copy(InputStream inp, OutputStream out) throws IOException {
    int nread;/* w w  w .ja v a  2s.  c  o m*/
    byte[] buf = new byte[4096];
    long total = 0;
    while ((nread = inp.read(buf)) > 0) {
        total += nread;
        out.write(buf, 0, nread);
    }
    return total;
}

From source file:Main.java

public static byte[] readAll(InputStream is) throws Exception {
    ByteArrayOutputStream baos = new ByteArrayOutputStream(1024);
    byte[] buf = new byte[1024];
    int c = is.read(buf);
    while (-1 != c) {
        baos.write(buf, 0, c);//from ww  w. ja  v  a 2 s. c  o m
        c = is.read(buf);
    }
    baos.flush();
    baos.close();
    return baos.toByteArray();
}

From source file:Main.java

private static long copyLarge(InputStream input, OutputStream output, byte[] buffer) throws IOException {
    long count = 0;
    int n;/* ww w  . j a va  2 s. c  om*/
    while (EOF != (n = input.read(buffer))) {
        output.write(buffer, 0, n);
        count += n;
    }
    return count;
}

From source file:Main.java

public static String loadJSONFromAsset(Context context, String fileName) {
    String json = null;//  w w  w.j  a  v  a 2s  .  c  om
    try {
        InputStream is = context.getAssets().open(fileName);
        int size = is.available();
        byte[] buffer = new byte[size];
        is.read(buffer);
        is.close();
        json = new String(buffer, "UTF-8");
    } catch (IOException ex) {
        Log.d(TAG, "Exception Occurred : " + ex.getMessage());
        return null;
    }
    return json;

}

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.
 *
 * @throws java.io.IOException If any error occurs during the copy.
 *//*from  w  w w  .j a v  a 2 s . com*/
public static void copy(InputStream in, OutputStream out) throws IOException {
    byte[] b = new byte[IO_BUFFER_SIZE];
    int read;
    while ((read = in.read(b)) != -1) {
        out.write(b, 0, read);
    }
}

From source file:Main.java

public static void transfer(InputStream in, OutputStream out) throws IOException {
    byte[] buf = new byte[1024 * 1024];
    int len;// www  . j a  va 2  s.co  m
    while ((len = in.read(buf)) > 0)
        out.write(buf, 0, len);
}

From source file:Main.java

public static void writeStream(InputStream inputStream, OutputStream outputStream) {
    byte[] buffer = new byte[1024];
    int len = 0;/*from  w ww. j  a v  a2 s . co m*/
    try {
        while ((len = inputStream.read(buffer)) != -1) {
            outputStream.write(buffer, 0, len);
        }
        outputStream.flush();
    } catch (IOException e) {
        e.printStackTrace();
    }

}