Search in sources :

Example 56 with OutputStream

use of java.io.OutputStream in project disunity by ata4.

the class BundleWriter method write.

public void write(Bundle bundle, Progress progress) throws IOException {
    this.bundle = bundle;
    // add offset placeholders
    levelOffsetMap.clear();
    bundle.entries().stream().filter(entry -> {
        if (bundle.entries().size() == 1) {
            return true;
        }
        String name = entry.name();
        return name.equals("mainData") || name.startsWith("level");
    }).forEach(entry -> levelOffsetMap.put(entry, new MutablePair<>(0L, 0L)));
    BundleHeader header = bundle.header();
    header.levelByteEnd().clear();
    header.levelByteEnd().addAll(levelOffsetMap.values());
    header.numberOfLevelsToDownload(levelOffsetMap.size());
    // write header
    out.writeStruct(header);
    header.headerSize((int) out.position());
    // write bundle data
    if (header.compressed()) {
        // write data to temporary file
        try (DataWriter outData = DataWriters.forFile(dataFile, CREATE, WRITE, TRUNCATE_EXISTING)) {
            writeData(outData, progress);
        }
        // configure LZMA encoder
        LzmaEncoderProps props = new LzmaEncoderProps();
        // 8 MiB
        props.setDictionarySize(1 << 23);
        // maximum
        props.setNumFastBytes(273);
        props.setUncompressedSize(Files.size(dataFile));
        props.setEndMarkerMode(true);
        // stream the temporary bundle data compressed into the bundle file
        try (OutputStream os = new LzmaOutputStream(new BufferedOutputStream(out.stream()), props)) {
            Files.copy(dataFile, os);
        }
        for (MutablePair<Long, Long> levelOffset : levelOffsetMap.values()) {
            levelOffset.setLeft(out.size());
        }
    } else {
        // write data directly to file
        writeData(out, progress);
    }
    // update header
    int fileSize = (int) out.size();
    header.completeFileSize(fileSize);
    header.minimumStreamedBytes(fileSize);
    out.position(0);
    out.writeStruct(header);
}
Also used : DataWriters(info.ata4.io.DataWriters) OutputStream(java.io.OutputStream) LzmaOutputStream(net.contrapunctus.lzma.LzmaOutputStream) Progress(info.ata4.junity.progress.Progress) DataWriter(info.ata4.io.DataWriter) Files(java.nio.file.Files) StandardOpenOption(java.nio.file.StandardOpenOption) IOException(java.io.IOException) BufferedOutputStream(java.io.BufferedOutputStream) ArrayList(java.util.ArrayList) LinkedHashMap(java.util.LinkedHashMap) IOUtils(org.apache.commons.io.IOUtils) List(java.util.List) MutablePair(org.apache.commons.lang3.tuple.MutablePair) Closeable(java.io.Closeable) Map(java.util.Map) Optional(java.util.Optional) LzmaEncoderProps(info.ata4.io.lzma.LzmaEncoderProps) Path(java.nio.file.Path) InputStream(java.io.InputStream) MutablePair(org.apache.commons.lang3.tuple.MutablePair) OutputStream(java.io.OutputStream) LzmaOutputStream(net.contrapunctus.lzma.LzmaOutputStream) BufferedOutputStream(java.io.BufferedOutputStream) LzmaEncoderProps(info.ata4.io.lzma.LzmaEncoderProps) LzmaOutputStream(net.contrapunctus.lzma.LzmaOutputStream) BufferedOutputStream(java.io.BufferedOutputStream) DataWriter(info.ata4.io.DataWriter)

Example 57 with OutputStream

use of java.io.OutputStream in project bazel by bazelbuild.

the class ApkManifestAction method newDeterministicWriter.

@Override
public DeterministicWriter newDeterministicWriter(final ActionExecutionContext ctx) throws IOException {
    ApkManifestCreator manifestCreator = new ApkManifestCreator(new ArtifactDigester() {

        @Override
        public byte[] getDigest(Artifact artifact) throws IOException {
            return ctx.getMetadataHandler().getMetadata(artifact).digest;
        }
    });
    final ApkManifest manifest = manifestCreator.createManifest();
    return new DeterministicWriter() {

        @Override
        public void writeOutputFile(OutputStream out) throws IOException {
            if (textOutput) {
                TextFormat.print(manifest, new PrintStream(out));
            } else {
                manifest.writeTo(out);
            }
        }
    };
}
Also used : PrintStream(java.io.PrintStream) OutputStream(java.io.OutputStream) IOException(java.io.IOException) ApkManifest(com.google.devtools.build.lib.rules.android.apkmanifest.ApkManifestOuterClass.ApkManifest) Artifact(com.google.devtools.build.lib.actions.Artifact)

