Search in sources :

Example 16 with BufferedOutputStream

use of java.io.BufferedOutputStream in project buck by facebook.

the class HttpDownloader method fetch.

@Override
public boolean fetch(BuckEventBus eventBus, URI uri, Optional<PasswordAuthentication> authentication, Path output) throws IOException {
    if (!("https".equals(uri.getScheme()) || "http".equals(uri.getScheme()))) {
        return false;
    }
    DownloadEvent.Started started = DownloadEvent.started(uri);
    eventBus.post(started);
    try {
        HttpURLConnection connection = createConnection(uri);
        if (authentication.isPresent()) {
            if ("https".equals(uri.getScheme()) && connection instanceof HttpsURLConnection) {
                PasswordAuthentication p = authentication.get();
                String authStr = p.getUserName() + ":" + new String(p.getPassword());
                String authEncoded = BaseEncoding.base64().encode(authStr.getBytes(StandardCharsets.UTF_8));
                connection.addRequestProperty("Authorization", "Basic " + authEncoded);
            } else {
                LOG.info("Refusing to send basic authentication over plain http.");
                return false;
            }
        }
        if (HttpURLConnection.HTTP_OK != connection.getResponseCode()) {
            LOG.info("Unable to download %s: %s", uri, connection.getResponseMessage());
            return false;
        }
        long contentLength = connection.getContentLengthLong();
        try (InputStream is = new BufferedInputStream(connection.getInputStream());
            OutputStream os = new BufferedOutputStream(Files.newOutputStream(output))) {
            long read = 0;
            while (true) {
                int r = is.read();
                read++;
                if (r == -1) {
                    break;
                }
                if (read % PROGRESS_REPORT_EVERY_N_BYTES == 0) {
                    eventBus.post(new DownloadProgressEvent(uri, contentLength, read));
                }
                os.write(r);
            }
        }
        return true;
    } finally {
        eventBus.post(DownloadEvent.finished(started));
    }
}
Also used : BufferedInputStream(java.io.BufferedInputStream) InputStream(java.io.InputStream) OutputStream(java.io.OutputStream) BufferedOutputStream(java.io.BufferedOutputStream) HttpURLConnection(java.net.HttpURLConnection) BufferedInputStream(java.io.BufferedInputStream) BufferedOutputStream(java.io.BufferedOutputStream) HttpsURLConnection(javax.net.ssl.HttpsURLConnection) PasswordAuthentication(java.net.PasswordAuthentication)

Example 17 with BufferedOutputStream

use of java.io.BufferedOutputStream in project buck by facebook.

the class CachingBuildEngine method updateAndStoreManifest.

// Update the on-disk manifest with the new dep-file rule key and push it to the cache.
private void updateAndStoreManifest(BuildRule rule, RuleKey key, ImmutableSet<SourcePath> inputs, RuleKeyAndInputs manifestKey, ArtifactCache cache) throws IOException {
    Preconditions.checkState(useManifestCaching(rule));
    final Path manifestPath = getManifestPath(rule);
    Manifest manifest = new Manifest();
    // If we already have a manifest downloaded, use that.
    if (rule.getProjectFilesystem().exists(manifestPath)) {
        try (InputStream inputStream = rule.getProjectFilesystem().newFileInputStream(manifestPath)) {
            manifest = new Manifest(inputStream);
        }
    } else {
        // Ensure the path to manifest exist
        rule.getProjectFilesystem().createParentDirs(manifestPath);
    }
    // this efficiently and it's not clear how much benefit this will give us.
    if (manifest.size() >= maxDepFileCacheEntries) {
        manifest = new Manifest();
    }
    // Update the manifest with the new output rule key.
    manifest.addEntry(fileHashCache, key, pathResolver, manifestKey.getInputs(), inputs);
    // Serialize the manifest to disk.
    try (OutputStream outputStream = rule.getProjectFilesystem().newFileOutputStream(manifestPath)) {
        manifest.serialize(outputStream);
    }
    final Path tempFile = Files.createTempFile("buck.", ".manifest");
    // `ArtifactCache` interface uses raw paths.
    try (InputStream inputStream = rule.getProjectFilesystem().newFileInputStream(manifestPath);
        OutputStream outputStream = new GZIPOutputStream(new BufferedOutputStream(Files.newOutputStream(tempFile)))) {
        ByteStreams.copy(inputStream, outputStream);
    }
    cache.store(ArtifactInfo.builder().addRuleKeys(manifestKey.getRuleKey()).build(), BorrowablePath.borrowablePath(tempFile)).addListener(() -> {
        try {
            Files.deleteIfExists(tempFile);
        } catch (IOException e) {
            LOG.warn(e, "Error occurred while deleting temporary manifest file for %s", manifestPath);
        }
    }, MoreExecutors.directExecutor());
}
Also used : Path(java.nio.file.Path) LazyPath(com.facebook.buck.io.LazyPath) BorrowablePath(com.facebook.buck.io.BorrowablePath) GZIPOutputStream(java.util.zip.GZIPOutputStream) GZIPInputStream(java.util.zip.GZIPInputStream) BufferedInputStream(java.io.BufferedInputStream) InputStream(java.io.InputStream) GZIPOutputStream(java.util.zip.GZIPOutputStream) BufferedOutputStream(java.io.BufferedOutputStream) OutputStream(java.io.OutputStream) IOException(java.io.IOException) BufferedOutputStream(java.io.BufferedOutputStream)

