Search in sources :

Example 96 with Writer

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.
        }
    }
}
Also used : IOException(java.io.IOException) Writer(java.io.Writer)

Example 97 with Writer

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();
}
Also used : GsonBuilder(com.google.gson.GsonBuilder) FileWriter(java.io.FileWriter) Gson(com.google.gson.Gson) FileWriter(java.io.FileWriter) Writer(java.io.Writer)

Example 98 with Writer

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;
    }
}
Also used : JavaCompiler(javax.tools.JavaCompiler) Diagnostic(javax.tools.Diagnostic) IOException(java.io.IOException) JavaFileObject(javax.tools.JavaFileObject) TranslatingJavacPhaseTracer(com.facebook.buck.jvm.java.tracing.TranslatingJavacPhaseTracer) JavaFileObject(javax.tools.JavaFileObject) DiagnosticCollector(javax.tools.DiagnosticCollector) PrintWriter(java.io.PrintWriter) Writer(java.io.Writer) PrintWriter(java.io.PrintWriter)

Example 99 with Writer

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);
    }
}
Also used : WriterExporter(com.google.refine.exporters.WriterExporter) StreamExporter(com.google.refine.exporters.StreamExporter) OutputStream(java.io.OutputStream) Properties(java.util.Properties) WriterExporter(com.google.refine.exporters.WriterExporter) Exporter(com.google.refine.exporters.Exporter) StreamExporter(com.google.refine.exporters.StreamExporter) CsvExporter(com.google.refine.exporters.CsvExporter) ServletException(javax.servlet.ServletException) IOException(java.io.IOException) ServletException(javax.servlet.ServletException) Project(com.google.refine.model.Project) CsvExporter(com.google.refine.exporters.CsvExporter) OutputStreamWriter(java.io.OutputStreamWriter) Engine(com.google.refine.browsing.Engine) Writer(java.io.Writer) OutputStreamWriter(java.io.OutputStreamWriter)

Example 100 with Writer

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();
    }
}
Also used : JSONWriter(org.json.JSONWriter) FileOutputStream(java.io.FileOutputStream) OutputStreamWriter(java.io.OutputStreamWriter) JSONWriter(org.json.JSONWriter) Writer(java.io.Writer) OutputStreamWriter(java.io.OutputStreamWriter)

Aggregations

Writer (java.io.Writer)1259 OutputStreamWriter (java.io.OutputStreamWriter)512 IOException (java.io.IOException)414 StringWriter (java.io.StringWriter)300 File (java.io.File)269 FileOutputStream (java.io.FileOutputStream)196 BufferedWriter (java.io.BufferedWriter)178 FileWriter (java.io.FileWriter)174 PrintWriter (java.io.PrintWriter)159 OutputStream (java.io.OutputStream)120 Test (org.junit.Test)109 InputStreamReader (java.io.InputStreamReader)71 ByteArrayOutputStream (java.io.ByteArrayOutputStream)64 BufferedReader (java.io.BufferedReader)62 Reader (java.io.Reader)62 HashMap (java.util.HashMap)59 Map (java.util.Map)59 ArrayList (java.util.ArrayList)58 InputStream (java.io.InputStream)54 Properties (java.util.Properties)39