List of usage examples for org.apache.commons.lang3 Validate exclusiveBetween
public static void exclusiveBetween(final double start, final double end, final double value, final String message)
From source file:com.thinkbiganalytics.discovery.util.ParserHelper.java
/** * Extracts the given number of rows from the file and returns a new reader for the sample. * This method protects memory in the case where a large file can be submitted with no delimiters. *///from w w w . java 2 s .c o m public static String extractSampleLines(InputStream is, Charset charset, int rows) throws IOException { StringWriter sw = new StringWriter(); Validate.notNull(is, "empty input stream"); Validate.notNull(charset, "charset cannot be null"); Validate.exclusiveBetween(1, MAX_ROWS, rows, "invalid number of sample rows"); // Sample the file in case there are no newlines StringWriter swBlock = new StringWriter(); IOUtils.copyLarge(new InputStreamReader(is, charset), swBlock, -1, MAX_CHARS); try (BufferedReader br = new BufferedReader(new StringReader(swBlock.toString()))) { IOUtils.closeQuietly(swBlock); String line = br.readLine(); int linesRead = 0; for (int i = 1; i <= rows && line != null; i++) { sw.write(line); sw.write("\n"); linesRead++; line = br.readLine(); } if (linesRead <= 1 && sw.toString().length() >= MAX_CHARS) { throw new IOException("Failed to detect newlines for sample file."); } } return sw.toString(); }