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 String readTextFile(InputStream inputStream) {
    ByteArrayOutputStream outputStream = new ByteArrayOutputStream();

    byte buf[] = new byte[1024];
    int len;//from   www.j a  v  a  2 s .com
    try {
        while ((len = inputStream.read(buf)) != -1) {
            outputStream.write(buf, 0, len);
        }
        outputStream.close();
        inputStream.close();
    } catch (IOException e) {

    }
    return outputStream.toString();
}

From source file:Main.java

/**
 * Fully reads the given InputStream and returns it as a byte array
 *
 * @param inputStream The input stream to read
 * @return The byte array containing the data from the input stream
 * @throws IOException If an I/O error occurs
 *///  w  w  w  . j  a v  a 2 s .  co  m
public static byte[] toByteArray(InputStream inputStream) throws IOException {
    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    byte buffer[] = new byte[8192];
    while (true) {
        int read = inputStream.read(buffer);
        if (read == -1) {
            break;
        }
        baos.write(buffer, 0, read);
    }
    return baos.toByteArray();
}

From source file:Main.java

public static void copyFile(InputStream in, OutputStream out) throws IOException {
    byte[] buffer = new byte[8192];
    int read;// w ww .  ja  v  a  2 s  .c o  m
    while ((read = in.read(buffer)) != -1) {
        out.write(buffer, 0, read);
    }
    out.flush();
}

From source file:Main.java

public static byte[] readStreamToBytes(InputStream is) throws IOException {
    ByteArrayOutputStream bos = new ByteArrayOutputStream();
    byte[] byteArray = new byte[BYTE_ARRAY_SIZE];
    int length = 0;
    while ((length = is.read(byteArray)) != -1) {
        bos.write(byteArray, 0, length);
    }/*w  w  w. j  a v  a  2  s .c  o m*/
    bos.close();
    is.close();
    return bos.toByteArray();
}

From source file:com.janoz.usenet.support.LogUtil.java

private static void copyStream(InputStream in, OutputStream out) throws IOException {
    byte[] buffer = new byte[BUFF_SIZE];
    int len;/*ww w . j a v a2 s  .co m*/

    while ((len = in.read(buffer)) >= 0) {
        out.write(buffer, 0, len);
    }
    in.close();
    out.close();
}

From source file:Main.java

public static boolean equals(InputStream ins1, InputStream ins2) throws IOException {
    byte[] buf1 = new byte[DEFAULT_BUFFER_SIZE];
    byte[] buf2 = new byte[DEFAULT_BUFFER_SIZE];
    do {/*from  w w w  . j av a 2  s.  c  om*/
        int len1 = ins1.read(buf1);
        int len2 = ins2.read(buf2);
        if (len1 != len2)
            return false;
        if (len1 == -1)
            return true;
        for (int i = 0; i < len1; i++) {
            if (buf1[i] != buf2[i])
                return false;
        }
    } while (true);
}

From source file:Main.java

/**
 * Copies all available data from in to out without closing any stream.
 * //from  w  w  w . ja  v  a  2  s  .  com
 * @return number of bytes copied
 */
public static int copyAllBytes(InputStream in, OutputStream out) throws IOException {
    int byteCount = 0;
    byte[] buffer = new byte[4096];
    while (true) {
        int read = in.read(buffer);
        if (read == -1) {
            break;
        }
        out.write(buffer, 0, read);
        byteCount += read;
    }
    return byteCount;
}

From source file:Main.java

private static void saveStream(InputStream is, String savePath) throws IOException {
    FileOutputStream output = new FileOutputStream(savePath);
    int readlen;//from   w  ww . j av a 2s.  c  om

    byte[] buf = new byte[BUFF_SIZE];
    while ((readlen = is.read(buf)) > 0) {
        output.write(buf, 0, readlen);
    }
    output.close();
}

From source file:Main.java

public static String getJsonDataFromAssets(Context context, String fileName) {
    StringBuilder stringBuilder = new StringBuilder();
    InputStream inputStream = context.getClass().getClassLoader().getResourceAsStream("assets/" + fileName);
    try {// w w  w.  java  2 s  . c  om
        byte[] buffer = new byte[inputStream.available()];
        inputStream.read(buffer);
        String json = new String(buffer, "utf-8");
        stringBuilder = stringBuilder.append(json);
    } catch (IOException e) {
        e.printStackTrace();
    } finally {
        try {
            inputStream.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
    return stringBuilder.toString();
}

From source file:Main.java

public static byte[] readInputStream(InputStream inStream) throws IOException {
    ByteArrayOutputStream outSteam = new ByteArrayOutputStream();
    byte[] buffer = new byte[BUFFER_SIZE];
    int len = 0;//w  ww. j  ava 2 s.c  o  m
    while ((len = inStream.read(buffer)) != -1) {
        outSteam.write(buffer, 0, len);
    }
    outSteam.close();
    inStream.close();
    return outSteam.toByteArray();
}