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[] inputStream2ByteArray(InputStream in) throws Exception {
    ByteArrayOutputStream bos = new ByteArrayOutputStream();
    byte[] buf = new byte[BYTE_ARRAY_LENTH];
    int len = -1;
    while ((len = in.read(buf)) != -1) {
        bos.write(buf, 0, len);/*from www  .  j  av  a  2s. c o m*/
    }

    return bos.toByteArray();
}

From source file:Main.java

public static long copy(InputStream from, OutputStream to) throws IOException {
    byte[] buf = new byte[4096];
    long total = 0L;

    while (true) {
        int r = from.read(buf);
        if (r == -1) {
            return total;
        }/*from w w w  . j  a v a2s.  c  om*/

        to.write(buf, 0, r);
        total += (long) r;
    }
}

From source file:Main.java

static public void copyDataBase() throws IOException {

    OutputStream databaseOutputStream = new FileOutputStream("/mnt/sdcard/jwdroid.db");
    InputStream databaseInputStream = new FileInputStream("/data/data/com.jwdroid/databases/jwdroid");

    byte[] buffer = new byte[1];
    int length;//from  w w  w  .  j  av a2 s . c  o m
    while ((length = databaseInputStream.read(buffer)) > 0) {
        databaseOutputStream.write(buffer);
        Log.w("Bytes: ", ((Integer) length).toString());
        Log.w("value", buffer.toString());
    }

    databaseOutputStream.flush();
    databaseOutputStream.close();
    databaseInputStream.close();
}

From source file:Main.java

public static void readAsFile(InputStream inSream, File file) throws Exception {
    FileOutputStream outStream = new FileOutputStream(file);
    byte[] buffer = new byte[1024];
    int len = -1;
    while ((len = inSream.read(buffer)) != -1) {
        outStream.write(buffer, 0, len);
    }/*  w  w w  . j a v  a2s.c  om*/
    outStream.close();
    inSream.close();
}

From source file:Main.java

public static void copyInputStreamToFile(InputStream in, File file) {
    try {/*from  w w w.j  av  a  2 s .  com*/
        OutputStream out = new FileOutputStream(file);
        byte[] buf = new byte[1024];
        int len;
        while ((len = in.read(buf)) > 0) {
            out.write(buf, 0, len);
        }
        out.close();
        in.close();
    } catch (Exception e) {
        e.printStackTrace();
    }
}

From source file:Main.java

public static byte[] readStream(InputStream inStream) throws Exception {
    ByteArrayOutputStream outstream = new ByteArrayOutputStream();
    byte[] buffer = new byte[1024];
    int len = -1;
    while ((len = inStream.read(buffer)) != -1) {
        outstream.write(buffer, 0, len);
    }/*from ww  w .j  av  a2s .co m*/
    outstream.close();
    inStream.close();

    return outstream.toByteArray();
}

From source file:fr.logfiletoes.Main.java

/**
 * Convert InputStream to String/*from w  ww .  j a  v  a2  s .  co m*/
 * @param is InputStream read to convert to String
 * @return String from InputStream
 * @throws IOException 
 */
public static String inputSteamToString(InputStream is) throws IOException {
    byte[] buffer = new byte[1024];
    StringBuilder sb = new StringBuilder();
    while (is.available() > 0) {
        is.read(buffer);
        sb.append(new String(buffer));
    }
    return sb.toString();
}

From source file:com.cnten.platform.util.FileUtil.java

public static void writeStream(InputStream is, OutputStream os) throws Exception {
    byte[] buff = new byte[1024];
    int readCount = 0;
    readCount = is.read(buff);
    while (readCount != -1) {
        os.write(buff, 0, readCount);//  www .  ja v a  2 s .c  o  m
        readCount = is.read(buff);
    }
}

From source file:Main.java

/**
 * Get text file from assets//from ww w .j  a  va2  s  .  c  om
 *
 * @param context
 * @param fileName
 * @return
 */
public static String getTextFromAssets(Context context, String fileName) {
    String result = null;
    try {
        InputStream in = getFileFromAssets(context, fileName);
        int length = in.available();
        byte[] buffer = new byte[length];
        in.read(buffer);
        result = new String(buffer, Charset.forName(ENCODING));
    } catch (IOException e) {
        e.printStackTrace();
    }
    return result;
}

From source file:Main.java

private static void copyFile(File source, File destination) throws IOException {
    InputStream in = new FileInputStream(source);
    OutputStream out = new FileOutputStream(destination);

    byte[] buffer = new byte[1024];
    int length;/*from  w w w .  j a  v a  2  s.c o m*/
    while ((length = in.read(buffer)) > 0) {
        out.write(buffer, 0, length);
    }
    out.flush();
    out.close();
    in.close();
}