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 readRawTextFile(Context ctx, int resId) {
    InputStream inputStream = ctx.getResources().openRawResource(resId);

    ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();

    int i;/*from w  w w  .j ava  2 s  .  c om*/
    try {
        i = inputStream.read();
        while (i != -1) {
            byteArrayOutputStream.write(i);
            i = inputStream.read();
        }
        inputStream.close();
    } catch (IOException e) {
        return null;
    }
    return byteArrayOutputStream.toString();
}

From source file:dk.netarkivet.common.utils.InputStreamUtils.java

/**
 * Reads a raw line from an InputStream, up till \n. Since HTTP allows \r\n and \n as terminators, this gets the
 * whole line. This code is adapted from org.apache.commons.httpclient.HttpParser
 *
 * @param inputStream A stream to read from.
 * @return Array of bytes read or null if none are available.
 * @throws IOException if the underlying reads fail
 *//*  ww w . j a  v a 2s.  c  o  m*/
public static byte[] readRawLine(InputStream inputStream) throws IOException {
    ByteArrayOutputStream buf = new ByteArrayOutputStream();
    int ch;
    while ((ch = inputStream.read()) >= 0) {
        buf.write(ch);
        if (ch == '\n') {
            break;
        }
    }
    if (buf.size() == 0) {
        return null;
    }
    return buf.toByteArray();
}

From source file:Main.java

/**
 * Returns the ASCII characters up to but not including the next "\r\n", or
 * "\n".//  w  ww  .j  a v  a 2s. c  om
 *
 * @throws java.io.EOFException if the stream is exhausted before the next newline
 * character.
 */
public static String readAsciiLine(InputStream in) throws IOException {
    // TODO: support UTF-8 here instead
    StringBuilder result = new StringBuilder(80);
    while (true) {
        int c = in.read();
        if (c == -1) {
            throw new EOFException();
        } else if (c == '\n') {
            break;
        }

        result.append((char) c);
    }
    int length = result.length();
    if (length > 0 && result.charAt(length - 1) == '\r') {
        result.setLength(length - 1);
    }
    return result.toString();
}

From source file:Main.java

private static String getStringFromRaw(Context context, int id) {
    String str;//  w w  w. j  av a 2 s  . c  o m
    try {
        Resources r = context.getResources();
        InputStream is = r.openRawResource(id);
        ByteArrayOutputStream baos = new ByteArrayOutputStream();
        int i = is.read();
        while (i != -1) {
            baos.write(i);
            i = is.read();
        }

        str = baos.toString();
        is.close();
    } catch (IOException e) {
        str = "";
    }

    return str;
}

From source file:com.github.harmanpa.jrecon.utils.Compression.java

public static byte[] decompress(byte[] data) throws IOException {
    InputStream is = new BZip2CompressorInputStream(new ByteArrayInputStream(data));
    ByteArrayOutputStream baos = new ByteArrayOutputStream(128);
    int b;//  w w  w .  j  av  a  2  s . c  o  m
    while ((b = is.read()) > -1) {
        baos.write(b);
    }
    is.close();
    return baos.toByteArray();
}