Java Code Examples for java.io.BufferedReader.lines()

The following are Jave code examples for showing how to use lines() of the java.io.BufferedReader class. You can vote up the examples you like. Your votes will be used in our system to get more good examples.
Example 1
Project: Mastering-Java-EE-Development-with-WildFly   File: PayrollItemReader.java   Source Code and License Vote up 6 votes
@Override
public void open(Serializable prevCheckpointInfo) throws Exception {
	JobOperator jobOperator = getJobOperator();
	Properties jobParameters = jobOperator.getParameters(jobContext.getExecutionId());
	String resourceName = (String) jobParameters.get(INPUT_DATA_FILE_NAME);
	InputStream inputStream = new FileInputStream(resourceName);
	br = new BufferedReader(new InputStreamReader(inputStream));

	Stream<String> lines = br.lines();
	if (prevCheckpointInfo != null)
		recordNumber = (Integer) prevCheckpointInfo;
	else
		recordNumber = 0;
	stringLines = lines.toArray();
	logger.info("[SimpleItemReader] Opened Payroll file for reading from record number: " + recordNumber);
}
 
Example 2
Project: jdk8u-jdk   File: Lines.java   Source Code and License Vote up 6 votes
public void testIterator() throws IOException {
    MockLineReader r = new MockLineReader(6);
    BufferedReader br = new BufferedReader(r);
    String line = br.readLine();
    assertEquals(r.getLineNumber(), 1, "Read one line");
    Stream<String> s = br.lines();
    Iterator<String> it = s.iterator();
    // Ensure iterate with only next works
    for (int i = 0; i < 5; i++) {
        String str = it.next();
        assertEquals(str, "Line " + (i + 2), "Addtional five lines");
    }
    // NoSuchElementException
    try {
        it.next();
        fail("Should have run out of lines.");
    } catch (NoSuchElementException nsse) {}
}
 
Example 3
Project: openjdk-jdk10   File: Lines.java   Source Code and License Vote up 6 votes
public void testIterator() throws IOException {
    MockLineReader r = new MockLineReader(6);
    BufferedReader br = new BufferedReader(r);
    String line = br.readLine();
    assertEquals(r.getLineNumber(), 1, "Read one line");
    Stream<String> s = br.lines();
    Iterator<String> it = s.iterator();
    // Ensure iterate with only next works
    for (int i = 0; i < 5; i++) {
        String str = it.next();
        assertEquals(str, "Line " + (i + 2), "Addtional five lines");
    }
    // NoSuchElementException
    try {
        it.next();
        fail("Should have run out of lines.");
    } catch (NoSuchElementException nsse) {}
}
 
Example 4
Project: jdk8u-jdk   File: Lines.java   Source Code and License Vote up 5 votes
public void testInterlacedRead() throws IOException {
    MockLineReader r = new MockLineReader(10);
    BufferedReader br = new BufferedReader(r);
    char[] buf = new char[5];
    Stream<String> s = br.lines();
    Iterator<String> it = s.iterator();

    br.read(buf);
    assertEquals(new String(buf), "Line ");
    assertEquals(it.next(), "1");
    try {
        s.iterator().next();
        fail("Should failed on second attempt to get iterator from s");
    } catch (IllegalStateException ise) {}
    br.read(buf, 0, 2);
    assertEquals(new String(buf, 0, 2), "Li");
    // Get stream again should continue from where left
    // Only read remaining of the line
    br.lines().limit(1L).forEach(line -> assertEquals(line, "ne 2"));
    br.read(buf, 0, 2);
    assertEquals(new String(buf, 0, 2), "Li");
    br.read(buf, 0, 2);
    assertEquals(new String(buf, 0, 2), "ne");
    assertEquals(it.next(), " 3");
    // Line 4
    br.readLine();
    // interator pick
    assertEquals(it.next(), "Line 5");
    // Another stream instantiated by lines()
    AtomicInteger line_no = new AtomicInteger(6);
    br.lines().forEach(l -> assertEquals(l, "Line " + line_no.getAndIncrement()));
    // Read after EOL
    assertFalse(it.hasNext());
}
 
