Java Code Examples for java.io.BufferedReader#reset()

The following examples show how to use java.io.BufferedReader#reset() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: TaskIo.java    From jdotxt with GNU General Public License v3.0 7 votes vote down vote up
private static String readLine(BufferedReader r) throws IOException {
	StringBuilder sb = new StringBuilder();
	boolean eol = false;
	int c;
	
	while(!eol && (c = r.read()) >= 0) {
		sb.append((char)c);
		eol = (c == '\r' || c == '\n');
		
		// check for \r\n
		if (c == '\r') {
			r.mark(1);
			c = r.read();
			if (c != '\n') {
				r.reset();
			} else {
				sWindowsLineBreaks = true;
				sb.append((char)c);
			}
		}
	}
	return sb.length() == 0 ? null : sb.toString();
}
 
Example 2
Source File: CSVProcessor.java    From dragonwell8_jdk with GNU General Public License v2.0 6 votes vote down vote up
private static void statInSeveralPasses(BufferedReader br, int column)
        throws IOException {
    System.out.println("#-----Statistics in several passes-------#");
    //Create a comparator to compare records by the column.
    Comparator<String> comparator
            = Comparator.comparing(
                    (String str) -> parseDouble(getCell(str, column)));
    //Find max record by using Collectors.maxBy(...)
    System.out.println(
            "Max: " + br.lines().collect(maxBy(comparator)).get());
    br.reset();
    //Find min record by using Collectors.minBy(...)
    System.out.println(
            "Min: " + br.lines().collect(minBy(comparator)).get());
    br.reset();
    //Compute the average value and sum with
    //Collectors.toDoubleSummaryStatistics(...)
    DoubleSummaryStatistics doubleSummaryStatistics
            = br.lines().collect(summarizingDouble(
                str -> parseDouble(getCell(str, column))));
    System.out.println("Average: " + doubleSummaryStatistics.getAverage());
    System.out.println("Sum: " + doubleSummaryStatistics.getSum());
}
 
Example 3
Source File: JHLogAnalyzer.java    From big-c with Apache License 2.0 6 votes vote down vote up
private String readLine(BufferedReader reader) throws IOException {
  resBuffer.setLength(0);
  reader.mark(maxJobDelimiterLineLength);
  for(String line = reader.readLine();
            line != null; line = reader.readLine()) {
    if(isEndOfJobLog(line)) {
      if(resBuffer.length() == 0)
        resBuffer.append(line);
      else
        reader.reset();
      break;
    }
    if(resBuffer.length() == 0)
      resBuffer.append(line);
    else if(resBuffer.length() < 32000)
      resBuffer.append(line);
    if(line.endsWith(" .") || line.endsWith("\" ")) {
      break;
    }
    reader.mark(maxJobDelimiterLineLength);
  }
  String result = resBuffer.length() == 0 ? null : resBuffer.toString();
  resBuffer.setLength(0);
  return result;
}
 
Example 4
Source File: JHLogAnalyzer.java    From hadoop with Apache License 2.0 6 votes vote down vote up
private String readLine(BufferedReader reader) throws IOException {
  resBuffer.setLength(0);
  reader.mark(maxJobDelimiterLineLength);
  for(String line = reader.readLine();
            line != null; line = reader.readLine()) {
    if(isEndOfJobLog(line)) {
      if(resBuffer.length() == 0)
        resBuffer.append(line);
      else
        reader.reset();
      break;
    }
    if(resBuffer.length() == 0)
      resBuffer.append(line);
    else if(resBuffer.length() < 32000)
      resBuffer.append(line);
    if(line.endsWith(" .") || line.endsWith("\" ")) {
      break;
    }
    reader.mark(maxJobDelimiterLineLength);
  }
  String result = resBuffer.length() == 0 ? null : resBuffer.toString();
  resBuffer.setLength(0);
  return result;
}
 
Example 5
Source File: LoadConfigurationServlet.java    From candybean with GNU Affero General Public License v3.0 6 votes vote down vote up
private String readComments(BufferedReader br) throws IOException {
	String line;
	String comments = "";
	br.mark(0);
	while ((line = br.readLine()) != null) {
		if(line.startsWith("#") || line.isEmpty()){
			comments = comments.concat(line);
		}else{
			br.reset();
			break;
		}
		br.mark(0);
	}
	return comments;
	
}
 
