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 byte[] getBytesFromInput(InputStream in) throws IOException {
    ByteArrayOutputStream out = new ByteArrayOutputStream();
    byte[] buffer = new byte[in.available()];
    in.read(buffer);
    out.write(buffer);//  ww w.j av a2s.  co  m
    return out.toByteArray();
}

From source file:Main.java

public static String getSha1(FileDescriptor fd) {
    MessageDigest md;//  w  w w. j  av a2s . com
    try {
        md = MessageDigest.getInstance("SHA-1");
    } catch (NoSuchAlgorithmException e) {
        throw new RuntimeException(e);
    }
    byte[] b = new byte[4096];
    FileInputStream fis = new FileInputStream(fd);
    InputStream is = new BufferedInputStream(fis, 4096);
    try {
        for (int n; (n = is.read(b)) != -1;) {
            md.update(b, 0, n);
        }
    } catch (IOException e) {
        Log.w(TAG, "IOException while computing SHA-1");
        return null;
    }
    byte[] sha1hash = new byte[40];
    sha1hash = md.digest();
    return getHex(sha1hash);
}

From source file:Main.java

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

From source file:Main.java

public static void copy(InputStream in, OutputStream out) throws IOException {
    byte[] buffer = new byte[1024 * 32];
    int read;//from   w w w. jav a  2  s.  c  om
    while ((read = in.read(buffer)) != -1) {
        out.write(buffer, 0, read);
    }
}

From source file:StreamUtils.java

/**
 * Reads the content of an input stream and writes it into an output stream.
 * The copy is made in chunks of 512 bytes.
 * @param is the input//from  w  w w .j a va2s. c o m
 * @param os the output
 * @throws IOException thrown by the {@code read} and {@code write} methods of the streams
 */
public static void readWrite(InputStream is, OutputStream os) throws IOException {
    byte[] buf = new byte[512];
    int nRead;
    while ((nRead = is.read(buf)) != -1) {
        os.write(buf, 0, nRead);
    }
}

From source file:Main.java

public static String loadJSONFromAsset(Context context, String jsonFile) {
    String json = null;/*w  ww  . j a  va2  s  .  c o  m*/
    try {
        InputStream is = context.getAssets().open(jsonFile);
        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

/**
 * @param in The input stream to copy from.
 * @param out The output stream to copy to.
 * @throws IOException If any error occurs during the copy.
 *//*from w  ww . j ava2 s .c  om*/
private static void copyStream(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 String runCommand(String[] command, String workdirectory) {
    String result = "";
    //AbLogUtil.d(AbAppUtil.class, "#"+command);
    try {//from  www .j av a  2s  .  c  om
        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 void appendStreamToStream(InputStream is, OutputStream os) throws IOException {
    final byte[] buffer = new byte[256];
    try {/*from   ww w  .j a  v a  2  s.c  o m*/
        int n;
        while ((n = is.read(buffer)) != -1) {
            os.write(buffer, 0, n);
        }
    } finally {
        is.close();
    }
}

From source file:Main.java

public static long copyLarge(InputStream input, OutputStream output, byte[] buffer) throws IOException {
    long count = 0;
    int n = 0;/*  www .  j a v  a  2  s.  co  m*/
    while (EOF != (n = input.read(buffer))) {
        output.write(buffer, 0, n);
        count += n;
    }
    return count;
}