Search in sources :

Example 11 with Reader

use of java.io.Reader in project groovy by apache.

the class IOGroovyMethods method eachLine.

/**
     * Iterates through the given reader line by line.  Each line is passed to the
     * given 1 or 2 arg closure. If the closure has two arguments, the line count is passed
     * as the second argument. The Reader is closed before this method returns.
     *
     * @param self      a Reader, closed after the method returns
     * @param firstLine the line number value used for the first line (default is 1, set to 0 to start counting from 0)
     * @param closure   a closure which will be passed each line (or for 2 arg closures the line and line count)
     * @return the last value returned by the closure
     * @throws IOException if an IOException occurs.
     * @since 1.5.7
     */
public static <T> T eachLine(Reader self, int firstLine, @ClosureParams(value = FromString.class, options = { "String", "String,Integer" }) Closure<T> closure) throws IOException {
    BufferedReader br;
    int count = firstLine;
    T result = null;
    if (self instanceof BufferedReader)
        br = (BufferedReader) self;
    else
        br = new BufferedReader(self);
    try {
        while (true) {
            String line = br.readLine();
            if (line == null) {
                break;
            } else {
                result = callClosureForLine(closure, line, count);
                count++;
            }
        }
        Reader temp = self;
        self = null;
        temp.close();
        return result;
    } finally {
        closeWithWarning(self);
        closeWithWarning(br);
    }
}
Also used : BufferedReader(java.io.BufferedReader) Reader(java.io.Reader) InputStreamReader(java.io.InputStreamReader) BufferedReader(java.io.BufferedReader) FromString(groovy.transform.stc.FromString)

Example 12 with Reader

use of java.io.Reader in project groovy by apache.

the class IOGroovyMethods method splitEachLine.

/**
     * Iterates through the given reader line by line, splitting each line using
     * the given regex separator Pattern. For each line, the given closure is called with
     * a single parameter being the list of strings computed by splitting the line
     * around matches of the given regular expression.  The Reader is closed afterwards.
     * <p>
     * Here is an example:
     * <pre>
     * def s = 'The 3 quick\nbrown 4 fox'
     * def result = ''
     * new StringReader(s).splitEachLine(~/\d/){ parts ->
     *     result += "${parts[0]}_${parts[1]}|"
     * }
     * assert result == 'The _ quick|brown _ fox|'
     * </pre>
     *
     * @param self    a Reader, closed after the method returns
     * @param pattern the regular expression Pattern for the delimiter
     * @param closure a closure
     * @return the last value returned by the closure
     * @throws IOException if an IOException occurs.
     * @throws java.util.regex.PatternSyntaxException
     *                     if the regular expression's syntax is invalid
     * @see java.lang.String#split(java.lang.String)
     * @since 1.6.8
     */
public static <T> T splitEachLine(Reader self, Pattern pattern, @ClosureParams(value = FromString.class, options = { "List<String>", "String[]" }, conflictResolutionStrategy = PickFirstResolver.class) Closure<T> closure) throws IOException {
    BufferedReader br;
    T result = null;
    if (self instanceof BufferedReader)
        br = (BufferedReader) self;
    else
        br = new BufferedReader(self);
    try {
        while (true) {
            String line = br.readLine();
            if (line == null) {
                break;
            } else {
                List vals = Arrays.asList(pattern.split(line));
                result = closure.call(hasSingleStringArg(closure) ? vals.get(0) : vals);
            }
        }
        Reader temp = self;
        self = null;
        temp.close();
        return result;
    } finally {
        closeWithWarning(self);
        closeWithWarning(br);
    }
}
Also used : BufferedReader(java.io.BufferedReader) Reader(java.io.Reader) InputStreamReader(java.io.InputStreamReader) BufferedReader(java.io.BufferedReader) List(java.util.List) FromString(groovy.transform.stc.FromString)

Example 13 with Reader

use of java.io.Reader in project groovy by apache.

the class ClassicGroovyTestGeneratorHelper method parse.

