Example usage for com.google.common.io CharStreams readLines

List of usage examples for com.google.common.io CharStreams readLines

Introduction

In this page you can find the example usage for com.google.common.io CharStreams readLines.

Prototype

public static List<String> readLines(Readable r) throws IOException 

Source Link

Document

Reads all of the lines from a Readable object.

Usage

From source file:com.dmdirc.addons.exec.ExecCommand.java

@Override
public void execute(@Nonnull final WindowModel origin, final CommandArguments args,
        final CommandContext context) {
    final String[] commandArray = CommandUtils.parseArguments(args.getArgumentsAsString());

    try {/*  www  . ja  v  a2  s. c  o m*/
        // This checks the command to execute has correct quotes
        // (if necessary). Without this /exec "command arg1 arg2 would error.
        if (commandArray.length == 0) {
            showError(origin, args.isSilent(), "Could not execute: Invalid file name provided");
        } else if (!new File(commandArray[0]).exists()) {
            showError(origin, args.isSilent(), "Could not execute: " + commandArray[0] + " does not exist.");
        } else {
            final Process p = Runtime.getRuntime().exec(commandArray);
            if (args.isSilent()) {
                StreamUtils.readStream(p.getInputStream());
                StreamUtils.readStream(p.getErrorStream());
            } else {
                final List<String> execOutput = CharStreams
                        .readLines(new InputStreamReader(p.getInputStream()));
                final List<String> errorOutput = CharStreams
                        .readLines(new InputStreamReader(p.getErrorStream()));
                for (String line : execOutput) {
                    showOutput(origin, args.isSilent(), line);
                }
                for (String line : errorOutput) {
                    showError(origin, args.isSilent(), line);
                }
            }
        }
    } catch (IOException ex) {
        LOG.info(LogUtils.USER_ERROR, "Unable to run application: {}", ex.getMessage(), ex);
    }
}

From source file:util.mybatis.comment.ClassCommentPlugin.java

/**
 * ?./* w w w  .  j  av  a2  s  . co  m*/
 *
 * @param introspectedTable the introspected table
 * @param topLevelClass the top level class
 */
public void classGenerated(IntrospectedTable introspectedTable, TopLevelClass topLevelClass) {

    Map<String, Object> local = Maps.newLinkedHashMap();
    local.put("todo", new ClassWrapper(introspectedTable));
    local.put("tags", "");

    String classComment = CodetemplatesLoader.getInstance().get("typecomment_context");
    classComment = VelocityUtils.evaluate(classComment, local);
    try {
        List<String> lines = CharStreams.readLines(CharStreams.newReaderSupplier(classComment));
        if (lines == null) {
            return;
        }
        boolean isfirst = true;
        for (String line : lines) {
            line = line.trim();
            if (Strings.isNullOrEmpty(line)) {
                continue;
            }
            if (!isfirst) {
                line = " " + line;
            }
            topLevelClass.addJavaDocLine(line);
            isfirst = false;
        }
    } catch (IOException e) {
        throw new RuntimeException(e);
    }
}

From source file:org.apache.mahout.classifier.sequencelearning.hmm.PosTagger.java

/**
 * Given an URL, this function fetches the data file, parses it, assigns POS
 * Tag/word IDs and fills the hiddenSequences/observedSequences lists with
 * data from those files. The data is expected to be in the following format
 * (one word per line): word pos-tag np-tag sentences are closed with the .
 * pos tag/* w  ww  .  ja  va  2  s .co  m*/
 *
 * @param url       Where the data file is stored
 * @param assignIDs Should IDs for unknown words/tags be assigned? (Needed for
 *                  training data, not needed for test data)
 * @throws IOException in case data file cannot be read.
 */
