use of org.apache.flink.shaded.guava30.com.google.common.io.Closer in project atlas by alibaba.
the class JarMergerWithOverride method addJar.
public void addJar(@NonNull File file, boolean removeEntryTimestamp) throws IOException {
logger.verbose("addJar(%1$s)", file);
init();
Closer localCloser = Closer.create();
try {
FileInputStream fis = localCloser.register(new FileInputStream(file));
ZipInputStream zis = localCloser.register(new ZipInputStream(fis));
// loop on the entries of the jar file package and put them in the final jar
ZipEntry entry;
while ((entry = zis.getNextEntry()) != null) {
// do not take directories or anything inside a potential META-INF folder.
if (entry.isDirectory()) {
continue;
}
String name = entry.getName();
if (filter != null && !filter.checkEntry(entry.getName())) {
continue;
}
JarEntry newEntry;
// Preserve the STORED method of the input entry.
if (entry.getMethod() == JarEntry.STORED) {
newEntry = new JarEntry(entry);
} else {
// Create a new entry so that the compressed len is recomputed.
newEntry = new JarEntry(name);
}
if (removeEntryTimestamp) {
newEntry.setTime(0);
}
// add the entry to the jar archive
logger.verbose("addJar(%1$s): entry %2$s", file, name);
duplicates.put(name, file.getAbsolutePath());
if (duplicates.get(name).size() > 1) {
logger.info("[Duplicated]" + name + ":" + file.getAbsolutePath() + ":" + duplicates.get(name));
continue;
}
jarOutputStream.putNextEntry(newEntry);
// read the content of the entry from the input stream, and write it into the archive.
int count;
while ((count = zis.read(buffer)) != -1) {
jarOutputStream.write(buffer, 0, count);
}
// close the entries for this file
jarOutputStream.closeEntry();
zis.closeEntry();
}
} catch (ZipAbortException e) {
throw new IOException("check exception", e);
} finally {
localCloser.close();
}
}
use of org.apache.flink.shaded.guava30.com.google.common.io.Closer in project randomizedtesting by randomizedtesting.
the class ExecutionTimesReport method writeHints.
/**
* Writes back hints file.
*/
public static void writeHints(File file, Map<String, List<Long>> hints) throws IOException {
Closer closer = Closer.create();
try {
BufferedWriter w = closer.register(Files.newWriter(file, Charsets.UTF_8));
if (!(hints instanceof SortedMap)) {
hints = new TreeMap<String, List<Long>>(hints);
}
Joiner joiner = Joiner.on(',');
for (Map.Entry<String, List<Long>> e : hints.entrySet()) {
w.write(e.getKey());
w.write("=");
joiner.appendTo(w, e.getValue());
w.write("\n");
}
} catch (Throwable t) {
throw closer.rethrow(t);
} finally {
closer.close();
}
}
use of org.apache.flink.shaded.guava30.com.google.common.io.Closer in project randomizedtesting by randomizedtesting.
the class DumpStreamsFromEventStream method main.
public static void main(String[] args) throws Exception {
File inputFile;
if (args.length != 1) {
System.err.println("Usage: [input.events]");
System.exit(1);
return;
} else {
inputFile = new File(args[0]);
}
Closer closer = Closer.create();
try {
OutputStream sysout = new BufferedOutputStream(new FileOutputStream(new File(inputFile.getAbsolutePath() + ".sysout")));
closer.register(sysout);
OutputStream syserr = new BufferedOutputStream(new FileOutputStream(new File(inputFile.getAbsolutePath() + ".syserr")));
closer.register(syserr);
InputStream is = new BufferedInputStream(new FileInputStream(inputFile));
closer.register(is);
JsonReader input = new JsonReader(new InputStreamReader(is, Charsets.UTF_8));
input.setLenient(true);
JsonToken peek;
while (true) {
peek = input.peek();
if (peek == JsonToken.END_DOCUMENT) {
return;
}
input.beginArray();
EventType type = EventType.valueOf(input.nextString());
switch(type) {
case APPEND_STDERR:
IStreamEvent.class.cast(type.deserialize(input)).copyTo(syserr);
break;
case APPEND_STDOUT:
IStreamEvent.class.cast(type.deserialize(input)).copyTo(sysout);
break;
default:
input.skipValue();
}
input.endArray();
}
} catch (Throwable t) {
throw closer.rethrow(t);
} finally {
closer.close();
}
}
use of org.apache.flink.shaded.guava30.com.google.common.io.Closer in project batfish by batfish.
the class PluginConsumer method serializeObject.
/**
* Serializes the given object to a file with the given output name.
*/
public void serializeObject(Serializable object, Path outputFile) {
try {
try (Closer closer = Closer.create()) {
OutputStream out = closer.register(Files.newOutputStream(outputFile));
BufferedOutputStream bout = closer.register(new BufferedOutputStream(out));
serializeToLz4Data(object, bout);
}
} catch (IOException e) {
throw new BatfishException("Failed to serialize object to output file: " + outputFile, e);
}
}
use of org.apache.flink.shaded.guava30.com.google.common.io.Closer in project batfish by batfish.
the class PluginConsumer method serializeToLz4Data.
/**
* Serializes the given object to the given stream, using LZ4 compression.
*/
private void serializeToLz4Data(Serializable object, OutputStream out) {
// This is a hack:
// XStream requires that its streams be closed to properly finish serialization,
// but we do not actually want to close the passed-in output stream.
out = new CloseIgnoringOutputStream(out);
try (Closer closer = Closer.create()) {
OutputStream los = closer.register(new LZ4FrameOutputStream(out));
ObjectOutputStream oos;
if (_serializeToText) {
XStream xstream = new XStream(new DomDriver("UTF-8"));
oos = closer.register(xstream.createObjectOutputStream(los));
} else {
oos = closer.register(new ObjectOutputStream(los));
}
oos.writeObject(object);
} catch (IOException e) {
throw new BatfishException("Failed to convert object to LZ4 data", e);
}
}
Aggregations