Example 6
Source File: CSVProcessor.java    From openjdk-jdk8u-backup with GNU General Public License v2.0 6 votes vote down vote up
private static void statInSeveralPasses(BufferedReader br, int column)
        throws IOException {
    System.out.println("#-----Statistics in several passes-------#");
    //Create a comparator to compare records by the column.
    Comparator<String> comparator
            = Comparator.comparing(
                    (String str) -> parseDouble(getCell(str, column)));
    //Find max record by using Collectors.maxBy(...)
    System.out.println(
            "Max: " + br.lines().collect(maxBy(comparator)).get());
    br.reset();
    //Find min record by using Collectors.minBy(...)
    System.out.println(
            "Min: " + br.lines().collect(minBy(comparator)).get());
    br.reset();
    //Compute the average value and sum with
    //Collectors.toDoubleSummaryStatistics(...)
    DoubleSummaryStatistics doubleSummaryStatistics
            = br.lines().collect(summarizingDouble(
                str -> parseDouble(getCell(str, column))));
    System.out.println("Average: " + doubleSummaryStatistics.getAverage());
    System.out.println("Sum: " + doubleSummaryStatistics.getSum());
}
 
Example 7
Source File: CSVProcessor.java    From jdk8u-dev-jdk with GNU General Public License v2.0 6 votes vote down vote up
private static void statInSeveralPasses(BufferedReader br, int column)
        throws IOException {
    System.out.println("#-----Statistics in several passes-------#");
    //Create a comparator to compare records by the column.
    Comparator<String> comparator
            = Comparator.comparing(
                    (String str) -> parseDouble(getCell(str, column)));
    //Find max record by using Collectors.maxBy(...)
    System.out.println(
            "Max: " + br.lines().collect(maxBy(comparator)).get());
    br.reset();
    //Find min record by using Collectors.minBy(...)
    System.out.println(
            "Min: " + br.lines().collect(minBy(comparator)).get());
    br.reset();
    //Compute the average value and sum with
    //Collectors.toDoubleSummaryStatistics(...)
    DoubleSummaryStatistics doubleSummaryStatistics
            = br.lines().collect(summarizingDouble(
                str -> parseDouble(getCell(str, column))));
    System.out.println("Average: " + doubleSummaryStatistics.getAverage());
    System.out.println("Sum: " + doubleSummaryStatistics.getSum());
}
 
Example 8
Source File: LoggingServiceImpl.java    From flow-platform-x with Apache License 2.0 6 votes vote down vote up
@Override
public Page<String> read(ExecutedCmd cmd, Pageable pageable) {
    BufferedReader reader = getReader(cmd.getId());

    if (Objects.isNull(reader)) {
        return LogNotFound;
    }

    try (Stream<String> lines = reader.lines()) {
        int i = pageable.getPageNumber() * pageable.getPageSize();

        List<String> logs = lines.skip(i)
            .limit(pageable.getPageSize())
            .collect(Collectors.toList());

        return new PageImpl<>(logs, pageable, cmd.getLogSize());
    } finally {
        try {
            reader.reset();
        } catch (IOException e) {
            // reset will be failed if all lines been read
            logReaderCache.invalidate(cmd.getId());
        }
    }
}
 
Example 9
Source File: CSVProcessor.java    From jdk8u60 with GNU General Public License v2.0 6 votes vote down vote up
private static void statInSeveralPasses(BufferedReader br, int column)
        throws IOException {
    System.out.println("#-----Statistics in several passes-------#");
    //Create a comparator to compare records by the column.
    Comparator<String> comparator
            = Comparator.comparing(
                    (String str) -> parseDouble(getCell(str, column)));
    //Find max record by using Collectors.maxBy(...)
    System.out.println(
            "Max: " + br.lines().collect(maxBy(comparator)).get());
    br.reset();
    //Find min record by using Collectors.minBy(...)
    System.out.println(
            "Min: " + br.lines().collect(minBy(comparator)).get());
    br.reset();
    //Compute the average value and sum with
    //Collectors.toDoubleSummaryStatistics(...)
    DoubleSummaryStatistics doubleSummaryStatistics
            = br.lines().collect(summarizingDouble(
                str -> parseDouble(getCell(str, column))));
    System.out.println("Average: " + doubleSummaryStatistics.getAverage());
    System.out.println("Sum: " + doubleSummaryStatistics.getSum());
}
 
