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 int read(byte b[], int off, int len) throws IOException 

Source Link

Document

Reads up to len bytes of data from the input stream into an array of bytes.

Usage

From source file:Main.java

public static int readShort(InputStream stream) throws IOException {
    final byte[] tmp = new byte[2];
    stream.read(tmp, 0, 2);
    return (tmp[1] & 0xFF) + ((tmp[0] & 0xFF) << 8);
}

From source file:Main.java

public static long readInt(InputStream stream) throws IOException {
    final byte[] tmp = new byte[4];
    stream.read(tmp, 0, 4);
    return (((long) (tmp[0] & 0xFF)) << 24) + +((tmp[1] & 0xFF) << 16) + +((tmp[2] & 0xFF) << 8)
            + +(tmp[3] & 0xFF);/* w ww.j a  va  2s. c om*/
}

From source file:Main.java

public static int readInt(InputStream stream) {
    final byte[] tmp = new byte[4];
    try {/* w w  w  .j  av  a 2s  .  co m*/
        stream.read(tmp, 0, 4);
    } catch (IOException e) {
        return -1;
    }
    return (tmp[0] << 24) + ((tmp[1] & 0xFF) << 16) + ((tmp[2] & 0xFF) << 8) + (tmp[3] & 0xFF);
}

From source file:Main.java

/**
 * Implements InputStream.read(int) in terms of InputStream.read(byte[], int, int).
 * InputStream assumes that you implement InputStream.read(int) and provides default
 * implementations of the others, but often the opposite is more efficient.
 *///from  w  w  w .j av  a  2  s  .co m
public static int readSingleByte(InputStream in) throws IOException {
    byte[] buffer = new byte[1];
    int result = in.read(buffer, 0, 1);
    return (result != -1) ? buffer[0] & 0xff : -1;
}

From source file:Main.java

public static int readShort(InputStream stream) {
    final byte[] tmp = new byte[2];
    try {//from w w w.ja  v  a 2s  . co m
        stream.read(tmp, 0, 2);
    } catch (IOException e) {
        return -1;
    }
    //   int i = ((tmp[1] & 0xFF) + ((tmp[0] & 0xFF) << 8));
    //   if (i > Short.MAX_VALUE)
    //   System.out.println("i = " + i);
    return ((tmp[1] & 0xFF) + ((tmp[0] & 0xFF) << 8));
}

From source file:Main.java

public static byte[] InputStream2Bytes(InputStream is) {
    String str = "";
    byte[] readByte = new byte[1024];
    try {/*w ww .jav a  2 s .  c  om*/
        while (is.read(readByte, 0, 1024) != -1) {
            str += new String(readByte).trim();
        }
        return str.getBytes();
    } catch (Exception e) {
        e.printStackTrace();
    }
    return null;
}

From source file:Main.java

/**
 * Reads all data from stream and close it silently
 *
 * @param is Input stream//from   ww w .  j av a 2 s.c o m
 */
public static void readAndCloseStream(InputStream is) {
    final byte[] bytes = new byte[DEFAULT_BUFFER_SIZE];
    try {
        while (is.read(bytes, 0, DEFAULT_BUFFER_SIZE) != -1) {
        }
    } catch (IOException e) {
        // Do nothing
    } finally {
        closeSilently(is);
    }
}

From source file:Main.java

public static byte[] readFileContents(InputStream input) throws IOException {
    byte contents[] = new byte[10000], buf[] = new byte[1024];
    InputStream in = new BufferedInputStream(input);
    int bytes_read = 0;

    for (;;) {//ww w . j  a  va2  s .c o  m
        int tmp = in.read(buf, 0, buf.length);
        if (tmp == -1)
            break;
        System.arraycopy(buf, 0, contents, bytes_read, tmp);
        bytes_read += tmp;
    }

    byte[] retval = new byte[bytes_read];
    System.arraycopy(contents, 0, retval, 0, bytes_read);
    return retval;
}

From source file:Main.java

public static String getTypeByStream(InputStream is) {
    byte[] b = new byte[4];
    try {/*  w  ww . j  av a2  s .c o  m*/
        is.read(b, 0, b.length);
    } catch (IOException e) {
        e.printStackTrace();
    }
    String type = bytesToHexString(b).toUpperCase();
    if (type.contains("FFD8FF")) {
        return "jpg";
    } else if (type.contains("89504E47")) {
        return "png";
    } else if (type.contains("47494638")) {
        return "gif";
    } else if (type.contains("49492A00")) {
        return "tif";
    } else if (type.contains("424D")) {
        return "bmp";
    }
    return type;
}

From source file:Main.java

public static String readFile(Context context, String file, String code) {
    int len = 0;//w  w w.ja va 2  s . co m
    byte[] buf = null;
    String grammar = "";
    try {
        InputStream in = context.getAssets().open(file);
        len = in.available();
        buf = new byte[len];
        in.read(buf, 0, len);

        grammar = new String(buf, code);
    } catch (Exception e) {
        e.printStackTrace();
    }
    return grammar;
}