List of usage examples for com.google.common.io LineReader LineReader
public LineReader(Readable readable)
From source file:org.openqa.selenium.firefox.Preferences.java
private void readPreferences(Reader reader) throws IOException { LineReader allLines = new LineReader(reader); String line = allLines.readLine(); while (line != null) { Matcher matcher = PREFERENCE_PATTERN.matcher(line); if (matcher.matches()) { allPrefs.put(matcher.group(1), preferenceAsValue(matcher.group(2))); }//from ww w .j a v a2 s .c o m line = allLines.readLine(); } }
From source file:org.apache.gobblin.password.PasswordManager.java
private List<TextEncryptor> getEncryptors(CachedInstanceKey cacheKey) { List<TextEncryptor> encryptors = new ArrayList<>(); int numOfEncryptionKeys = cacheKey.numOfEncryptionKeys; String suffix = ""; int i = 1;/*from ww w.j av a2s . c o m*/ if (cacheKey.masterPasswordFile == null || numOfEncryptionKeys < 1) { return encryptors; } Exception exception = null; do { Path currentMasterPasswordFile = new Path(cacheKey.masterPasswordFile + suffix); try (Closer closer = Closer.create()) { if (!fs.exists(currentMasterPasswordFile) || fs.getFileStatus(currentMasterPasswordFile).isDirectory()) { continue; } InputStream in = closer.register(fs.open(currentMasterPasswordFile)); String masterPassword = new LineReader(new InputStreamReader(in, Charsets.UTF_8)).readLine(); TextEncryptor encryptor = useStrongEncryptor ? new StrongTextEncryptor() : new BasicTextEncryptor(); // setPassword() needs to be called via reflection since the TextEncryptor interface doesn't have this method. encryptor.getClass().getMethod("setPassword", String.class).invoke(encryptor, masterPassword); encryptors.add(encryptor); suffix = "." + String.valueOf(i); } catch (FileNotFoundException fnf) { // It is ok for password files not being present LOG.warn("Master password file " + currentMasterPasswordFile + " not found."); } catch (IOException ioe) { exception = ioe; LOG.warn("Master password could not be read from file " + currentMasterPasswordFile); } catch (Exception e) { LOG.warn("Encryptor could not be instantiated."); } } while (i++ < numOfEncryptionKeys); // Throw exception if could not read any existing password file if (encryptors.size() < 1 && exception != null) { throw new RuntimeException("Master Password could not be read from any master password file.", exception); } return encryptors; }
From source file:org.icgc.dcc.submission.validation.platform.BaseSubmissionPlatformStrategy.java
@SneakyThrows public Fields getFileHeader(String fileName) { @Cleanup//from www . ja va 2 s . c o m InputStreamReader isr = new InputStreamReader(fileSystem.open(getFile(fileName)), UTF_8); return fields(FIELD_SPLITTER.split(new LineReader(isr).readLine())); }
From source file:org.eclipse.ocl.examples.build.utilities.BackportToXtext_2_3_1.java
private void convertFile(File file) { try {//w w w . j av a 2 s . c om Reader reader = new FileReader(file); LineReader lineReader = new LineReader(reader); StringBuilder s = new StringBuilder(); try { for (String line; (line = lineReader.readLine()) != null;) { s.append(line); s.append("\n"); } } catch (IOException e) { LOG.error("Failed to read '" + file + "'", e); return; } try { reader.close(); } catch (IOException e) { LOG.error("Failed to close '" + file + "'", e); return; } String sFirst = s.toString(); String s1 = sFirst.replaceAll( ResourceDescriptionsProvider.class.getName().replaceAll("\\.", "\\.") + "\\.PERSISTED_DESCRIPTIONS", "\"" + ResourceDescriptionsProvider.PERSISTED_DESCRIPTIONS + "\""); String s2 = s1.replaceAll( org.eclipse.xtext.resource.impl.BinaryGrammarResourceFactoryImpl.class.getName() .replaceAll("\\.", "\\."), org.eclipse.ocl.xtext.base.services.BinaryGrammarResourceFactoryImpl.class.getName()); // // Bug 466354 // String s3 = s2.replaceAll("import org\\.eclipse\\.xtext\\.ISetupExtension;", ""); String s4 = s3.replaceAll("ISetup, ISetupExtension \\{", "ISetup \\{"); String s5 = s4.replaceAll("@since 2\\.9\\s*\\*/\\s*@Override", "\\*/"); String sLast = s5; if (!sLast.equals(sFirst)) { try { Writer writer = new FileWriter(file); try { writer.write(sLast); writer.flush(); } catch (IOException e) { LOG.error("Failed to write '" + file + "'", e); return; } finally { writer.close(); } } catch (IOException e) { LOG.error("Failed to re-open '" + file + "'", e); return; } LOG.info("Backported " + file); } } catch (FileNotFoundException e) { LOG.error("Failed to open '" + file + "'", e); return; } }
From source file:google.registry.tools.CreateLrpTokensCommand.java
@Override public void run() throws Exception { checkArgument((assignee == null) == (assigneesFile != null), "Exactly one of either assignee or filename must be specified."); checkArgument((assigneesFile == null) || (metadata == null), "Metadata cannot be specified along with a filename."); checkArgument((assignee == null) || (metadataColumns == null), "Metadata columns cannot be specified along with an assignee."); final Set<String> validTlds = ImmutableSet.copyOf(Splitter.on(',').split(tlds)); for (String tld : validTlds) { assertTldExists(tld);//from www . ja va 2 s.co m } LineReader reader = new LineReader((assigneesFile != null) ? Files.newReader(assigneesFile.toFile(), UTF_8) : new StringReader(assignee)); String line = null; do { ImmutableSet.Builder<LrpTokenEntity> tokensToSaveBuilder = new ImmutableSet.Builder<>(); for (String token : generateTokens(BATCH_SIZE)) { line = reader.readLine(); if (!isNullOrEmpty(line)) { ImmutableList<String> values = ImmutableList .copyOf(Splitter.onPattern(COMMA_EXCEPT_WHEN_QUOTED_REGEX) // Results should not be surrounded in double quotes. .trimResults(CharMatcher.is('\"')).split(line)); LrpTokenEntity.Builder tokenBuilder = new LrpTokenEntity.Builder().setAssignee(values.get(0)) .setToken(token).setValidTlds(validTlds); if (metadata != null) { tokenBuilder.setMetadata(metadata); } else if (metadataColumns != null) { ImmutableMap.Builder<String, String> metadataBuilder = ImmutableMap.builder(); for (ImmutableMap.Entry<String, Integer> entry : metadataColumns.entrySet()) { checkArgument(values.size() > entry.getValue(), "Entry for %s does not have a value for %s (index %s)", values.get(0), entry.getKey(), entry.getValue()); metadataBuilder.put(entry.getKey(), values.get(entry.getValue())); } tokenBuilder.setMetadata(metadataBuilder.build()); } tokensToSaveBuilder.add(tokenBuilder.build()); } } final ImmutableSet<LrpTokenEntity> tokensToSave = tokensToSaveBuilder.build(); // Wrap in a retrier to deal with transient 404 errors (thrown as RemoteApiExceptions). retrier.callWithRetry(new Callable<Void>() { @Override public Void call() throws Exception { saveTokens(tokensToSave); return null; } }, RemoteApiException.class); } while (line != null); }
From source file:com.google.devtools.j2objc.gen.SourceBuilder.java
/** * Fix line indention, based on brace count. *///from ww w. jav a 2s . c o m public String reindent(String code) { try { // Remove indention from each line. StringBuffer sb = new StringBuffer(); LineReader lr = new LineReader(new StringReader(code)); String line = lr.readLine(); while (line != null) { sb.append(line.trim()); line = lr.readLine(); if (line != null) { sb.append('\n'); } } String strippedCode = sb.toString(); // Now indent it again. int indent = indention * DEFAULT_INDENTION; sb.setLength(0); // reset buffer lr = new LineReader(new StringReader(strippedCode)); line = lr.readLine(); while (line != null) { if (line.startsWith("}")) { indent -= DEFAULT_INDENTION; } if (!line.startsWith("#line")) { sb.append(pad(indent)); } sb.append(line); if (line.endsWith("{")) { indent += DEFAULT_INDENTION; } line = lr.readLine(); if (line != null) { sb.append('\n'); } } return sb.toString(); } catch (IOException e) { // Should never happen with string readers. throw new AssertionError(e); } }
From source file:uk.ac.ebi.mdk.service.query.PubChemCompoundAdapter.java
/** * @inheritDoc/*from w w w .ja v a2s . co m*/ */ @Override public Collection<String> getNames(PubChemCompoundIdentifier identifier) { List<String> synonyms = new ArrayList<String>(); String address = prefix + identifier.getAccession() + "/synonyms/TXT/"; InputStream in = null; try { in = new URL(address).openStream(); LineReader reader = new LineReader(new InputStreamReader(in)); String line = ""; while ((line = reader.readLine()) != null) { synonyms.add(line); } } catch (IOException e) { LOGGER.error("could not open stream for " + identifier, e); } finally { try { if (in != null) in.close(); } catch (IOException e) { // ignore } } return synonyms; }
From source file:org.opencastproject.silencedetection.ffmpeg.FFmpegSilenceDetector.java
/** * Create nonsilent sequences detection pipeline. * Parse audio stream and store all positions, where the volume level fall under the threshold. * * @param properties/*from w ww .ja v a 2s . c om*/ * @param track source track */ public FFmpegSilenceDetector(Properties properties, Track track, Workspace workspace) throws SilenceDetectionFailedException, MediaPackageException, IOException { long minSilenceLength = Long.parseLong( properties.getProperty(SilenceDetectionProperties.SILENCE_MIN_LENGTH, DEFAULT_SILENCE_MIN_LENGTH)); long minVoiceLength = Long.parseLong( properties.getProperty(SilenceDetectionProperties.VOICE_MIN_LENGTH, DEFAULT_VOICE_MIN_LENGTH)); long preSilenceLength = Long.parseLong( properties.getProperty(SilenceDetectionProperties.SILENCE_PRE_LENGTH, DEFAULT_SILENCE_PRE_LENGTH)); String thresholdDB = properties.getProperty(SilenceDetectionProperties.SILENCE_THRESHOLD_DB, DEFAULT_THRESHOLD_DB); String binary = properties.getProperty(FFMPEG_BINARY_CONFIG, FFMPEG_BINARY_DEFAULT); trackId = track.getIdentifier(); /* Make sure the element can be analyzed using this analysis implementation */ if (!track.hasAudio()) { logger.warn("Track {} has no audio stream to run a silece detection on", trackId); throw new SilenceDetectionFailedException("Element has no audio stream"); } /* Make sure we are not allowed to move the beginning of a segment into the last segment */ if (preSilenceLength > minSilenceLength) { logger.error("Pre silence length ({}) is configured to be greater than minimun silence length ({})", preSilenceLength, minSilenceLength); throw new SilenceDetectionFailedException("preSilenceLength > minSilenceLength"); } try { File mediaFile = workspace.get(track.getURI()); filePath = mediaFile.getAbsolutePath(); } catch (NotFoundException e) { throw new SilenceDetectionFailedException("Error finding the media file in workspace", e); } catch (IOException e) { throw new SilenceDetectionFailedException("Error reading media file in workspace", e); } if (track.getDuration() == null) { throw new MediaPackageException("Track " + trackId + " does not have a duration"); } logger.info("Track {} loaded, duration is {} s", filePath, track.getDuration() / 1000); logger.info("Starting silence detection of {}", filePath); String mediaPath = filePath.replaceAll(" ", "\\ "); DecimalFormat decimalFmt = new DecimalFormat("0.000", new DecimalFormatSymbols(Locale.US)); String minSilenceLengthInSeconds = decimalFmt.format((double) minSilenceLength / 1000.0); String filter = "silencedetect=noise=" + thresholdDB + ":duration=" + minSilenceLengthInSeconds; String[] command = new String[] { binary, "-nostats", "-i", mediaPath, "-filter:a", filter, "-f", "null", "-" }; String commandline = StringUtils.join(command, " "); logger.info("Running {}", commandline); ProcessBuilder pbuilder = new ProcessBuilder(command); List<String> segmentsStrings = new LinkedList<String>(); Process process = pbuilder.start(); BufferedReader reader = new BufferedReader(new InputStreamReader(process.getErrorStream())); try { LineReader lr = new LineReader(reader); String line = lr.readLine(); while (null != line) { /* We want only lines from the silence detection filter */ logger.debug("FFmpeg output: {}", line); if (line.startsWith("[silencedetect ")) { segmentsStrings.add(line); } line = lr.readLine(); } } catch (IOException e) { logger.error("Error executing ffmpeg: {}", e.getMessage()); } finally { reader.close(); } /** * Example output: * [silencedetect @ 0x2968e40] silence_start: 466.486 * [silencedetect @ 0x2968e40] silence_end: 469.322 | silence_duration: 2.83592 */ LinkedList<MediaSegment> segmentsTmp = new LinkedList<MediaSegment>(); if (segmentsStrings.size() == 0) { /* No silence found -> Add one segment for the whole track */ logger.info("No silence found. Adding one large segment."); segmentsTmp.add(new MediaSegment(0, track.getDuration())); } else { long lastSilenceEnd = 0; long lastSilenceStart = 0; Pattern patternStart = Pattern.compile("silence_start\\:\\ \\d+\\.\\d+"); Pattern patternEnd = Pattern.compile("silence_end\\:\\ \\d+\\.\\d+"); for (String seginfo : segmentsStrings) { /* Match silence ends */ Matcher matcher = patternEnd.matcher(seginfo); String time = ""; while (matcher.find()) { time = matcher.group().substring(13); } if (!"".equals(time)) { long silenceEnd = (long) (Double.parseDouble(time) * 1000); if (silenceEnd > lastSilenceEnd) { logger.debug("Found silence end at {}", silenceEnd); lastSilenceEnd = silenceEnd; } continue; } /* Match silence start -> End of segments */ matcher = patternStart.matcher(seginfo); time = ""; while (matcher.find()) { time = matcher.group().substring(15); } if (!"".equals(time)) { lastSilenceStart = (long) (Double.parseDouble(time) * 1000); logger.debug("Found silence start at {}", lastSilenceStart); if (lastSilenceStart - lastSilenceEnd > minVoiceLength) { /* Found a valid segment */ long segmentStart = java.lang.Math.max(0, lastSilenceEnd - preSilenceLength); logger.info("Adding segment from {} to {}", segmentStart, lastSilenceStart); segmentsTmp.add(new MediaSegment(segmentStart, lastSilenceStart)); } } } /* Add last segment if it is no silence and the segment is long enough */ if (lastSilenceStart < lastSilenceEnd && track.getDuration() - lastSilenceEnd > minVoiceLength) { long segmentStart = java.lang.Math.max(0, lastSilenceEnd - preSilenceLength); logger.info("Adding final segment from {} to {}", segmentStart, track.getDuration()); segmentsTmp.add(new MediaSegment(segmentStart, track.getDuration())); } } logger.info("Segmentation of track {} yielded {} segments", trackId, segmentsTmp.size()); segments = segmentsTmp; }
From source file:com.ibm.watson.retrieveandrank.app.rest.RetrieveAndRankProxyResource.java
/** * Gets a list of queries from the resource file on disk. The queries are provided by the Cranfield dataset.. This * API randomly selects n queries (by default 8) and returns those to the client. * * @param numQueries//from www . j a v a 2 s . c o m * the number of queries the client wishes to retrieve, by default 8 * @return a list of n queries. The returned query contains a query string and and query ID for ground truth lookup. */ @Path("/sampleQueries") @GET @Produces(MediaType.APPLICATION_JSON) public Response getSampleQueries(@QueryParam("numQueries") @DefaultValue("-1") int numQueries) { ServerErrorPayload error = null; final InputStream samples = RetrieveAndRankProxyResource.class.getResourceAsStream("/SampleQueries.json"); if (samples != null) { try (InputStreamReader streamReader = new InputStreamReader(samples)) { final LineReader reader = new LineReader(streamReader); String line = reader.readLine(); String json = null; while (line != null) { if (json != null) { json += "\n" + line; } else { json = line; } line = reader.readLine(); } final JsonObject contents = new JsonParser().parse(json).getAsJsonObject(); final JsonArray queries = contents.get("queries").getAsJsonArray(); final int max = queries.size() - 1; if (numQueries == -1) { numQueries = max; } else { numQueries = Math.min(numQueries, max); } final Random randomizer = new Random(); final SampleQueriesPayload payload = new SampleQueriesPayload(); for (int i = 0; i < numQueries; i++) { final int index = randomizer.nextInt(max - 1); final JsonObject query = queries.get(index).getAsJsonObject(); final SampleQueryPayload sqp = new SampleQueryPayload(); sqp.setId(i); sqp.setQueryId(query.get("id").getAsInt()); sqp.setQuery(query.get("query").getAsString()); payload.addQuery(sqp); } return Response.ok(payload).type(MediaType.APPLICATION_JSON).build(); } catch (final IOException e) { final String message = Messages.getString("RetrieveAndRankProxyResource.RNR_SAMPLE_QUERIES_ERROR");//$NON-NLS-1$ error = new ServerErrorPayload(message); UtilityFunctions.logger.error(message, e); } } return Response.serverError().entity(error).type(MediaType.APPLICATION_JSON).build(); }
From source file:gobblin.password.PasswordManager.java
public static Optional<String> getMasterPassword(FileSystem fs, Path masterPasswordFile) { try (Closer closer = Closer.create()) { if (!fs.exists(masterPasswordFile) || fs.getFileStatus(masterPasswordFile).isDirectory()) { LOG.warn(masterPasswordFile//www . j a v a2 s .co m + " does not exist or is not a file. Cannot decrypt any encrypted password."); return Optional.absent(); } InputStream in = closer.register(fs.open(masterPasswordFile)); return Optional.of(new LineReader(new InputStreamReader(in, Charsets.UTF_8)).readLine()); } catch (IOException e) { throw new RuntimeException("Failed to obtain master password from " + masterPasswordFile, e); } }