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

private static String readStream(InputStream is) {
    try {//from w w w .  jav a 2  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) {
        e.printStackTrace();
        return "";
    }
}

From source file:Main.java

public static void readBytes(InputStream in, long count) throws IOException {
    for (long i = 0; i < count; i++) {
        try {//from ww  w.j a v  a  2 s .c om
            if (in.read() == -1) {
                throw new AssertionError("Unexpected end of stream after " + i + " bytes");
            }
        } catch (SocketTimeoutException e) {
            throw new AssertionError("Timeout while reading " + count + " bytes (read " + i + " bytes)");
        }
    }
}

From source file:Main.java

/**
 * Equivalent to {@link InputStream#read()} but without checked exceptions.
 *//*from   w  w  w .j  a v a  2 s.c  o m*/
public static int read(InputStream inputStream) {
    try {
        return inputStream.read();
    } catch (final Exception e) {
        throw new RuntimeException(e);
    }
}

From source file:Main.java

public static String readStream(InputStream in) throws IOException {

    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    int i;//from w  w  w .  java  2s .  c  om
    while ((i = in.read()) != -1) {
        baos.write(i);
    }
    return baos.toString();
}

From source file:Main.java

/**
 * Reads and discards all characters from the input stream until a \r\n or EOF is encountered
 * @param in//  ww  w .  j a va2  s  .c  o  m
 * @return
 */
public static int discardUntilNewLine(InputStream in) {
    int ch;
    int num = 0;

    while (true) {
        try {
            ch = in.read();
            if (ch == -1)
                break;
            num++;
            if (ch == '\n')
                break;
        } catch (IOException e) {
            break;
        }
    }
    return num;
}

From source file:Main.java

static public final boolean isGzipStm(InputStream in) throws IOException {
    boolean ms = in.markSupported();
    if (ms)/*  www  .j  a  va 2s . c o m*/
        in.mark(10);
    int b1 = in.read();
    int b2 = in.read();
    if (ms)
        in.reset();
    return ((b2 << 8 | b1) == GZIPInputStream.GZIP_MAGIC);
}

From source file:com.tc.simple.apn.quicktests.Test2.java

public static void send(byte[] p12, String token, int id, String json) {
    IAPNSocketPool factory = new APNSocketPool();
    SocketWrapper socket = null;// ww  w.  j  a  v a 2s  . c o m
    try {

        socket = factory.checkOut(p12, "xxxxxxxxx", false);
        Payload payload = new Payload();
        payload.setJson(json);
        payload.setToken(token);

        byte[] message = new PushByteFactory().buildPushBytes(id, payload);
        socket.write(message);

        InputStream response = socket.getSocket().getInputStream();
        int errorCode = response.read();

        if (errorCode > 0) {
            new APNSocketPool().killSocket(socket);
        } else {

        }

    } catch (java.net.SocketTimeoutException n) {

    } catch (Exception n) {
        n.printStackTrace();
    } finally {
        factory.checkIn(socket);
    }
}

From source file:Main.java

public static String inputStream2String(InputStream is) throws IOException {
    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    int i = -1;/* ww  w  . ja v a  2 s. c o  m*/
    while ((i = is.read()) != -1) {
        baos.write(i);
    }
    return baos.toString();
}

From source file:Main.java

public static String getStrFromInputStream(InputStream is) {
    int i;//  www . j ava2  s.  c om
    char c;
    StringBuilder sb = new StringBuilder();
    try {
        while ((i = is.read()) != -1) {
            c = (char) i;
            sb.append(c);
        }
    } catch (IOException e) {
        e.printStackTrace();
    }
    return sb.toString();
}

From source file:Main.java

/**
 * There are should be at least 4 bytes in input stream. Otherwise result may be not defined.
 * If input stream contains no more data, method return {@link Integer#MAX_VALUE}.
 * //  w w  w  .  java  2s.c om
 * @param input
 * @return Next Integer from input stream.
 * @throws IOException
 */
public static int readInt(InputStream input) throws IOException {
    int value = Integer.MAX_VALUE;
    int buf;
    if ((buf = input.read()) != -1) {
        value = buf;
        value = value | (input.read() << 8);
        value = value | (input.read() << 16);
        value = value | (input.read() << 24);
    }
    return value;
}