use of java.io.Writer in project buck by facebook.
the class LogFileHandler method publish.
@Override
public void publish(LogRecord record) {
String commandId = state.threadIdToCommandId(record.getThreadID());
String formattedMsg = getFormatter().format(record);
for (Writer writer : state.getWriters(commandId)) {
try {
writer.write(formattedMsg);
if (record.getLevel().intValue() >= Level.SEVERE.intValue()) {
writer.flush();
}
} catch (IOException e) {
// NOPMD
// There's a chance the writer may have been concurrently closed.
}
}
}
use of java.io.Writer in project MyDiary by erttyy8821.
the class ExportAsyncTask method outputBackupJson.
private void outputBackupJson() throws IOException {
Writer writer = new FileWriter(backupJsonFilePath);
Gson gson = new GsonBuilder().create();
gson.toJson(backupManager, writer);
writer.close();
}
use of java.io.Writer in project buck by facebook.
the class Jsr199Javac method buildWithClasspath.
private int buildWithClasspath(JavacExecutionContext context, BuildTarget invokingRule, ImmutableList<String> options, ImmutableList<ResolvedJavacPluginProperties> annotationProcessors, ImmutableSortedSet<Path> javaSourceFilePaths, Path pathToSrcsList, JavaCompiler compiler, StandardJavaFileManager fileManager, Iterable<? extends JavaFileObject> compilationUnits, JavacOptions.AbiGenerationMode abiGenerationMode) {
// since we do not print them out to console in case of error
try {
context.getProjectFilesystem().writeLinesToPath(FluentIterable.from(javaSourceFilePaths).transform(Object::toString).transform(ARGFILES_ESCAPER), pathToSrcsList);
} catch (IOException e) {
context.getEventSink().reportThrowable(e, "Cannot write list of .java files to compile to %s file! Terminating compilation.", pathToSrcsList);
return 1;
}
DiagnosticCollector<JavaFileObject> diagnostics = new DiagnosticCollector<>();
List<String> classNamesForAnnotationProcessing = ImmutableList.of();
Writer compilerOutputWriter = new PrintWriter(context.getStdErr());
JavaCompiler.CompilationTask compilationTask = compiler.getTask(compilerOutputWriter, context.getUsedClassesFileWriter().wrapFileManager(fileManager), diagnostics, options, classNamesForAnnotationProcessing, compilationUnits);
boolean isSuccess = false;
BuckTracing.setCurrentThreadTracingInterfaceFromJsr199Javac(new Jsr199TracingBridge(context.getEventSink(), invokingRule));
Object abiValidatingTaskListener = null;
if (abiGenerationMode != JavacOptions.AbiGenerationMode.CLASS) {
abiValidatingTaskListener = SourceBasedAbiStubber.newValidatingTaskListener(context.getClassLoaderCache(), compilationTask, new FileManagerBootClasspathOracle(fileManager), abiGenerationMode == JavacOptions.AbiGenerationMode.SOURCE ? Diagnostic.Kind.ERROR : Diagnostic.Kind.WARNING);
}
try {
try (// in some unusual situations
TranslatingJavacPhaseTracer tracer = TranslatingJavacPhaseTracer.setupTracing(invokingRule, context.getClassLoaderCache(), context.getEventSink(), compilationTask, abiValidatingTaskListener);
// may choke with novel errors that don't occur on the command line.
AnnotationProcessorFactory processorFactory = new AnnotationProcessorFactory(context.getEventSink(), compiler.getClass().getClassLoader(), context.getClassLoaderCache(), invokingRule)) {
compilationTask.setProcessors(processorFactory.createProcessors(annotationProcessors));
// Invoke the compilation and inspect the result.
isSuccess = compilationTask.call();
} catch (IOException e) {
LOG.warn(e, "Unable to close annotation processor class loader. We may be leaking memory.");
}
} finally {
// Clear the tracing interface so we have no chance of leaking it to code that shouldn't
// be using it.
BuckTracing.clearCurrentThreadTracingInterfaceFromJsr199Javac();
}
for (Diagnostic<? extends JavaFileObject> diagnostic : diagnostics.getDiagnostics()) {
LOG.debug("javac: %s", DiagnosticPrettyPrinter.format(diagnostic));
}
List<Diagnostic<? extends JavaFileObject>> cleanDiagnostics = DiagnosticCleaner.clean(diagnostics.getDiagnostics());
if (isSuccess) {
context.getUsedClassesFileWriter().writeFile(context.getProjectFilesystem(), context.getObjectMapper());
return 0;
} else {
if (context.getVerbosity().shouldPrintStandardInformation()) {
int numErrors = 0;
int numWarnings = 0;
for (Diagnostic<? extends JavaFileObject> diagnostic : cleanDiagnostics) {
Diagnostic.Kind kind = diagnostic.getKind();
if (kind == Diagnostic.Kind.ERROR) {
++numErrors;
handleMissingSymbolError(invokingRule, diagnostic, context);
} else if (kind == Diagnostic.Kind.WARNING || kind == Diagnostic.Kind.MANDATORY_WARNING) {
++numWarnings;
}
context.getStdErr().println(DiagnosticPrettyPrinter.format(diagnostic));
}
if (numErrors > 0 || numWarnings > 0) {
context.getStdErr().printf("Errors: %d. Warnings: %d.\n", numErrors, numWarnings);
}
}
return 1;
}
}
use of java.io.Writer in project OpenRefine by OpenRefine.
the class ExportRowsCommand method doPost.
@Override
public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
ProjectManager.singleton.setBusy(true);
try {
Project project = getProject(request);
Engine engine = getEngine(request, project);
Properties params = getRequestParameters(request);
String format = params.getProperty("format");
Exporter exporter = ExporterRegistry.getExporter(format);
if (exporter == null) {
exporter = new CsvExporter('\t');
}
String contentType = params.getProperty("contentType");
if (contentType == null) {
contentType = exporter.getContentType();
}
response.setHeader("Content-Type", contentType);
if (exporter instanceof WriterExporter) {
String encoding = params.getProperty("encoding");
response.setCharacterEncoding(encoding != null ? encoding : "UTF-8");
Writer writer = encoding == null ? response.getWriter() : new OutputStreamWriter(response.getOutputStream(), encoding);
((WriterExporter) exporter).export(project, params, engine, writer);
writer.close();
} else if (exporter instanceof StreamExporter) {
response.setCharacterEncoding("UTF-8");
OutputStream stream = response.getOutputStream();
((StreamExporter) exporter).export(project, params, engine, stream);
stream.close();
// } else if (exporter instanceof UrlExporter) {
// ((UrlExporter) exporter).export(project, options, engine);
} else {
// TODO: Should this use ServletException instead of respondException?
respondException(response, new RuntimeException("Unknown exporter type"));
}
} catch (Exception e) {
// Use generic error handling rather than our JSON handling
throw new ServletException(e);
} finally {
ProjectManager.singleton.setBusy(false);
}
}
use of java.io.Writer in project OpenRefine by OpenRefine.
the class ProjectMetadataUtilities method saveToFile.
protected static void saveToFile(ProjectMetadata projectMeta, File metadataFile) throws JSONException, IOException {
Writer writer = new OutputStreamWriter(new FileOutputStream(metadataFile));
try {
JSONWriter jsonWriter = new JSONWriter(writer);
projectMeta.write(jsonWriter);
} finally {
writer.close();
}
}
Aggregations