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 void WriteFile(InputStream stream, String fileName) throws IOException {
    FileOutputStream fos = new FileOutputStream(fileName);
    byte[] buff = new byte[1024];
    int readLen;/*from  www.ja  v  a  2s  . c om*/
    do {
        readLen = stream.read(buff);
        if (readLen > 0)
            fos.write(buff, 0, readLen);
    } while (readLen > 0);

    fos.close();
}

From source file:Main.java

public static String readData(InputStream inSream, String charsetName) throws Exception {
    ByteArrayOutputStream outStream = new ByteArrayOutputStream();
    byte[] buffer = new byte[1024];
    int len = -1;
    while ((len = inSream.read(buffer)) != -1) {
        outStream.write(buffer, 0, len);
    }//w ww  .  j av  a  2  s  .  com
    byte[] data = outStream.toByteArray();
    outStream.close();
    inSream.close();
    return new String(data, charsetName);
}

From source file:Main.java

public static String readStream(InputStream inStream) throws Exception {
    ByteArrayOutputStream outStream = new ByteArrayOutputStream();
    byte[] buffer = new byte[1024];
    int len = -1;
    while ((len = inStream.read(buffer)) != -1) {
        outStream.write(buffer, 0, len);
    }/*from  w w  w .  j  a  va 2s .  c o m*/
    outStream.close();
    inStream.close();
    return outStream.toString();
}

From source file:Main.java

private static void copyFile(InputStream in, OutputStream out) throws IOException {
    byte[] buffer = new byte[1024];
    int read;/* w  w  w  . j a  v  a  2s.  co  m*/
    while ((read = in.read(buffer)) != -1) {
        out.write(buffer, 0, read);
    }
}

From source file:Main.java

public static byte[] getFileByte(InputStream is) {
    try {//from w w w. j  a v a  2  s  .  co  m
        byte[] buffer = new byte[1024];
        int len = -1;
        ByteArrayOutputStream outStream = new ByteArrayOutputStream();
        while ((len = is.read(buffer)) != -1) {
            outStream.write(buffer, 0, len);
        }
        byte[] data = outStream.toByteArray();
        outStream.close();
        is.close();
        return data;
    } catch (IOException e) {
        e.printStackTrace();
    }
    return null;
}

From source file:Main.java

public static byte[] getByteArrayFromInputStream(InputStream is) throws IOException {
    ByteArrayOutputStream baos = new ByteArrayOutputStream(10000);
    byte[] buffer = new byte[10000];
    int bytes;// www  . j av a2 s . c  o  m
    while ((bytes = is.read(buffer)) != -1) {
        baos.write(buffer, 0, bytes);
    }
    is.close();
    return baos.toByteArray();
}

From source file:cn.org.once.cstack.utils.TestUtils.java

/**
 * Download from github binaries and deploy file.
 *
 * @param path/*from   www.ja v  a2  s  . c o m*/
 * @return
 * @throws IOException
 */
public static MockMultipartFile downloadAndPrepareFileToDeploy(String remoteFile, String path)
        throws IOException {
    URL url;
    File file = new File(remoteFile);
    try (OutputStream outputStream = new FileOutputStream(file)) {
        url = new URL(path);
        InputStream input = url.openStream();

        int read;
        byte[] bytes = new byte[1024];

        while ((read = input.read(bytes)) != -1) {
            outputStream.write(bytes, 0, read);
        }
    } catch (IOException e) {
        StringBuilder msgError = new StringBuilder(512);
        msgError.append(remoteFile);
        msgError.append(",");
        msgError.append(path);
        logger.debug(msgError.toString(), e);
    }
    return new MockMultipartFile("file", file.getName(), "multipart/form-data", new FileInputStream(file));

}

From source file:Main.java

public static String getImageType(InputStream in) {
    if (in == null) {
        return null;
    }/*from ww w .ja  v  a 2  s . c o m*/
    try {
        byte[] bytes = new byte[8];
        in.read(bytes);
        return getImageType(bytes);
    } catch (IOException e) {
        return null;
    }
}

From source file:Main.java

public static byte[] stream2ByteArray(InputStream inStream) throws IOException {
    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    int len = 0;/*w w  w.jav a2s  .c  o  m*/
    byte[] buffer = new byte[1024 * 10];
    while ((len = inStream.read(buffer)) != -1) {
        baos.write(buffer, 0, len);
    }
    byte[] result = baos.toByteArray();
    baos.close();
    inStream.close();
    return result;
}

From source file:Main.java

private static String readFully(InputStream inputStream) throws IOException {
    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    byte[] buffer = new byte[1024];
    int length = 0;
    while ((length = inputStream.read(buffer)) != -1) {
        baos.write(buffer, 0, length);//from   www.  j a  v  a  2  s  . co  m
    }
    return new String(baos.toByteArray());
}