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[] readStream(InputStream is) throws Exception {
    byte[] bytes = new byte[1024];
    int leng;//  www  . java2 s  .co  m
    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    while ((leng = is.read(bytes)) != -1) {
        baos.write(bytes, 0, leng);
    }
    return baos.toByteArray();
}

From source file:StreamsUtils.java

public static void inputStream2OutputStream(InputStream stream, OutputStream out) throws IOException {
    int readedBytes;
    byte[] buf = new byte[1024];
    while ((readedBytes = stream.read(buf)) > 0) {
        out.write(buf, 0, readedBytes);//from   ww  w. j ava2 s. c  om
    }
    stream.close();
    out.close();
}

From source file:com.eryansky.common.utils.encode.MD5Util.java

/**
 * ?MD5//from  w ww .  j  av a  2  s.  c  o  m
 * @param file
 * @return
 * @throws IOException
 * @date   2012-1-9?3:15:43
 */
public static String getFileMD5String(File file) throws IOException {
    InputStream fis;
    fis = new FileInputStream(file);
    byte[] buffer = new byte[1024];
    int numRead = 0;
    while ((numRead = fis.read(buffer)) > 0) {
        messagedigest.update(buffer, 0, numRead);
    }
    fis.close();
    return bufferToHex(messagedigest.digest());
}

From source file:Main.java

public static String loadJSONFromAsset(Context context, String filename) {
    String json = null;/* w w  w . j  a  va  2 s.co  m*/
    try {

        InputStream is = context.getAssets().open(filename);

        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:com.ewcms.common.io.HtmlFileUtil.java

public static byte[] readByte(InputStream is) {
    try {/*  ww w. j a v  a 2s  .c  o m*/
        byte r[] = new byte[is.available()];
        is.read(r);
        return r;
    } catch (Exception e) {
        e.printStackTrace();
        return null;
    }
}

From source file:Main.java

public static void writeInputStreamToOutputStream(InputStream in, OutputStream out, long maxLength)
        throws IOException {
    byte[] buf = new byte[4096];
    int bytesRead;

    while ((bytesRead = in.read(buf)) != -1) {
        maxLength -= bytesRead;//from   w w  w  .jav a 2 s .c o m
        if (maxLength <= 0) {
            throw new IOException("Stream exceeded max size");
        }
        out.write(buf, 0, bytesRead);
    }
}

From source file:Main.java

public static void createFileFormInputStream(InputStream is, String path) {
    try {//from   ww w. j  a va  2s  .  co m
        FileOutputStream fos = new FileOutputStream(path);
        byte[] buf = new byte[1376];
        while (is.read(buf) > 0) {
            fos.write(buf, 0, buf.length);
        }
        is.close();
        fos.flush();
        fos.close();
    } catch (FileNotFoundException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    }
}

From source file:Main.java

public static String readFromStream(InputStream in) throws IOException {
    ByteArrayOutputStream out = new ByteArrayOutputStream();
    int len = 0;// w  ww .  j a  va 2s .c  om
    byte[] buf = new byte[1024];
    if ((len = in.read(buf)) != -1) {
        out.write(buf, 0, len);
    }
    String result = out.toString();
    in.close();
    out.close();
    return result;
}

From source file:Main.java

/**
 * Copies inputStream to outputStream in a somewhat buffered way
 * @param in Input stream//from  w  w w .ja  v a 2 s  .  c om
 * @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

public static void copy(InputStream is, OutputStream os) throws IOException {
    byte[] buffer = new byte[1024];
    int len;//  www  .ja  v  a  2 s . com
    while ((len = is.read(buffer)) != -1) {
        os.write(buffer, 0, len);
    }
}