/** run the JSR parser implementation over the supplied source text*/
public void parse(String theSrcText, String testName) throws Exception {
    System.out.println("-------------------------------");
    System.out.println("  " + testName);
    System.out.println("-------------------------------");
    try {
        Reader reader = new BufferedReader(new StringReader(theSrcText));
        GroovyRecognizer recognizer = GroovyRecognizer.make(reader);
        recognizer.compilationUnit();
        System.out.println(decorateWithLineNumbers(theSrcText));
    } catch (RecognitionException parseException) {
        System.out.println(decorateWithLineNumbersAndErrorMessage(theSrcText, parseException));
        throw parseException;
    }
    System.out.println("-------------------------------");
}
Also used : BufferedReader(java.io.BufferedReader) StringReader(java.io.StringReader) StringReader(java.io.StringReader) Reader(java.io.Reader) BufferedReader(java.io.BufferedReader) GroovyRecognizer(org.codehaus.groovy.antlr.parser.GroovyRecognizer) RecognitionException(antlr.RecognitionException)

Example 14 with Reader

use of java.io.Reader in project groovy by apache.

the class IOGroovyMethods method withReader.

/**
     * Allows this reader to be used within the closure, ensuring that it
     * is closed before this method returns.
     *
     * @param reader  the reader which is used and then closed
     * @param closure the closure that the writer is passed into
     * @return the value returned by the closure
     * @throws IOException if an IOException occurs.
     * @since 1.5.2
     */
public static <T> T withReader(Reader reader, @ClosureParams(FirstParam.class) Closure<T> closure) throws IOException {
    try {
        T result = closure.call(reader);
        Reader temp = reader;
        reader = null;
        temp.close();
        return result;
    } finally {
        closeWithWarning(reader);
    }
}
Also used : Reader(java.io.Reader) InputStreamReader(java.io.InputStreamReader) BufferedReader(java.io.BufferedReader)

Example 15 with Reader

use of java.io.Reader in project groovy by apache.

the class InvokerHelper method write.

/**
     * Writes an object to a Writer using Groovy's default representation for the object.
     */
public static void write(Writer out, Object object) throws IOException {
    if (object instanceof String) {
        out.write((String) object);
    } else if (object instanceof Object[]) {
        out.write(toArrayString((Object[]) object));
    } else if (object instanceof Map) {
        out.write(toMapString((Map) object));
    } else if (object instanceof Collection) {
        out.write(toListString((Collection) object));
    } else if (object instanceof Writable) {
        Writable writable = (Writable) object;
        writable.writeTo(out);
    } else if (object instanceof InputStream || object instanceof Reader) {
        // Copy stream to stream
        Reader reader;
        if (object instanceof InputStream) {
            reader = new InputStreamReader((InputStream) object);
        } else {
            reader = (Reader) object;
        }
        char[] chars = new char[8192];
        int i;
        while ((i = reader.read(chars)) != -1) {
            out.write(chars, 0, i);
        }
        reader.close();
    } else {
        out.write(toString(object));
    }
}
Also used : InputStreamReader(java.io.InputStreamReader) InputStream(java.io.InputStream) Collection(java.util.Collection) Writable(groovy.lang.Writable) Reader(java.io.Reader) InputStreamReader(java.io.InputStreamReader) GroovyObject(groovy.lang.GroovyObject) GString(groovy.lang.GString) Map(java.util.Map) LinkedHashMap(java.util.LinkedHashMap) SpreadMap(groovy.lang.SpreadMap)

Aggregations

Reader (java.io.Reader)1498 InputStreamReader (java.io.InputStreamReader)526 StringReader (java.io.StringReader)498 IOException (java.io.IOException)348 BufferedReader (java.io.BufferedReader)242 InputStream (java.io.InputStream)219 TokenStream (org.apache.lucene.analysis.TokenStream)171 Test (org.junit.Test)170 SqlSessionFactoryBuilder (org.apache.ibatis.session.SqlSessionFactoryBuilder)159 Connection (java.sql.Connection)137 ScriptRunner (org.apache.ibatis.jdbc.ScriptRunner)126 FileReader (java.io.FileReader)108 FileInputStream (java.io.FileInputStream)107 File (java.io.File)105 BeforeClass (org.junit.BeforeClass)99 Tokenizer (org.apache.lucene.analysis.Tokenizer)91 SqlSession (org.apache.ibatis.session.SqlSession)83 StringWriter (java.io.StringWriter)81 ArrayList (java.util.ArrayList)77 Writer (java.io.Writer)63