Android Reader Read nextPairedString(Reader r, char b, char c)

Here you can find the source of nextPairedString(Reader r, char b, char c)

Description

Parse paired String with given delimiters from Reader.

Parameter

Parameter Description
b opposite delimiter.
c delimiter.
r Reader.

Return

next nested matching String up to c from r.

Declaration

public static String nextPairedString(Reader r, char b, char c) 

Method Source Code

//package com.java2s;
import java.io.IOException;
import java.io.Reader;
import java.io.StringWriter;

public class Main {
    /**/* www. j ava 2s .  co m*/
     * Parse paired String with given delimiters from Reader.
     * @param b opposite delimiter.
     * @param c delimiter.
     * @param r Reader.
     * @return next nested matching String up to c from r.
     */
    public static String nextPairedString(Reader r, char b, char c) {
        StringWriter sw = new StringWriter();
        try {
            int level = 0;
            char buffer;
            int i;
            // read chars != c, ignore new lines ?
            while ((i = r.read()) > -1) {
                buffer = (char) i;
                if (buffer == b) {
                    level++;
                }
                if (buffer == c) {
                    level--;
                    if (level < 0) {
                        break; // skip last closing 'brace' 
                    }
                }
                sw.write(buffer);
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
        return sw.toString();
    }
}

Related

  1. slurpLines(final Reader reader)
  2. slurpValidLines(final Reader reader)
  3. toList(Reader reader)
  4. nextString(Reader r)
  5. nextString(Reader r, char c)