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.apache.mahout.clustering.kmeans.EigenSeedGenerator.java

public static Path buildFromEigens(Configuration conf, Path input, Path output, int k, DistanceMeasure measure)
        throws IOException {
    // delete the output directory
    FileSystem fs = FileSystem.get(output.toUri(), conf);
    HadoopUtil.delete(conf, output);//from  ww w .ja  va2  s. c o m
    Path outFile = new Path(output, "part-eigenSeed");
    boolean newFile = fs.createNewFile(outFile);
    if (newFile) {
        Path inputPathPattern;

        if (fs.getFileStatus(input).isDir()) {
            inputPathPattern = new Path(input, "*");
        } else {
            inputPathPattern = input;
        }

        FileStatus[] inputFiles = fs.globStatus(inputPathPattern, PathFilters.logsCRCFilter());
        SequenceFile.Writer writer = SequenceFile.createWriter(fs, conf, outFile, Text.class,
                ClusterWritable.class);
        Map<Integer, Double> maxEigens = Maps.newHashMapWithExpectedSize(k); // store
                                                                             // max
                                                                             // value
                                                                             // of
                                                                             // each
                                                                             // column
        Map<Integer, Text> chosenTexts = Maps.newHashMapWithExpectedSize(k);
        Map<Integer, ClusterWritable> chosenClusters = Maps.newHashMapWithExpectedSize(k);

        for (FileStatus fileStatus : inputFiles) {
            if (!fileStatus.isDir()) {
                for (Pair<Writable, VectorWritable> record : new SequenceFileIterable<Writable, VectorWritable>(
                        fileStatus.getPath(), true, conf)) {
                    Writable key = record.getFirst();
                    VectorWritable value = record.getSecond();

                    for (Vector.Element e : value.get().nonZeroes()) {
                        int index = e.index();
                        double v = Math.abs(e.get());

                        if (!maxEigens.containsKey(index) || v > maxEigens.get(index)) {
                            maxEigens.put(index, v);
                            Text newText = new Text(key.toString());
                            chosenTexts.put(index, newText);
                            Kluster newCluster = new Kluster(value.get(), index, measure);
                            newCluster.observe(value.get(), 1);
                            ClusterWritable clusterWritable = new ClusterWritable();
                            clusterWritable.setValue(newCluster);
                            chosenClusters.put(index, clusterWritable);
                        }
                    }
                }
            }
        }

        try {
            for (Integer key : maxEigens.keySet()) {
                writer.append(chosenTexts.get(key), chosenClusters.get(key));
            }
            log.info("EigenSeedGenerator:: Wrote {} Klusters to {}", chosenTexts.size(), outFile);
        } finally {
            Closeables.close(writer, false);
        }
    }

    return outFile;
}

From source file:se.curity.examples.oauth.OAuthJwtFilter.java

@Override
public void destroy() {
    _logger.info("Destroying OAuthFilter");
    try {//from w ww  .  jav a 2  s  .  c om
        Closeables.close(_jwtValidator, true);
    } catch (IOException e) {
        _logger.warn("Problem closing jwk client", e);
    }
}

From source file:org.apache.mahout.classifier.df.DFUtils.java

/**
 * Write a string to a path.//from   www . j a  v  a  2  s .c o m
 * @param conf From which the file system will be picked
 * @param path Where the string will be written
 * @param string The string to write
 * @throws IOException if things go poorly
 */
public static void storeString(Configuration conf, Path path, String string) throws IOException {
    DataOutputStream out = null;
    try {
        out = path.getFileSystem(conf).create(path);
        out.write(string.getBytes(Charset.defaultCharset()));
    } finally {
        Closeables.close(out, false);
    }
}

From source file:net.opentsdb.contrib.tsquare.web.controller.ExtendedApiController.java

