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);
}
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);
}
}
};
}
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;
}
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");
}
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;
}
};
}
Aggregations