Example usage for java.io CharArrayWriter reset

List of usage examples for java.io CharArrayWriter reset

Introduction

In this page you can find the example usage for java.io CharArrayWriter reset.

Prototype

public void reset() 

Source Link

Document

Resets the buffer so that you can use it again without throwing away the already allocated buffer.

Usage

From source file:Main.java

public static void main(String[] args) {

    CharArrayWriter chw = new CharArrayWriter();

    CharSequence csq = "java2s.com";

    // append character sequence to the writer
    chw.append(csq);//from w  w  w .  j  a v a 2s  . co  m

    // print character sequence
    System.out.println(csq);

    // invoke reset()
    chw.reset();

    csq = "1234567890";

    chw.append(csq);
    System.out.println(chw.toString());

}

From source file:CharArrayWriterDemo.java

public static void main(String args[]) throws IOException {
    CharArrayWriter f = new CharArrayWriter();
    String s = "This should end up in the array";
    char buf[] = new char[s.length()];
    s.getChars(0, s.length(), buf, 0);//from  w w  w .  ja va2  s.c om
    f.write(buf);
    System.out.println(f.toString());

    char c[] = f.toCharArray();
    for (int i = 0; i < c.length; i++) {
        System.out.print(c[i]);
    }

    FileWriter f2 = new FileWriter("test.txt");
    f.writeTo(f2);
    f2.close();
    f.reset();
    for (int i = 0; i < 3; i++)
        f.write('X');
}

From source file:org.apache.jmeter.save.CSVSaveService.java

/**
 * Reads from file and splits input into strings according to the delimiter,
 * taking note of quoted strings.//  w  w w.  j  a  v  a 2  s  .  c  o m
 * <p>
 * Handles DOS (CRLF), Unix (LF), and Mac (CR) line-endings equally.
 * <p>
 * A blank line - or a quoted blank line - both return an array containing
 * a single empty String.
 * @param infile
 *            input file - must support mark(1)
 * @param delim
 *            delimiter (e.g. comma)
 * @return array of strings, will be empty if there is no data, i.e. if the input is at EOF.
 * @throws IOException
 *             also for unexpected quote characters
 */
public static String[] csvReadFile(BufferedReader infile, char delim) throws IOException {
    int ch;
    ParserState state = ParserState.INITIAL;
    List<String> list = new ArrayList<>();
    CharArrayWriter baos = new CharArrayWriter(200);
    boolean push = false;
    while (-1 != (ch = infile.read())) {
        push = false;
        switch (state) {
        case INITIAL:
            if (ch == QUOTING_CHAR) {
                state = ParserState.QUOTED;
            } else if (isDelimOrEOL(delim, ch)) {
                push = true;
            } else {
                baos.write(ch);
                state = ParserState.PLAIN;
            }
            break;
        case PLAIN:
            if (ch == QUOTING_CHAR) {
                baos.write(ch);
                throw new IOException("Cannot have quote-char in plain field:[" + baos.toString() + "]");
            } else if (isDelimOrEOL(delim, ch)) {
                push = true;
                state = ParserState.INITIAL;
            } else {
                baos.write(ch);
            }
            break;
        case QUOTED:
            if (ch == QUOTING_CHAR) {
                state = ParserState.EMBEDDEDQUOTE;
            } else {
                baos.write(ch);
            }
            break;
        case EMBEDDEDQUOTE:
            if (ch == QUOTING_CHAR) {
                baos.write(QUOTING_CHAR); // doubled quote => quote
                state = ParserState.QUOTED;
            } else if (isDelimOrEOL(delim, ch)) {
                push = true;
                state = ParserState.INITIAL;
            } else {
                baos.write(QUOTING_CHAR);
                throw new IOException(
                        "Cannot have single quote-char in quoted field:[" + baos.toString() + "]");
            }
            break;
        default:
            throw new IllegalStateException("Unexpected state " + state);
        } // switch(state)
        if (push) {
            if (ch == '\r') {// Remove following \n if present
                infile.mark(1);
                if (infile.read() != '\n') {
                    infile.reset(); // did not find \n, put the character
                                    // back
                }
            }
            String s = baos.toString();
            list.add(s);
            baos.reset();
        }
        if ((ch == '\n' || ch == '\r') && state != ParserState.QUOTED) {
            break;
        }
    } // while not EOF
    if (ch == -1) {// EOF (or end of string) so collect any remaining data
        if (state == ParserState.QUOTED) {
            throw new IOException("Missing trailing quote-char in quoted field:[\"" + baos.toString() + "]");
        }
        // Do we have some data, or a trailing empty field?
        if (baos.size() > 0 // we have some data
                || push // we've started a field
                || state == ParserState.EMBEDDEDQUOTE // Just seen ""
        ) {
            list.add(baos.toString());
        }
    }
    return list.toArray(new String[list.size()]);
}

From source file:org.commoncrawl.util.ArcFileWriter.java

private String escapeURI(String uri, String charsetEncoding) throws IOException {

    boolean needToChange = false;

    StringBuffer out = new StringBuffer(uri.length());

    Charset charset;//from   w w  w. ja  va  2  s . com

    CharArrayWriter charArrayWriter = new CharArrayWriter();

    if (charsetEncoding == null)
        throw new NullPointerException("charsetName");

    try {
        charset = Charset.forName(charsetEncoding);
    } catch (IllegalCharsetNameException e) {
        throw new UnsupportedEncodingException(charsetEncoding);
    } catch (UnsupportedCharsetException e) {
        throw new UnsupportedEncodingException(charsetEncoding);
    }

    for (int i = 0; i < uri.length();) {
        int c = (int) uri.charAt(i);
        // System.out.println("Examining character: " + c);
        if (dontNeedEncoding.get(c)) {
            out.append((char) c);
            i++;
        } else {
            // convert to external encoding before hex conversion
            do {
                charArrayWriter.write(c);
                /*
                 * If this character represents the start of a Unicode surrogate pair,
                 * then pass in two characters. It's not clear what should be done if
                 * a bytes reserved in the surrogate pairs range occurs outside of a
                 * legal surrogate pair. For now, just treat it as if it were any
                 * other character.
                 */
                if (c >= 0xD800 && c <= 0xDBFF) {
                    /*
                     * System.out.println(Integer.toHexString(c) +
                     * " is high surrogate");
                     */
                    if ((i + 1) < uri.length()) {
                        int d = (int) uri.charAt(i + 1);
                        /*
                         * System.out.println("\tExamining " + Integer.toHexString(d));
                         */
                        if (d >= 0xDC00 && d <= 0xDFFF) {
                            /*
                             * System.out.println("\t" + Integer.toHexString(d) +
                             * " is low surrogate");
                             */
                            charArrayWriter.write(d);
                            i++;
                        }
                    }
                }
                i++;
            } while (i < uri.length() && !dontNeedEncoding.get((c = (int) uri.charAt(i))));

            charArrayWriter.flush();
            String str = new String(charArrayWriter.toCharArray());
            byte[] ba = str.getBytes(charsetEncoding);
            for (int j = 0; j < ba.length; j++) {
                out.append('%');
                char ch = Character.forDigit((ba[j] >> 4) & 0xF, 16);
                // converting to use uppercase letter as part of
                // the hex value if ch is a letter.
                if (Character.isLetter(ch)) {
                    ch -= caseDiff;
                }
                out.append(ch);
                ch = Character.forDigit(ba[j] & 0xF, 16);
                if (Character.isLetter(ch)) {
                    ch -= caseDiff;
                }
                out.append(ch);
            }
            charArrayWriter.reset();
            needToChange = true;
        }
    }

    return (needToChange ? out.toString() : uri);
}