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

public static String getStringFromAssets(String strFileName, Resources resources) {
    String result = null;/*from  w  w w  .  j  av  a  2s.c  om*/
    try {
        InputStream in = resources.getAssets().open(strFileName);
        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);
    } catch (Exception e) {
        e.printStackTrace();
    }
    return result;
}

From source file:Main.java

public static String inputStream2String(InputStream in) throws IOException {
    StringBuffer buf = new StringBuffer();
    if (null != in) {
        byte[] b = new byte[BUF];
        int n;/* ww w .j av  a 2s. c  o  m*/
        while ((n = in.read()) != -1) {
            buf.append(new String(b, 0, n));
        }
    }
    return buf.toString();
}

From source file:Main.java

public static void post(String actionUrl, String file) {
    try {//from ww w  . jav a  2 s .  co  m
        URL url = new URL(actionUrl);
        HttpURLConnection con = (HttpURLConnection) url.openConnection();
        con.setDoInput(true);
        con.setDoOutput(true);
        con.setUseCaches(false);
        con.setRequestMethod("POST");
        con.setRequestProperty("Charset", "UTF-8");
        con.setRequestProperty("Content-Type", "multipart/form-data;boundary=*****");
        DataOutputStream ds = new DataOutputStream(con.getOutputStream());
        FileInputStream fStream = new FileInputStream(file);
        int bufferSize = 1024; // 1MB
        byte[] buffer = new byte[bufferSize];
        int bufferLength = 0;
        int length;
        while ((length = fStream.read(buffer)) != -1) {
            bufferLength = bufferLength + 1;
            ds.write(buffer, 0, length);
        }
        fStream.close();
        ds.flush();
        InputStream is = con.getInputStream();
        int ch;
        StringBuilder b = new StringBuilder();
        while ((ch = is.read()) != -1) {
            b.append((char) ch);
        }
        new String(b.toString().getBytes("ISO-8859-1"), "utf-8");
        ds.close();
    } catch (MalformedURLException e) {
        e.printStackTrace();
    } catch (ProtocolException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    }
}

From source file:Main.java

/** Returns whitespace-separated strings from the input stream, or null if the end of the stream has been reached */
public static String readToken(InputStream in) {
    StringBuilder sb = new StringBuilder();
    boolean first = true;
    int ch;//ww  w  .  jav a 2s.co  m
    while (true) {
        try {
            ch = in.read();
            if (ch == -1)
                return sb.length() > 0 ? sb.toString() : null;
            if (Character.isWhitespace(ch)) {
                if (first)
                    continue;
                break;
            }
            sb.append((char) ch);
            first = false;
        } catch (IOException e) {
            break;
        }
    }
    return sb.toString();
}

From source file:Main.java

/** Returns the value associated with the tag<P>
 * Must be invoked after the tag and all attributes have been
 * dealt with//  w ww. java2  s  . c o  m
 */
public static String getValue(InputStream is) {
    errormessage = null;
    try {
        sb.setLength(0);
        for (char c = (char) is.read(); c != '<'; c = (char) is.read())
            sb.append("" + c);
        return sb.toString();
    } catch (Exception s) {
        errormessage = s.getMessage();
        return null;
    }

}

From source file:Main.java

/**
 * Converts a DER-encoded Oid from an InputStream to a dot-separated
 * String representation./*  w ww  .j  a  va 2  s.c  o m*/
 *
 * @param oid DER-encoded Oid InputStream
 * @return Oid in dot-separated String representation
 *
 */
public static byte[] OidStream2DER(InputStream oid) throws IOException {

    int tag;
    int length;
    ByteArrayOutputStream outOid = new ByteArrayOutputStream();

    try {
        tag = oid.read();
        length = oid.read();
        outOid.write(tag);
        outOid.write(length);

        for (int i = 0; i < length; i++) {
            outOid.write(oid.read());
        }
    } catch (IOException e) {
        throw new IOException("I/O Error occurred when reading InputStream");
    }

    return outOid.toByteArray();
}

From source file:com.googlecode.pondskum.client.listener.FileWritingConnectionListener.java

private static List<String> dumpContent(final InputStream incoming) throws IOException {
    InputStream in = new BufferedInputStream(incoming);
    int content;// w ww.  j  a  v  a2s.c o  m
    StringBuilder sb = new StringBuilder();

    while ((content = in.read()) != -1) {
        sb.append((char) content);
    }

    return Arrays.asList(sb.toString());
}

From source file:com.github.jknack.handlebars.InheritanceTest.java

static String toString(final InputStream input) throws IOException {
    StringBuilder buffer = new StringBuilder(1024 * 4);
    int ch;//ww w .  j  a  va 2  s  . co m
    while ((ch = input.read()) != -1) {
        buffer.append((char) ch);
    }
    buffer.trimToSize();
    input.close();
    return buffer.toString();
}

From source file:com.inmobi.conduit.distcp.tools.util.TestThrottledInputStream.java

private static void copyByteByByte(InputStream in, OutputStream out) throws IOException {

    int ch = in.read();
    while (ch >= 0) {
        out.write(ch);//from w w  w . jav  a  2  s. c  o  m
        ch = in.read();
    }
}

From source file:com.ariatemplates.seleniumjavarobot.Main.java

private static void closeOnStreamEnd(final SeleniumJavaRobot seleniumJavaRobot, final InputStream inputStream) {
    Thread thread = new Thread(new Runnable() {
        public void run() {
            try {
                while (inputStream.read() > -1) {
                    // do nothing
                }/*from   w w  w.  j a va  2s .c om*/
            } catch (IOException e) {
            }
            try {
                seleniumJavaRobot.stop();
            } catch (InterruptedException e) {
            }
        }
    });
    thread.setDaemon(true);
    thread.start();
}