Example 58 with OutputStream

use of java.io.OutputStream in project bazel by bazelbuild.

the class GrpcActionCache method createFileFromStream.

private ContentDigest createFileFromStream(Map<ContentDigest, Pair<Path, FileMetadata>> metadataMap, Iterator<CasDownloadReply> replies) throws IOException, CacheNotFoundException {
    Preconditions.checkArgument(replies.hasNext());
    CasDownloadReply reply = replies.next();
    if (reply.hasStatus()) {
        handleDownloadStatus(reply.getStatus());
    }
    BlobChunk chunk = reply.getData();
    ContentDigest digest = chunk.getDigest();
    Preconditions.checkArgument(metadataMap.containsKey(digest));
    Pair<Path, FileMetadata> metadata = metadataMap.get(digest);
    Path path = metadata.first;
    FileSystemUtils.createDirectoryAndParents(path.getParentDirectory());
    try (OutputStream stream = path.getOutputStream()) {
        ByteString data = chunk.getData();
        data.writeTo(stream);
        long bytesLeft = digest.getSizeBytes() - data.size();
        while (bytesLeft > 0) {
            Preconditions.checkArgument(replies.hasNext());
            reply = replies.next();
            if (reply.hasStatus()) {
                handleDownloadStatus(reply.getStatus());
            }
            chunk = reply.getData();
            data = chunk.getData();
            Preconditions.checkArgument(!chunk.hasDigest());
            Preconditions.checkArgument(chunk.getOffset() == digest.getSizeBytes() - bytesLeft);
            data.writeTo(stream);
            bytesLeft -= data.size();
        }
        path.setExecutable(metadata.second.getExecutable());
    }
    return digest;
}
Also used : Path(com.google.devtools.build.lib.vfs.Path) BlobChunk(com.google.devtools.build.lib.remote.RemoteProtocol.BlobChunk) CasDownloadReply(com.google.devtools.build.lib.remote.RemoteProtocol.CasDownloadReply) ByteString(com.google.protobuf.ByteString) OutputStream(java.io.OutputStream) FileMetadata(com.google.devtools.build.lib.remote.RemoteProtocol.FileMetadata) ContentDigest(com.google.devtools.build.lib.remote.RemoteProtocol.ContentDigest)

Example 59 with OutputStream

use of java.io.OutputStream in project bazel by bazelbuild.

the class IdlClassTest method idlClass.

@Test
public void idlClass() throws IOException {
    File classJar = tempFolder.newFile("lib.jar");
    File manifestProto = tempFolder.newFile("lib.manifest");
    File tempDir = tempFolder.newFolder("temp_files");
    File outputClassJar = tempFolder.newFile("lib-idl.jar");
    File outputSourceJar = tempFolder.newFile("lib-idl-src.jar");
    List<String> classes = Arrays.asList("Baz.class", "Baz$0.class", "Baz$1.class", "c/g/Foo.class", "c/g/Foo$0.class", "c/g/Foo$Inner.class", "c/g/Foo$Inner$InnerMost.class", "c/g/Bar.class", "c/g/Bar2.class", "c/g/Bar$Inner.class", "c/g/Bar2$Inner.class");
    try (OutputStream os = new FileOutputStream(classJar);
        ZipOutputStream zos = new ZipOutputStream(os)) {
        for (String path : classes) {
            zos.putNextEntry(new ZipEntry(path));
        }
    }
    tempFolder.newFolder("c");
    tempFolder.newFolder("c/g");
    tempFolder.newFolder("wrong");
    tempFolder.newFolder("wrong/source");
    tempFolder.newFolder("wrong/source/dir");
    for (String file : Arrays.asList("c/g/Foo.java", "c/g/Bar.java", "wrong/source/dir/Baz.java")) {
        tempFolder.newFile(file);
    }
    try (OutputStream os = new FileOutputStream(manifestProto)) {
        MANIFEST.writeTo(os);
    }
    IdlClass.main(new String[] { "--manifest_proto", manifestProto.toString(), "--class_jar", classJar.toString(), "--output_class_jar", outputClassJar.toString(), "--output_source_jar", outputSourceJar.toString(), "--temp_dir", tempDir.toString(), "--idl_source_base_dir", tempFolder.getRoot().getPath(), "c/g/Bar.java", "wrong/source/dir/Baz.java" });
    List<String> classJarEntries = getJarEntries(outputClassJar);
    assertThat(classJarEntries).containsExactly("c/g/Bar.class", "c/g/Bar$Inner.class", "c/g/Bar2.class", "c/g/Bar2$Inner.class", "Baz.class", "Baz$0.class", "Baz$1.class");
    List<String> sourceJarEntries = getJarEntries(outputSourceJar);
    assertThat(sourceJarEntries).containsExactly("c/g/Bar.java", "Baz.java");
}
Also used : ZipOutputStream(java.util.zip.ZipOutputStream) OutputStream(java.io.OutputStream) ZipOutputStream(java.util.zip.ZipOutputStream) FileOutputStream(java.io.FileOutputStream) FileOutputStream(java.io.FileOutputStream) ZipEntry(java.util.zip.ZipEntry) File(java.io.File) ZipFile(java.util.zip.ZipFile) Test(org.junit.Test)

