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

/**
 * Copy./*from  ww  w  . jav  a  2  s. c om*/
 *
 * @param in the in
 * @param out the out
 * @throws IOException Signals that an I/O exception has occurred.
 */
private static void copy(InputStream in, BufferedOutputStream out) throws IOException {
    int byte_;
    while ((byte_ = in.read()) != -1)
        out.write(byte_);
}

From source file:Main.java

public static void skipAll(InputStream in) throws IOException {
    do {//  w w w.  jav  a  2 s.co m
        in.skip(Long.MAX_VALUE);
    } while (in.read() != -1);
}

From source file:Main.java

public static String readLine(InputStream in) throws IOException {
    StringBuilder result = new StringBuilder();
    for (int c; (c = in.read()) != -1 && c != '\n';) {
        if (c != '\r') {
            result.append((char) c);
        }/* w w w . ja v  a2  s . c  o  m*/
    }
    return result.toString();
}

From source file:MainClass.java

public static long getCRC32(InputStream in) throws IOException {

    Checksum cs = new CRC32();

    // more efficient to read chunks of data at a time
    for (int b = in.read(); b != -1; b = in.read()) {
        cs.update(b);//  www  . j av a2s.  c om
    }
    return cs.getValue();
}

From source file:Main.java

public static long readEncodedLength(InputStream input, int[] bytesReadOut) {
    try {/*w ww . ja  v  a  2s. c  om*/
        int lbyte = input.read();
        if (lbyte == -1)
            return -1;
        if (lbyte == 0)
            throw new IllegalStateException("First length byte is 0");
        // there will always be 24 extra leading zeros from the first 3 bytes
        int nbytes = 1 + Integer.numberOfLeadingZeros(lbyte) - 24;

        // start with the first byte with the leading 1 masked out
        long value = lbyte ^ Integer.highestOneBit(lbyte);
        // read successive bytes, shifting current value each time (big-endian, most significant bytes first)
        for (int i = 1; i < nbytes; i++) {
            lbyte = input.read();
            if (lbyte == -1)
                return -1;
            value = (value << 8) | lbyte;
        }
        if (bytesReadOut != null)
            bytesReadOut[0] = nbytes;
        return value;
    } catch (IOException ex) {
        throw new IllegalStateException(ex);
    }
}

From source file:Main.java

public static final String readLine(InputStream in, String charset) throws IOException {
    int rd = in.read();
    if (rd == -1)
        return null;
    byte r = (byte) rd;
    int i = 0;//ww w  . j  a va 2 s  .  c  o  m
    int l = 50;
    byte[] buf = new byte[l];
    while (r != '\n') {
        if (i >= l - 1) {
            l += 50;
            byte[] old = buf;
            buf = new byte[l];
            System.arraycopy(old, 0, buf, 0, old.length);
        }
        if (r != '\r')
            buf[i++] = r;
        rd = in.read();
        if (rd == -1)
            break;
        r = (byte) rd;
    }
    if (charset == null) {
        return new String(buf, 0, i);
    }
    return new String(buf, 0, i, charset);
}

From source file:Main.java

public static String convertStreamToString(InputStream is) throws IOException {
    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    int i = is.read();
    while (i != -1) {
        baos.write(i);/*from  www .ja v  a 2s.  c om*/
        i = is.read();
    }
    return baos.toString();
}

From source file:Main.java

public static void whois(String query, String server) throws IOException {
    Socket sock = new Socket(server, 43);
    int c = 0;/* w  w  w.j  av  a 2s.c  om*/

    OutputStream os = sock.getOutputStream();
    InputStream is = sock.getInputStream();
    query += "\r\n";
    os.write(query.getBytes("iso8859_1"));

    while (c != -1) {
        c = is.read();
        if (c != -1)
            System.out.println((char) c);
    }
}

From source file:Main.java

private static String getAaptResult(String sdkPath, String apkPath, final Pattern pattern) {
    try {//from  w ww  .  j a v a2s.c o  m
        final File apkFile = new File(apkPath);
        final ByteArrayOutputStream aaptOutput = new ByteArrayOutputStream();
        final String command = getAaptDumpBadgingCommand(sdkPath, apkFile.getName());

        Process process = Runtime.getRuntime().exec(command, null, apkFile.getParentFile());

        InputStream inputStream = process.getInputStream();
        for (int last = inputStream.read(); last != -1; last = inputStream.read()) {
            aaptOutput.write(last);
        }

        String packageId = "";
        final String aaptResult = aaptOutput.toString();
        if (aaptResult.length() > 0) {
            final Matcher matcher = pattern.matcher(aaptResult);
            if (matcher.find()) {
                packageId = matcher.group(1);
            }
        }
        return packageId;
    } catch (IOException e) {
        throw new RuntimeException(e);
    }
}

From source file:Main.java

public static String readStream(InputStream is) {
    try {//  w  w w .j  a v a2 s .  com
        ByteArrayOutputStream bo = new ByteArrayOutputStream();
        int i = is.read();
        while (i != -1) {
            bo.write(i);
            i = is.read();
        }
        return bo.toString();
    } catch (IOException e) {
        return "";
    }
}