use of java.io.InputStream in project bazel by bazelbuild.
the class SingleJar method run.
// Only visible for testing.
protected int run(List<String> args) throws IOException {
List<String> expandedArgs = new OptionFileExpander(fileSystem).expandArguments(args);
processCommandlineArgs(expandedArgs);
InputStream buildInfo = createBuildData();
ZipCombiner combiner = null;
try {
combiner = new ZipCombiner(outputMode, createEntryFilter(normalize, allowedPaths), fileSystem.getOutputStream(outputJar));
if (launcherBin != null) {
combiner.prependExecutable(fileSystem.getInputStream(launcherBin));
}
Date date = normalize ? ZipCombiner.DOS_EPOCH : null;
// Add a manifest file.
JarUtils.addMetaInf(combiner, date);
combiner.addFile(MANIFEST_FILENAME, date, createManifest());
if (includeBuildData) {
// Add the build data file.
combiner.addFile(BUILD_DATA_FILENAME, date, buildInfo);
}
// Copy the resources to the top level of the jar file.
for (String classpathResource : classpathResources) {
String entryName = getName(classpathResource);
if (warnDuplicateFiles && combiner.containsFile(entryName)) {
System.err.println("File " + entryName + " clashes with a previous file");
continue;
}
combiner.addFile(entryName, date, fileSystem.getInputStream(classpathResource));
}
// Copy the resources into the jar file.
for (String resource : resources) {
String from, to;
int i = resource.indexOf(':');
if (i < 0) {
to = from = resource;
} else {
from = resource.substring(0, i);
to = resource.substring(i + 1);
}
if (warnDuplicateFiles && combiner.containsFile(to)) {
System.err.println("File " + from + " at " + to + " clashes with a previous file");
continue;
}
combiner.addFile(to, date, fileSystem.getInputStream(from));
}
// Copy the jars into the jar file.
for (String inputJar : inputJars) {
File jar = fileSystem.getFile(inputJar);
combiner.addZip(jar);
}
// Close the output file. If something goes wrong here, delete the file.
combiner.close();
combiner = null;
} finally {
// This part is only executed if an exception occurred.
if (combiner != null) {
try {
// We may end up calling close twice, but that's ok.
combiner.close();
} catch (IOException e) {
// There's already an exception in progress - this won't add any
// additional information.
}
// Ignore return value - there's already an exception in progress.
fileSystem.delete(outputJar);
}
}
return 0;
}
use of java.io.InputStream in project bazel by bazelbuild.
the class ExtraDataListTest method testByteStream.
@Test
public void testByteStream() throws IOException {
byte[] buffer = new byte[] { (byte) 0xfe, (byte) 0xca, 0x03, 0x00, 0x00, 0x11, 0x22, (byte) 0xef, (byte) 0xbe, 0x03, 0x00, 0x33, 0x44, 0x55 };
ExtraDataList extra = new ExtraDataList(buffer);
byte[] bytes = new byte[7];
InputStream in = extra.getByteStream();
in.read(bytes);
// Expect 0xcafe 0x0003 0x00 0x11 0x22 in little endian
assertThat(bytes).isEqualTo(new byte[] { (byte) 0xfe, (byte) 0xca, 0x03, 0x00, 0x00, 0x11, 0x22 });
in.read(bytes);
// Expect 0xbeef 0x0003 0x33 0x44 0x55 in little endian
assertThat(bytes).isEqualTo(new byte[] { (byte) 0xef, (byte) 0xbe, 0x03, 0x00, 0x33, 0x44, 0x55 });
assertThat(in.read(bytes)).isEqualTo(-1);
}
use of java.io.InputStream in project bazel by bazelbuild.
the class ZipReaderTest method testZip64.
@Test
public void testZip64() throws IOException {
// Generated with: 'echo "foo" > entry; zip -fz -q out.zip entry'
byte[] data = new byte[] { 0x50, 0x4b, 0x03, 0x04, 0x2d, 0x00, 0x00, 0x00, 0x00, 0x00, 0x5d, (byte) 0x86, (byte) 0xa6, 0x46, (byte) 0xa8, 0x65, 0x32, 0x7e, (byte) 0xff, (byte) 0xff, (byte) 0xff, (byte) 0xff, (byte) 0xff, (byte) 0xff, (byte) 0xff, (byte) 0xff, 0x05, 0x00, 0x30, 0x00, 0x65, 0x6e, 0x74, 0x72, 0x79, 0x55, 0x54, 0x09, 0x00, 0x03, (byte) 0xb2, 0x7e, 0x4a, 0x55, (byte) 0xb2, 0x7e, 0x4a, 0x55, 0x75, 0x78, 0x0b, 0x00, 0x01, 0x04, 0x46, 0x3a, 0x04, 0x00, 0x04, (byte) 0x88, 0x13, 0x00, 0x00, 0x01, 0x00, 0x10, 0x00, 0x04, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x04, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x66, 0x6f, 0x6f, 0x0a, 0x50, 0x4b, 0x01, 0x02, 0x1e, 0x03, 0x2d, 0x00, 0x00, 0x00, 0x00, 0x00, 0x5d, (byte) 0x86, (byte) 0xa6, 0x46, (byte) 0xa8, 0x65, 0x32, 0x7e, 0x04, 0x00, 0x00, 0x00, (byte) 0xff, (byte) 0xff, (byte) 0xff, (byte) 0xff, 0x05, 0x00, 0x24, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, (byte) 0xa0, (byte) 0x81, 0x00, 0x00, 0x00, 0x00, 0x65, 0x6e, 0x74, 0x72, 0x79, 0x55, 0x54, 0x05, 0x00, 0x03, (byte) 0xb2, 0x7e, 0x4a, 0x55, 0x75, 0x78, 0x0b, 0x00, 0x01, 0x04, 0x46, 0x3a, 0x04, 0x00, 0x04, (byte) 0x88, 0x13, 0x00, 0x00, 0x01, 0x00, 0x08, 0x00, 0x04, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x50, 0x4b, 0x06, 0x06, 0x2c, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x1e, 0x03, 0x2d, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x57, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x57, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x50, 0x4b, 0x06, 0x07, 0x00, 0x00, 0x00, 0x00, (byte) 0xae, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0x50, 0x4b, 0x05, 0x06, 0x00, 0x00, 0x00, 0x00, 0x01, 0x00, 0x01, 0x00, 0x57, 0x00, 0x00, 0x00, (byte) 0xff, (byte) 0xff, (byte) 0xff, (byte) 0xff, 0x00, 0x00 };
try (FileOutputStream out = new FileOutputStream(test)) {
out.write(data);
}
String foo = "foo\n";
byte[] expectedFooData = foo.getBytes(UTF_8);
ExtraDataList extras = new ExtraDataList();
extras.add(new ExtraData((short) 0x0001, ZipUtil.longToLittleEndian(expectedFooData.length)));
byte[] extra = extras.getBytes();
CRC32 crc = new CRC32();
crc.reset();
crc.update(expectedFooData);
try (ZipReader reader = new ZipReader(test, UTF_8)) {
ZipFileEntry fooEntry = reader.getEntry("entry");
InputStream fooIn = reader.getInputStream(fooEntry);
byte[] fooData = new byte[expectedFooData.length];
fooIn.read(fooData);
assertThat(fooData).isEqualTo(expectedFooData);
assertThat(fooEntry.getName()).isEqualTo("entry");
assertThat(fooEntry.getComment()).isEqualTo("");
assertThat(fooEntry.getMethod()).isEqualTo(Compression.STORED);
assertThat(fooEntry.getVersionNeeded()).isEqualTo(Feature.ZIP64_SIZE.getMinVersion());
assertThat(fooEntry.getSize()).isEqualTo(expectedFooData.length);
assertThat(fooEntry.getCompressedSize()).isEqualTo(expectedFooData.length);
assertThat(fooEntry.getCrc()).isEqualTo(crc.getValue());
assertThat(fooEntry.getExtra().get((short) 0x0001).getBytes()).isEqualTo(extra);
}
}
use of java.io.InputStream in project bazel by bazelbuild.
the class ZipReaderTest method testRawFileData.
@Test
public void testRawFileData() throws IOException {
CRC32 crc = new CRC32();
Deflater deflator = new Deflater(Deflater.DEFAULT_COMPRESSION, true);
try (ZipOutputStream zout = new ZipOutputStream(new FileOutputStream(test))) {
ZipEntry foo = new ZipEntry("foo");
foo.setComment("foo comment.");
foo.setMethod(ZipEntry.DEFLATED);
zout.putNextEntry(foo);
zout.write("foo".getBytes(UTF_8));
zout.closeEntry();
ZipEntry bar = new ZipEntry("bar");
bar.setComment("bar comment.");
bar.setMethod(ZipEntry.STORED);
bar.setSize("bar".length());
bar.setCompressedSize("bar".length());
crc.reset();
crc.update("bar".getBytes(UTF_8));
bar.setCrc(crc.getValue());
zout.putNextEntry(bar);
zout.write("bar".getBytes(UTF_8));
zout.closeEntry();
}
try (ZipReader reader = new ZipReader(test, UTF_8)) {
ZipFileEntry fooEntry = reader.getEntry("foo");
InputStream fooIn = reader.getRawInputStream(fooEntry);
byte[] fooData = new byte[10];
fooIn.read(fooData);
byte[] expectedFooData = new byte[10];
deflator.reset();
deflator.setInput("foo".getBytes(UTF_8));
deflator.finish();
deflator.deflate(expectedFooData);
assertThat(fooData).isEqualTo(expectedFooData);
ZipFileEntry barEntry = reader.getEntry("bar");
InputStream barIn = reader.getRawInputStream(barEntry);
byte[] barData = new byte[3];
barIn.read(barData);
byte[] expectedBarData = "bar".getBytes(UTF_8);
assertThat(barData).isEqualTo(expectedBarData);
assertThat(barIn.read()).isEqualTo(-1);
assertThat(barIn.read(barData)).isEqualTo(-1);
assertThat(barIn.read(barData, 0, 3)).isEqualTo(-1);
thrown.expect(IOException.class);
thrown.expectMessage("Reset is not supported on this type of stream.");
barIn.reset();
}
}
use of java.io.InputStream in project bazel by bazelbuild.
the class ZipReaderTest method testFileData.
@Test
public void testFileData() throws IOException {
CRC32 crc = new CRC32();
try (ZipOutputStream zout = new ZipOutputStream(new FileOutputStream(test))) {
ZipEntry foo = new ZipEntry("foo");
foo.setComment("foo comment.");
foo.setMethod(ZipEntry.DEFLATED);
zout.putNextEntry(foo);
zout.write("foo".getBytes(UTF_8));
zout.closeEntry();
ZipEntry bar = new ZipEntry("bar");
bar.setComment("bar comment.");
bar.setMethod(ZipEntry.STORED);
bar.setSize("bar".length());
bar.setCompressedSize("bar".length());
crc.reset();
crc.update("bar".getBytes(UTF_8));
bar.setCrc(crc.getValue());
zout.putNextEntry(bar);
zout.write("bar".getBytes(UTF_8));
zout.closeEntry();
}
try (ZipReader reader = new ZipReader(test, UTF_8)) {
ZipFileEntry fooEntry = reader.getEntry("foo");
InputStream fooIn = reader.getInputStream(fooEntry);
byte[] fooData = new byte[3];
fooIn.read(fooData);
byte[] expectedFooData = "foo".getBytes(UTF_8);
assertThat(fooData).isEqualTo(expectedFooData);
assertThat(fooIn.read()).isEqualTo(-1);
assertThat(fooIn.read(fooData)).isEqualTo(-1);
assertThat(fooIn.read(fooData, 0, 3)).isEqualTo(-1);
ZipFileEntry barEntry = reader.getEntry("bar");
InputStream barIn = reader.getInputStream(barEntry);
byte[] barData = new byte[3];
barIn.read(barData);
byte[] expectedBarData = "bar".getBytes(UTF_8);
assertThat(barData).isEqualTo(expectedBarData);
assertThat(barIn.read()).isEqualTo(-1);
assertThat(barIn.read(barData)).isEqualTo(-1);
assertThat(barIn.read(barData, 0, 3)).isEqualTo(-1);
thrown.expect(IOException.class);
thrown.expectMessage("Reset is not supported on this type of stream.");
barIn.reset();
}
}
Aggregations