Example 5
Project: openjdk-jdk10   File: Linker.java   Source Code and License Vote up 5 votes
void link() throws Exception {
    Process p = Runtime.getRuntime().exec(linkerCmd);
    final int exitCode = p.waitFor();
    if (exitCode != 0) {
        InputStream stderr = p.getErrorStream();
        if (stderr.read() == -1) {
            stderr = p.getInputStream();
        }
        BufferedReader br = new BufferedReader(new InputStreamReader(stderr));
        Stream<String> lines = br.lines();
        StringBuilder sb = new StringBuilder();
        lines.iterator().forEachRemaining(e -> sb.append(e));
        throw new InternalError(sb.toString());
    }
    File objFile = new File(objectFileName);
    if (objFile.exists()) {
        if (!objFile.delete()) {
            throw new InternalError("Failed to delete " + objectFileName + " file");
        }
    }
    // Make non-executable for all.
    File libFile = new File(libraryFileName);
    if (libFile.exists() && !options.osName.startsWith("Windows")) {
        if (!libFile.setExecutable(false, false)) {
            throw new InternalError("Failed to change attribute for " + libraryFileName + " file");
        }
    }

}
 
Example 6
Project: openjdk-jdk10   File: Lines.java   Source Code and License Vote up 5 votes
public void testInterlacedRead() throws IOException {
    MockLineReader r = new MockLineReader(10);
    BufferedReader br = new BufferedReader(r);
    char[] buf = new char[5];
    Stream<String> s = br.lines();
    Iterator<String> it = s.iterator();

    br.read(buf);
    assertEquals(new String(buf), "Line ");
    assertEquals(it.next(), "1");
    try {
        s.iterator().next();
        fail("Should failed on second attempt to get iterator from s");
    } catch (IllegalStateException ise) {}
    br.read(buf, 0, 2);
    assertEquals(new String(buf, 0, 2), "Li");
    // Get stream again should continue from where left
    // Only read remaining of the line
    br.lines().limit(1L).forEach(line -> assertEquals(line, "ne 2"));
    br.read(buf, 0, 2);
    assertEquals(new String(buf, 0, 2), "Li");
    br.read(buf, 0, 2);
    assertEquals(new String(buf, 0, 2), "ne");
    assertEquals(it.next(), " 3");
    // Line 4
    br.readLine();
    // interator pick
    assertEquals(it.next(), "Line 5");
    // Another stream instantiated by lines()
    AtomicInteger line_no = new AtomicInteger(6);
    br.lines().forEach(l -> assertEquals(l, "Line " + line_no.getAndIncrement()));
    // Read after EOL
    assertFalse(it.hasNext());
}
 
Example 7
Project: mix-them   File: RuleRuns.java   Source Code and License Vote up 4 votes
/**
 	* Returns a list of test runs for the rule.
 	* @param url The URL of rule additional parameters file
 	* @return Returns a list of test runs for the rule
   	*/	
public static List<RuleRun> getRuns(Rule rule, URL url) throws FileNotFoundException, IOException, NumberFormatException {
   		List<RuleRun> runs = new LinkedList<RuleRun>();		
   		runs.add(new RuleRun(Collections.emptyMap()));
	if (url != null) {
		File file = new File(url.getFile());			
		BufferedReader reader = Files.newBufferedReader(file.toPath(), StandardCharsets.UTF_8);
		Stream<String> entries = reader.lines();
		entries.forEach(entry -> {
			String[] parts = entry.split("\\s");
			if (parts.length > 1) {
				String suffix = parts[0];
				Map<RuleParam, ParamValue> params = new EnumMap<RuleParam, ParamValue>(RuleParam.class);
				switch (rule) {
					case _RANDOM_ALT_LINE:
						int seed = Integer.parseInt(parts[1]);
						params.put(RuleParam._RANDOM_SEED, ParamValue.createInt(seed));
						break;
					case _JOIN:
						int col = Integer.parseInt(parts[1]);
						params.put(RuleParam._JOIN_COL1, ParamValue.createInt(col));
						if (parts.length > 2) {
							col = Integer.parseInt(parts[2]);
							params.put(RuleParam._JOIN_COL2, ParamValue.createInt(col));
						}	
						break;
					case _ZIP_LINE:
					case _ZIP_CELL:
					case _ZIP_CHAR:
						String sep = parts[1];
						params.put(RuleParam._ZIP_SEP, ParamValue.createString(sep));
				}
				if (suffix.equals(DEFAULT_OUTPUT_FILE)) {
					runs.add(new RuleRun(null, params));
				} else {
					runs.add(new RuleRun(suffix, params));
				}
			}
		});
	}
   		return runs;
 	}
 
Example 8
Project: openjdk-jdk10   File: Linker.java   Source Code and License Vote up 4 votes
Linker(Main main) throws Exception {
    this.options = main.options;
    String name = options.outputName;
    objectFileName = name;
    libraryFileName = name;

    if (options.linkerpath != null && !(new File(options.linkerpath).exists())) {
        throw new InternalError("Invalid linker path: " + options.linkerpath);
    }
    String linkerPath;
    String linkerCheck;

    switch (options.osName) {
        case "Linux":
            if (name.endsWith(".so")) {
                objectFileName = name.substring(0, name.length() - ".so".length());
            }
            linkerPath = (options.linkerpath != null) ? options.linkerpath : "ld";
            linkerCmd = linkerPath + " -shared -z noexecstack -o " + libraryFileName + " " + objectFileName;
            linkerCheck = linkerPath + " -v";
            break;
        case "SunOS":
            if (name.endsWith(".so")) {
                objectFileName = name.substring(0, name.length() - ".so".length());
            }
            objectFileName = objectFileName + ".o";
            linkerPath = (options.linkerpath != null) ? options.linkerpath : "ld";
            linkerCmd = linkerPath + " -shared -o " + libraryFileName + " " + objectFileName;
            linkerCheck = linkerPath + " -V";
            break;
        case "Mac OS X":
            if (name.endsWith(".dylib")) {
                objectFileName = name.substring(0, name.length() - ".dylib".length());
            }
            objectFileName = objectFileName + ".o";
            linkerPath = (options.linkerpath != null) ? options.linkerpath : "ld";
            linkerCmd = linkerPath + " -dylib -o " + libraryFileName + " " + objectFileName;
            linkerCheck = linkerPath + " -v";
            break;
        default:
            if (options.osName.startsWith("Windows")) {
                if (name.endsWith(".dll")) {
                    objectFileName = name.substring(0, name.length() - ".dll".length());
                }
                objectFileName = objectFileName + ".obj";
                linkerPath = (options.linkerpath != null) ? options.linkerpath : getWindowsLinkPath();
                if (linkerPath == null) {
                    throw new InternalError("Can't locate Microsoft Visual Studio amd64 link.exe");
                }
                linkerCmd = linkerPath + " /DLL /OPT:NOREF /NOLOGO /NOENTRY" + " /OUT:" + libraryFileName + " " + objectFileName;
                linkerCheck = null; // link.exe presence is verified already
                break;
            } else {
                throw new InternalError("Unsupported platform: " + options.osName);
            }
    }

    // Check linker presence on platforms by printing its version
    if (linkerCheck != null) {
        Process p = Runtime.getRuntime().exec(linkerCheck);
        final int exitCode = p.waitFor();
        if (exitCode != 0) {
            InputStream stderr = p.getErrorStream();
            BufferedReader br = new BufferedReader(new InputStreamReader(stderr));
            Stream<String> lines = br.lines();
            StringBuilder sb = new StringBuilder();
            lines.iterator().forEachRemaining(e -> sb.append(e));
            throw new InternalError(sb.toString());
        }
    }
}