All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.luoaijun.utils.net.StreamUtils Maven / Gradle / Ivy

The newest version!
package com.luoaijun.utils.net;

import java.io.BufferedReader;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;


public class StreamUtils {

    /**
     * @param is
     * @return  return
     * @throws Exception
     */
    public static byte[] streamToByteArray(InputStream is) throws Exception {
        ByteArrayOutputStream bos = new ByteArrayOutputStream();
        byte[] b = new byte[1024];
        int len;
        while ((len = is.read(b)) != -1) {
            bos.write(b, 0, len);

        }

        byte[] array = bos.toByteArray();

        bos.close();
        return array;

    }

    /**
     * @param is
     * @return  return
     * @throws Exception
     */
    public static String streamToString(InputStream is) throws Exception {
        BufferedReader reader = new BufferedReader(new InputStreamReader(is));
        StringBuilder builder = new StringBuilder();
        String line;
        while ((line = reader.readLine()) != null) {
            builder.append(line + "\r\n");
        }
        return builder.toString();

    }

}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy