Example usage for java.io InputStreamReader read

List of usage examples for java.io InputStreamReader read

Introduction

In this page you can find the example usage for java.io InputStreamReader read.

Prototype

public int read() throws IOException 

Source Link

Document

Reads a single character.

Usage

From source file:org.botlibre.util.Utils.java

/**
 * Get the contents of the stream to a .self file and parse it.
 *//*w  w  w .  j av a2 s  .  com*/
public static String loadTextFile(InputStream stream, String encoding, int maxSize) {
    if (encoding.trim().isEmpty()) {
        encoding = "UTF-8";
    }

    // FEFF because this is the Unicode char represented by the UTF-8 byte order mark (EF BB BF).
    String UTF8_BOM = "\uFEFF";

    StringWriter writer = new StringWriter();
    InputStreamReader reader = null;
    try {
        reader = new InputStreamReader(stream, encoding);
        int size = 0;
        int next = reader.read();
        boolean first = true;
        while (next >= 0) {
            if (first && next == UTF8_BOM.charAt(0)) {
                // skip
            } else {
                writer.write(next);
            }
            next = reader.read();
            if (size > maxSize) {
                throw new BotException(
                        "File size limit exceeded: " + size + " > " + maxSize + " token: " + next);
            }
            size++;
        }
    } catch (IOException exception) {
        throw new BotException("IO Error: " + exception.getMessage(), exception);
    } finally {
        if (reader != null) {
            try {
                reader.close();
            } catch (IOException ignore) {
            }
        }
        if (stream != null) {
            try {
                stream.close();
            } catch (IOException ignore) {
            }
        }
    }
    return writer.toString();
}

From source file:com.jaxio.celerio.util.IOUtil.java

/**
 * Write to a string the bytes read from an input stream.
 *
 * @param charset the charset used to read the input stream
 * @return the inputstream as a string//from   w  w  w.  ja  v a 2 s  .co m
 */
public String inputStreamToString(InputStream is, String charset) throws IOException {
    InputStreamReader isr = null;
    if (null == charset) {
        isr = new InputStreamReader(is);
    } else {
        isr = new InputStreamReader(is, charset);
    }
    StringWriter sw = new StringWriter();
    int c = -1;
    while ((c = isr.read()) != -1) {
        sw.write(c);
    }
    isr.close();
    return sw.getBuffer().toString();
}

From source file:com.ficeto.esp.EspExceptionDecoder.java

private int listenOnProcess(String[] arguments) {
    try {/*from  w w w.  j  a v a  2s .  c  om*/
        final Process p = ProcessUtils.exec(arguments);
        Thread thread = new Thread() {
            public void run() {
                try {
                    InputStreamReader reader = new InputStreamReader(p.getInputStream());
                    int c;
                    String line = "";
                    while ((c = reader.read()) != -1) {
                        if ((char) c == '\r')
                            continue;
                        if ((char) c == '\n') {
                            printLine(line);
                            line = "";
                        } else {
                            line += (char) c;
                        }
                    }
                    printLine(line);
                    reader.close();

                    reader = new InputStreamReader(p.getErrorStream());
                    while ((c = reader.read()) != -1)
                        System.err.print((char) c);
                    reader.close();
                } catch (Exception e) {
                }
            }
        };
        thread.start();
        int res = p.waitFor();
        thread.join();
        return res;
    } catch (Exception e) {
    }
    return -1;
}

From source file:com.hp.avmon.home.service.LicenseService.java

public void callLinuxCpuId() {
    Runtime rt = Runtime.getRuntime();
    Process p = null;// w  w  w .j a v a  2  s.c o  m
    try {
        String vbexepath = getLicensePath() + "/getcpusn";
        p = rt.exec(vbexepath);

        InputStreamReader isr_normal = new InputStreamReader(p.getInputStream());
        int ch = 0;
        StringBuffer strbuf = new StringBuffer();
        while ((ch = isr_normal.read()) != -1) {
            strbuf.append((char) ch);
        }
        p.waitFor();

    } catch (Exception e) {
        // e.printStackTrace();
    }
}

From source file:com.hp.avmon.home.service.LicenseService.java

public void callWindowsCpuId() {
    Runtime rt = Runtime.getRuntime();
    Process p = null;/*w  w  w. j a  v  a 2  s  . c o  m*/
    try {
        String exportPath = getLicensePath();
        String vbexepath = "cmd.exe /c" + exportPath + "/" + "vbcpuid_server.exe";
        p = rt.exec(vbexepath);

        InputStreamReader isr_normal = new InputStreamReader(p.getInputStream());
        int ch = 0;
        StringBuffer strbuf = new StringBuffer();
        while ((ch = isr_normal.read()) != -1) {
            strbuf.append((char) ch);
        }
        p.waitFor();

    } catch (Exception e) {
        // e.printStackTrace();
    }
}

From source file:org.alloy.metal.xml.merge.MergeContext.java

public String serialize(InputStream in) {
    InputStreamReader reader = null;
    int temp;//from   w w  w . j a  v  a 2  s . co  m
    StringBuilder item = new StringBuilder();
    boolean eof = false;
    try {
        reader = new InputStreamReader(in);
        while (!eof) {
            temp = reader.read();
            if (temp == -1) {
                eof = true;
            } else {
                item.append((char) temp);
            }
        }
    } catch (IOException e) {
        LOG.error("Unable to merge source and patch locations", e);
    } finally {
        if (reader != null) {
            try {
                reader.close();
            } catch (Throwable e) {
                LOG.error("Unable to merge source and patch locations", e);
            }
        }
    }

    return item.toString();
}

