Example usage for com.google.common.io Closeables close

List of usage examples for com.google.common.io Closeables close

Introduction

In this page you can find the example usage for com.google.common.io Closeables close.

Prototype

public static void close(@Nullable Closeable closeable, boolean swallowIOException) throws IOException 

Source Link

Document

Closes a Closeable , with control over whether an IOException may be thrown.

Usage

From source file:org.qcri.pca.MeanAndSpanJob.java

/**
 * After running the job, we can load the results from HDFS with this method
 * /*from  w  w  w  . j  a va2 s  .  co m*/
 * @param meanSpanPath
 *          the path to the single file having the results
 * @param normalizeMean
 *          normalize the mean as well
 * @param conf
 *          the configuration
 * @throws IOException
 *           when face problem parsing the result file
 */
public void loadResults(Path meanSpanPath, boolean normalizeMean, Configuration conf) throws IOException {
    SequenceFileIterator<IntWritable, VectorWritable> iterator = new SequenceFileIterator<IntWritable, VectorWritable>(
            meanSpanPath, true, conf);
    try {
        Pair<IntWritable, VectorWritable> next;
        next = iterator.next();
        if (next.getFirst().get() == MEANVECTOR)
            meanVector = next.getSecond().get();
        else
            spanVector = next.getSecond().get();
        next = iterator.next();
        if (next.getFirst().get() == MEANVECTOR)
            meanVector = next.getSecond().get();
        else
            spanVector = next.getSecond().get();
    } finally {
        Closeables.close(iterator, false);
    }
    if (normalizeMean)
        meanVector.assign(spanVector, new DoubleDoubleFunction() {
            @Override
            public double apply(double v, double span) {
                return v / (span != 0 ? span : 1);
            }
        });
}

From source file:org.openqa.selenium.io.FileHandler.java

public static String readAsString(File toRead) throws IOException {
    Reader reader = null;//from w ww.j a  v  a  2  s . c  o m
    try {
        reader = new BufferedReader(new FileReader(toRead));
        StringBuilder builder = new StringBuilder();

        char[] buffer = new char[4096];
        int read;
        while ((read = reader.read(buffer)) != -1) {
            char[] target = new char[read];
            System.arraycopy(buffer, 0, target, 0, read);
            builder.append(target);
        }

        return builder.toString();
    } finally {
        Closeables.close(reader, false);
    }
}

From source file:com.replaymod.replaystudio.replay.ZipReplayFile.java

@Override
public OutputStream write(String entry) throws IOException {
    saveInputFile();/*w w  w  .  j a v  a 2s .c  om*/
    File file = changedEntries.get(entry);
    if (file == null) {
        file = new File(changedFiles, entry);
        createParentDirs(file);
        changedEntries.put(entry, file);
    }
    OutputStream out = new BufferedOutputStream(new FileOutputStream(file));
    Closeables.close(outputStreams.put(entry, out), true);
    if (removedEntries.remove(entry)) {
        deleteIfExists(new File(removedFiles, entry).toPath());
    }
    return out;
}

From source file:eu.interedition.text.query.QueryCriterion.java

