List of usage examples for java.io BufferedReader ready
public boolean ready() throws IOException
From source file:org.caboclo.util.Credentials.java
/** * Check if there are credentials previously stored on disk, for a specified * cloud provider/*from w w w . ja v a2 s . c om*/ * * @param currentServer The cloud storage provider * @return Credentials for the specified cloud provider, that were * previously saved on file */ public String checkCredentialsFile(String currentServer) { StringBuilder path = new StringBuilder(); path.append(System.getProperty("user.home")).append(java.io.File.separator).append("backupcredentials"); File credentialsFile = new File(path.toString()); BufferedReader input = null; String token = ""; try { //Creates file only if it does not exist boolean createdNow = credentialsFile.createNewFile(); //Return with empty token, because file did not exist if (createdNow) { return token; } input = new BufferedReader(new FileReader(credentialsFile)); String line; while (input.ready()) { line = input.readLine(); if (line.startsWith(currentServer)) { int colon = line.indexOf(":"); String encodedCred = line.substring(colon + 1); token = decryptCredentials(new Base64().decode(encodedCred.getBytes())); } } } catch (IllegalBlockSizeException ex) { Logger.getLogger(this.getClass().getName()).log(Level.SEVERE, "Error while checking credentials file", ex.getMessage()); } catch (BadPaddingException ex) { Logger.getLogger(this.getClass().getName()).log(Level.SEVERE, "Error while checking credentials file", ex.getMessage()); } catch (InvalidKeyException ex) { Logger.getLogger(this.getClass().getName()).log(Level.SEVERE, "Error while checking credentials file", ex.getMessage()); } catch (NoSuchPaddingException ex) { Logger.getLogger(this.getClass().getName()).log(Level.SEVERE, "Error while checking credentials file", ex.getMessage()); } catch (NoSuchAlgorithmException ex) { Logger.getLogger(this.getClass().getName()).log(Level.SEVERE, "Error while checking credentials file", ex.getMessage()); } catch (IOException ex) { Logger.getLogger(this.getClass().getName()).log(Level.SEVERE, "Error while checking credentials file", ex.getMessage()); } return token; }
From source file:dataflow.docker.DockerProcess.java
/** * Wait for the process to finish and periodically grab the output. * * @param builder/* w ww . j av a 2 s.c om*/ * @param prefix * @param command * @param logger * @param outStream * @return */ public int waitForAndLogProcess(Logger logger, PrintStream outStream) { String commandText = StringUtils.join(command, " "); try { synchronized (process) { BufferedReader stdInput = new BufferedReader(new InputStreamReader(process.getInputStream())); BufferedReader stdError = new BufferedReader(new InputStreamReader(process.getErrorStream())); // In milliseconds. final long TIMEOUT = 1000; boolean wait = true; while (wait) { // We periodically check if the process has terminated and if not, // print out all the processes output process.wait(TIMEOUT); // Print all the output String line; while ((stdInput.ready()) && ((line = stdInput.readLine()) != null)) { if (outStream != null) { outStream.println(line); } else { logger.info(line); } } while ((stdError.ready()) && ((line = stdError.readLine()) != null)) { // TODO(jlewi): We should use logger.log and use function arguments // to specify what priority the output should be logged at. logger.error(line); } try { process.exitValue(); // Process is done. wait = false; } catch (IllegalThreadStateException e) { // Process hasn't completed yet. } } logger.info("Exit Value: " + process.exitValue()); if (process.exitValue() != 0) { logger.error("Command: " + commandText + " exited with non-zero status."); } } return process.exitValue(); } catch (IOException e) { throw new RuntimeException("There was a problem executing the command:\n" + commandText + "\n. The Exception was:\n" + e.getMessage()); } catch (InterruptedException e) { throw new RuntimeException("Execution was interupted. The command was:\n" + commandText + "\n. The Exception was:\n" + e.getMessage()); } }
From source file:org.apache.flink.runtime.operators.DataSinkTaskTest.java
@Test @SuppressWarnings("unchecked") public void testSortingDataSinkTask() { int keyCnt = 100; int valCnt = 20; double memoryFraction = 1.0; super.initEnvironment(MEMORY_MANAGER_SIZE, NETWORK_BUFFER_SIZE); super.addInput(new UniformRecordGenerator(keyCnt, valCnt, true), 0); DataSinkTask<Record> testTask = new DataSinkTask<Record>(); // set sorting super.getTaskConfig().setInputLocalStrategy(0, LocalStrategy.SORT); super.getTaskConfig().setInputComparator(new RecordComparatorFactory(new int[] { 1 }, ((Class<? extends Key<?>>[]) new Class[] { IntValue.class })), 0); super.getTaskConfig().setRelativeMemoryInput(0, memoryFraction); super.getTaskConfig().setFilehandlesInput(0, 8); super.getTaskConfig().setSpillingThresholdInput(0, 0.8f); super.registerFileOutputTask(testTask, MockOutputFormat.class, new File(tempTestPath).toURI().toString()); try {/* w w w. jav a 2s . c o m*/ testTask.invoke(); } catch (Exception e) { LOG.debug(e); Assert.fail("Invoke method caused exception."); } File tempTestFile = new File(this.tempTestPath); Assert.assertTrue("Temp output file does not exist", tempTestFile.exists()); FileReader fr = null; BufferedReader br = null; try { fr = new FileReader(tempTestFile); br = new BufferedReader(fr); Set<Integer> keys = new HashSet<Integer>(); int curVal = -1; while (br.ready()) { String line = br.readLine(); Integer key = Integer.parseInt(line.substring(0, line.indexOf("_"))); Integer val = Integer.parseInt(line.substring(line.indexOf("_") + 1, line.length())); // check that values are in correct order Assert.assertTrue("Values not in ascending order", val >= curVal); // next value hit if (val > curVal) { if (curVal != -1) { // check that we saw 100 distinct keys for this values Assert.assertTrue("Keys missing for value", keys.size() == 100); } // empty keys set keys.clear(); // update current value curVal = val; } Assert.assertTrue("Duplicate key for value", keys.add(key)); } } catch (FileNotFoundException e) { Assert.fail("Out file got lost..."); } catch (IOException ioe) { Assert.fail("Caught IOE while reading out file"); } finally { if (br != null) { try { br.close(); } catch (Throwable t) { } } if (fr != null) { try { fr.close(); } catch (Throwable t) { } } } }
From source file:eu.stratosphere.pact.runtime.task.DataSinkTaskTest.java
@Test public void testDataSinkTask() { int keyCnt = 100; int valCnt = 20; super.initEnvironment(MEMORY_MANAGER_SIZE, NETWORK_BUFFER_SIZE); super.addInput(new UniformRecordGenerator(keyCnt, valCnt, false), 0); DataSinkTask<Record> testTask = new DataSinkTask<Record>(); super.registerFileOutputTask(testTask, MockOutputFormat.class, new File(tempTestPath).toURI().toString()); try {/*from ww w . j ava 2s .c o m*/ testTask.invoke(); } catch (Exception e) { LOG.debug(e); Assert.fail("Invoke method caused exception."); } File tempTestFile = new File(this.tempTestPath); Assert.assertTrue("Temp output file does not exist", tempTestFile.exists()); FileReader fr = null; BufferedReader br = null; try { fr = new FileReader(tempTestFile); br = new BufferedReader(fr); HashMap<Integer, HashSet<Integer>> keyValueCountMap = new HashMap<Integer, HashSet<Integer>>(keyCnt); while (br.ready()) { String line = br.readLine(); Integer key = Integer.parseInt(line.substring(0, line.indexOf("_"))); Integer val = Integer.parseInt(line.substring(line.indexOf("_") + 1, line.length())); if (!keyValueCountMap.containsKey(key)) { keyValueCountMap.put(key, new HashSet<Integer>()); } keyValueCountMap.get(key).add(val); } Assert.assertTrue("Invalid key count in out file. Expected: " + keyCnt + " Actual: " + keyValueCountMap.keySet().size(), keyValueCountMap.keySet().size() == keyCnt); for (Integer key : keyValueCountMap.keySet()) { Assert.assertTrue("Invalid value count for key: " + key + ". Expected: " + valCnt + " Actual: " + keyValueCountMap.get(key).size(), keyValueCountMap.get(key).size() == valCnt); } } catch (FileNotFoundException e) { Assert.fail("Out file got lost..."); } catch (IOException ioe) { Assert.fail("Caught IOE while reading out file"); } finally { if (br != null) { try { br.close(); } catch (Throwable t) { } } if (fr != null) { try { fr.close(); } catch (Throwable t) { } } } }
From source file:eu.stratosphere.pact.runtime.task.DataSinkTaskTest.java
@Test public void testUnionDataSinkTask() { int keyCnt = 100; int valCnt = 20; super.initEnvironment(MEMORY_MANAGER_SIZE, NETWORK_BUFFER_SIZE); super.addInput(new UniformRecordGenerator(keyCnt, valCnt, 0, 0, false), 0); super.addInput(new UniformRecordGenerator(keyCnt, valCnt, keyCnt, 0, false), 0); super.addInput(new UniformRecordGenerator(keyCnt, valCnt, keyCnt * 2, 0, false), 0); super.addInput(new UniformRecordGenerator(keyCnt, valCnt, keyCnt * 3, 0, false), 0); DataSinkTask<Record> testTask = new DataSinkTask<Record>(); super.registerFileOutputTask(testTask, MockOutputFormat.class, new File(tempTestPath).toURI().toString()); try {//from w ww . ja va2 s . co m testTask.invoke(); } catch (Exception e) { LOG.debug(e); Assert.fail("Invoke method caused exception."); } File tempTestFile = new File(this.tempTestPath); Assert.assertTrue("Temp output file does not exist", tempTestFile.exists()); FileReader fr = null; BufferedReader br = null; try { fr = new FileReader(tempTestFile); br = new BufferedReader(fr); HashMap<Integer, HashSet<Integer>> keyValueCountMap = new HashMap<Integer, HashSet<Integer>>(keyCnt); while (br.ready()) { String line = br.readLine(); Integer key = Integer.parseInt(line.substring(0, line.indexOf("_"))); Integer val = Integer.parseInt(line.substring(line.indexOf("_") + 1, line.length())); if (!keyValueCountMap.containsKey(key)) { keyValueCountMap.put(key, new HashSet<Integer>()); } keyValueCountMap.get(key).add(val); } Assert.assertTrue("Invalid key count in out file. Expected: " + keyCnt + " Actual: " + keyValueCountMap.keySet().size(), keyValueCountMap.keySet().size() == keyCnt * 4); for (Integer key : keyValueCountMap.keySet()) { Assert.assertTrue("Invalid value count for key: " + key + ". Expected: " + valCnt + " Actual: " + keyValueCountMap.get(key).size(), keyValueCountMap.get(key).size() == valCnt); } } catch (FileNotFoundException e) { Assert.fail("Out file got lost..."); } catch (IOException ioe) { Assert.fail("Caught IOE while reading out file"); } finally { if (br != null) { try { br.close(); } catch (Throwable t) { } } if (fr != null) { try { fr.close(); } catch (Throwable t) { } } } }
From source file:org.talend.components.api.service.internal.ComponentServiceImpl.java
/** * reads a stream following the maven-dependency-plugin plugin :list format * //from ww w .j a v a 2s .co m * <pre> * {@code The following files have been resolved: org.apache.maven:maven-core:jar:3.3.3:compile org.springframework:spring-beans:jar:4.2.0.RELEASE:test org.talend.components:components-common:jar:0.4.0.BUILD-SNAPSHOT:compile log4j:log4j:jar:1.2.17:test org.eclipse.aether:aether-impl:jar:1.0.0.v20140518:compile * } * </pre> * * @param depStream of the dependencies file * @return a list of maven url strings * @throws IOException if read fails. */ private Set<String> parseDependencies(InputStream depStream) throws IOException { Set<String> mvnUris = new HashSet<>(); BufferedReader reader = new BufferedReader(new InputStreamReader(depStream, "UTF-8")); // java 8 version // reader.lines().filter(line -> StringUtils.countMatches(line, ":") > 3).// // filter(line -> !line.endsWith("test")).// // forEach(line -> mvnUris.add(parseMvnUri(line))); while (reader.ready()) { String line = reader.readLine(); if ((org.apache.commons.lang3.StringUtils.countMatches(line, ":") > 3) && !line.endsWith("test")) { mvnUris.add(parseMvnUri(line)); } // else not an expected dependencies so ignor it. } return mvnUris; }
From source file:org.caboclo.util.Credentials.java
/** * Deletes the access credentials of the specified cloud provider on * credentials database file/*from w w w.jav a2 s . com*/ * * @param server */ public void removeCredentials(String server) { StringBuilder path = new StringBuilder(); path.append(System.getProperty("user.home")).append(java.io.File.separator).append("backupcredentials"); BufferedReader input; try { //Create file if it does not exist File credFile = new File(path.toString()); if (!credFile.exists()) { credFile.createNewFile(); } //Stores contents of credentials file, except by line for the //specified cloud server input = new BufferedReader(new FileReader(credFile)); StringBuilder builder = new StringBuilder(); while (input.ready()) { String line = input.readLine(); System.out.println("Cred line: " + line); if (!line.startsWith(server)) { System.out.println("Not-deleted line: " + line); builder.append(line).append(System.lineSeparator()); } } input.close(); //Write new contents in the credentials file BufferedWriter output = new BufferedWriter(new FileWriter(credFile)); output.write(builder.toString()); output.flush(); output.close(); } catch (IOException ex) { Logger.getLogger(this.getClass().getName()).log(Level.SEVERE, null, ex); } }
From source file:org.apache.hadoop.hbase.rest.Dispatcher.java
protected byte[] readInputBuffer(HttpServletRequest request) throws HBaseRestException { try {// w w w . jav a 2 s . co m String resultant = ""; BufferedReader r = request.getReader(); int defaultmaxlength = 10 * 1024 * 1024; int maxLength = this.conf == null ? defaultmaxlength : this.conf.getInt("hbase.rest.input.limit", defaultmaxlength); int bufferLength = 640; // TODO make s maxLength and c size values in configuration if (!r.ready()) { Thread.sleep(1000); // If r is not ready wait 1 second if (!r.ready()) { // If r still is not ready something is wrong, return // blank. return new byte[0]; } } char[] c;// 40 characters * sizeof(UTF16) while (true) { c = new char[bufferLength]; int n = r.read(c, 0, bufferLength); if (n == -1) break; resultant += new String(c, 0, n); if (resultant.length() > maxLength) { resultant = resultant.substring(0, maxLength); break; } } return Bytes.toBytes(resultant.trim()); } catch (Exception e) { throw new HBaseRestException(e); } }
From source file:org.rimudb.ScriptRunner.java
public void executeScript() throws Exception { if (getFilename() == null && getResourceName() == null) { throw new IOException("Either a resource name or a filename must be defined"); }/*from w w w. j ava2 s .co m*/ if (getDatabase() == null) { throw new RimuDBException("Database is not defined"); } if (!getDatabase().isConnected()) { throw new RimuDBException("Database is not connected"); } log.info("Executing script: " + getFilename()); Connection connection = null; Statement statement = null; String sqlQuery = ""; int lineNumber = 0; try { connection = getDatabase().getDatabaseConnection(); statement = connection.createStatement(); StringBuffer query = new StringBuffer(); BufferedReader reader = null; if (getResourceName() != null) { InputStream is = getClass().getResourceAsStream(getResourceName()); reader = new BufferedReader(new InputStreamReader(is)); } else if (getFilename() != null) { reader = new BufferedReader(new FileReader(getFilename())); } else { throw new IllegalArgumentException("Either a resource name or a filename needs to be set"); } while (reader.ready()) { String line = reader.readLine(); lineNumber++; // Check for EOF if (line == null) { break; } // Ignore comments if (isComment(line)) { continue; } // Change delimiter command boolean changeDelimiter = line.matches("\\{delimiter=.+\\}"); if (changeDelimiter) { String s = line.substring(11); int pos = s.indexOf("}"); String delimiter = s.substring(0, pos); setEndOfQueryDelimiter(delimiter); log.info("Changing to delimiter '" + delimiter + "'"); continue; } boolean ignoreErrorLine = (line.startsWith("{ignoreError}")); if (ignoreErrorLine) { line = line.substring(13); } boolean endOfStatement = isEndOfStatement(line); if (query.length() > 0) { query.append(" "); } query.append(line.trim()); if (endOfStatement) { // Remove the query delimiter sqlQuery = trimQueryDelimiter(query.toString()); query.setLength(0); // Execute the statement try { boolean result = statement.execute(sqlQuery); if (result) { log.warn("SQL statement returned a result set. [" + sqlQuery + "]"); } else { int updateCount = statement.getUpdateCount(); log.info("Update count = " + updateCount + " [" + sqlQuery + "]"); } } catch (SQLException sqle) { if (!continueOnFailure && !ignoreErrorLine) { throw sqle; } else { log.error("Line: " + lineNumber + " Error executing SQL=" + sqlQuery, sqle); } } } } } catch (SQLException e) { log.error("Line: " + lineNumber + " Error executing SQL=" + sqlQuery); throw new RimuDBException("Line: " + lineNumber + ": " + e.toString()); } finally { if (statement != null) { try { statement.close(); } catch (Exception e) { } } if (connection != null) { try { connection.close(); } catch (Exception e) { } } log.info("Completed script: " + getFilename()); } }