use of java.io.BufferedWriter in project antlr4 by antlr.
the class Tool method getOutputFileWriter.
/** This method is used by all code generators to create new output
* files. If the outputDir set by -o is not present it will be created.
* The final filename is sensitive to the output directory and
* the directory where the grammar file was found. If -o is /tmp
* and the original grammar file was foo/t.g4 then output files
* go in /tmp/foo.
*
* The output dir -o spec takes precedence if it's absolute.
* E.g., if the grammar file dir is absolute the output dir is given
* precedence. "-o /tmp /usr/lib/t.g4" results in "/tmp/T.java" as
* output (assuming t.g4 holds T.java).
*
* If no -o is specified, then just write to the directory where the
* grammar file was found.
*
* If outputDirectory==null then write a String.
*/
public Writer getOutputFileWriter(Grammar g, String fileName) throws IOException {
if (outputDirectory == null) {
return new StringWriter();
}
// output directory is a function of where the grammar file lives
// for subdir/T.g4, you get subdir here. Well, depends on -o etc...
File outputDir = getOutputDirectory(g.fileName);
File outputFile = new File(outputDir, fileName);
if (!outputDir.exists()) {
outputDir.mkdirs();
}
FileOutputStream fos = new FileOutputStream(outputFile);
OutputStreamWriter osw;
if (grammarEncoding != null) {
osw = new OutputStreamWriter(fos, grammarEncoding);
} else {
osw = new OutputStreamWriter(fos);
}
return new BufferedWriter(osw);
}
use of java.io.BufferedWriter in project jstorm by alibaba.
the class SandBoxMaker method generatePolicyFile.
public String generatePolicyFile(Map<String, String> replaceMap) throws IOException {
// dynamic generate policy file, no static file
String tmpPolicy = StormConfig.supervisorTmpDir(conf) + File.separator + UUID.randomUUID().toString();
InputStream inputStream = SandBoxMaker.class.getClassLoader().getResourceAsStream(SANBOX_TEMPLATE_NAME);
PrintWriter writer = new PrintWriter(new BufferedWriter(new FileWriter(tmpPolicy)));
BufferedReader reader = null;
InputStreamReader inputReader = null;
try {
inputReader = new InputStreamReader(inputStream);
reader = new BufferedReader(new LineNumberReader(inputReader));
String line = null;
while ((line = reader.readLine()) != null) {
String replaced = replaceLine(line, replaceMap);
writer.println(replaced);
}
return tmpPolicy;
} catch (Exception e) {
LOG.error("Failed to generate policy file\n", e);
throw new IOException(e);
} finally {
if (inputStream != null) {
inputStream.close();
}
if (writer != null) {
writer.close();
}
if (reader != null) {
reader.close();
}
if (inputReader != null) {
inputReader.close();
}
}
}
use of java.io.BufferedWriter in project groovy-core by groovy.
the class StringGroovyMethods method denormalize.
/**
* Return a CharSequence with lines (separated by LF, CR/LF, or CR)
* terminated by the platform specific line separator.
*
* @param self a CharSequence object
* @return the denormalized toString() of this CharSequence
* @see #denormalize(String)
* @since 1.8.2
*/
public static String denormalize(final CharSequence self) {
final String s = self.toString();
// TODO: Put this lineSeparator property somewhere everyone can use it.
if (lineSeparator == null) {
final StringWriter sw = new StringWriter(2);
try {
// We use BufferedWriter rather than System.getProperty because
// it has the security manager rigamarole to deal with the possible exception.
final BufferedWriter bw = new BufferedWriter(sw);
bw.newLine();
bw.flush();
lineSeparator = sw.toString();
} catch (IOException ioe) {
// This shouldn't happen, but this is the same default used by
// BufferedWriter on a security exception.
lineSeparator = "\n";
}
}
final int len = s.length();
if (len < 1) {
return s;
}
final StringBuilder sb = new StringBuilder((110 * len) / 100);
int i = 0;
while (i < len) {
final char ch = s.charAt(i++);
switch(ch) {
case '\r':
sb.append(lineSeparator);
// Eat the following LF if any.
if ((i < len) && (s.charAt(i) == '\n')) {
++i;
}
break;
case '\n':
sb.append(lineSeparator);
break;
default:
sb.append(ch);
break;
}
}
return sb.toString();
}
use of java.io.BufferedWriter in project groovy-core by groovy.
the class IOGroovyMethods method transformLine.
/**
* Transforms the lines from a reader with a Closure and
* write them to a writer. Both Reader and Writer are
* closed after the operation.
*
* @param reader Lines of text to be transformed. Reader is closed afterwards.
* @param writer Where transformed lines are written. Writer is closed afterwards.
* @param closure Single parameter closure that is called to transform each line of
* text from the reader, before writing it to the writer.
* @throws IOException if an IOException occurs.
* @since 1.0
*/
public static void transformLine(Reader reader, Writer writer, @ClosureParams(value = SimpleType.class, options = "java.lang.String") Closure closure) throws IOException {
BufferedReader br = new BufferedReader(reader);
BufferedWriter bw = new BufferedWriter(writer);
String line;
try {
while ((line = br.readLine()) != null) {
Object o = closure.call(line);
if (o != null) {
bw.write(o.toString());
bw.newLine();
}
}
bw.flush();
Writer temp2 = writer;
writer = null;
temp2.close();
Reader temp1 = reader;
reader = null;
temp1.close();
} finally {
closeWithWarning(br);
closeWithWarning(reader);
closeWithWarning(bw);
closeWithWarning(writer);
}
}
use of java.io.BufferedWriter in project groovy-core by groovy.
the class IOGroovyMethods method filterLine.
/**
* Filter the lines from this Reader, and return a Writable which can be
* used to stream the filtered lines to a destination. The closure should
* return <code>true</code> if the line should be passed to the writer.
*
* @param reader this reader
* @param closure a closure used for filtering
* @return a Writable which will use the closure to filter each line
* from the reader when the Writable#writeTo(Writer) is called.
* @since 1.0
*/
public static Writable filterLine(Reader reader, @ClosureParams(value = SimpleType.class, options = "java.lang.String") final Closure closure) {
final BufferedReader br = new BufferedReader(reader);
return new Writable() {
public Writer writeTo(Writer out) throws IOException {
BufferedWriter bw = new BufferedWriter(out);
String line;
BooleanClosureWrapper bcw = new BooleanClosureWrapper(closure);
while ((line = br.readLine()) != null) {
if (bcw.call(line)) {
bw.write(line);
bw.newLine();
}
}
bw.flush();
return out;
}
public String toString() {
StringWriter buffer = new StringWriter();
try {
writeTo(buffer);
} catch (IOException e) {
throw new StringWriterIOException(e);
}
return buffer.toString();
}
};
}
Aggregations