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

/**
 * This method copies data from input stream to output stream.
 * //from w w  w  .j  a  v  a2s.  co  m
 * @param is
 * @param os
 */
public static void CopyStream(InputStream is, OutputStream os) {
    final int buffer_size = 1024;
    try {
        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 ex) {
        ex.printStackTrace();
    }
}

From source file:Main.java

public static byte[] readBytes(InputStream is, long count) throws IOException {
    byte[] bytes = new byte[(int) count];

    int start = 0;

    do {//from   ww w. jav a  2s .  c  om
        int read = is.read(bytes, start, (int) count);

        // If we're at EOF, but trying to read the first set of bytes, return null
        if (read == -1) {
            if (start > 0) {
                throw new EOFException();
            }

            return null;
        }

        start += read;
        count -= read;
    } while (count > 0);

    return bytes;
}

From source file:Main.java

static private byte[] readBytesFromStream(InputStream input) throws IOException {

    ByteArrayOutputStream buffer = new ByteArrayOutputStream();

    int nRead;//  www .  j a va 2s  .c o  m
    byte[] data = new byte[16384];

    while ((nRead = input.read(data, 0, data.length)) != -1) {
        buffer.write(data, 0, nRead);
    }

    buffer.flush();
    input.close();
    return buffer.toByteArray();
}

From source file:Main.java

public static byte[] InputStream2Bytes(InputStream is) {
    String str = "";
    byte[] readByte = new byte[1024];
    @SuppressWarnings("unused")
    int readCount = -1;
    try {//w  w  w  .  j a  v a  2s.co m
        while ((readCount = is.read(readByte, 0, 1024)) != -1) {
            str += new String(readByte).trim();
        }
        return str.getBytes();
    } catch (Exception e) {
        e.printStackTrace();
    }
    return null;
}

From source file:Main.java

/** 
 * Reads bytes from the specified inputstream into the specified target buffer until it is filled up     
 *///from   w w w . java  2 s .co m
public static void readBytesUntilFull(InputStream in, byte[] targetBuffer) throws IOException {
    int totalBytesRead = 0;
    int read;
    final int targetBytes = targetBuffer.length;
    do {
        read = in.read(targetBuffer, totalBytesRead, (targetBytes - totalBytesRead));
        if (read != -1) {
            totalBytesRead += read;
        } else {
            throw new IOException("Unexpected EOF reached before read buffer was filled");
        }
    } while (totalBytesRead < targetBytes);
}

From source file:Main.java

public static int upZipFile(File zipFile, String folderPath) throws IOException {
    ZipFile zfile = new ZipFile(zipFile);
    Enumeration zList = zfile.entries();
    ZipEntry ze = null;/*from w  w w  .j  av  a  2s.com*/
    byte[] buf = new byte[1024];
    while (zList.hasMoreElements()) {
        ze = (ZipEntry) zList.nextElement();
        if (ze.isDirectory()) {
            String dirstr = folderPath + ze.getName();
            dirstr = new String(dirstr.getBytes("8859_1"), "GB2312");
            File f = new File(dirstr);
            f.mkdirs();
            continue;
        }
        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 0;
}

From source file:Main.java

public static byte[] inputStreamToByte(InputStream in) throws IOException {

    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;//from  w  ww.  j a v  a2  s .  co m
    return outStream.toByteArray();
}

From source file:Main.java

public static byte[] InputStreamTOByteArray(InputStream in) throws IOException {
    ByteArrayOutputStream outStream = new ByteArrayOutputStream();
    int count = -1;
    byte[] data = new byte[BUFFER_SIZE];
    while ((count = in.read(data, 0, BUFFER_SIZE)) != -1) {
        outStream.write(data, 0, count);
    }//from ww  w  .j ava 2 s . c  o  m
    data = null;
    outStream.close();
    return outStream.toByteArray();

}

From source file:Main.java

public static void copyStream(InputStream is, OutputStream os) {
    final int buffer_size = 1024;

    try {//from   ww w .j  a  v a 2s .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

/**
 * Converts an {@link InputStream} to byte[].
 * // w ww  .  j a  v  a 2s .com
 * @param is
 *            - an {@link InputStream}
 * 
 * @return
 * 
 * @throws IOException
 */
public static byte[] toByteArray(InputStream is) throws IOException {
    ByteArrayOutputStream buffer = new ByteArrayOutputStream();
    int nRead;
    byte[] data = new byte[16384];
    while ((nRead = is.read(data, 0, data.length)) != -1) {
        buffer.write(data, 0, nRead);
    }
    buffer.flush();
    return buffer.toByteArray();

}