List of usage examples for com.google.common.io LineReader LineReader
public LineReader(Readable readable)
From source file:org.apache.maven.plugins.shade.resource.ServicesResourceTransformer.java
public void processResource(String resource, InputStream is, final List<Relocator> relocators) throws IOException { ServiceStream out = serviceEntries.get(resource); if (out == null) { out = new ServiceStream(); serviceEntries.put(resource, out); }//ww w. jav a 2 s .c om final ServiceStream fout = out; final String content = IOUtils.toString(is); StringReader reader = new StringReader(content); LineReader lineReader = new LineReader(reader); String line; while ((line = lineReader.readLine()) != null) { String relContent = line; for (Relocator relocator : relocators) { if (relocator.canRelocateClass(relContent)) { relContent = relocator.applyToSourceContent(relContent); } } fout.append(relContent + "\n"); } if (this.relocators == null) { this.relocators = relocators; } }
From source file:com.thinkbiganalytics.util.ColumnSpec.java
/** * Method for defining a column specification as a pipe-delimited format: column|data type|comment (optional)|pk|created|modified. Each row separated by a newline *//*from w ww .j a v a 2s .co m*/ public static ColumnSpec[] createFromString(String specString) { if (StringUtils.isEmpty(specString)) { return null; } List<ColumnSpec> specs = new Vector<>(); try { LineReader lineReader = new LineReader(new StringReader(specString)); String line; while ((line = lineReader.readLine()) != null) { String[] parts = line.split("\\|"); int len = parts.length; if (len > 0) { String columnName = ""; String comment = ""; String dataType = "string"; boolean pk = false; boolean modifiedDt = false; boolean createDt = false; String otherName = ""; switch (len) { default: case 7: otherName = parts[6]; case 6: modifiedDt = "1".equals(parts[5].trim()); case 5: createDt = "1".equals(parts[4].trim()); case 4: pk = "1".equals(parts[3].trim()); case 3: comment = parts[2]; case 2: dataType = parts[1]; case 1: columnName = parts[0]; } specs.add(new ColumnSpec(columnName, dataType, comment, pk, createDt, modifiedDt, otherName)); } } return specs.toArray(new ColumnSpec[0]); } catch (IOException e) { throw new RuntimeException("Failed to parse column specs[" + specString + "]", e); } }
From source file:com.axelor.meta.loader.DataLoader.java
private boolean isConfig(File file, Pattern pattern) { try {//from w w w.j a v a 2 s .c om Reader reader = new FileReader(file); LineReader lines = new LineReader(reader); String line = null; while ((line = lines.readLine()) != null) { if (pattern.matcher(line).find()) { return true; } } reader.close(); } catch (IOException e) { log.error(e.getMessage(), e); } return false; }
From source file:org.apache.gobblin.metastore.nameParser.GuidDatasetUrnStateStoreNameParser.java
public GuidDatasetUrnStateStoreNameParser(FileSystem fs, Path jobStatestoreRootDir) throws IOException { this.fs = fs; this.sanitizedNameToDatasetURNMap = Maps.synchronizedBiMap(HashBiMap.<String, String>create()); this.versionIdentifier = new Path(jobStatestoreRootDir, StateStoreNameVersion.V1.getDatasetUrnNameMapFile()); if (this.fs.exists(versionIdentifier)) { this.version = StateStoreNameVersion.V1; try (InputStream in = this.fs.open(versionIdentifier)) { LineReader lineReader = new LineReader(new InputStreamReader(in, Charsets.UTF_8)); String shortenName = lineReader.readLine(); while (shortenName != null) { String datasetUrn = lineReader.readLine(); this.sanitizedNameToDatasetURNMap.put(shortenName, datasetUrn); shortenName = lineReader.readLine(); }//from w w w. j ava 2 s. c o m } } else { this.version = StateStoreNameVersion.V0; } }
From source file:utils.PasswordManager.java
public static Optional<String> getMasterPassword(String masterPwdLoc) { Closer closer = Closer.create();//from www. ja v a 2 s. c o m try { File file = new File(masterPwdLoc); if (!file.exists() || file.isDirectory()) { LOG.warn(masterPwdLoc + " does not exist or is not a file. Cannot decrypt any encrypted password."); return Optional.absent(); } InputStream in = new FileInputStream(file); return Optional.of(new LineReader(new InputStreamReader(in, Charsets.UTF_8)).readLine()); } catch (IOException e) { throw new RuntimeException("Failed to obtain master password from " + masterPwdLoc, e); } finally { try { closer.close(); } catch (IOException e) { throw new RuntimeException("Failed to close inputstream for " + masterPwdLoc, e); } } }
From source file:controllers.GrokPatternsController.java
@BodyParser.Of(BodyParser.MultipartFormData.class) public Result upload() { String path = getRefererPath(); Http.MultipartFormData body = request().body().asMultipartFormData(); Http.MultipartFormData.FilePart patterns = body.getFile("patterns"); final String[] replaceParam = body.asFormUrlEncoded().get("replace"); boolean replace = replaceParam != null; if (patterns != null) { Collection<GrokPatternSummary> grokPatterns = Lists.newArrayList(); try {/* w w w . j ava 2 s . c om*/ File file = patterns.getFile(); String patternsContent = Files.toString(file, StandardCharsets.UTF_8); final LineReader lineReader = new LineReader(new StringReader(patternsContent)); Pattern pattern = Pattern.compile("^([A-z0-9_]+)\\s+(.*)$"); String line; while ((line = lineReader.readLine()) != null) { Matcher m = pattern.matcher(line); if (m.matches()) { final GrokPatternSummary grokPattern = new GrokPatternSummary(); grokPattern.name = m.group(1); grokPattern.pattern = m.group(2); grokPatterns.add(grokPattern); } } } catch (IOException e) { Logger.error("Could not parse uploaded file: " + e); flash("error", "The uploaded pattern file could not be parsed: does it have the right format?"); return redirect(path); } try { extractorService.bulkLoadGrokPatterns(grokPatterns, replace); flash("success", "Grok patterns added successfully."); } catch (APIException | IOException e) { flash("error", "There was an error adding the grok patterns, please check the file format."); } } else { flash("error", "You didn't upload any pattern file"); } return redirect(path); }
From source file:com.github.nzyuzin.candiderl.game.map.MapFactory.java
private char[][] readMapFile() throws IOException { File mapFile = new File(GameConstants.MAP_FILENAME); LineReader lineReader = new LineReader(new FileReader(mapFile)); ArrayList<char[]> result = new ArrayList<>(); String line;/*w w w .j a va 2s . co m*/ while ((line = lineReader.readLine()) != null) { if (line.length() == 0) { break; } result.add(line.toCharArray()); } return result.toArray(new char[result.size()][]); }
From source file:com.palantir.stash.stashbot.outputhandler.CommandOutputHandlerFactory.java
/** * Returns an output handler which provides an ImmutableList of branches in the form "refs/heads/foo" when the * command prints out a list of the form "XXfoo\n" where X doesn't matter. * //from ww w. ja v a 2 s.co m * @return ImmutableList<String> branchNames */ public CommandOutputHandler<Object> getBranchContainsOutputHandler() { return new CommandOutputHandler<Object>() { private ArrayList<String> branches; private LineReader lr; private boolean processed = false; @Override public void complete() throws ProcessException { } @Override public void setWatchdog(Watchdog watchdog) { } @Override public Object getOutput() { if (processed == false) { throw new IllegalStateException("getOutput() called before process()"); } return ImmutableList.copyOf(branches); } @Override public void process(InputStream output) throws ProcessException { processed = true; if (branches == null) { branches = new ArrayList<String>(); lr = new LineReader(new InputStreamReader(output)); } String branch = ""; while (branch != null) { try { branch = lr.readLine(); } catch (IOException e) { // dear god, why is this happening? throw new RuntimeException(e); } // format is " somebranch\n* branchIamOn\n someOtherBranch" if (branch != null) { branches.add("refs/heads/" + branch.substring(2)); } } } }; }
From source file:uk.ac.ebi.mdk.service.query.PubChemCompoundAdapter.java
/** * @inheritDoc//from w ww.j a va 2 s .c om */ @Override public Collection<PubChemCompoundIdentifier> searchName(String name, boolean approximate) { List<PubChemCompoundIdentifier> cids = new ArrayList<PubChemCompoundIdentifier>(5); InputStream in = null; try { String address = new URI("http", "pubchem.ncbi.nlm.nih.gov", "/rest/pug/compound/name/" + name + "/cids/TXT/", null).toASCIIString(); in = new URL(address).openStream(); LineReader lines = new LineReader(new InputStreamReader(in)); String line; while ((line = lines.readLine()) != null) { if (numeric.matcher(line).matches()) { cids.add(new PubChemCompoundIdentifier(line)); } } } catch (IOException e) { LOGGER.error("could not complete search for " + name, e); } catch (URISyntaxException e) { System.out.println(e); LOGGER.error("could not encode URI for " + name, e); } finally { try { if (in != null) { in.close(); } } catch (IOException e) { // ignore } } return cids; }
From source file:co.cask.cdap.common.service.CommandPortService.java
/** * Starts accepting incoming request. This method would block until this service is stopped. * * @throws IOException If any I/O error occurs on the socket connection. *///from ww w. java 2s .co m private void serve() throws IOException { while (isRunning()) { try { Socket socket = serverSocket.accept(); BufferedWriter writer = new BufferedWriter( new OutputStreamWriter(socket.getOutputStream(), "UTF-8")); try { // Read the client command and dispatch String command = new LineReader(new InputStreamReader(socket.getInputStream(), "UTF-8")) .readLine(); CommandHandler handler = handlers.get(command); if (handler != null) { try { handler.handle(writer); } catch (Throwable t) { LOG.error(String.format("Exception thrown from CommandHandler for command %s", command), t); } } } finally { writer.flush(); socket.close(); } } catch (Throwable th) { // NOTE: catch any exception to keep the main service running // Trigger by serverSocket.close() through the call from stop(). LOG.debug(th.getMessage(), th); } } }