Example 60 with OutputStream

use of java.io.OutputStream in project bazel by bazelbuild.

the class JavaSource2CFGDOT method getMethodTreeAndCompilationUnit.

/**
     * @return The AST of a specific method in a specific class as well as the
     *         {@link CompilationUnitTree} in a specific file (or null they do
     *         not exist).
     */
public static Entry</*@Nullable*/
MethodTree, /*@Nullable*/
CompilationUnitTree> getMethodTreeAndCompilationUnit(String file, final String method, String clas) {
    final Holder<MethodTree> m = new Holder<>();
    final Holder<CompilationUnitTree> c = new Holder<>();
    BasicTypeProcessor typeProcessor = new BasicTypeProcessor() {

        @Override
        protected TreePathScanner<?, ?> createTreePathScanner(CompilationUnitTree root) {
            c.value = root;
            return new TreePathScanner<Void, Void>() {

                @Override
                public Void visitMethod(MethodTree node, Void p) {
                    ExecutableElement el = TreeUtils.elementFromDeclaration(node);
                    if (el.getSimpleName().contentEquals(method)) {
                        m.value = node;
                        // compilation).
                        throw new RuntimeException();
                    }
                    return null;
                }
            };
        }
    };
    Context context = new Context();
    JavaCompiler javac = new JavaCompiler(context);
    javac.attrParseOnly = true;
    JavacFileManager fileManager = (JavacFileManager) context.get(JavaFileManager.class);
    JavaFileObject l = fileManager.getJavaFileObjectsFromStrings(List.of(file)).iterator().next();
    PrintStream err = System.err;
    try {
        // redirect syserr to nothing (and prevent the compiler from issuing
        // warnings about our exception.
        System.setErr(new PrintStream(new OutputStream() {

            @Override
            public void write(int b) throws IOException {
            }
        }));
        javac.compile(List.of(l), List.of(clas), List.of(typeProcessor));
    } catch (Throwable e) {
    // ok
    } finally {
        System.setErr(err);
    }
    return new Entry<MethodTree, CompilationUnitTree>() {

        @Override
        public CompilationUnitTree setValue(CompilationUnitTree value) {
            return null;
        }

        @Override
        public CompilationUnitTree getValue() {
            return c.value;
        }

        @Override
        public MethodTree getKey() {
            return m.value;
        }
    };
}
Also used : Context(com.sun.tools.javac.util.Context) PrintStream(java.io.PrintStream) CompilationUnitTree(com.sun.source.tree.CompilationUnitTree) MethodTree(com.sun.source.tree.MethodTree) JavaFileManager(javax.tools.JavaFileManager) Holder(javax.xml.ws.Holder) ExecutableElement(javax.lang.model.element.ExecutableElement) OutputStream(java.io.OutputStream) TreePathScanner(com.sun.source.util.TreePathScanner) JavaCompiler(com.sun.tools.javac.main.JavaCompiler) JavacFileManager(com.sun.tools.javac.file.JavacFileManager) JavaFileObject(javax.tools.JavaFileObject) Entry(java.util.Map.Entry) BasicTypeProcessor(org.checkerframework.javacutil.BasicTypeProcessor)

Aggregations

OutputStream (java.io.OutputStream)4178 IOException (java.io.IOException)1673 FileOutputStream (java.io.FileOutputStream)1331 InputStream (java.io.InputStream)1304 File (java.io.File)925 ByteArrayOutputStream (java.io.ByteArrayOutputStream)830 Test (org.junit.Test)735 BufferedOutputStream (java.io.BufferedOutputStream)477 FileInputStream (java.io.FileInputStream)431 Socket (java.net.Socket)375 ByteArrayInputStream (java.io.ByteArrayInputStream)233 URL (java.net.URL)231 OutputStreamWriter (java.io.OutputStreamWriter)230 HttpURLConnection (java.net.HttpURLConnection)189 BufferedInputStream (java.io.BufferedInputStream)178 InputStreamReader (java.io.InputStreamReader)176 BufferedReader (java.io.BufferedReader)159 FileNotFoundException (java.io.FileNotFoundException)158 GZIPOutputStream (java.util.zip.GZIPOutputStream)157 Path (java.nio.file.Path)155