/** * Read the lines from the content of the resource file at the given path on the classpath. * * @param resourcePath the logical path to the classpath, file, or URL resource * @param classLoader the classloader that should be used to load the resource as a stream; may be null * @param clazz the class that should be used to load the resource as a stream; may be null * @param lineProcessor the function that this method calls for each line read from the supplied stream; may not be null * @throws IOException if an I/O error occurs */ public static void readLines(String resourcePath, ClassLoader classLoader, Class<?> clazz, Consumer<String> lineProcessor) throws IOException { try (InputStream stream = IoUtil.getResourceAsStream(resourcePath, classLoader, clazz, null, null)) { IoUtil.readLines(stream, lineProcessor); } }
/** * Reads the lines starting with a given line number from the specified file on the classpath. Any lines preceding the * given line number will be included as empty lines, meaning the line numbers will match the input file. * * @param startingLineNumber the 1-based number designating the first line to be included * @param classpathResource the path to the file on the classpath * @return the string containing the subset of the file contents; never null but possibly empty */ protected String readLines(int startingLineNumber, String classpathResource) { try (InputStream stream = getClass().getClassLoader().getResourceAsStream(classpathResource);) { assertThat(stream).isNotNull(); StringBuilder sb = new StringBuilder(); AtomicInteger counter = new AtomicInteger(); IoUtil.readLines(stream, line -> { if (counter.incrementAndGet() >= startingLineNumber) sb.append(line); sb.append(System.lineSeparator()); }); return sb.toString(); } catch (IOException e) { fail("Unable to read '" + classpathResource + "'"); } assert false : "should never get here"; return null; }
/** * Read the contents of the supplied {@link InputStream}, replace all variables found in the content, and write the * result to a temporary file. * * @param stream the input stream containing zero or more {@link Strings#replaceVariables(String, java.util.function.Function) * variable expressions} * @param variables the variables * @return the temporary file that exists in the data directory * @throws IOException if there is a problem reading the input stream or writing to the temporary file */ protected static File replaceVariables(InputStream stream, AvailableVariables variables) throws IOException { File tmpFile = Testing.Files.createTestingFile(); try (OutputStream ostream = new FileOutputStream(tmpFile)) { IoUtil.readLines(stream, (line) -> { String newLine = Strings.replaceVariables(line, variables::variableForName); try { ostream.write(newLine.getBytes(StandardCharsets.UTF_8)); } catch (IOException e) { throw new RuntimeException("Error writing to file '" + tmpFile + "'", e); } }, StandardCharsets.UTF_8); } return tmpFile; } }
protected List<Document> loadTestDocuments(String pathOnClasspath) { List<Document> results = new ArrayList<>(); try (InputStream stream = Testing.Files.readResourceAsStream(pathOnClasspath);) { assertThat(stream).isNotNull(); IoUtil.readLines(stream, line -> { Document doc = Document.parse(line); assertThat(doc.size()).isGreaterThan(0); results.add(doc); }); } catch (IOException e) { fail("Unable to find or read file '" + pathOnClasspath + "': " + e.getMessage()); } return results; }
/** * Read the lines from the content of the resource file at the given path on the classpath. * * @param resourcePath the logical path to the classpath, file, or URL resource * @param classLoader the classloader that should be used to load the resource as a stream; may be null * @param clazz the class that should be used to load the resource as a stream; may be null * @param lineProcessor the function that this method calls for each line read from the supplied stream; may not be null * @throws IOException if an I/O error occurs */ public static void readLines(String resourcePath, ClassLoader classLoader, Class<?> clazz, Consumer<String> lineProcessor) throws IOException { try (InputStream stream = IoUtil.getResourceAsStream(resourcePath, classLoader, clazz, null, null)) { IoUtil.readLines(stream, lineProcessor); } }
/** * Reads the lines starting with a given line number from the specified file on the classpath. Any lines preceding the * given line number will be included as empty lines, meaning the line numbers will match the input file. * * @param startingLineNumber the 1-based number designating the first line to be included * @param classpathResource the path to the file on the classpath * @return the string containing the subset of the file contents; never null but possibly empty */ protected String readLines(int startingLineNumber, String classpathResource) { try (InputStream stream = getClass().getClassLoader().getResourceAsStream(classpathResource);) { assertThat(stream).isNotNull(); StringBuilder sb = new StringBuilder(); AtomicInteger counter = new AtomicInteger(); IoUtil.readLines(stream, line -> { if (counter.incrementAndGet() >= startingLineNumber) sb.append(line); sb.append(System.lineSeparator()); }); return sb.toString(); } catch (IOException e) { fail("Unable to read '" + classpathResource + "'"); } assert false : "should never get here"; return null; }
/** * Read the contents of the supplied {@link InputStream}, replace all variables found in the content, and write the * result to a temporary file. * * @param stream the input stream containing zero or more {@link Strings#replaceVariables(String, java.util.function.Function) * variable expressions} * @param variables the variables * @return the temporary file that exists in the data directory * @throws IOException if there is a problem reading the input stream or writing to the temporary file */ protected static File replaceVariables(InputStream stream, AvailableVariables variables) throws IOException { File tmpFile = Testing.Files.createTestingFile(); try (OutputStream ostream = new FileOutputStream(tmpFile)) { IoUtil.readLines(stream, (line) -> { String newLine = Strings.replaceVariables(line, variables::variableForName); try { ostream.write(newLine.getBytes(StandardCharsets.UTF_8)); } catch (IOException e) { throw new RuntimeException("Error writing to file '" + tmpFile + "'", e); } }, StandardCharsets.UTF_8); } return tmpFile; } }