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);
}
}
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);
}
}
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("-------------------------------");
}
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);
}
}
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));
}
}
Aggregations