Search in sources :

Example 56 with Closer

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();
    }
}
Also used : Closer(com.google.common.io.Closer) ZipInputStream(java.util.zip.ZipInputStream) ZipEntry(java.util.zip.ZipEntry) ZipAbortException(com.android.builder.packaging.ZipAbortException) IOException(java.io.IOException) JarEntry(java.util.jar.JarEntry) FileInputStream(java.io.FileInputStream)

Example 57 with Closer

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();
    }
}
Also used : Closer(com.google.common.io.Closer) Joiner(com.google.common.base.Joiner) SortedMap(java.util.SortedMap) ArrayList(java.util.ArrayList) List(java.util.List) HashMap(java.util.HashMap) Map(java.util.Map) TreeMap(java.util.TreeMap) SortedMap(java.util.SortedMap) BufferedWriter(java.io.BufferedWriter)

Example 58 with Closer

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();
    }
}
Also used : Closer(com.google.common.io.Closer) InputStreamReader(java.io.InputStreamReader) EventType(com.carrotsearch.ant.tasks.junit4.events.EventType) BufferedInputStream(java.io.BufferedInputStream) FileInputStream(java.io.FileInputStream) InputStream(java.io.InputStream) OutputStream(java.io.OutputStream) FileOutputStream(java.io.FileOutputStream) BufferedOutputStream(java.io.BufferedOutputStream) FileInputStream(java.io.FileInputStream) IStreamEvent(com.carrotsearch.ant.tasks.junit4.events.IStreamEvent) BufferedInputStream(java.io.BufferedInputStream) FileOutputStream(java.io.FileOutputStream) JsonReader(com.carrotsearch.ant.tasks.junit4.gson.stream.JsonReader) JsonToken(com.carrotsearch.ant.tasks.junit4.gson.stream.JsonToken) File(java.io.File) BufferedOutputStream(java.io.BufferedOutputStream)

Example 59 with Closer

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);
    }
}
Also used : Closer(com.google.common.io.Closer) BatfishException(org.batfish.common.BatfishException) LZ4FrameOutputStream(net.jpountz.lz4.LZ4FrameOutputStream) BufferedOutputStream(java.io.BufferedOutputStream) ObjectOutputStream(java.io.ObjectOutputStream) OutputStream(java.io.OutputStream) FilterOutputStream(java.io.FilterOutputStream) IOException(java.io.IOException) BufferedOutputStream(java.io.BufferedOutputStream)

Example 60 with Closer

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);
    }
}
Also used : Closer(com.google.common.io.Closer) BatfishException(org.batfish.common.BatfishException) DomDriver(com.thoughtworks.xstream.io.xml.DomDriver) XStream(com.thoughtworks.xstream.XStream) LZ4FrameOutputStream(net.jpountz.lz4.LZ4FrameOutputStream) BufferedOutputStream(java.io.BufferedOutputStream) ObjectOutputStream(java.io.ObjectOutputStream) OutputStream(java.io.OutputStream) FilterOutputStream(java.io.FilterOutputStream) IOException(java.io.IOException) ObjectOutputStream(java.io.ObjectOutputStream) LZ4FrameOutputStream(net.jpountz.lz4.LZ4FrameOutputStream)

Aggregations

Closer (com.google.common.io.Closer)213 IOException (java.io.IOException)95 File (java.io.File)26 Test (org.testng.annotations.Test)21 Path (org.apache.hadoop.fs.Path)18 Test (org.junit.Test)18 Properties (java.util.Properties)16 Closer (org.apache.flink.shaded.guava30.com.google.common.io.Closer)16 FileOutputStream (java.io.FileOutputStream)15 ArrayList (java.util.ArrayList)15 WorkUnit (org.apache.gobblin.source.workunit.WorkUnit)13 FileInputStream (java.io.FileInputStream)12 InputStream (java.io.InputStream)12 OutputStream (java.io.OutputStream)12 Map (java.util.Map)12 ByteArrayInputStream (java.io.ByteArrayInputStream)10 DataInputStream (java.io.DataInputStream)10 UncheckedIOException (java.io.UncheckedIOException)10 Configuration (org.apache.hadoop.conf.Configuration)10 Text (org.apache.hadoop.io.Text)9