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 byte[] InputStreamToByte(InputStream is) throws IOException {
    ByteArrayOutputStream bytestream = new ByteArrayOutputStream();
    int ch;//  w  w w . j  a  v  a 2  s . c  o  m
    while ((ch = is.read()) != -1) {
        bytestream.write(ch);
    }
    byte imgdata[] = bytestream.toByteArray();
    bytestream.close();

    return imgdata;
}

From source file:Main.java

public static byte[] InputStreamToByte(InputStream is) throws IOException {

    ByteArrayOutputStream bytestream = new ByteArrayOutputStream();
    int ch;/*from  ww  w  .  j av a  2  s  .c  o  m*/
    while ((ch = is.read()) != -1) {
        bytestream.write(ch);
    }
    byte byteData[] = bytestream.toByteArray();
    bytestream.close();
    return byteData;
}

From source file:Main.java

public static String drain(InputStream inputStream) throws IOException {
    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    int buffer;/*from  ww  w.ja va  2  s.  c  om*/
    while ((buffer = inputStream.read()) >= 0) {
        baos.write(buffer);
    }
    return baos.toString();
}

From source file:ch.aonyx.broker.ib.api.util.InputStreamUtils.java

private static byte readByte(final InputStream inputStream) {
    try {/*from w  ww  .ja v a 2  s .com*/
        final byte c = (byte) inputStream.read();
        return c;
    } catch (final IOException e) {
        final String detailedMessage = "problem reading byte";
        LOGGER.error("{}: ", detailedMessage, e);
        throw new IOStreamException(INPUT_OUTPUT_STREAM_EXCEPTION, detailedMessage, e);
    }
}

From source file:Main.java

/**
 * Returns the ASCII characters up to but not including the next "\r\n", or
 * "\n"./*from w  ww.j  ava2  s  . c  o  m*/
 *
 * @throws java.io.EOFException if the stream is exhausted before the next newline
 *                              character.
 */
public static String readAsciiLine(InputStream in) throws IOException {
    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

/**
 * Reads until a '\n', and returns the line as a string, with '\n'
 * included.//from w  ww.ja v a2  s  .co  m
 *
 * Returns the empty string if EOF is reached.
 */
public static String readLine(InputStream input) throws IOException {
    StringBuilder builder = new StringBuilder();
    while (true) {
        int v = input.read();
        if (v == -1) {
            break;
        }

        char ch = (char) v;
        builder.append(ch);

        if (ch == '\n') {
            break;
        }
    }
    return builder.toString();
}

From source file:Main.java

public static String readContents(InputStream input) {
    StringBuilder sb = new StringBuilder();
    int ch;/*from www. java  2 s.co  m*/
    while (true) {
        try {
            ch = input.read();
            if (ch != -1)
                sb.append((char) ch);
            else
                break;
        } catch (IOException e) {
            break;
        }
    }
    return sb.toString();
}

From source file:com.elit2.app.model.FileUpload.java

public static boolean proccessFile(String path, FileItemStream item) {
    try {/* ww w.j a v a  2s  .  c o  m*/
        File f = new File(path + File.separator + "images");
        if (!f.exists()) {
            f.mkdir();
        }
        File savedFile = new File(f.getAbsolutePath() + File.separator + item.getName());
        FileOutputStream fos = new FileOutputStream(savedFile);
        InputStream is = item.openStream();

        int x = 0;
        byte[] b = new byte[1024];
        while ((x = is.read()) != -1) {
            fos.write(b, 0, x);
        }
        fos.flush();
        fos.close();
        return true;
    } catch (Exception ex) {
        ex.printStackTrace();
    }
    return false;
}

From source file:Main.java

public static String ConvertToString(InputStream is) {
    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    int i = -1;// ww  w.  j a va2 s. c om
    try {
        while ((i = is.read()) != -1) {
            baos.write(i);

        }
        return baos.toString();
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    } finally {
        try {
            is.close();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }

    return "";
}

From source file:Main.java

public static void skip(InputStream stream, int numBytes) throws IOException {
    numBytes -= stream.skip(numBytes);//from   w ww  .j a v  a  2 s.  c  o  m
    for (; numBytes > 0; --numBytes) {
        if (stream.read() == -1) {
            throw new IOException("Unexpected end of stream");
        }
    }
}