@RequestMapping(value = "/grep", method = RequestMethod.GET)
public void grep(@RequestParam(required = false, defaultValue = "") String type,
        @RequestParam(required = false, defaultValue = "wildcard") String method,
        @RequestParam(required = true) String q, final HttpServletResponse servletResponse) throws IOException {

    if (log.isInfoEnabled()) {
        log.info("Suggest {} using {} expression: {}", type, method, q);
    }//from  w ww . j  ava2s  .c  om

    // Do we have a valid type? Note that an empty "type" is valid.
    if (!Strings.isNullOrEmpty(type)) {
        Preconditions.checkArgument(getTsdbManager().getKnownUidKinds().contains(type), "Unknown type: %s",
                type);
    }

    // We can only query hbase using regex, so convert a wildcard query into
    // a regex if necessary.
    final String regex;
    if ("wildcard".equalsIgnoreCase(method)) {
        regex = TsWebUtils.wildcardToRegex(q);
        log.debug("Converted wildcard expression {} to regex: {}", q, regex);
    } else {
        regex = q;
    }

    final UidQuery query = getTsdbManager().newUidQuery();
    query.setRegex(regex);

    if (Strings.isNullOrEmpty(type)) {
        query.includeAllKinds();
    } else {
        query.includeKind(type);
    }

    servletResponse.setContentType("application/json");
    final OutputStream stream = servletResponse.getOutputStream();
    final JsonGenerator json = new JsonFactory().createJsonGenerator(stream);

    try {
        json.writeStartArray();

        query.run(new QueryCallback<Uid>() {
            @Override
            public boolean onResult(final Uid resultObject) {
                try {
                    json.writeString(resultObject.getName());
                    return true;
                } catch (IOException e) {
                    throw new IllegalArgumentException("Unable to serialize " + resultObject + " to JSON", e);
                }
            }
        });

        json.writeEndArray();
        json.flush();
    } finally {
        Closeables.close(stream, false);
    }
}

From source file:org.apache.mahout.math.hadoop.similarity.cooccurrence.Vectors.java

public static void write(Vector vector, Path path, Configuration conf, boolean laxPrecision)
        throws IOException {
    FileSystem fs = FileSystem.get(path.toUri(), conf);
    FSDataOutputStream out = fs.create(path);
    try {//from   w  w w.j a  va 2 s  .co  m
        VectorWritable vectorWritable = new VectorWritable(vector);
        vectorWritable.setWritesLaxPrecision(laxPrecision);
        vectorWritable.write(out);
    } finally {
        Closeables.close(out, false);
    }
}

From source file:com.xebialabs.overthere.util.OverthereUtils.java

public static void closeQuietly(Closeable c) {
    try {/*from  w ww .j a  va 2  s.c om*/
        Closeables.close(c, true);
    } catch (IOException e) {
        // Will not happen because of true...
    }
}

From source file:com.metamx.druid.index.v1.CompressedFloatsIndexedSupplier.java

@Override
public IndexedFloats get() {
    return new IndexedFloats() {
        int currIndex = -1;
        ResourceHolder<FloatBuffer> holder;
        FloatBuffer buffer;//from  w w  w.j  av a  2 s.c  o  m

        @Override
        public int size() {
            return totalSize;
        }

        @Override
        public float get(int index) {
            int bufferNum = index / sizePer;
            int bufferIndex = index % sizePer;

            if (bufferNum != currIndex) {
                loadBuffer(bufferNum);
            }

            return buffer.get(buffer.position() + bufferIndex);
        }

        @Override
        public void fill(int index, float[] toFill) {
            if (totalSize - index < toFill.length) {
                throw new IndexOutOfBoundsException(
                        String.format("Cannot fill array of size[%,d] at index[%,d].  Max size[%,d]",
                                toFill.length, index, totalSize));
            }

            int bufferNum = index / sizePer;
            int bufferIndex = index % sizePer;

            int leftToFill = toFill.length;
            while (leftToFill > 0) {
                if (bufferNum != currIndex) {
                    loadBuffer(bufferNum);
                }

                buffer.mark();
                buffer.position(buffer.position() + bufferIndex);
                final int numToGet = Math.min(buffer.remaining(), leftToFill);
                buffer.get(toFill, toFill.length - leftToFill, numToGet);
                buffer.reset();
                leftToFill -= numToGet;
                ++bufferNum;
                bufferIndex = 0;
            }
        }

        private void loadBuffer(int bufferNum) {
            Closeables.closeQuietly(holder);
            holder = baseFloatBuffers.get(bufferNum);
            buffer = holder.get();
            currIndex = bufferNum;
        }

        @Override
        public String toString() {
            return "CompressedFloatsIndexedSupplier_Anonymous{" + "currIndex=" + currIndex + ", sizePer="
                    + sizePer + ", numChunks=" + baseFloatBuffers.size() + ", totalSize=" + totalSize + '}';
        }

        @Override
        public void close() throws IOException {
            Closeables.close(holder, false);
        }
    };
}

From source file:org.apache.mahout.text.WholeFileRecordReader.java

@Override
public boolean nextKeyValue() throws IOException {
    if (!processed) {
        byte[] contents = new byte[(int) fileSplit.getLength()];
        Path file = fileSplit.getPath();
        FileSystem fs = file.getFileSystem(this.configuration);

        if (!fs.isFile(file)) {
            return false;
        }//  w  w w  .  j  ava  2s .  c  o  m

        FileStatus[] fileStatuses;
        if (pathFilter != null) {
            fileStatuses = fs.listStatus(file, pathFilter);
        } else {
            fileStatuses = fs.listStatus(file);
        }

        FSDataInputStream in = null;
        if (fileStatuses.length == 1) {
            try {
                in = fs.open(fileStatuses[0].getPath());
                IOUtils.readFully(in, contents, 0, contents.length);
                value.setCapacity(contents.length);
                value.set(contents, 0, contents.length);
            } finally {
                Closeables.close(in, false);
            }
            processed = true;
            return true;
        }
    }
    return false;
}