Example 10
Source File: CSVProcessor.java    From TencentKona-8 with GNU General Public License v2.0 6 votes vote down vote up
private static void statInSeveralPasses(BufferedReader br, int column)
        throws IOException {
    System.out.println("#-----Statistics in several passes-------#");
    //Create a comparator to compare records by the column.
    Comparator<String> comparator
            = Comparator.comparing(
                    (String str) -> parseDouble(getCell(str, column)));
    //Find max record by using Collectors.maxBy(...)
    System.out.println(
            "Max: " + br.lines().collect(maxBy(comparator)).get());
    br.reset();
    //Find min record by using Collectors.minBy(...)
    System.out.println(
            "Min: " + br.lines().collect(minBy(comparator)).get());
    br.reset();
    //Compute the average value and sum with
    //Collectors.toDoubleSummaryStatistics(...)
    DoubleSummaryStatistics doubleSummaryStatistics
            = br.lines().collect(summarizingDouble(
                str -> parseDouble(getCell(str, column))));
    System.out.println("Average: " + doubleSummaryStatistics.getAverage());
    System.out.println("Sum: " + doubleSummaryStatistics.getSum());
}
 
Example 11
Source File: CSVProcessor.java    From openjdk-8 with GNU General Public License v2.0 6 votes vote down vote up
private static void statInSeveralPasses(BufferedReader br, int column)
        throws IOException {
    System.out.println("#-----Statistics in several passes-------#");
    //Create a comparator to compare records by the column.
    Comparator<String> comparator
            = Comparator.comparing(
                    (String str) -> parseDouble(getCell(str, column)));
    //Find max record by using Collectors.maxBy(...)
    System.out.println(
            "Max: " + br.lines().collect(maxBy(comparator)).get());
    br.reset();
    //Find min record by using Collectors.minBy(...)
    System.out.println(
            "Min: " + br.lines().collect(minBy(comparator)).get());
    br.reset();
    //Compute the average value and sum with
    //Collectors.toDoubleSummaryStatistics(...)
    DoubleSummaryStatistics doubleSummaryStatistics
            = br.lines().collect(summarizingDouble(
                str -> parseDouble(getCell(str, column))));
    System.out.println("Average: " + doubleSummaryStatistics.getAverage());
    System.out.println("Sum: " + doubleSummaryStatistics.getSum());
}
 
Example 12
Source File: CSVProcessor.java    From jdk8u-jdk with GNU General Public License v2.0 6 votes vote down vote up
private static void statInSeveralPasses(BufferedReader br, int column)
        throws IOException {
    System.out.println("#-----Statistics in several passes-------#");
    //Create a comparator to compare records by the column.
    Comparator<String> comparator
            = Comparator.comparing(
                    (String str) -> parseDouble(getCell(str, column)));
    //Find max record by using Collectors.maxBy(...)
    System.out.println(
            "Max: " + br.lines().collect(maxBy(comparator)).get());
    br.reset();
    //Find min record by using Collectors.minBy(...)
    System.out.println(
            "Min: " + br.lines().collect(minBy(comparator)).get());
    br.reset();
    //Compute the average value and sum with
    //Collectors.toDoubleSummaryStatistics(...)
    DoubleSummaryStatistics doubleSummaryStatistics
            = br.lines().collect(summarizingDouble(
                str -> parseDouble(getCell(str, column))));
    System.out.println("Average: " + doubleSummaryStatistics.getAverage());
    System.out.println("Sum: " + doubleSummaryStatistics.getSum());
}
 
