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(char cbuf[], int offset, int length) throws IOException 

Source Link

Document

Reads characters into a portion of an array.

Usage

From source file:com.wanikani.wklib.Connection.java

private static String readStream(Meter meter, InputStream is) throws IOException {
    InputStreamReader ir;
    StringBuffer sb;/*  w ww  . j  a va 2 s  .  co m*/
    char buf[];
    int rd;

    buf = new char[1024];
    sb = new StringBuffer();
    ir = new InputStreamReader(is, "UTF-8");
    while (true) {
        rd = ir.read(buf, 0, buf.length);
        if (rd < 0)
            break;
        meter.count(rd);
        sb.append(buf, 0, rd);
    }

    meter.sync();
    return sb.toString();
}

From source file:org.ourbeehive.mbp.io.FileHandler.java

/**
 * Read file with the specified encoding.
 * /*w  ww  .java  2  s  .  c  o m*/
 * @param fileName
 *            java.lang.String
 * @param enc
 *            java.lang.String
 * @return java.lang.String
 * @throws IOException
 */
public static String readTextFile(String fileName, String enc) throws IOException {

    InputStreamReader in = null;
    StringBuffer all = new StringBuffer();
    int bufferLength = 512;
    int readLength = 0;
    char[] ch = new char[bufferLength];

    try {
        in = new InputStreamReader(new FileInputStream(fileName), enc);

        readLength = in.read(ch, 0, bufferLength);
        while (readLength != -1) {
            all.append(ch, 0, readLength);
            readLength = in.read(ch, 0, bufferLength);
        }
    } finally {
        // try {
        if (in != null) {
            in.close();
        }
        // } catch (Exception ex) {
        // throw new ReadFileException(ex);
        // }
    }

    return all.toString();
}

From source file:com.sun.faces.generate.RenderKitSpecificationGenerator.java

public static void appendResourceToStringBuffer(String resourceName, StringBuffer sb) throws Exception {
    InputStreamReader isr = null;
    URL url = null;/*from  w ww  . j  a va  2  s  . c  om*/
    URLConnection conn = null;
    char[] chars = new char[1024];
    int len = 0;

    url = getCurrentLoader(sb).getResource(resourceName);
    conn = url.openConnection();
    conn.setUseCaches(false);
    isr = new InputStreamReader(conn.getInputStream());
    while (-1 != (len = isr.read(chars, 0, 1024))) {
        sb.append(chars, 0, len);
    }
    isr.close();
}

From source file:net.grinder.util.LogCompressUtils.java

/**
 * Compress multiple Files with the given encoding.
 *
 * @param logFiles     files to be compressed
 * @param fromEncoding log file encoding
 * @param toEncoding   compressed log file encoding
 * @return compressed file byte array/*w w w. j  ava2 s .co m*/
 */
public static byte[] compress(File[] logFiles, Charset fromEncoding, Charset toEncoding) {
    FileInputStream fis = null;
    InputStreamReader isr = null;
    ByteArrayOutputStream out = null;
    ZipOutputStream zos = null;
    OutputStreamWriter osw = null;
    if (toEncoding == null) {
        toEncoding = Charset.defaultCharset();
    }
    if (fromEncoding == null) {
        fromEncoding = Charset.defaultCharset();
    }
    try {
        out = new ByteArrayOutputStream();
        zos = new ZipOutputStream(out);
        osw = new OutputStreamWriter(zos, toEncoding);
        for (File each : logFiles) {
            try {
                fis = new FileInputStream(each);
                isr = new InputStreamReader(fis, fromEncoding);
                ZipEntry zipEntry = new ZipEntry(each.getName());
                zipEntry.setTime(each.lastModified());
                zos.putNextEntry(zipEntry);
                char[] buffer = new char[COMPRESS_BUFFER_SIZE];
                int count;
                while ((count = isr.read(buffer, 0, COMPRESS_BUFFER_SIZE)) != -1) {
                    osw.write(buffer, 0, count);
                }
                osw.flush();
                zos.flush();
                zos.closeEntry();
            } catch (IOException e) {
                LOGGER.error("Error occurs while compressing {} : {}", each.getAbsolutePath(), e.getMessage());
                LOGGER.debug("Details ", e);
            } finally {
                IOUtils.closeQuietly(isr);
                IOUtils.closeQuietly(fis);
            }
        }
        zos.finish();
        zos.flush();
        return out.toByteArray();
    } catch (IOException e) {
        LOGGER.error("Error occurs while compressing log : {} ", e.getMessage());
        LOGGER.debug("Details : ", e);
        return null;
    } finally {
        IOUtils.closeQuietly(zos);
        IOUtils.closeQuietly(out);
        IOUtils.closeQuietly(osw);
    }
}

From source file:info.staticfree.android.units.Units.java

/**
 * Read an InputStream into a String until it hits EOF.
 *
 * @param in//from w w  w  .  j  a v  a 2s .co  m
 * @return the complete contents of the InputStream
 * @throws IOException
 */
static public String inputStreamToString(InputStream in) throws IOException {
    final int bufsize = 8196;
    final char[] cbuf = new char[bufsize];

    final StringBuffer buf = new StringBuffer(bufsize);

    final InputStreamReader in_reader = new InputStreamReader(in);

    for (int readBytes = in_reader.read(cbuf, 0, bufsize); readBytes > 0; readBytes = in_reader.read(cbuf, 0,
            bufsize)) {
        buf.append(cbuf, 0, readBytes);
    }

    return buf.toString();
}

