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[] suckFile(String inputfile) throws IOException {
    InputStream is = context.openFileInput(inputfile);
    int size = is.available();
    byte[] content = new byte[size];

    is.read(content);

    is.close();//  w  w  w  . ja v a  2  s .c om
    return content;
}

From source file:Main.java

static void copyData(InputStream from, OutputStream to) throws IOException {
    byte[] buffer = new byte[1024];
    int length;// w  w w.  ja v a  2  s . co  m
    while ((length = from.read(buffer)) > 0) {
        to.write(buffer, 0, length);
    }
    to.flush();
    to.close();
    from.close();
}

From source file:Main.java

public static String readIt(InputStream stream) throws IOException {
    StringBuffer out = new StringBuffer();
    byte[] b = new byte[4096];
    for (int n; (n = stream.read(b)) != -1;) {
        out.append(new String(b, 0, n));
    }/*from w  w w .java  2 s. c o m*/
    return out.toString();
}

From source file:Util.java

public static void copyInputstreamToFile(InputStream in, OutputStream out) {
    try {//from ww w  .  j a v a  2 s .  c  o  m
        byte[] buf = new byte[1024];
        int len;
        while ((len = in.read(buf)) > 0) {
            out.write(buf, 0, len);
        }
        in.close();
        out.close();
    } catch (IOException ex) {

    }
}

From source file:Main.java

public static void copyAssets(Context context, String assetsName, String destFilePath) throws IOException {
    File file = new File(destFilePath);
    FileOutputStream out = new FileOutputStream(file);

    InputStream in = context.getAssets().open(assetsName);

    byte[] buf = new byte[1024];
    int len;/*from ww  w.  j av  a2 s.  c  om*/
    while ((len = in.read(buf)) != -1) {
        out.write(buf, 0, len);
    }
    in.close();
    out.close();
}

From source file:Main.java

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

From source file:Main.java

/**
 * Copies inputStream to outputStream in a somewhat buffered way
 * @param in Input stream/*  w w  w.j a v a  2s  . c o m*/
 * @param out Output stream
 * @throws IOException if the operation fails
 */
public static void copyStream(InputStream in, OutputStream out) throws IOException {
    byte[] buffer = new byte[BUFFER_SIZE];
    int read;
    while ((read = in.read(buffer)) != -1) {
        out.write(buffer, 0, read);
    }
}

From source file:Main.java

/**
 * InputStream convert to string/*from ww w.  j  ava  2s. c  o  m*/
 *
 * @param in
 * @return
 * @throws IOException
 */
public static String inputStream2String(InputStream in) throws IOException {
    StringBuffer out = new StringBuffer();
    byte[] b = new byte[4096];
    for (int n; (n = in.read(b)) != -1;) {
        out.append(new String(b, 0, n));
    }
    return out.toString();
}

From source file:Main.java

/**
 * <pre>//from  w w w .  j a v a2 s.  co m
 * Convenient method to copy file.
 * </pre>
 * @param in {@link InputStream} of source file
 * @param out {@link OutputStream} of destination file
 */
public static void copyFile(InputStream in, OutputStream out) throws IOException {
    byte[] buffer = new byte[1024];
    int read;
    while ((read = in.read(buffer)) != -1) {
        out.write(buffer, 0, read);
    }

    in.close();
    out.flush();
    out.close();
}

From source file:Main.java

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