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.cf.taste.impl.similarity.precompute.FileSimilarItemsWriter.java

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

From source file:com.msiiplab.recsys.rwr.GroupLensDataModel.java

private static File convertGLFile(File originalFile) throws IOException {
    // Now translate the file; remove commas, then convert "::" delimiter to
    // comma//from  w  w w . j a  v  a  2  s  .  c  om
    File resultFile = new File(new File(System.getProperty("java.io.tmpdir")), "ratings.txt");
    if (resultFile.exists()) {
        resultFile.delete();
    }
    Writer writer = null;
    try {
        writer = new OutputStreamWriter(new FileOutputStream(resultFile), Charsets.UTF_8);
        for (String line : new FileLineIterable(originalFile, false)) {
            int lastDelimiterStart = line.lastIndexOf(COLON_DELIMTER);
            if (lastDelimiterStart < 0) {
                writer.close();
                throw new IOException("Unexpected input format on line: " + line);
            }
            String subLine = line.substring(0, lastDelimiterStart);
            String convertedLine = COLON_DELIMITER_PATTERN.matcher(subLine).replaceAll(",");
            writer.write(convertedLine);
            writer.write('\n');
        }
    } catch (IOException ioe) {
        resultFile.delete();
        throw ioe;
    } finally {
        Closeables.close(writer, false);
    }
    return resultFile;
}

From source file:org.apache.druid.java.util.common.io.smoosh.SmooshedFileMapper.java

public static SmooshedFileMapper load(File baseDir) throws IOException {
    File metaFile = FileSmoosher.metaFile(baseDir);

    BufferedReader in = null;/*  w  w w  . j  a va2 s  .com*/
    try {
        in = new BufferedReader(new InputStreamReader(new FileInputStream(metaFile), StandardCharsets.UTF_8));

        String line = in.readLine();
        if (line == null) {
            throw new ISE("First line should be version,maxChunkSize,numChunks, got null.");
        }

        String[] splits = line.split(",");
        if (!"v1".equals(splits[0])) {
            throw new ISE("Unknown version[%s], v1 is all I know.", splits[0]);
        }
        if (splits.length != 3) {
            throw new ISE("Wrong number of splits[%d] in line[%s]", splits.length, line);
        }
        final Integer numFiles = Integer.valueOf(splits[2]);
        List<File> outFiles = Lists.newArrayListWithExpectedSize(numFiles);

        for (int i = 0; i < numFiles; ++i) {
            outFiles.add(FileSmoosher.makeChunkFile(baseDir, i));
        }

        Map<String, Metadata> internalFiles = Maps.newTreeMap();
        while ((line = in.readLine()) != null) {
            splits = line.split(",");

            if (splits.length != 4) {
                throw new ISE("Wrong number of splits[%d] in line[%s]", splits.length, line);
            }
            internalFiles.put(splits[0], new Metadata(Integer.parseInt(splits[1]), Integer.parseInt(splits[2]),
                    Integer.parseInt(splits[3])));
        }

        return new SmooshedFileMapper(outFiles, internalFiles);
    } finally {
        Closeables.close(in, false);
    }
}

From source file:de.tuberlin.dima.cuttlefish.preprocessing.parsing.NewsItemExtractor.java

public void extract(NewsItemProcessor processor) throws Exception {
    for (File directory : directories) {
        for (File zipFile : directory.listFiles(ZIP_FILES)) {
            log.info("Parsing news in {}", zipFile.getName());
            ZipFile newsArchive = new ZipFile(zipFile);
            Enumeration<? extends ZipEntry> entries = newsArchive.entries();
            while (entries.hasMoreElements()) {
                InputStream newsItemAsXml = null;
                try {
                    newsItemAsXml = newsArchive.getInputStream(entries.nextElement());
                    NewsItem newsItem = NewsItemXmlParser.toNewsItem(newsItemAsXml);

                    processor.process(newsItem);

                } finally {
                    Closeables.close(newsItemAsXml, true);
                }/*from www .  ja va  2s . c om*/
            }
        }
    }
}

From source file:org.robotbrains.data.cloud.timeseries.server.logging.Log4jLoggingProvider.java

/**
 * Start the logging provider.//from  ww w  .j av a2  s.  co m
 */
public void startup() {
    InputStream configurationStream = null;
    try {
        ConfigurationSource source;
        if (configurationFilePath != null) {
            File configurationFile = new File(configurationFilePath);
            configurationStream = new FileInputStream(configurationFile);
            source = new ConfigurationSource(configurationStream, configurationFile);
        } else {
            configurationStream = getClass().getClassLoader().getResourceAsStream("log4j.xml");
            source = new ConfigurationSource(configurationStream);
        }
        Configurator.initialize(null, source);
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    } finally {
        try {
            Closeables.close(configurationStream, true);
        } catch (IOException e) {
            // Won't happen
        }
    }
}

From source file:org.apache.mahout.utils.vectors.arff.ARFFIterator.java