Example 18 with BufferedOutputStream

use of java.io.BufferedOutputStream in project buck by facebook.

the class DefaultDefectReporter method writeReport.

private void writeReport(DefectReport defectReport, OutputStream outputStream) throws IOException {
    try (BufferedOutputStream baseOut = new BufferedOutputStream(outputStream);
        CustomZipOutputStream out = ZipOutputStreams.newOutputStream(baseOut, APPEND_TO_ZIP)) {
        if (defectReport.getSourceControlInfo().isPresent() && defectReport.getSourceControlInfo().get().getDiff().isPresent()) {
            addStringsAsFilesToArchive(out, ImmutableMap.of(DIFF_FILE_NAME, defectReport.getSourceControlInfo().get().getDiff().get()));
        }
        addFilesToArchive(out, defectReport.getIncludedPaths());
        out.putNextEntry(new CustomZipEntry(REPORT_FILE_NAME));
        objectMapper.writeValue(out, defectReport);
    }
}
Also used : CustomZipOutputStream(com.facebook.buck.zip.CustomZipOutputStream) CustomZipEntry(com.facebook.buck.zip.CustomZipEntry) BufferedOutputStream(java.io.BufferedOutputStream)

Example 19 with BufferedOutputStream

use of java.io.BufferedOutputStream in project buck by facebook.

the class BaseRunner method writeResult.

/**
   * The test result file is written as XML to avoid introducing a dependency on JSON (see class
   * overview).
   */
protected void writeResult(String testClassName, List<TestResult> results) throws IOException, ParserConfigurationException, TransformerException {
    // XML writer logic taken from:
    // http://www.genedavis.com/library/xml/java_dom_xml_creation.jsp
    DocumentBuilder docBuilder = DocumentBuilderFactory.newInstance().newDocumentBuilder();
    Document doc = docBuilder.newDocument();
    doc.setXmlVersion("1.1");
    Element root = doc.createElement("testcase");
    root.setAttribute("name", testClassName);
    root.setAttribute("runner_capabilities", getRunnerCapabilities());
    doc.appendChild(root);
    for (TestResult result : results) {
        Element test = doc.createElement("test");
        // suite attribute
        test.setAttribute("suite", result.testClassName);
        // name attribute
        test.setAttribute("name", result.testMethodName);
        // success attribute
        boolean isSuccess = result.isSuccess();
        test.setAttribute("success", Boolean.toString(isSuccess));
        // type attribute
        test.setAttribute("type", result.type.toString());
        // time attribute
        long runTime = result.runTime;
        test.setAttribute("time", String.valueOf(runTime));
        // Include failure details, if appropriate.
        Throwable failure = result.failure;
        if (failure != null) {
            String message = failure.getMessage();
            test.setAttribute("message", message);
            String stacktrace = stackTraceToString(failure);
            test.setAttribute("stacktrace", stacktrace);
        }
        // stdout, if non-empty.
        if (result.stdOut != null) {
            Element stdOutEl = doc.createElement("stdout");
            stdOutEl.appendChild(doc.createTextNode(result.stdOut));
            test.appendChild(stdOutEl);
        }
        // stderr, if non-empty.
        if (result.stdErr != null) {
            Element stdErrEl = doc.createElement("stderr");
            stdErrEl.appendChild(doc.createTextNode(result.stdErr));
            test.appendChild(stdErrEl);
        }
        root.appendChild(test);
    }
    // Create an XML transformer that pretty-prints with a 2-space indent.
    // The transformer factory uses a system property to find the class to use. We need to default
    // to the system default since we have the user's classpath and they may not have everything set
    // up for the XSLT transform to work.
    String vendor = System.getProperty("java.vm.vendor");
    String factoryClass;
    if ("IBM Corporation".equals(vendor)) {
        // Used in the IBM JDK --- from
        // https://www.ibm.com/support/knowledgecenter/SSYKE2_8.0.0/com.ibm.java.aix.80.doc/user/xml/using_xml.html
        factoryClass = "com.ibm.xtq.xslt.jaxp.compiler.TransformerFactoryImpl";
    } else {
        // Used in the OpenJDK and the Oracle JDK.
        factoryClass = "com.sun.org.apache.xalan.internal.xsltc.trax.TransformerFactoryImpl";
    }
    // When we get this far, we're exiting, so no need to reset the property.
    System.setProperty("javax.xml.transform.TransformerFactory", factoryClass);
    TransformerFactory transformerFactory = TransformerFactory.newInstance();
    Transformer trans = transformerFactory.newTransformer();
    trans.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "no");
    trans.setOutputProperty(OutputKeys.INDENT, "yes");
    trans.setOutputProperty("{http://xml.apache.org/xslt}indent-amount", "2");
    // Write the result to a file.
    String testSelectorSuffix = "";
    if (!testSelectorList.isEmpty()) {
        testSelectorSuffix += ".test_selectors";
    }
    if (isDryRun) {
        testSelectorSuffix += ".dry_run";
    }
    OutputStream output;
    if (outputDirectory != null) {
        File outputFile = new File(outputDirectory, testClassName + testSelectorSuffix + ".xml");
        output = new BufferedOutputStream(new FileOutputStream(outputFile));
    } else {
        output = System.out;
    }
    StreamResult streamResult = new StreamResult(output);
    DOMSource source = new DOMSource(doc);
    trans.transform(source, streamResult);
    if (outputDirectory != null) {
        output.close();
    }
}
Also used : DOMSource(javax.xml.transform.dom.DOMSource) TransformerFactory(javax.xml.transform.TransformerFactory) Transformer(javax.xml.transform.Transformer) StreamResult(javax.xml.transform.stream.StreamResult) Element(org.w3c.dom.Element) OutputStream(java.io.OutputStream) FileOutputStream(java.io.FileOutputStream) BufferedOutputStream(java.io.BufferedOutputStream) Document(org.w3c.dom.Document) DocumentBuilder(javax.xml.parsers.DocumentBuilder) FileOutputStream(java.io.FileOutputStream) File(java.io.File) BufferedOutputStream(java.io.BufferedOutputStream)

