@Override protected InputStream openObjectStream(File object) throws IOException { return FileUtils.openInputStream(object); }
public byte[] readFileToByteArray(File file) throws IOException { InputStream in = org.apache.commons.io.FileUtils.openInputStream(file); try { return org.apache.commons.io.IOUtils.toByteArray(in); } finally { in.close(); } } }
public ReportIterator(File file, Parser<E> parser) { try { this.parser = parser; this.stream = FileUtils.openInputStream(file); } catch (IOException e) { throw Throwables.propagate(e); } }
/** * Reads the contents of a file into a byte array. * The file is always closed. * * @param file the file to read, must not be {@code null} * @return the file contents, never {@code null} * @throws IOException in case of an I/O error * @since 1.1 */ public static byte[] readFileToByteArray(final File file) throws IOException { try (InputStream in = openInputStream(file)) { final long fileLength = file.length(); // file.length() may return 0 for system-dependent entities, treat 0 as unknown length - see IO-453 return fileLength > 0 ? IOUtils.toByteArray(in, fileLength) : IOUtils.toByteArray(in); } }
public FileAndMd5(File file) { try (InputStream fis = FileUtils.openInputStream(file)) { this.file = file; this.md5 = DigestUtils.md5Hex(fis); } catch (IOException e) { throw new IllegalStateException("Fail to compute md5 of " + file, e); } }
private static String computeMd5(File file) { try (InputStream fis = new BufferedInputStream(FileUtils.openInputStream(file))) { return DigestUtils.md5Hex(fis); } catch (IOException e) { throw new IllegalStateException("Fail to compute md5 of " + file, e); } }
/** * Reads the contents of a file line by line to a List of Strings. * The file is always closed. * * @param file the file to read, must not be {@code null} * @param encoding the encoding to use, {@code null} means platform default * @return the list of Strings representing each line in the file, never {@code null} * @throws IOException in case of an I/O error * @since 2.3 */ public static List<String> readLines(final File file, final Charset encoding) throws IOException { try (InputStream in = openInputStream(file)) { return IOUtils.readLines(in, Charsets.toCharset(encoding)); } }
/** * Reads the contents of a file into a String. * The file is always closed. * * @param file the file to read, must not be {@code null} * @param encoding the encoding to use, {@code null} means platform default * @return the file contents, never {@code null} * @throws IOException in case of an I/O error * @since 2.3 */ public static String readFileToString(final File file, final Charset encoding) throws IOException { try (InputStream in = openInputStream(file)) { return IOUtils.toString(in, Charsets.toCharset(encoding)); } }
private void restore(DbSession dbSession, File backupFile, QProfileDto profile) { try (Reader reader = new InputStreamReader(FileUtils.openInputStream(backupFile), UTF_8)) { backuper.restore(dbSession, reader, profile); } catch (IOException e) { throw new IllegalStateException("Fail to create temporary backup file: " + backupFile, e); } } }
public CloseableIterator<O> traverse() { try { return new ObjectInputStreamIterator<>(FileUtils.openInputStream(file)); } catch (IOException e) { throw new IllegalStateException("Fail to traverse file: " + file, e); } }
public static void configure(File logbackFile, Map<String, String> substitutionVariables) { try { FileInputStream input = FileUtils.openInputStream(logbackFile); configure(input, substitutionVariables); } catch (IOException e) { throw new IllegalArgumentException("Fail to load the Logback configuration: " + logbackFile, e); } }
public List<Rule> parse(File file) { try (Reader reader = new InputStreamReader(FileUtils.openInputStream(file), UTF_8)) { return parse(reader); } catch (IOException e) { throw new SonarException("Fail to load the file: " + file, e); } }
private File unpack200(String pluginKey, File compressedFile) { LOGGER.debug("Unpacking plugin {}", pluginKey); File jar = newTempFile(); try (InputStream input = new GZIPInputStream(new BufferedInputStream(FileUtils.openInputStream(compressedFile))); JarOutputStream output = new JarOutputStream(new BufferedOutputStream(FileUtils.openOutputStream(jar)))) { Pack200.newUnpacker().unpack(input, output); } catch (IOException e) { throw new IllegalStateException(format("Fail to download plugin [%s]. Pack200 error.", pluginKey), e); } return jar; }
@Override public Optional<CloseableIterator<String>> readFileSource(int fileRef) { ensureInitialized(); File file = delegate.readFileSource(fileRef); if (file == null) { return Optional.empty(); } try { return Optional.of(new CloseableLineIterator(IOUtils.lineIterator(FileUtils.openInputStream(file), UTF_8))); } catch (IOException e) { throw new IllegalStateException("Fail to traverse file: " + file, e); } }
OpenedObject(FetchedFile<T> fetchedFile) throws IOException { this(fetchedFile.getObject(), FileUtils.openInputStream(fetchedFile.getFile()), fetchedFile.getResourceCloser()); }
@Test public void test_openInputStream_existsButIsDirectory() throws Exception { final File directory = new File(getTestDirectory(), "subdir"); directory.mkdirs(); try (FileInputStream in = FileUtils.openInputStream(directory)) { fail(); } catch (final IOException ioe) { // expected } }
FileAndMd5() throws IOException { this.file = temp.newFile(); FileUtils.write(this.file, RandomStringUtils.random(3)); try (InputStream fis = FileUtils.openInputStream(this.file)) { this.md5 = DigestUtils.md5Hex(fis); } catch (IOException e) { throw new IllegalStateException("Fail to compute md5 of " + this.file, e); } }
@Test public void test_openInputStream_notExists() throws Exception { final File directory = new File(getTestDirectory(), "test.txt"); try (FileInputStream in = FileUtils.openInputStream(directory)) { fail(); } catch (final IOException ioe) { // expected } }
@Test public void test_openInputStream_exists() throws Exception { final File file = new File(getTestDirectory(), "test.txt"); TestUtils.createLineBasedFile(file, new String[]{"Hello"}); try (FileInputStream in = FileUtils.openInputStream(file)) { assertEquals('H', in.read()); } }
@Test public void read_file_source() throws Exception { ScannerReportWriter writer = new ScannerReportWriter(dir); try (FileOutputStream outputStream = new FileOutputStream(writer.getSourceFile(1))) { IOUtils.write("line1\nline2", outputStream); } try (InputStream inputStream = FileUtils.openInputStream(underTest.readFileSource(1))) { assertThat(IOUtils.readLines(inputStream)).containsOnly("line1", "line2"); } }