Example 13
Source File: WC.java    From jdk8u60 with GNU General Public License v2.0 5 votes vote down vote up
private static void collectInFourPasses(BufferedReader reader)
        throws IOException {
    /*
     * Input is read as a stream of lines by lines().
     * Every line is turned into a stream of chars by the flatMapToInt(...)
     * method.
     * Length of the stream is counted by count().
     */
    System.out.println("Character count = "
            + reader.lines().flatMapToInt(String::chars).count());
    /*
     * Input is read as a stream of lines by lines().
     * Every line is split by nonWordPattern into words by flatMap(...)
     * method.
     * Empty lines are removed by the filter(...) method.
     * Length of the stream is counted by count().
     */
    reader.reset();
    System.out.println("Word count = "
            + reader.lines()
            .flatMap(nonWordPattern::splitAsStream)
            .filter(str -> !str.isEmpty()).count());

    reader.reset();
    System.out.println("Newline count = " + reader.lines().count());
    /*
     * Input is read as a stream of lines by lines().
     * Every line is mapped to its length.
     * Maximum of the lengths is calculated.
     */
    reader.reset();
    System.out.println("Max line length = "
            + reader.lines().mapToInt(String::length).max().getAsInt());
}
 
Example 14
Source File: UTF8BufferedReader.java    From ldparteditor with MIT License 5 votes vote down vote up
/**
 * Creates the standard {@code BufferedReader} for reading LDraw files
 *
 * @param fileName
 *            file to read from
 * @throws FileNotFoundException
 * @throws LDParsingException
 * @throws UnsupportedEncodingException
 */
public UTF8BufferedReader(String fileName) throws FileNotFoundException, LDParsingException, UnsupportedEncodingException {
    myReader = new BufferedReader(new InputStreamReader(new FileInputStream(fileName), "UTF-8")); //$NON-NLS-1$
    char[] bom = new char[3];
    try {
        myReader.mark(3);
        if (3 == myReader.read(bom, 0, 3)) {
            if (bom[0] != (char) 0xEF || bom[1] != (char) 0xBB || bom[2] != (char) 0xBF) {
                myReader.reset();
            }
        }
    } catch (IOException e) {
        throw new LDParsingException();
    }
}
 
Example 15
Source File: MatrixMarketReader.java    From Drop-seq with MIT License 5 votes vote down vote up
private static String readAndReset(final BufferedReader reader, final int numChars) {
    try {
        reader.mark(numChars);
        final char[] buf = new char[numChars];
        final int charsRead = reader.read(buf);
        reader.reset();
        if (charsRead != numChars) {
            return null;
        }
        return new String(buf);
    } catch (IOException e) {
        throw new RuntimeIOException(e);
    }
}
 
Example 16
Source File: WC.java    From TencentKona-8 with GNU General Public License v2.0 5 votes vote down vote up
private static void collectInFourPasses(BufferedReader reader)
        throws IOException {
    /*
     * Input is read as a stream of lines by lines().
     * Every line is turned into a stream of chars by the flatMapToInt(...)
     * method.
     * Length of the stream is counted by count().
     */
    System.out.println("Character count = "
            + reader.lines().flatMapToInt(String::chars).count());
    /*
     * Input is read as a stream of lines by lines().
     * Every line is split by nonWordPattern into words by flatMap(...)
     * method.
     * Empty lines are removed by the filter(...) method.
     * Length of the stream is counted by count().
     */
    reader.reset();
    System.out.println("Word count = "
            + reader.lines()
            .flatMap(nonWordPattern::splitAsStream)
            .filter(str -> !str.isEmpty()).count());

    reader.reset();
    System.out.println("Newline count = " + reader.lines().count());
    /*
     * Input is read as a stream of lines by lines().
     * Every line is mapped to its length.
     * Maximum of the lengths is calculated.
     */
    reader.reset();
    System.out.println("Max line length = "
            + reader.lines().mapToInt(String::length).max().getAsInt());
}
 
Example 17
Source File: WC.java    From openjdk-jdk8u-backup with GNU General Public License v2.0 5 votes vote down vote up
private static void collectInFourPasses(BufferedReader reader)
        throws IOException {
    /*
     * Input is read as a stream of lines by lines().
     * Every line is turned into a stream of chars by the flatMapToInt(...)
     * method.
     * Length of the stream is counted by count().
     */
    System.out.println("Character count = "
            + reader.lines().flatMapToInt(String::chars).count());
    /*
     * Input is read as a stream of lines by lines().
     * Every line is split by nonWordPattern into words by flatMap(...)
     * method.
     * Empty lines are removed by the filter(...) method.
     * Length of the stream is counted by count().
     */
    reader.reset();
    System.out.println("Word count = "
            + reader.lines()
            .flatMap(nonWordPattern::splitAsStream)
            .filter(str -> !str.isEmpty()).count());

    reader.reset();
    System.out.println("Newline count = " + reader.lines().count());
    /*
     * Input is read as a stream of lines by lines().
     * Every line is mapped to its length.
     * Maximum of the lengths is calculated.
     */
    reader.reset();
    System.out.println("Max line length = "
            + reader.lines().mapToInt(String::length).max().getAsInt());
}
 