Example 20 with BufferedOutputStream

use of java.io.BufferedOutputStream in project buck by facebook.

the class OfflineScribeLogger method storeOffline.

private synchronized void storeOffline(String category, byte[] scribeData) {
    // Do not try to store if issues already encountered.
    if (!storingAvailable) {
        return;
    }
    // Check if this data can be logged at all.
    long spaceLeft = maxScribeOfflineLogsBytes - bytesStoredSoFar;
    if (scribeData.length > spaceLeft) {
        LOG.warn("Not enough space left when trying to store data for category: %s.", category);
        return;
    }
    try {
        // Prepare directory and the stream.
        if (logFileStoreStream == null) {
            // Make sure directory exists.
            filesystem.mkdirs(logDir);
            logFileStoreStream = new BufferedOutputStream(new FileOutputStream(newLogPath.toFile()), BUFFER_SIZE);
        }
        // Write.
        logFileStoreStream.write(scribeData);
        bytesStoredSoFar += scribeData.length;
    } catch (Exception e) {
        storingAvailable = false;
        LOG.error(e, "Failed storing for offline logging for category: %s.", category);
    }
}
Also used : FileOutputStream(java.io.FileOutputStream) BufferedOutputStream(java.io.BufferedOutputStream) IOException(java.io.IOException) FileNotFoundException(java.io.FileNotFoundException)

Aggregations

BufferedOutputStream (java.io.BufferedOutputStream)2418 FileOutputStream (java.io.FileOutputStream)1659 IOException (java.io.IOException)1239 File (java.io.File)1060 OutputStream (java.io.OutputStream)791 BufferedInputStream (java.io.BufferedInputStream)527 InputStream (java.io.InputStream)374 FileInputStream (java.io.FileInputStream)332 DataOutputStream (java.io.DataOutputStream)242 ByteArrayOutputStream (java.io.ByteArrayOutputStream)228 ZipEntry (java.util.zip.ZipEntry)212 ZipOutputStream (java.util.zip.ZipOutputStream)209 FileNotFoundException (java.io.FileNotFoundException)191 ZipFile (java.util.zip.ZipFile)115 ArrayList (java.util.ArrayList)107 URL (java.net.URL)101 ObjectOutputStream (java.io.ObjectOutputStream)99 PrintStream (java.io.PrintStream)98 Test (org.junit.Test)94 ByteArrayInputStream (java.io.ByteArrayInputStream)89