From source file:TextBoxMIDlet.java

protected void startApp() {
    String str = null;//from   ww  w.  j  av a  2  s  . co  m
    try {
        InputStream is = getClass().getResourceAsStream("resources/text.txt");
        InputStreamReader r = new InputStreamReader(is);
        char[] buffer = new char[32];
        StringBuffer sb = new StringBuffer();
        int count;
        while ((count = r.read(buffer, 0, buffer.length)) > -1) {
            sb.append(buffer, 0, count);
        }
        str = sb.toString();
    } catch (IOException ex) {
        str = "Failed to load text";
    }
    textBox = new TextBox("TextBox Example", str, MAX_TEXT_SIZE, TextField.ANY);

    Ticker ticker = new Ticker("This is a ticker...");
    textBox.setTicker(ticker);
    display = Display.getDisplay(this);
    display.setCurrent(textBox);

}

From source file:TextBoxMIDlet.java

protected void startApp() {
    String str = null;//  ww  w. j av a2  s.c o m
    try {
        InputStream is = getClass().getResourceAsStream("resources/text.txt");
        InputStreamReader r = new InputStreamReader(is);
        char[] buffer = new char[32];
        StringBuffer sb = new StringBuffer();
        int count;
        while ((count = r.read(buffer, 0, buffer.length)) > -1) {
            sb.append(buffer, 0, count);
        }
        str = sb.toString();
    } catch (IOException ex) {
        str = "Failed to load text";
    }
    textBox = new TextBox("TextBox Example", str, MAX_TEXT_SIZE, TextField.ANY);
    Ticker ticker = new Ticker("This is a ticker...");
    textBox.setTicker(ticker);
    textBox.addCommand(OK_COMMAND);
    textBox.addCommand(EXIT_COMMAND);
    textBox.addCommand(CLEAR_COMMAND);
    textBox.addCommand(REVERSE_COMMAND);
    textBox.setCommandListener(this);
    display = Display.getDisplay(this);
    display.setCurrent(textBox);
}

From source file:gov.va.vinci.leo.tools.AsciiFilter.java

/**
 * @see gov.va.vinci.leo.tools.TextFilter#filter(java.io.InputStream, java.lang.String)
 * @param inputStream the input stream to read from.
 * @param charsetName the characterset the document is in.
 * @return New String with filtered Text
 * @throws java.io.IOException if there is an error reading the stream
 */// w ww  .j a v a 2  s.  c o m
@Override
public String filter(InputStream inputStream, String charsetName) throws IOException {
    if (inputStream == null)
        return null;
    if (StringUtils.isBlank(charsetName)) {
        throw new IllegalArgumentException("Character Set Name required");
    } //if

    InputStreamReader isr = new InputStreamReader(inputStream, charsetName);
    StringBuilder sb = new StringBuilder();
    int numRead = 0;
    char[] chars = new char[1024];
    while ((numRead = isr.read(chars, 0, 1024)) > -1) {
        sb.append(chars, 0, numRead);
    } //while
    return AsciiService.toASCII8(sb.toString());
}

From source file:gov.va.vinci.leo.tools.XmlFilter.java

/**
 * @see gov.va.vinci.leo.tools.TextFilter#filter(java.io.InputStream, java.lang.String)
 * @param inputStream An input stream that is read in, filtered, and returned as a string.
 * @param charsetName  A charset to use for the stream.
 * @return the text filtered. Non XML-1.0 characters become spaces.
 * @throws java.io.IOException of the stream cannot be read.
 *///from w  w  w  . j av a  2  s . c  o m
@Override
public String filter(InputStream inputStream, String charsetName) throws IOException {
    if (inputStream == null) {
        return null;
    }
    if (StringUtils.isBlank(charsetName)) {
        throw new IllegalArgumentException("Character Set Name required");
    } //if
      //Build the string from the InputStreamReader
    InputStreamReader isr = new InputStreamReader(inputStream, charsetName);
    StringBuilder sb = new StringBuilder();
    int numRead = 0;
    char[] chars = new char[1024];
    while ((numRead = isr.read(chars, 0, 1024)) > -1) {
        sb.append(chars, 0, numRead);
    } //while
    return XmlFilter.toXml10(sb.toString());
}

From source file:TextBox2MIDlet.java

protected void startApp() {
    if (!started) {
        // First time through - initialize            
        // Get the text to be displayed
        String str = null;/*  www  .  ja v  a2s. c  o m*/
        try {
            InputStream is = getClass().getResourceAsStream("test.txt");
            InputStreamReader r = new InputStreamReader(is);
            char[] buffer = new char[32];
            StringBuffer sb = new StringBuffer();
            int count;
            while ((count = r.read(buffer, 0, buffer.length)) > -1) {
                sb.append(buffer, 0, count);
            }
            str = sb.toString();
        } catch (IOException ex) {
            str = "Failed to load text";
        }

        // Create the TextBox
        textBox = new TextBox("TextBox Example", str, MAX_TEXT_SIZE, TextField.ANY);

        // Create a ticker and install it
        Ticker ticker = new Ticker("This is a ticker...");
        textBox.setTicker(ticker);

        // Install the TextBox as the current screen
        display = Display.getDisplay(this);
        display.setCurrent(textBox);

        started = true;
    }
}