public void listen(Session session, final Text text, final int pageSize, final AnnotationListener listener)
        throws IOException {
    final long contentLength = text.getLength();
    Reader contentStream = null;//from  w  w w.j a  va  2 s  .c  o  m
    try {
        contentStream = text.read().getInput();
        final SortedMap<Long, Set<Annotation>> starts = Maps.newTreeMap();
        final SortedMap<Long, Set<Annotation>> ends = Maps.newTreeMap();

        long offset = 0;
        long next = 0;
        long pageEnd = 0;

        listener.start(contentLength);

        final Set<Annotation> annotationData = Sets.newHashSet();
        while (true) {
            if ((offset % pageSize) == 0) {
                pageEnd = Math.min(offset + pageSize, contentLength);
                final TextRange pageRange = new TextRange(offset, pageEnd);
                final Iterable<Annotation> pageAnnotations = and(this, text(text), rangeOverlap(pageRange))
                        .iterate(session);
                for (Annotation a : pageAnnotations) {
                    for (TextTarget target : a.getTargets()) {
                        if (!text.equals(target.getText())) {
                            continue;
                        }
                        final long start = target.getStart();
                        final long end = target.getEnd();
                        if (start >= offset) {
                            Set<Annotation> starting = starts.get(start);
                            if (starting == null) {
                                starts.put(start, starting = Sets.newHashSet());
                            }
                            starting.add(a);
                            annotationData.add(a);
                        }
                        if (end <= pageEnd) {
                            Set<Annotation> ending = ends.get(end);
                            if (ending == null) {
                                ends.put(end, ending = Sets.newHashSet());
                            }
                            ending.add(a);
                            annotationData.add(a);
                        }
                    }
                }

                next = Math.min(starts.isEmpty() ? contentLength : starts.firstKey(),
                        ends.isEmpty() ? contentLength : ends.firstKey());
            }

            if (offset == next) {
                final Set<Annotation> startEvents = (!starts.isEmpty() && offset == starts.firstKey()
                        ? starts.remove(starts.firstKey())
                        : Sets.<Annotation>newHashSet());
                final Set<Annotation> endEvents = (!ends.isEmpty() && offset == ends.firstKey()
                        ? ends.remove(ends.firstKey())
                        : Sets.<Annotation>newHashSet());

                final Set<Annotation> emptyEvents = Sets.newHashSet(Sets.filter(endEvents, emptyIn(text)));
                endEvents.removeAll(emptyEvents);

                if (!endEvents.isEmpty())
                    listener.end(offset, filter(annotationData, endEvents, true));
                if (!startEvents.isEmpty())
                    listener.start(offset, filter(annotationData, startEvents, false));
                if (!emptyEvents.isEmpty())
                    listener.end(offset, filter(annotationData, emptyEvents, true));

                next = Math.min(starts.isEmpty() ? contentLength : starts.firstKey(),
                        ends.isEmpty() ? contentLength : ends.firstKey());
            }

            if (offset == contentLength) {
                break;
            }

            final long readTo = Math.min(pageEnd, next);
            if (offset < readTo) {
                final char[] currentText = new char[(int) (readTo - offset)];
                int read = contentStream.read(currentText);
                if (read > 0) {
                    listener.text(new TextRange(offset, offset + read), new String(currentText, 0, read));
                    offset += read;
                }
            }
        }

        listener.end();
    } finally {
        Closeables.close(contentStream, false);
    }
}

From source file:net.pterodactylus.sonitus.data.sink.Icecast2Sink.java

@Override
public void metadataUpdated(final Metadata metadata) {
    super.metadataUpdated(metadata);
    new Thread(new Runnable() {

        @Override// w w  w  .  j  a va  2  s .c om
        public void run() {
            String metadataString = String.format("%s (%s)", metadata.title(), "Sonitus");
            logger.info(String.format("Updating metadata to %s", metadataString));

            Socket socket = null;
            OutputStream socketOutputStream = null;
            try {
                socket = new Socket(server, port);
                socketOutputStream = socket.getOutputStream();

                sendLine(socketOutputStream,
                        String.format("GET /admin/metadata?pass=%s&mode=updinfo&mount=/%s&song=%s HTTP/1.0",
                                password, mountPoint, URLEncoder.encode(metadataString, "UTF-8")));
                sendLine(socketOutputStream,
                        String.format("Authorization: Basic %s", generatePassword(password)));
                sendLine(socketOutputStream, String.format("User-Agent: Mozilla/Sonitus"));
                sendLine(socketOutputStream, "");
                socketOutputStream.flush();

                new InputStreamDrainer(socket.getInputStream()).run();
            } catch (IOException ioe1) {
                logger.log(Level.WARNING, "Could not update metadata!", ioe1);
            } finally {
                try {
                    Closeables.close(socketOutputStream, true);
                    if (socket != null) {
                        socket.close();
                    }
                } catch (IOException ioe1) {
                    /* ignore. */
                }
            }
        }
    }).start();
}

From source file:org.apache.mahout.utils.SequenceFileDumper.java

@Override
public int run(String[] args) throws Exception {

    addInputOption();//from  w w  w  .  j a v  a 2  s  .c  om
    addOutputOption();
    addOption("substring", "b", "The number of chars to print out per value", false);
    addOption(buildOption("count", "c", "Report the count only", false, false, null));
    addOption("numItems", "n", "Output at most <n> key value pairs", false);
    addOption(
            buildOption("facets", "fa", "Output the counts per key.  Note, if there are a lot of unique keys, "
                    + "this can take up a fair amount of memory", false, false, null));
    addOption(buildOption("quiet", "q", "Print only file contents.", false, false, null));

    if (parseArguments(args, false, true) == null) {
        return -1;
    }

    Path[] pathArr;
    Configuration conf = new Configuration();
    Path input = getInputPath();
    FileSystem fs = input.getFileSystem(conf);
    if (fs.getFileStatus(input).isDir()) {
        pathArr = FileUtil.stat2Paths(fs.listStatus(input, PathFilters.logsCRCFilter()));
    } else {
        pathArr = new Path[1];
        pathArr[0] = input;
    }

    Writer writer;
    boolean shouldClose;
    if (hasOption("output")) {
        shouldClose = true;
        writer = Files.newWriter(new File(getOption("output")), Charsets.UTF_8);
    } else {
        shouldClose = false;
        writer = new OutputStreamWriter(System.out, Charsets.UTF_8);
    }
    try {
        for (Path path : pathArr) {
            if (!hasOption("quiet")) {
                writer.append("Input Path: ").append(String.valueOf(path)).append('\n');
            }

            int sub = Integer.MAX_VALUE;
            if (hasOption("substring")) {
                sub = Integer.parseInt(getOption("substring"));
            }
            boolean countOnly = hasOption("count");
            SequenceFileIterator<?, ?> iterator = new SequenceFileIterator<Writable, Writable>(path, true,
                    conf);
            if (!hasOption("quiet")) {
                writer.append("Key class: ").append(iterator.getKeyClass().toString());
                writer.append(" Value Class: ").append(iterator.getValueClass().toString()).append('\n');
            }
            OpenObjectIntHashMap<String> facets = null;
            if (hasOption("facets")) {
                facets = new OpenObjectIntHashMap<String>();
            }
            long count = 0;
            if (countOnly) {
                while (iterator.hasNext()) {
                    Pair<?, ?> record = iterator.next();
                    String key = record.getFirst().toString();
                    if (facets != null) {
                        facets.adjustOrPutValue(key, 1, 1); //either insert or add 1
                    }
                    count++;
                }
                writer.append("Count: ").append(String.valueOf(count)).append('\n');
            } else {
                long numItems = Long.MAX_VALUE;
                if (hasOption("numItems")) {
                    numItems = Long.parseLong(getOption("numItems"));
                    if (!hasOption("quiet")) {
                        writer.append("Max Items to dump: ").append(String.valueOf(numItems)).append("\n");
                    }
                }
                while (iterator.hasNext() && count < numItems) {
                    Pair<?, ?> record = iterator.next();
                    String key = record.getFirst().toString();
                    writer.append("Key: ").append(key);
                    String str = record.getSecond().toString();
                    writer.append(": Value: ").append(str.length() > sub ? str.substring(0, sub) : str);
                    writer.write('\n');
                    if (facets != null) {
                        facets.adjustOrPutValue(key, 1, 1); //either insert or add 1
                    }
                    count++;
                }
                if (!hasOption("quiet")) {
                    writer.append("Count: ").append(String.valueOf(count)).append('\n');
                }
            }
            if (facets != null) {
                List<String> keyList = Lists.newArrayListWithCapacity(facets.size());

                IntArrayList valueList = new IntArrayList(facets.size());
                facets.pairsSortedByKey(keyList, valueList);
                writer.append("-----Facets---\n");
                writer.append("Key\t\tCount\n");
                int i = 0;
                for (String key : keyList) {
                    writer.append(key).append("\t\t").append(String.valueOf(valueList.get(i++))).append('\n');
                }
            }
        }
        writer.flush();

    } finally {
        if (shouldClose) {
            Closeables.close(writer, false);
        }
    }

    return 0;
}

From source file:com.netease.news.utils.SequenceFileDumper.java

@Override
public int run(String[] args) throws Exception {

    addInputOption();//from  w  w w.ja  v a  2 s. c  o m
    addOutputOption();
    addOption("substring", "b", "The number of chars to print out per value", false);
    addOption(buildOption("count", "c", "Report the count only", false, false, null));
    addOption("numItems", "n", "Output at most <n> key value pairs", false);
    addOption(
            buildOption("facets", "fa", "Output the counts per key.  Note, if there are a lot of unique keys, "
                    + "this can take up a fair amount of memory", false, false, null));
    addOption(buildOption("quiet", "q", "Print only file contents.", false, false, null));

    if (parseArguments(args, false, true) == null) {
        return -1;
    }

    Path[] pathArr;
    Configuration conf = new Configuration();
    Path input = getInputPath();
    FileSystem fs = input.getFileSystem(conf);
    if (fs.getFileStatus(input).isDir()) {
        pathArr = FileUtil.stat2Paths(fs.listStatus(input, new OutputFilesFilter()));
    } else {
        pathArr = new Path[1];
        pathArr[0] = input;
    }

    Writer writer;
    boolean shouldClose;
    if (hasOption("output")) {
        shouldClose = true;
        writer = Files.newWriter(new File(getOption("output")), Charsets.UTF_8);
    } else {
        shouldClose = false;
        writer = new OutputStreamWriter(System.out, Charsets.UTF_8);
    }
    try {
        for (Path path : pathArr) {
            if (!hasOption("quiet")) {
                writer.append("Input Path: ").append(String.valueOf(path)).append('\n');
            }

            int sub = Integer.MAX_VALUE;
            if (hasOption("substring")) {
                sub = Integer.parseInt(getOption("substring"));
            }
            boolean countOnly = hasOption("count");
            SequenceFileIterator<?, ?> iterator = new SequenceFileIterator<Writable, Writable>(path, true,
                    conf);
            if (!hasOption("quiet")) {
                writer.append("Key class: ").append(iterator.getKeyClass().toString());
                writer.append(" Value Class: ").append(iterator.getValueClass().toString()).append('\n');
            }
            OpenObjectIntHashMap<String> facets = null;
            if (hasOption("facets")) {
                facets = new OpenObjectIntHashMap<String>();
            }
            long count = 0;
            if (countOnly) {
                while (iterator.hasNext()) {
                    Pair<?, ?> record = iterator.next();
                    String key = record.getFirst().toString();
                    if (facets != null) {
                        facets.adjustOrPutValue(key, 1, 1); //either insert or add 1
                    }
                    count++;
                }
                writer.append("Count: ").append(String.valueOf(count)).append('\n');
            } else {
                long numItems = Long.MAX_VALUE;
                if (hasOption("numItems")) {
                    numItems = Long.parseLong(getOption("numItems"));
                    if (!hasOption("quiet")) {
                        writer.append("Max Items to dump: ").append(String.valueOf(numItems)).append("\n");
                    }
                }
                while (iterator.hasNext() && count < numItems) {
                    Pair<?, ?> record = iterator.next();
                    String key = record.getFirst().toString();
                    writer.append("Key: ").append(key);
                    String str = record.getSecond().toString();
                    writer.append(": Value: ").append(str.length() > sub ? str.substring(0, sub) : str);
                    writer.write('\n');
                    if (facets != null) {
                        facets.adjustOrPutValue(key, 1, 1); //either insert or add 1
                    }
                    count++;
                }
                if (!hasOption("quiet")) {
                    writer.append("Count: ").append(String.valueOf(count)).append('\n');
                }
            }
            if (facets != null) {
                List<String> keyList = Lists.newArrayListWithCapacity(facets.size());

                IntArrayList valueList = new IntArrayList(facets.size());
                facets.pairsSortedByKey(keyList, valueList);
                writer.append("-----Facets---\n");
                writer.append("Key\t\tCount\n");
                int i = 0;
                for (String key : keyList) {
                    writer.append(key).append("\t\t").append(String.valueOf(valueList.get(i++))).append('\n');
                }
            }
        }
        writer.flush();

    } finally {
        if (shouldClose) {
            Closeables.close(writer, false);
        }
    }

    return 0;
}

