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 streamToStream(InputStream is, OutputStream os) throws IOException {
    final byte[] buffer = new byte[256];
    try {/* ww  w.j  av  a2 s .c  o  m*/
        int n;
        while ((n = is.read(buffer)) != -1) {
            os.write(buffer, 0, n);
        }
    } finally {
        os.flush();
        os.close();
        is.close();
    }
}

From source file:Main.java

public static long getMaxCpuFreq() {
    long longRet = 0;
    String result = "0";
    ProcessBuilder cmd;/*  w ww  .  j  a  va  2s . co m*/
    try {
        String[] args = { "/system/bin/cat", "/sys/devices/system/cpu/cpu0/cpufreq/cpuinfo_max_freq" };
        cmd = new ProcessBuilder(args);
        Process process = cmd.start();
        InputStream in = process.getInputStream();
        byte[] re = new byte[24];
        result = "";
        while (in.read(re) != -1) {
            result = result + new String(re);
        }
        in.close();
    } catch (IOException ex) {
        ex.printStackTrace();
        result = "0";
    }

    if (result.length() != 0) {
        try {
            longRet = Long.valueOf(result.trim());
        } catch (Exception e) {
            android.util.Log.e(TAG, "");
        }
    }
    return longRet;
}

From source file:Main.java

public static void streamTransfer(final InputStream in, final OutputStream out) {
    final byte[] buffer = new byte[8192];
    int read;/*  ww  w.jav a  2 s  . co  m*/
    try {
        while ((read = in.read(buffer)) != -1) {
            out.write(buffer, 0, read);
        }
    } catch (final IOException exception) {
        exception.printStackTrace();
    }
}

From source file:Main.java

/**
 * Returns the lowercase string representation of the file's MD5 sum.
 *///  w ww.j a va  2s.co m
public static String getMD5Sum(String file) throws IOException {
    try {
        MessageDigest digest = MessageDigest.getInstance("MD5");
        InputStream is = new FileInputStream(file);
        byte[] buffer = new byte[8192];
        int read = 0;
        while ((read = is.read(buffer)) > 0) {
            digest.update(buffer, 0, read);
        }
        is.close();
        byte[] md5sum = digest.digest();
        BigInteger bigInt = new BigInteger(1, md5sum);
        return bigInt.toString(16);
    } catch (NoSuchAlgorithmException e) {
        return "";
    }
}

From source file:Main.java

/**
 * <p>Copies the data from the {@link InputStream} into a {@link OutputStream}</p>
 *
 * @param input  data source stream.//  w ww.j  av a2s  .  co m
 * @param output target stream.
 *
 * @throws IOException if error happened.
 */
public static void copyStream(InputStream input, OutputStream output) throws IOException {
    byte[] buffer = new byte[BUFFER_SIZE];
    int bytesRead;
    while ((bytesRead = input.read(buffer)) != -1) {
        output.write(buffer, 0, bytesRead);
    }
}

From source file:Main.java

public static String getStringFromStream(InputStream is) {
    byte[] bytes = new byte[1024];
    int len = 0;//from www  . j  ava2s .  co  m
    String res = "";
    try {
        while ((len = is.read(bytes)) != -1) {
            res = res + new String(bytes, 0, len, "gbk");
        }
    } catch (IOException e) {
        e.printStackTrace();
    } finally {
        if (is != null) {
            try {
                is.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
    return res;
}

From source file:Main.java

/**
 * Extract a resource into a real file//w  w w. j  a  v a2 s  .  c om
 * 
 * @param in typically given as getResources().openRawResource(R.raw.something)
 * @param name of the resulting file
 * @param directory target directory
 * @return the resulting file
 * @throws IOException
 */
public static File extractResource(InputStream in, String filename, File directory) throws IOException {
    int n = in.available();
    byte[] buffer = new byte[n];
    in.read(buffer);
    in.close();
    File file = new File(directory, filename);
    FileOutputStream out = new FileOutputStream(file);
    out.write(buffer);
    out.close();
    return file;
}

From source file:Main.java

public static void streamCopy(final InputStream bodyIs, final OutputStream out) throws IOException {
    byte[] buffer = new byte[2048];
    int read;//from   ww w  .j ava2  s  .c o m
    while ((read = bodyIs.read(buffer)) > 0) {
        out.write(buffer, 0, read);
    }
    bodyIs.close();
    out.close();
}

From source file:Main.java

public static String LoadData(Context context, String inFile) {
    String tContents = "";
    try {/*from w w  w  . j ava  2 s  . c o m*/
        InputStream stream = context.getAssets().open(inFile);
        int size = stream.available();
        byte[] buffer = new byte[size];
        stream.read(buffer);
        stream.close();
        tContents = new String(buffer);
    } catch (IOException e) {
        // Handle exceptions

    }

    return tContents;
}

From source file:Main.java

public static String inputStream2String(InputStream in) throws IOException {
    byte[] buf = new byte[1024];
    ByteArrayOutputStream baos = new ByteArrayOutputStream();

    for (int i; (i = in.read(buf)) != -1;) {
        baos.write(buf, 0, i);/* w ww. j  av a2 s.  c  o m*/
    }

    return baos.toString("UTF-8");
}