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

private static void copyStream(InputStream is, OutputStream os) throws IOException {
    byte[] buf = new byte[1024];
    int len = 0;/* w  ww .ja  va 2  s  . c om*/
    while ((len = is.read(buf)) != -1) {
        os.write(buf, 0, len);
    }

}

From source file:Main.java

public static void copyFile(InputStream in, OutputStream out) throws IOException {
    byte[] buffer = new byte[1024];
    int read;/*from   ww w  .j a v  a 2s.  c  o m*/
    while ((read = in.read(buffer)) != -1) {
        out.write(buffer, 0, read);
    }
}

From source file:Main.java

/**
 * Get file content by filename/*from  w ww  .  j a v  a2s  .c om*/
 * 
 * @param c
 * @param filename
 * @return content String
 */
public static String getFileContent(Context c, String filename) {
    try {
        InputStream fin = c.getAssets().open(filename);
        byte[] buffer = new byte[fin.available()];
        fin.read(buffer);
        fin.close();
        return new String(buffer);
    } catch (IOException e) {
        Log.e("inspector", e.getLocalizedMessage());
    }
    return "";
}

From source file:Main.java

public static void copyAndClose(InputStream in, OutputStream out) throws IOException {
    byte[] cache = new byte[4096];
    int size;/*from   ww w .  ja  v a 2 s . c o m*/
    while ((size = in.read(cache)) > 0) {
        out.write(cache, 0, size);
    }
    in.close();
    out.close();
}

From source file:Main.java

public static byte[] calculateMd5(String filePath) throws IOException {
    try {/*w  ww .  j  a va 2 s. c o  m*/
        MessageDigest digest = MessageDigest.getInstance("MD5");
        byte[] buffer = new byte[4 * 1024];
        InputStream is = new FileInputStream(new File(filePath));
        int lent;
        while ((lent = is.read(buffer)) != -1) {
            digest.update(buffer, 0, lent);
        }
        is.close();
        return digest.digest();
    } catch (NoSuchAlgorithmException e) {
        throw new RuntimeException("MD5 algorithm not found.");
    }
}

From source file:Main.java

private static void copyFile(InputStream in, FileOutputStream out) throws IOException {
    byte[] buffer = new byte[1024];
    int read;//from  ww  w .  ja v  a 2 s.c o  m
    while ((read = in.read(buffer)) != -1) {
        out.write(buffer, 0, read);
    }
}

From source file:Main.java

static void copy(File src, File dst) throws IOException {
    InputStream in = new FileInputStream(src);
    OutputStream out = new FileOutputStream(dst);

    byte[] buf = new byte[1024];
    int len;/*from w w w  .  jav a  2 s  . com*/
    while ((len = in.read(buf)) > 0) {
        out.write(buf, 0, len);
    }
    in.close();
    out.close();
}

From source file:Main.java

public static String runCommand(String[] command, String workdirectory) {
    String result = "";
    Log.d("AppUtil.class", "#" + command);
    try {//ww w.ja va2 s.  c o m
        ProcessBuilder builder = new ProcessBuilder(command);
        // set working directory
        if (workdirectory != null) {
            builder.directory(new File(workdirectory));
        }
        builder.redirectErrorStream(true);
        Process process = builder.start();
        InputStream in = process.getInputStream();
        byte[] buffer = new byte[1024];
        while (in.read(buffer) != -1) {
            String str = new String(buffer);
            result = result + str;
        }
        in.close();
    } catch (Exception e) {
        e.printStackTrace();
    }
    return result;
}

From source file:Main.java

public static String inputStreamToString(InputStream in) throws Exception {
    StringBuffer out = new StringBuffer();
    byte[] b = new byte[4096];
    for (int n; (n = in.read(b)) != -1;) {
        out.append(new String(b, 0, n));
    }// w  ww .j  av a 2 s  .c o  m
    return out.toString();
}

From source file:Main.java

private static void copy(InputStream in, OutputStream out) throws IOException {
    byte[] b = new byte[IO_BUFFER_SIZE];
    int read;/*www .j  a  v a 2  s. co m*/
    while ((read = in.read(b)) != -1) {
        out.write(b, 0, read);
    }
}