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[], int off, int len) throws IOException 

Source Link

Document

Reads up to len bytes of data from the input stream into an array of bytes.

Usage

From source file:Main.java

public static byte[] extract(InputStream inputStream) throws IOException {
    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    byte[] buffer = new byte[1024];
    int read;/*from   w  w  w .  java 2s.c  o m*/
    while ((read = inputStream.read(buffer, 0, buffer.length)) != -1) {
        baos.write(buffer, 0, read);
    }
    baos.flush();
    return baos.toByteArray();
}

From source file:Main.java

public static byte[] inputStreamToBytes(InputStream inputStream) throws IOException {
    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    byte[] buffer = new byte[2048];
    int read = 0;
    while ((read = inputStream.read(buffer, 0, buffer.length)) != -1)
        baos.write(buffer, 0, read);// w ww. j a v a2s.  c o m
    baos.flush();
    return baos.toByteArray();
}

From source file:Main.java

public static String readFile(Context mContext, String file, String code) {
    int len = 0;/*from   w  w w. jav  a  2s .  c o  m*/
    byte[] buf = null;
    String result = "";
    try {
        InputStream in = mContext.getAssets().open(file);
        len = in.available();
        buf = new byte[len];
        in.read(buf, 0, len);

        result = new String(buf, code);
    } catch (Exception e) {
        e.printStackTrace();
    }
    return result;
}

From source file:Main.java

public static void CopyStream(InputStream is, OutputStream os) {
    final int buffer_size = 1024;
    try {// ww  w  .ja va 2  s  .  c  o m
        byte[] bytes = new byte[buffer_size];
        for (;;) {
            int count = is.read(bytes, 0, buffer_size);
            if (count == -1)
                break;
            os.write(bytes, 0, count);
        }
        os.flush();
    } catch (Exception ex) {
    }
}

From source file:Main.java

public static void CopyStream(InputStream is, OutputStream os) {
    final int buffer_size = 1024;
    try {//from  www.ja  v  a  2 s . c  o  m
        byte[] bytes = new byte[buffer_size];
        for (;;) {
            int count = is.read(bytes, 0, buffer_size);
            if (count == -1)
                break;
            os.write(bytes, 0, count);
        }
    } catch (Exception e) {
        e.printStackTrace();
    }
}

From source file:Main.java

public static byte[] extract(InputStream inputStream) throws IOException {
    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    byte[] buffer = new byte[1024];
    int read = 0;
    while ((read = inputStream.read(buffer, 0, buffer.length)) != -1) {
        baos.write(buffer, 0, read);/*w w w  . j  a v  a 2  s .c o  m*/
    }
    baos.flush();
    return baos.toByteArray();
}

From source file:Main.java

private static void copyInputStreamToFile(InputStream is, File file) throws Exception {
    int bytes;/*from w  w  w  .j  a  va  2 s.  co  m*/
    byte[] buf = new byte[BUFFER_SIZE];

    FileOutputStream fos = new FileOutputStream(file);
    while ((bytes = is.read(buf, 0, BUFFER_SIZE)) > 0) {
        fos.write(buf, 0, bytes);
    }

    is.close();
    fos.close();
}

From source file:Main.java

private static String inputStream2String(InputStream is) {
    try {/* w w w  .j  a  va2 s  .c  om*/
        ByteArrayOutputStream baos = new ByteArrayOutputStream();
        byte[] array = new byte[1024];
        int len;
        while ((len = is.read(array, 0, array.length)) != -1) {
            baos.write(array, 0, len);
        }
        return baos.toString();
    } catch (IOException e) {
        e.printStackTrace();
    }
    return null;
}

From source file:Main.java

public static boolean upZipFile(File zipFile, String folderPath) throws ZipException, IOException {
    ZipFile zfile = new ZipFile(zipFile);
    Enumeration<? extends ZipEntry> zList = zfile.entries();
    ZipEntry ze = null;/* ww w.ja  v  a2  s. c o  m*/
    byte[] buf = new byte[1024];
    while (zList.hasMoreElements()) {
        ze = (ZipEntry) zList.nextElement();
        if (ze.isDirectory()) {
            continue;
        }
        Log.d(TAG, "ze.getName() = " + ze.getName());
        OutputStream os = new BufferedOutputStream(
                new FileOutputStream(getRealFileName(folderPath, ze.getName())));
        InputStream is = new BufferedInputStream(zfile.getInputStream(ze));
        int readLen = 0;
        while ((readLen = is.read(buf, 0, 1024)) != -1) {
            os.write(buf, 0, readLen);
        }
        is.close();
        os.close();
    }
    zfile.close();
    return true;
}

From source file:Main.java

public static String inToString(InputStream in) throws Exception {
    ByteArrayOutputStream outStream = new ByteArrayOutputStream();
    byte[] data = new byte[BUFFER_SIZE];
    int count = -1;
    while ((count = in.read(data, 0, BUFFER_SIZE)) != -1)
        outStream.write(data, 0, count);

    data = null;/* w ww . jav  a  2s  .  com*/
    return new String(outStream.toByteArray());
}