From source file:com.panet.imeta.trans.steps.textfileinput.TextFileInput.java

public static final String getLine(LogWriter log, InputStreamReader reader, int formatNr, StringBuilder line)
        throws KettleFileException {
    int c = 0;/*from  w ww. java2  s . c  o  m*/
    line.setLength(0);

    try {
        switch (formatNr) {
        case TextFileInputMeta.FILE_FORMAT_DOS: {
            while (c >= 0) {
                c = reader.read();

                if (c == '\r' || c == '\n') {
                    c = reader.read(); // skip \n and \r
                    if (c != '\r' && c != '\n') {
                        // make sure its really a linefeed or cariage return
                        // raise an error this is not a DOS file
                        // so we have pulled a character from the next line
                        throw new KettleFileException(Messages.getString("TextFileInput.Log.SingleLineFound"));
                    }
                    return line.toString();
                }
                if (c >= 0)
                    line.append((char) c);
            }
        }
            break;
        case TextFileInputMeta.FILE_FORMAT_UNIX: {
            while (c >= 0) {
                c = reader.read();

                if (c == '\n' || c == '\r') {
                    return line.toString();
                }
                if (c >= 0)
                    line.append((char) c);
            }
        }
            break;
        case TextFileInputMeta.FILE_FORMAT_MIXED:
        // in mixed mode we suppose the LF is the last char and CR is ignored
        // not for MAC OS 9 but works for Mac OS X. Mac OS 9 can use UNIX-Format
        {
            while (c >= 0) {
                c = reader.read();

                if (c == '\n') {
                    return line.toString();
                } else if (c != '\r') {
                    if (c >= 0)
                        line.append((char) c);
                }
            }
        }
            break;
        }
    } catch (KettleFileException e) {
        throw e;
    } catch (Exception e) {
        if (line.length() == 0) {
            throw new KettleFileException(
                    Messages.getString("TextFileInput.Log.Error.ExceptionReadingLine", e.toString()), e);
        }
        return line.toString();
    }
    if (line.length() > 0)
        return line.toString();

    return null;
}

From source file:com.navjagpal.fileshare.WebServer.java

private void handleLoginRequest(DefaultHttpServerConnection serverConnection, HttpRequest request,
        RequestLine requestLine) throws HttpException, IOException {

    BasicHttpEntityEnclosingRequest enclosingRequest = new BasicHttpEntityEnclosingRequest(
            request.getRequestLine());//w w  w .j  a  v  a2s  .c  o m
    serverConnection.receiveRequestEntity(enclosingRequest);

    InputStream input = enclosingRequest.getEntity().getContent();
    InputStreamReader reader = new InputStreamReader(input);

    StringBuffer form = new StringBuffer();
    while (reader.ready()) {
        form.append((char) reader.read());
    }
    String password = form.substring(form.indexOf("=") + 1);
    if (password.equals(mSharedPreferences.getString(FileSharingService.PREFS_PASSWORD, ""))) {
        HttpResponse response = new BasicHttpResponse(new HttpVersion(1, 1), 302, "Found");
        response.addHeader("Location", "/");
        response.addHeader("Set-Cookie", "id=" + createCookie());
        response.setEntity(new StringEntity(getHTMLHeader() + "Success!" + getHTMLFooter()));
        serverConnection.sendResponseHeader(response);
        serverConnection.sendResponseEntity(response);
    } else {
        HttpResponse response = new BasicHttpResponse(new HttpVersion(1, 1), 401, "Unauthorized");
        response.setEntity(
                new StringEntity(getHTMLHeader() + "<p>Login failed.</p>" + getLoginForm() + getHTMLFooter()));
        serverConnection.sendResponseHeader(response);
        serverConnection.sendResponseEntity(response);
    }
}

From source file:org.sakaiproject.tool.assessment.qti.helper.AuthoringXml.java

/**
 * get a template as a string from its input stream
 * @param templateName//  ww  w .  j  a va 2 s.  co  m
 * @return the xml string
 */
public String getTemplateAsString(InputStream templateStream) {
    InputStreamReader reader;
    String xmlString = null;
    try {
        reader = new InputStreamReader(templateStream);
        StringWriter out = new StringWriter();
        int c;

        while ((c = reader.read()) != -1) {
            out.write(c);
        }

        reader.close();
        xmlString = (String) out.toString();
    } catch (Exception e) {
        log.error(e.getMessage(), e);
    }

    return xmlString;

}

From source file:org.pentaho.platform.dataaccess.datasource.wizard.csv.CsvUtils.java

protected String getLines(String fileLocation, int rows, String encoding) {
    File file = new File(fileLocation);

    // read one line, including all EOL characters
    InputStream in;//from   www.j a  va 2 s  . com
    InputStreamReader inr = null;
    StringBuilder line = new StringBuilder();
    int count = 0;
    try {
        in = new FileInputStream(file);
        inr = new InputStreamReader(in, encoding);

        int c = inr.read();
        boolean looking = true;
        while (looking && c > 0) {
            line.append((char) c);
            if (c == '\r' || c == '\n') {
                // look at the next char
                c = inr.read();
                if (c == '\r' || c == '\n') {
                    line.append((char) c);
                    c = inr.read();
                }
                count++;
                if (count == rows) {
                    looking = false;
                }
            } else {
                c = inr.read();
            }
        }
    } catch (IOException e) {
        //do nothing
    } finally {
        if (inr != null) {
            try {
                inr.close();
            } catch (IOException e) {
                // ignore this one
            }
        }
    }
    return line.toString();

}