Example 18
Source File: WC.java    From openjdk-8 with GNU General Public License v2.0 5 votes vote down vote up
private static void collectInFourPasses(BufferedReader reader)
        throws IOException {
    /*
     * Input is read as a stream of lines by lines().
     * Every line is turned into a stream of chars by the flatMapToInt(...)
     * method.
     * Length of the stream is counted by count().
     */
    System.out.println("Character count = "
            + reader.lines().flatMapToInt(String::chars).count());
    /*
     * Input is read as a stream of lines by lines().
     * Every line is split by nonWordPattern into words by flatMap(...)
     * method.
     * Empty lines are removed by the filter(...) method.
     * Length of the stream is counted by count().
     */
    reader.reset();
    System.out.println("Word count = "
            + reader.lines()
            .flatMap(nonWordPattern::splitAsStream)
            .filter(str -> !str.isEmpty()).count());

    reader.reset();
    System.out.println("Newline count = " + reader.lines().count());
    /*
     * Input is read as a stream of lines by lines().
     * Every line is mapped to its length.
     * Maximum of the lengths is calculated.
     */
    reader.reset();
    System.out.println("Max line length = "
            + reader.lines().mapToInt(String::length).max().getAsInt());
}
 
Example 19
Source File: WC.java    From jdk8u-dev-jdk with GNU General Public License v2.0 5 votes vote down vote up
private static void collectInFourPasses(BufferedReader reader)
        throws IOException {
    /*
     * Input is read as a stream of lines by lines().
     * Every line is turned into a stream of chars by the flatMapToInt(...)
     * method.
     * Length of the stream is counted by count().
     */
    System.out.println("Character count = "
            + reader.lines().flatMapToInt(String::chars).count());
    /*
     * Input is read as a stream of lines by lines().
     * Every line is split by nonWordPattern into words by flatMap(...)
     * method.
     * Empty lines are removed by the filter(...) method.
     * Length of the stream is counted by count().
     */
    reader.reset();
    System.out.println("Word count = "
            + reader.lines()
            .flatMap(nonWordPattern::splitAsStream)
            .filter(str -> !str.isEmpty()).count());

    reader.reset();
    System.out.println("Newline count = " + reader.lines().count());
    /*
     * Input is read as a stream of lines by lines().
     * Every line is mapped to its length.
     * Maximum of the lengths is calculated.
     */
    reader.reset();
    System.out.println("Max line length = "
            + reader.lines().mapToInt(String::length).max().getAsInt());
}
 
Example 20
Source File: WC.java    From jdk8u-jdk with GNU General Public License v2.0 5 votes vote down vote up
private static void collectInFourPasses(BufferedReader reader)
        throws IOException {
    /*
     * Input is read as a stream of lines by lines().
     * Every line is turned into a stream of chars by the flatMapToInt(...)
     * method.
     * Length of the stream is counted by count().
     */
    System.out.println("Character count = "
            + reader.lines().flatMapToInt(String::chars).count());
    /*
     * Input is read as a stream of lines by lines().
     * Every line is split by nonWordPattern into words by flatMap(...)
     * method.
     * Empty lines are removed by the filter(...) method.
     * Length of the stream is counted by count().
     */
    reader.reset();
    System.out.println("Word count = "
            + reader.lines()
            .flatMap(nonWordPattern::splitAsStream)
            .filter(str -> !str.isEmpty()).count());

    reader.reset();
    System.out.println("Newline count = " + reader.lines().count());
    /*
     * Input is read as a stream of lines by lines().
     * Every line is mapped to its length.
     * Maximum of the lengths is calculated.
     */
    reader.reset();
    System.out.println("Max line length = "
            + reader.lines().mapToInt(String::length).max().getAsInt());
}