private static void readFromURL(String url, boolean assignIDs) throws IOException {
    // initialize the data structure
    hiddenSequences = Lists.newLinkedList();
    observedSequences = Lists.newLinkedList();
    readLines = 0;

    // now read line by line of the input file
    List<Integer> observedSequence = Lists.newLinkedList();
    List<Integer> hiddenSequence = Lists.newLinkedList();

    for (String line : CharStreams.readLines(Resources.newReaderSupplier(new URL(url), Charsets.UTF_8))) {
        if (line.isEmpty()) {
            // new sentence starts
            int[] observedSequenceArray = new int[observedSequence.size()];
            int[] hiddenSequenceArray = new int[hiddenSequence.size()];
            for (int i = 0; i < observedSequence.size(); ++i) {
                observedSequenceArray[i] = observedSequence.get(i);
                hiddenSequenceArray[i] = hiddenSequence.get(i);
            }
            // now register those arrays
            hiddenSequences.add(hiddenSequenceArray);
            observedSequences.add(observedSequenceArray);
            // and reset the linked lists
            observedSequence.clear();
            hiddenSequence.clear();
            continue;
        }
        readLines++;
        // we expect the format [word] [POS tag] [NP tag]
        String[] tags = SPACE.split(line);
        // when analyzing the training set, assign IDs
        if (assignIDs) {
            if (!wordIDs.containsKey(tags[0])) {
                wordIDs.put(tags[0], nextWordId++);
            }
            if (!tagIDs.containsKey(tags[1])) {
                tagIDs.put(tags[1], nextTagId++);
            }
        }
        // determine the IDs
        Integer wordID = wordIDs.get(tags[0]);
        Integer tagID = tagIDs.get(tags[1]);
        // now construct the current sequence
        if (wordID == null) {
            observedSequence.add(0);
        } else {
            observedSequence.add(wordID);
        }

        if (tagID == null) {
            hiddenSequence.add(0);
        } else {
            hiddenSequence.add(tagID);
        }
    }

    // if there is still something in the pipe, register it
    if (!observedSequence.isEmpty()) {
        int[] observedSequenceArray = new int[observedSequence.size()];
        int[] hiddenSequenceArray = new int[hiddenSequence.size()];
        for (int i = 0; i < observedSequence.size(); ++i) {
            observedSequenceArray[i] = observedSequence.get(i);
            hiddenSequenceArray[i] = hiddenSequence.get(i);
        }
        // now register those arrays
        hiddenSequences.add(hiddenSequenceArray);
        observedSequences.add(observedSequenceArray);
    }
}

From source file:com.google.api.codegen.util.LicenseHeaderUtil.java

private ImmutableList<String> getResourceLines(String resourceFileName) {
    try {//w  w w . j  a v a 2 s  . c o  m
        InputStream fileStream = ConfigProto.class.getResourceAsStream(resourceFileName);
        if (fileStream == null) {
            throw new FileNotFoundException(resourceFileName);
        }
        InputStreamReader fileReader = new InputStreamReader(fileStream, Charsets.UTF_8);
        return ImmutableList.copyOf(CharStreams.readLines(fileReader));
    } catch (IOException e) {
        diagCollector.addDiag(Diag.error(SimpleLocation.TOPLEVEL, "Exception: %s", e.getMessage()));
        throw new RuntimeException(e);
    }
}

From source file:nallar.tickthreading.patcher.remapping.Deobfuscator.java

public void setup(File mapData) {
    try {/*w w w.  j ava2  s .  co  m*/
        mapData = mapData.getCanonicalFile();
        //noinspection IOResourceOpenedButNotSafelyClosed
        ZipFile mapZip = new ZipFile(mapData);
        ZipEntry classData = mapZip.getEntry("joined.srg");
        ZipInputSupplier zis = new ZipInputSupplier(mapZip, classData);
        InputSupplier<InputStreamReader> srgSupplier = CharStreams.newReaderSupplier(zis, Charsets.UTF_8);
        List<String> srgList = CharStreams.readLines(srgSupplier);
        rawMethodMaps = Maps.newHashMap();
        rawFieldMaps = Maps.newHashMap();
        Builder<String, String> builder = ImmutableBiMap.builder();
        Builder<String, String> mcpBuilder = ImmutableBiMap.builder();
        Splitter splitter = Splitter.on(CharMatcher.anyOf(": ")).omitEmptyStrings().trimResults();
        for (String line : srgList) {
            String[] parts = Iterables.toArray(splitter.split(line), String.class);
            String typ = parts[0];
            if ("CL".equals(typ)) {
                parseClass(builder, parts);
                parseMCPClass(mcpBuilder, parts);
            } else if ("MD".equals(typ)) {
                parseMethod(parts);
            } else if ("FD".equals(typ)) {
                parseField(parts);
            }
        }
        classNameBiMap = builder.build();
        // Special case some mappings for modloader mods
        mcpBuilder.put("BaseMod", "net/minecraft/src/BaseMod");
        mcpBuilder.put("ModLoader", "net/minecraft/src/ModLoader");
        mcpBuilder.put("EntityRendererProxy", "net/minecraft/src/EntityRendererProxy");
        mcpBuilder.put("MLProp", "net/minecraft/src/MLProp");
        mcpBuilder.put("TradeEntry", "net/minecraft/src/TradeEntry");
        mcpNameBiMap = mcpBuilder.build();
    } catch (IOException ioe) {
        Log.severe("An error occurred loading the deobfuscation map data", ioe);
    }
    methodNameMaps = Maps.newHashMapWithExpectedSize(rawMethodMaps.size());
    fieldNameMaps = Maps.newHashMapWithExpectedSize(rawFieldMaps.size());
}