From source file:com.android.build.gradle.ndk.internal.NdkHandler.java

/**
 * Determine the location of the NDK directory.
 *
 * The NDK directory can be set in the local.properties file or using the ANDROID_NDK_HOME
 * environment variable./*w  ww.jav  a 2 s. c  o  m*/
 */
private static File findNdkDirectory(Project project) {
    File rootDir = project.getRootDir();
    File localProperties = new File(rootDir, FN_LOCAL_PROPERTIES);

    if (localProperties.isFile()) {

        Properties properties = new Properties();
        InputStreamReader reader = null;
        try {
            //noinspection IOResourceOpenedButNotSafelyClosed
            FileInputStream fis = new FileInputStream(localProperties);
            reader = new InputStreamReader(fis, Charsets.UTF_8);
            properties.load(reader);
        } catch (FileNotFoundException ignored) {
            // ignore since we check up front and we don't want to fail on it anyway
            // in case there's an env var.
        } catch (IOException e) {
            throw new RuntimeException("Unable to read ${localProperties}", e);
        } finally {
            try {
                Closeables.close(reader, true /* swallowIOException */);
            } catch (IOException e) {
                // ignore.
            }
        }

        String ndkDirProp = properties.getProperty("ndk.dir");
        if (ndkDirProp != null) {
            return new File(ndkDirProp);
        }

    } else {
        String envVar = System.getenv("ANDROID_NDK_HOME");
        if (envVar != null) {
            return new File(envVar);
        }
    }
    return null;
}

From source file:org.apache.mahout.clustering.kmeans.RandomSeedGenerator.java

public static Path buildRandom(Configuration conf, Path input, Path output, int k, DistanceMeasure measure,
        Long seed) throws IOException {

    Preconditions.checkArgument(k > 0, "Must be: k > 0, but k = " + k);
    // delete the output directory
    FileSystem fs = FileSystem.get(output.toUri(), conf);
    HadoopUtil.delete(conf, output);/*  ww w  .ja  v a  2s  .c  o  m*/
    Path outFile = new Path(output, "part-randomSeed");
    boolean newFile = fs.createNewFile(outFile);
    if (newFile) {
        Path inputPathPattern;

        if (fs.getFileStatus(input).isDir()) {
            inputPathPattern = new Path(input, "*");
        } else {
            inputPathPattern = input;
        }

        FileStatus[] inputFiles = fs.globStatus(inputPathPattern, PathFilters.logsCRCFilter());
        SequenceFile.Writer writer = SequenceFile.createWriter(fs, conf, outFile, Text.class,
                ClusterWritable.class);

        Random random = (seed != null) ? RandomUtils.getRandom(seed) : RandomUtils.getRandom();

        List<Text> chosenTexts = Lists.newArrayListWithCapacity(k);
        List<ClusterWritable> chosenClusters = Lists.newArrayListWithCapacity(k);
        int nextClusterId = 0;

        int index = 0;
        for (FileStatus fileStatus : inputFiles) {
            if (fileStatus.isDir()) {
                continue;
            }
            for (Pair<Writable, VectorWritable> record : new SequenceFileIterable<Writable, VectorWritable>(
                    fileStatus.getPath(), true, conf)) {
                Writable key = record.getFirst();
                VectorWritable value = record.getSecond();
                Kluster newCluster = new Kluster(value.get(), nextClusterId++, measure);
                newCluster.observe(value.get(), 1);
                Text newText = new Text(key.toString());
                int currentSize = chosenTexts.size();
                if (currentSize < k) {
                    chosenTexts.add(newText);
                    ClusterWritable clusterWritable = new ClusterWritable();
                    clusterWritable.setValue(newCluster);
                    chosenClusters.add(clusterWritable);
                } else {
                    int j = random.nextInt(index);
                    if (j < k) {
                        chosenTexts.set(j, newText);
                        ClusterWritable clusterWritable = new ClusterWritable();
                        clusterWritable.setValue(newCluster);
                        chosenClusters.set(j, clusterWritable);
                    }
                }
                index++;
            }
        }

        try {
            for (int i = 0; i < chosenTexts.size(); i++) {
                writer.append(chosenTexts.get(i), chosenClusters.get(i));
            }
            log.info("Wrote {} Klusters to {}", k, outFile);
        } finally {
            Closeables.close(writer, false);
        }
    }

    return outFile;
}