@Override
protected Vector computeNext() {
    String line;/*from   www .j  av  a  2s.  com*/
    try {
        while ((line = reader.readLine()) != null) {
            line = line.trim();
            if (!line.isEmpty() && !line.startsWith(ARFFModel.ARFF_COMMENT)) {
                break;
            }
        }
    } catch (IOException ioe) {
        throw new IllegalStateException(ioe);
    }
    if (line == null) {
        try {
            Closeables.close(reader, true);
        } catch (IOException e) {
            throw new IllegalStateException(e);
        }
        return endOfData();
    }
    Vector result;
    Matcher contents = DATA_PATTERN.matcher(line);
    if (contents.find()) {
        line = contents.group(1);
        String[] splits = splitCSV(line);
        result = new RandomAccessSparseVector(model.getLabelSize());
        for (String split : splits) {
            int idIndex = split.indexOf(' ');
            int idx = Integer.parseInt(split.substring(0, idIndex).trim());
            String data = split.substring(idIndex).trim();
            if (!"?".equals(data)) {
                result.setQuick(idx, model.getValue(data, idx));
            }
        }
    } else {
        result = new DenseVector(model.getLabelSize());
        String[] splits = splitCSV(line);
        for (int i = 0; i < splits.length; i++) {
            String split = splits[i];
            split = split.trim();
            if (WORDS_WITHOUT_SPARSE.matcher(split).matches() && !"?".equals(split)) {
                result.setQuick(i, model.getValue(split, i));
            }
        }
    }
    return result;
}

From source file:com.android.ide.common.resources.ValidatingResourceParser.java

/**
 * Parse the given input and return false if it contains errors, <b>or</b> if
 * the context is already tagged as needing a full aapt run.
 *
 * @param path the full OS path to the file being parsed
 * @param input the input stream of the XML to be parsed (will be closed by this method)
 * @return true if parsing succeeds and false if it fails
 * @throws IOException if reading the contents fails
 *///from w  ww.jav a  2s .  c o m
public boolean parse(final String path, InputStream input) throws IOException {
    // No need to validate framework files
    if (mIsFramework) {
        try {
            Closeables.close(input, true /* swallowIOException */);
        } catch (IOException e) {
            // cannot happen
        }
        return true;
    }
    if (mContext.needsFullAapt()) {
        try {
            Closeables.close(input, true /* swallowIOException */);
        } catch (IOException e) {
            // cannot happen
        }
        return false;
    }

    KXmlParser parser = new KXmlParser();
    try {
        parser.setFeature(XmlPullParser.FEATURE_PROCESS_NAMESPACES, true);

        if (input instanceof FileInputStream) {
            input = new BufferedInputStream(input);
        }
        parser.setInput(input, SdkConstants.UTF_8);

        return parse(path, parser);
    } catch (XmlPullParserException e) {
        String message = e.getMessage();

        // Strip off position description
        int index = message.indexOf("(position:"); //$NON-NLS-1$ (Hardcoded in KXml)
        if (index != -1) {
            message = message.substring(0, index);
        }

        String error = String.format("%1$s:%2$d: Error: %3$s", //$NON-NLS-1$
                path, parser.getLineNumber(), message);
        mContext.addError(error);
        return false;
    } catch (RuntimeException e) {
        // Some exceptions are thrown by the KXmlParser that are not XmlPullParserExceptions,
        // such as this one:
        //    java.lang.RuntimeException: Undefined Prefix: w in org.kxml2.io.KXmlParser@...
        //        at org.kxml2.io.KXmlParser.adjustNsp(Unknown Source)
        //        at org.kxml2.io.KXmlParser.parseStartTag(Unknown Source)
        String message = e.getMessage();
        String error = String.format("%1$s:%2$d: Error: %3$s", //$NON-NLS-1$
                path, parser.getLineNumber(), message);
        mContext.addError(error);
        return false;
    } finally {
        try {
            Closeables.close(input, true /* swallowIOException */);
        } catch (IOException e) {
            // cannot happen
        }
    }
}

From source file:org.n52.sos.coding.json.JSONUtils.java

public static String print(final JsonNode node) {
    final StringWriter writer = new StringWriter();
    try {//from ww w  . jav  a  2s.  c  o m
        print(writer, node);
        writer.flush();
    } catch (IOException e) {
        // cannot happen
    } finally {
        try {
            Closeables.close(writer, true);
        } catch (IOException ioe) {
            LOGGER.error("Error while colsing closeable!", ioe);
        }
    }
    return writer.toString();
}

From source file:org.apache.mahout.utils.regex.AnalyzerTransformer.java

@Override
public String transformMatch(String match) {
    StringBuilder result = new StringBuilder();
    TokenStream ts = null;//  ww w .  jav  a  2 s.  c o m
    try {
        ts = analyzer.tokenStream(fieldName, new StringReader(match));
        ts.addAttribute(CharTermAttribute.class);
        ts.reset();
        TokenStreamIterator iter = new TokenStreamIterator(ts);
        while (iter.hasNext()) {
            result.append(iter.next()).append(' ');
        }
        ts.end();
    } catch (IOException e) {
        throw new IllegalStateException(e);
    } finally {
        try {
            Closeables.close(ts, true);
        } catch (IOException e) {
            log.error(e.getMessage(), e);
        }
    }
    return result.toString();
}

From source file:org.apache.mahout.cf.taste.impl.recommender.svd.FilePersistenceStrategy.java

@Override
public Factorization load() throws IOException {
    if (!file.exists()) {
        log.info("{} does not yet exist, no factorization found", file.getAbsolutePath());
        return null;
    }//from w ww .j  a v a2s  . c o m
    DataInputStream in = null;
    try {
        log.info("Reading factorization from {}...", file.getAbsolutePath());
        in = new DataInputStream(new BufferedInputStream(new FileInputStream(file)));
        return readBinary(in);
    } finally {
        Closeables.close(in, true);
    }
}