From source file:com.facebook.buck.jvm.java.JarDumper.java

private Stream<String> dumpClassFile(InputStream stream) throws IOException {
    byte[] textifiedClass;
    try (ByteArrayOutputStream bos = new ByteArrayOutputStream(); PrintWriter pw = new PrintWriter(bos)) { // NOPMD required by API
        ClassReader reader = new ClassReader(stream);
        TraceClassVisitor traceVisitor = new TraceClassVisitor(null, new Textifier(), pw);
        reader.accept(traceVisitor, asmFlags);
        textifiedClass = bos.toByteArray();
    }//from   w  ww. ja  v  a 2  s.  co  m

    try (InputStreamReader streamReader = new InputStreamReader(new ByteArrayInputStream(textifiedClass))) {
        return CharStreams.readLines(streamReader).stream();
    }
}

From source file:com.mgmtp.jfunk.core.util.CsvDataProcessor.java

/**
 * Processes the specified CSV file. For every line but the header line (which is required), the
 * specified command is executed./*  www . j a  v  a 2s.  com*/
 * 
 * @param reader
 *            the reader for loading the CSV data
 * @param delimiter
 *            the column separator
 * @param quoteChar
 *            the quote character ('\0' for no quoting)
 * @param command
 *            the command (i. e. a Groovy closure if used in a Groovy script) to be executed for
 *            every processed line
 */
public void processFile(final Reader reader, final String delimiter, final char quoteChar,
        final Runnable command) {
    try {
        List<String> inputLines = CharStreams.readLines(reader);

        StrTokenizer st = StrTokenizer.getCSVInstance();
        st.setDelimiterString(delimiter);
        if (quoteChar != '\0') {
            st.setQuoteChar(quoteChar);
        } else {
            st.setQuoteMatcher(StrMatcher.noneMatcher());
        }

        // extract header
        String headerLine = inputLines.remove(0);
        List<Column> columns = initColumns(st, headerLine);
        for (String line : inputLines) {
            st.reset(line);
            String[] colArray = st.getTokenArray();
            int len = colArray.length;
            checkState(len == columns.size(),
                    "Mismatch between number of header columns and number of line columns.");

            DataSource dataSource = dataSourceProvider.get();
            Configuration config = configProvider.get();
            for (int i = 0; i < len; ++i) {
                String value = StringUtils.trimToEmpty(colArray[i]);

                String dataSetKey = columns.get(i).dataSetKey;
                String key = columns.get(i).key;
                if (dataSetKey != null) {
                    if ("<auto>".equals(value)) {
                        dataSource.resetFixedValue(dataSetKey, key);
                    } else {
                        log.debug("Setting data set entry for " + this + " to value=" + value);
                        dataSource.setFixedValue(dataSetKey, key, value);
                    }
                } else {
                    log.debug("Setting property for " + this + " to value=" + value);
                    config.put(key, value);
                }
            }

            command.run();
        }
    } catch (IOException ex) {
        throw new JFunkException("Error processing CSV data", ex);
    }
}

From source file:com.facebook.buck.jvm.java.JarDumper.java

private Stream<String> dumpTextFile(InputStream inputStream) throws IOException {
    try (InputStreamReader streamReader = new InputStreamReader(inputStream)) {
        return CharStreams.readLines(streamReader).stream();
    }/*w  w  w. ja  va 2s.c  om*/
}

From source file:com.google.gxp.compiler.cli.GxpcFlags.java

/**
 * Creates an instance of the compiler based on command-line arguments.
 *
 * @param fs underlying {@code FileSystem} that filenames in {@code args}
 * refer to//w w  w.  j ava  2s .c o m
 * @param defaultDir default directory for source and output dirs
 * @param args command-line arguments. See <code>HELP_*</code> variables
 * defined in {@link GxpcFlags} for accepted flags
 * @throws Flags.UsageError if there is an error parsing the command line
 * arguments
 */
