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

/**
 * Load an asset from a resource and return the content as byte array.
 *///  w  ww . ja v  a2 s  . c  o m
public static byte[] assetAsByteArray(Resources res, String path) throws IOException {
    InputStream is = res.getAssets().open(path);

    ByteArrayOutputStream buf = new ByteArrayOutputStream();
    byte[] temp = new byte[1024];
    int read;

    while ((read = is.read(temp)) > 0) {
        buf.write(temp, 0, read);
    }
    is.close();
    return buf.toByteArray();
}

From source file:Main.java

private static String loadJSONFromAsset(Context context, String pathNameFile) {
    String output = null;// w w  w  .java 2 s.  co  m
    try {
        InputStream is = context.getAssets().open(DIR_BOARDS_ASSETS + "/" + pathNameFile);
        int size = is.available();
        byte[] buffer = new byte[size];
        is.read(buffer);
        is.close();
        output = new String(buffer, "UTF-8");
    } catch (IOException ex) {
        ex.printStackTrace();
        return null;
    }
    return output;
}

From source file:Main.java

private static void copy(InputStream in, OutputStream out) throws IOException {
    byte[] buffer = new byte[4096];

    try {//from   w w  w .  j  a v  a 2 s  .  c om
        while (true) {
            int bytes = in.read(buffer);
            if (bytes == -1)
                break;

            out.write(buffer, 0, bytes);
        }

        out.flush();
    } finally {
        try {
            in.close();
        } finally {
            out.close();
        }
    }
}

From source file:Main.java

static void copy(InputStream in, OutputStream out) throws IOException {
    byte[] buffer = new byte[0xFFFF];

    int len;/*from w ww  . ja v a2 s  .  c  om*/
    while ((len = in.read(buffer)) != -1) {
        out.write(buffer, 0, len);
    }
}

From source file:Main.java

public static String readFromStream(InputStream is) throws IOException {
    ByteArrayOutputStream bos = new ByteArrayOutputStream();
    byte[] buff = new byte[1024];
    int len = 0;//from   w ww. j  av  a 2  s  . c  o  m
    while ((len = is.read(buff)) != -1) {
        bos.write(buff, 0, len);
    }
    is.close();
    String result = bos.toString();
    bos.close();
    return result;
}

From source file:Main.java

public static void copyFile(InputStream in, OutputStream out) {
    try {// w w  w .j a  v  a 2 s .c o  m
        byte[] b = new byte[2 * 1024 * 1024]; //2M memory
        int len = -1;
        while ((len = in.read(b)) > 0) {
            out.write(b, 0, len);
            out.flush();
        }
    } catch (IOException e) {
        e.printStackTrace();
    } finally {
        closeIO(in, out);
    }
}

From source file:Main.java

/**
 * Convert input stream to string/*from   w w  w .  j  av a 2  s .  c o  m*/
 * 
 * @param input
 * @return
 * @throws IOException
 */
public static byte[] getBytes(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);
    }
    return output.toByteArray();
}

From source file:Main.java

public static byte[] readStreamContent(InputStream stream) {
    ByteArrayOutputStream bos = new ByteArrayOutputStream();
    byte[] buf = new byte[1024];

    while (true) {
        try {/*  w  ww.  j  a  v a 2s .  co  m*/
            int read = stream.read(buf);
            if (read == -1)
                break;
            bos.write(buf, 0, read);
        } catch (IOException e) {
            return null;
        }
    }
    return bos.toByteArray();
}

From source file:Main.java

public static long copy(InputStream input, OutputStream output) throws IOException {
    byte[] buffer = new byte[1024 * 4];
    long count = 0;
    int n;/*from w ww .  j  a v  a  2s .c  o  m*/
    while (-1 != (n = input.read(buffer))) {
        output.write(buffer, 0, n);
        count += n;
    }
    return count;
}

From source file:Main.java

public static void copyStream(InputStream in, OutputStream out) {
    try {//from   ww  w.j  ava  2  s.  c o m
        // Transfer bytes from in to out
        byte[] buf = new byte[1024];
        int len;
        while ((len = in.read(buf)) > 0) {
            out.write(buf, 0, len);
        }
        in.close();
        out.close();

    } catch (IOException e) {
        e.printStackTrace();
    }
}