InputStream 을 문자열로 반환하는 넘.
public static String readStringFromStream(InputStream in) throws IOException {   
        StringBuilder sb = new StringBuilder(1024);
   
        for (int i = in.read(); i != -1; i = in.read()) {
            sb.append((char) i);
        }   
        in.close();   
        return sb.toString();
}

InputStream 을 byte array로 반환하는 넘
private static final int DEFAULT_BUFFER_SIZE = 1024 * 4;

public static byte[] readBytesFromStream(InputStream in) throws IOException {
        int i = in.available();
        if (i < DEFAULT_BUFFER_SIZE) {
            i = DEFAULT_BUFFER_SIZE;
        }
        ByteArrayOutputStream bos = new ByteArrayOutputStream(i);
        copy(in, bos);
        in.close();
        return bos.toByteArray();
}

public static int copy(final InputStream input, final OutputStream output) throws IOException {
         return copy(input, output, DEFAULT_BUFFER_SIZE);
}
     
public static int copy(final InputStream input, final OutputStream output, int bufferSize) throws IOException {
        int avail = input.available();
        if (avail > 262144) {
            avail = 262144;
        }
        if (avail > bufferSize) {
            bufferSize = avail;
        }
        final byte[] buffer = new byte[bufferSize];
        int n = 0;
        n = input.read(buffer);
        int total = 0;
        while (-1 != n) {
            output.write(buffer, 0, n);
            total += n;
            n = input.read(buffer);
        }
        return total;
}

Posted by 짱가쟁이