GxpcFlags(FileSystem fs, FileRef defaultDir, String... args) throws CmdLineException, IOException {

    // If there is only one argument, and it starts with an '@', then treat it
    // as an options file, relative to the current working directory.
    if ((args.length == 1) && (args[0].startsWith("@"))) {
        FileRef optionsFile = defaultDir.join(args[0].substring(1));
        Reader in = optionsFile.openReader(Charsets.UTF_8);
        List<String> lines = CharStreams.readLines(in);
        in.close();
        List<String> parsedTokens = Lists.newArrayList();
        for (String line : lines) {
            for (String token : line.trim().split("\\s+")) {
                if (token.length() > 0) {
                    parsedTokens.add(token);
                }
            }
        }
        args = parsedTokens.toArray(new String[parsedTokens.size()]);
    }

    commandLine = new CommandLine(args);

    Set<FileRef> underlyingInputFiles = getFileRefs(fs, commandLine.trailingArgs);

    FileRef outputDir = (commandLine.FLAG_dir == null) ? defaultDir : fs.parseFilename(commandLine.FLAG_dir);

    List<FileRef> sourcePaths = (commandLine.FLAG_source == null) ? Collections.singletonList(defaultDir)
            : fs.parseFilenameList(commandLine.FLAG_source);

    SourcePathFileSystem sourcePathFs = new SourcePathFileSystem(fs, sourcePaths, underlyingInputFiles,
            outputDir);

    sourceFiles = ImmutableSet.copyOf(sourcePathFs.getSourceFileRefs());
    schemaFiles = getFileRefs(fs, commandLine.FLAG_schema);

    // Compute Output Languages
    Set<OutputLanguage> tmpOutputLanguages = EnumSet.noneOf(OutputLanguage.class);
    for (String outputLanguage : commandLine.FLAG_output_language) {
        tmpOutputLanguages.add(OutputLanguage.valueOf(outputLanguage.toUpperCase()));
    }
    outputLanguages = ImmutableSet.copyOf(tmpOutputLanguages);

    allowedOutputFiles = getFileRefs(sourcePathFs, commandLine.FLAG_output);

    alertPolicy = computeAlertPolicy(commandLine.FLAG_warn, commandLine.FLAG_error);

    // Compute Dependency File
    dependencyFile = (commandLine.FLAG_depend == null) ? null : fs.parseFilename(commandLine.FLAG_depend);

    // Compute Properties File
    propertiesFile = (commandLine.FLAG_output_properties && commandLine.FLAG_message_source != null)
            ? outputDir.join("/" + commandLine.FLAG_message_source.replace(".", "/") + "_en.properties")
            : null;

    isVerboseEnabled = commandLine.FLAG_verbose;
    isDebugEnabled = commandLine.FLAG_g;

    // Compute Dot Phases
    dotPhases = computeDotPhases(commandLine.getParser(), commandLine.FLAG_dot);

    // Compute SourceEntityResolver
    // use the sourcePathFs so that gxp:///foo/bar is resolved in a way that
    // includes both source files and genfiles
    sourceEntityResolver = new FileSystemEntityResolver(sourcePathFs);

    // Compute CodeGeneratorFactory (Always do this last)
    codeGeneratorFactory = new DefaultCodeGeneratorFactory();
    codeGeneratorFactory.setRuntimeMessageSource(commandLine.FLAG_message_source);
    codeGeneratorFactory.setDynamicModeEnabled(commandLine.FLAG_dynamic);
    codeGeneratorFactory.setSourceFiles(getSourceFiles());
    codeGeneratorFactory.setSchemaFiles(getSchemaFiles());
    codeGeneratorFactory.setSourcePaths(sourcePaths);
    codeGeneratorFactory.setAlertPolicy(getAlertPolicy());
}

From source file:org.geogit.cli.test.functional.GlobalState.java

public static List<String> runAndParseCommand(boolean failFast, String... command) throws Exception {
    runCommand(failFast, command);/*from ww  w  .ja  v  a 2s  . c o m*/
    InputSupplier<InputStreamReader> readerSupplier = CharStreams.newReaderSupplier(
            ByteStreams.newInputStreamSupplier(stdOut.toByteArray()), Charset.forName("UTF-8"));
    List<String> lines = CharStreams.readLines(readerSupplier);
    return lines;
}