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 abstract int read() throws IOException;

Source Link

Document

Reads the next byte of data from the input stream.

Usage

From source file:Main.java

/**
 * read inputstream and return the byte array
 * @param inputStream/*ww w .  j a  v a  2 s.  com*/
 * @return
 * @throws IOException
 */
public static byte[] readBytesFromInputStream(InputStream inputStream) throws IOException {
    ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
    int read = inputStream.read();
    while (read != -1) {
        byteArrayOutputStream.write(read);
        read = inputStream.read();
    }

    return byteArrayOutputStream.toByteArray();
}

From source file:com.krawler.esp.utils.HttpPost.java

public static String GetResponse(String postdata) {
    try {//from   w  w  w .  ja  v  a 2s .  c  om
        String s = URLEncoder.encode(postdata, "UTF-8");
        // URL u = new URL("http://google.com");
        URL u = new URL("http://localhost:7070/service/soap/");
        URLConnection uc = u.openConnection();
        uc.setDoOutput(true);
        uc.setDoInput(true);
        uc.setAllowUserInteraction(false);

        DataOutputStream dstream = new DataOutputStream(uc.getOutputStream());

        // The POST line
        dstream.writeBytes(s);
        dstream.close();

        // Read Response
        InputStream in = uc.getInputStream();
        int x;
        while ((x = in.read()) != -1) {
            System.out.write(x);
        }
        in.close();

        BufferedReader r = new BufferedReader(new InputStreamReader(in));
        StringBuffer buf = new StringBuffer();
        String line;
        while ((line = r.readLine()) != null) {
            buf.append(line);
        }
        return buf.toString();
    } catch (Exception e) {
        // throw e;
        return e.toString();
    }
}

From source file:Main.java

public static String determineEncoding(InputStream stream) throws IOException {
    stream.mark(20000);/*ww  w .j a  v  a2 s .  c  om*/
    try {
        int b0 = stream.read();
        int b1 = stream.read();
        int b2 = stream.read();
        int b3 = stream.read();

        if (b0 == 0xFE && b1 == 0xFF)
            return "UTF-16BE";
        else if (b0 == 0xFF && b1 == 0xFE)
            return "UTF-16LE";
        else if (b0 == 0xEF && b1 == 0xBB && b2 == 0xBF)
            return "UTF-8";
        else if (b0 == 0x00 && b1 == 0x3C && b2 == 0x00 && b3 == 0x3F)
            return "UTF-16BE";
        else if (b0 == 0x3C && b1 == 0x00 && b2 == 0x3F && b3 == 0x00)
            return "UTF-16LE";
        else if (b0 == 0x3C && b1 == 0x3F && b2 == 0x78 && b3 == 0x6D) {
            //            UTF-8, ISO 646, ASCII, some part of ISO 8859, Shift-JIS, EUC, or any other 7-bit, 8-bit, or mixed-width encoding 
            //            which ensures that the characters of ASCII have their normal positions, width, and values; the actual encoding 
            //            declaration must be read to detect which of these applies, but since all of these encodings use the same bit patterns 
            //            for the relevant ASCII characters, the encoding declaration itself may be read reliably
            InputStreamReader rdr = new InputStreamReader(stream, "US-ASCII");
            String hdr = readFirstLine(rdr);
            return extractEncoding(hdr);
        } else
            return null;
    } finally {
        stream.reset();
    }
}

From source file:Main.java

public static final int readInt(InputStream stream, int length) throws IOException {
    int result = 0;
    for (int i = 0; i != length; ++i) {
        int b = stream.read();
        if (b == -1) {
            throw new EOFException();
        }//from   w w  w. ja va2s  . c o  m
        result |= (b << (i * 8));
    }
    return result;
}

From source file:Main.java

public static String loadFromAssetsFile(String fname, Context context) {
    String result = null;//from w ww  .  j av a2  s .c o  m
    try {
        InputStream in = context.getAssets().open(fname);
        int ch = 0;
        ByteArrayOutputStream baos = new ByteArrayOutputStream();
        while ((ch = in.read()) != -1) {
            baos.write(ch);
        }
        byte[] buff = baos.toByteArray();
        baos.close();
        in.close();
        result = new String(buff, "UTF-8");
        result = result.replaceAll("\\r\\n", "\n");
    } catch (Exception e) {
        e.printStackTrace();
    }
    return result;
}

From source file:Main.java

public static byte[] toBytes(InputStream in) throws IOException {
    ByteArrayOutputStream out = new ByteArrayOutputStream();
    int ch;//from   w ww  . j a  v a2  s.  c o m
    while ((ch = in.read()) != -1) {
        out.write(ch);
    }
    byte buffer[] = out.toByteArray();
    out.close();
    return buffer;
}

From source file:Main.java

public static byte[] inputStreamToByte(InputStream is) {
    try {/*from w  ww.  ja  v  a 2  s.  c o  m*/
        ByteArrayOutputStream bytestream = new ByteArrayOutputStream();
        int ch;
        while ((ch = is.read()) != -1) {
            bytestream.write(ch);
        }
        byte imgdata[] = bytestream.toByteArray();
        bytestream.close();
        return imgdata;
    } catch (Exception e) {
        e.printStackTrace();
    }
    return null;
}

From source file:net.sf.firemox.token.CardPropertiesOperation.java

/**
 * Read and return the enum from the given input stream.
 * /*  www .  ja v  a  2  s  .co m*/
 * @param input
 *          the stream containing the enum to read.
 * @return the enum from the given input stream.
 * @throws IOException
 *           If some other I/O error occurs
 */
public static CardPropertiesOperation deserialize(InputStream input) throws IOException {
    return values()[input.read()];
}

From source file:citibob.licensor.MakeLauncher.java

static void exec(File dir, String... cmds) throws IOException, InterruptedException {
    Process proc = Runtime.getRuntime().exec(cmds, null, dir);
    InputStream in = proc.getInputStream();
    int c;//from   w  ww.j  a  v  a 2s.c  om
    while ((c = in.read()) >= 0)
        System.out.write(c);
    proc.waitFor();
    System.out.println("---> exit value = " + proc.exitValue());
}

From source file:Main.java

public static byte[] inputStreamToByte(InputStream is) {
    try {//from   w  w w . j  a  v a2s  . co  m
        ByteArrayOutputStream bytestream = new ByteArrayOutputStream();
        int ch;
        while ((ch = is.read()) != -1) {
            bytestream.write(ch);
        }
        byte imgdata[] = bytestream.toByteArray();
        bytestream.close();
        return imgdata;
    } catch (Exception e) {
        e.printStackTrace();
    }

    return null;
}