Convert Byte Array To InputStream - Java Network

Java examples for Network:URL Download

Description

Convert Byte Array To InputStream

Demo Code



import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.InputStream;

public class ByteArrayToInputStream {

    public static final void main(String[] args) {
        String string = "this is a test";
        byte[] content = string.getBytes();
        int size = content.length;
        InputStream is = null;/*from  www  .  java  2 s  .com*/
        byte[] b = new byte[size];

        try {
            is = new ByteArrayInputStream(content);
            is.read(b);
            System.out.println(new String(b));
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            if (is != null) {
                try {
                    is.close();
                } catch (IOException e) {

                } finally {
                    is = null;
                }
            }
        }
    }
}

Related Tutorials