From source file:org.plista.kornakapi.core.training.SemanticModel.java

/**
 * Key is set to handle concurent writes from DocumentTopicInferenceTrainer and LDATrainer
 * @throws IOException/*from  ww w .  j av  a  2 s . c  o m*/
 */
private void readKey() throws IOException {
    Path keyPath = path.suffix("/key.txt");
    if (fs.exists(keyPath)) {
        Reader reader = new SequenceFile.Reader(fs, keyPath, lconf);
        IntWritable key = new IntWritable();
        Text val = new Text();
        reader.next(key, val);
        this.key = val.toString();
        Closeables.close(reader, false);
    }
}

From source file:org.jclouds.examples.rackspace.DeleteAll.java

private void deleteCloudBlockStorage() throws IOException {
    CinderApi cinderApi = ContextBuilder
            .newBuilder(System.getProperty("provider.cbs", "rackspace-cloudblockstorage-us"))
            .credentials(username, apiKey).buildApi(CinderApi.class);

    for (String region : cinderApi.getConfiguredRegions()) {
        try {/*from  w  w w.  j  av  a2 s  .co  m*/
            System.out.format("Delete Snapshots in %s%n", region);
            SnapshotApi snapshotApi = cinderApi.getSnapshotApi(region);

            for (Snapshot snapshot : snapshotApi.list()) {
                System.out.format("  %s%n", snapshot.getName());
                snapshotApi.delete(snapshot.getId());
            }

            System.out.format("Delete Volumes in %s%n", region);
            VolumeApi volumeApi = cinderApi.getVolumeApi(region);

            for (Volume volume : volumeApi.list()) {
                System.out.format("  %s%n", volume.getName());
                volumeApi.delete(volume.getId());
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    Closeables.close(cinderApi, true);
}

From source file:com.kixeye.chassis.bootstrap.configuration.zookeeper.CuratorFrameworkBuilder.java

private CuratorFramework buildCuratorWithExhibitor(Configuration configuration) {
    LOGGER.debug("configuring zookeeper connection through Exhibitor...");
    ExhibitorEnsembleProvider ensembleProvider = new KixeyeExhibitorEnsembleProvider(exhibitors,
            new KixeyeExhibitorRestClient(configuration.getBoolean(EXHIBITOR_USE_HTTPS.getPropertyName())),
            configuration.getString(EXHIBITOR_URI_PATH.getPropertyName()),
            configuration.getInt(EXHIBITOR_POLL_INTERVAL.getPropertyName()),
            new ExponentialBackoffRetry(configuration.getInt(EXHIBITOR_INITIAL_SLEEP_MILLIS.getPropertyName()),
                    configuration.getInt(EXHIBITOR_MAX_RETRIES.getPropertyName()),
                    configuration.getInt(EXHIBITOR_RETRIES_MAX_MILLIS.getPropertyName())));

    //without this (undocumented) step, curator will attempt (and fail) to connect to a local instance of zookeeper (default behavior if no zookeeper connection string is provided) for
    //several seconds until the EnsembleProvider polls to get the SERVER list from Exhibitor. Polling before staring curator
    //ensures that the SERVER list from Exhibitor is already downloaded before curator attempts to connect to zookeeper.
    try {/*from ww  w .java 2s  .  co  m*/
        ensembleProvider.pollForInitialEnsemble();
    } catch (Exception e) {
        try {
            Closeables.close(ensembleProvider, true);
        } catch (IOException e1) {
        }
        throw new BootstrapException("Failed to initialize Exhibitor with host(s) " + exhibitors.getHostnames(),
                e);
    }

    CuratorFramework curator = CuratorFrameworkFactory.builder().ensembleProvider(ensembleProvider)
            .retryPolicy(buildZookeeperRetryPolicy(configuration)).build();
    curator.getConnectionStateListenable().addListener(new ConnectionStateListener() {
        public void stateChanged(CuratorFramework client, ConnectionState newState) {
            LOGGER.debug("Connection state to ZooKeeper changed: " + newState);
        }
    });

    return curator;
}