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 void readTillEnd(InputStream in, byte[] buf, boolean eofOK) throws IOException {
    int toRead = buf.length;
    int numRead = 0;
    while (numRead < toRead) {
        int nread = in.read(buf, numRead, toRead - numRead);
        if (nread < 0) {
            if (eofOK) {
                // EOF hit, fill with zeros
                Arrays.fill(buf, numRead, toRead, (byte) 0);
                numRead = toRead;//from   w w  w  .  j  a va  2 s. co  m
            } else {
                // EOF hit, throw.
                throw new IOException("Premature EOF");
            }
        } else {
            numRead += nread;
        }
    }
}

From source file:Main.java

/**
 * Copies data from IS to OS in chunks of 1024 bytes.
 * /*from   w w w .j  a  va 2s . c om*/
 * 
 * @param inputStream
 *            where data needs to be read from
 * @param outputStream
 *            where data needs to be written
 * @throws IOException
 *             in case of error.
 */
public static void copyStream(InputStream inputStream, OutputStream outputStream) throws IOException {
    final int BUFFER_SIZE = 1024;
    byte[] bytes = new byte[BUFFER_SIZE];
    while (true) {
        int count = inputStream.read(bytes, 0, BUFFER_SIZE);
        if (count == -1)
            break;
        outputStream.write(bytes, 0, count);
    }
}

From source file:Main.java

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

From source file:Main.java

public static byte[] getAssetAsBuffer(Context ctx, String assetName) throws IOException {
    InputStream is = ctx.getAssets().open(assetName);
    int read, available, offset = 0;
    byte[] result = new byte[available = is.available()];
    while (available > 0 && (read = is.read(result, offset, available)) != -1) {
        offset += read;/* ww w .ja  v  a  2  s  . c  o  m*/
        available = is.available();
        if (offset + available > result.length) {
            byte[] newResult = new byte[offset + available];
            System.arraycopy(result, 0, newResult, 0, offset);
            result = newResult;
        }
    }
    return result;
}

From source file:Main.java

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

From source file:Main.java

public static byte[] stream2Bytes(InputStream in, int length) throws IOException {
    byte[] bytes = new byte[length];
    int count;//from w  w w  .  j a  va2 s. c o  m
    int pos = 0;
    while (pos < length && ((count = in.read(bytes, pos, length - pos)) != -1)) {
        pos += count;
    }
    if (pos != length) {
        throw new IOException("Expected " + length + " bytes, read " + pos + " bytes");
    }
    return bytes;
}

From source file:Main.java

public static final byte[] input2byte(InputStream inStream) throws IOException {
    ByteArrayOutputStream swapStream = new ByteArrayOutputStream();
    byte[] buff = new byte[100];
    int rc = 0;/*from w  w w.j a v a  2 s  . c o  m*/
    while ((rc = inStream.read(buff, 0, 100)) > 0) {
        swapStream.write(buff, 0, rc);
    }
    byte[] in2b = swapStream.toByteArray();
    return in2b;
}