Example usage for java.io StringReader read

List of usage examples for java.io StringReader read

Introduction

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

Prototype

public int read(char cbuf[], int off, int len) throws IOException 

Source Link

Document

Reads characters into a portion of an array.

Usage

From source file:Main.java

private static void __htmlTextToPlainText(String text, Writer writer) throws IOException {
    StringReader sr = new StringReader(text);
    char[] chunk = new char[1024 * 64];

    while (true) {
        int charsRead = sr.read(chunk, 0, chunk.length);
        if (charsRead <= 0)
            break;

        int lastPos = 0;
        for (int i = 0; i < chunk.length; i++) {
            if (chunk[i] == '<') {
                writer.write(chunk, lastPos, i - lastPos);
                writer.write("&lt;");
                lastPos = i + 1;// w  w  w .j av  a  2  s .  c  o m
            } else if (chunk[i] == '>') {
                writer.write(chunk, lastPos, i - lastPos);
                writer.write("&gt;");
                lastPos = i + 1;
            }
        }
        if (lastPos < chunk.length) {
            writer.write(chunk, lastPos, chunk.length - lastPos);
        }
    }
}