@Override public InputStream newInputStream(final Path path, final OpenOption... options) throws IllegalArgumentException, NoSuchFileException, UnsupportedOperationException, IOException, SecurityException { return Files.newInputStream(path, options); }
private Properties loadProperties(Path propertiesPath) { Properties prop = new Properties(); try (InputStream propFileInpStream = Files.newInputStream(propertiesPath)) { if (propFileInpStream == null) { logger.info("{} not available, skip to the next ConfigurationStrategy. \n", propertiesPath.toString()); valid = Boolean.FALSE; } else { prop.load(propFileInpStream); propFileInpStream.close(); valid = Boolean.TRUE; setUpValues(prop); } } catch (Exception e) { logger.error(e.getMessage()); } return prop; } }
/** * @throws IllegalArgumentException * @throws NoSuchFileException * @throws IOException * @throws SecurityException * @see <a href="http://docs.oracle.com/javase/7/docs/api/java/nio/file/Files.html#newBufferedReader(java.nio.file.Path, java.nio.charset.Charset)">Original JavaDoc</a> */ public static BufferedReader newBufferedReader(final Path path, final Charset cs) throws IllegalArgumentException, NoSuchFileException, IOException, SecurityException { checkNotNull("path", path); checkNotNull("cs", cs); return new BufferedReader(new InputStreamReader(newInputStream(path), cs.newDecoder())); }
/** * @throws IllegalArgumentException * @throws NoSuchFileException * @throws IOException * @throws SecurityException * @see <a href="http://docs.oracle.com/javase/7/docs/api/java/nio/file/Files.html#newBufferedReader(java.nio.file.Path, java.nio.charset.Charset)">Original JavaDoc</a> */ public static BufferedReader newBufferedReader(final Path path, final Charset cs) throws IllegalArgumentException, NoSuchFileException, IOException, SecurityException { checkNotNull("path", path); checkNotNull("cs", cs); return new BufferedReader(new InputStreamReader(newInputStream(path), cs.newDecoder())); }
/** * @throws IllegalArgumentException * @throws NoSuchFileException * @throws IOException * @throws SecurityException * @see <a href="http://docs.oracle.com/javase/7/docs/api/java/nio/file/Files.html#newBufferedReader(java.nio.file.Path, java.nio.charset.Charset)">Original JavaDoc</a> */ public static BufferedReader newBufferedReader(final Path path, final Charset cs) throws IllegalArgumentException, NoSuchFileException, IOException, SecurityException { checkNotNull("path", path); checkNotNull("cs", cs); return new BufferedReader(new InputStreamReader(newInputStream(path), cs.newDecoder())); }
public static byte[] readAllBytes(final Path path) throws IOException, OutOfMemoryError, SecurityException { long size = size(path); if (size > (long) Integer.MAX_VALUE) { throw new OutOfMemoryError("Required array size too large"); } final InputStream in = newInputStream(path); final ByteArrayOutputStream buffer = new ByteArrayOutputStream((int) size); int read; byte[] data = new byte[BUFFER_SIZE]; try { while ((read = in.read(data, 0, data.length)) != -1) { buffer.write(data, 0, read); } buffer.flush(); return buffer.toByteArray(); } catch (java.io.IOException e) { throw new IOException(e); } finally { try { in.close(); } catch (java.io.IOException e) { throw new IOException(e); } } }
@Test public void newInputStreamNull() { assertThatThrownBy(() -> Files.newInputStream(null)) .isInstanceOf(IllegalArgumentException.class) .hasMessage("Parameter named 'path' should be not null!"); }
public static long copy(final Path source, final OutputStream out) throws IOException, SecurityException { checkNotNull("source", source); checkNotNull("out", out); final InputStream in = newInputStream(source); try { return internalCopy(in, out); } finally { try { in.close(); } catch (java.io.IOException e) { throw new IOException(e); } } }
public static long copy(final Path source, final OutputStream out) throws IOException, SecurityException { checkNotNull("source", source); checkNotNull("out", out); final InputStream in = newInputStream(source); try { return internalCopy(in, out); } finally { try { in.close(); } catch (java.io.IOException e) { throw new IOException(e); } } }
public static long copy(final Path source, final OutputStream out) throws IOException, SecurityException { checkNotNull("source", source); checkNotNull("out", out); final InputStream in = newInputStream(source); try { return internalCopy(in, out); } finally { try { in.close(); } catch (java.io.IOException e) { throw new IOException(e); } } }
@Test public void newInputStreamNonExistent() { assertThatThrownBy(() -> Files.newInputStream(Paths.get("/path/to/some/file.txt"))) .isInstanceOf(NoSuchFileException.class); }
@Test public void newInputStreamOnDir() { final Path dir = newTempDir(); assertThatThrownBy(() -> Files.newInputStream(dir)) .isInstanceOf(NoSuchFileException.class); }
@Test public void copyIn2PathNull2() { assertThatThrownBy(() -> Files.copy(Files.newInputStream(Files.createTempFile("foo", "bar")), null, REPLACE_EXISTING)) .isInstanceOf(IllegalArgumentException.class) .hasMessage("Parameter named 'target' should be not null!"); }
@Test public void copyIn2PathNull4() { assertThatThrownBy(() -> Files.copy(Files.newInputStream(Files.createTempFile("foo", "bar")), newTempDir().resolve("my_new_file.txt"), new CopyOption[]{null})) .isInstanceOf(IllegalArgumentException.class) .hasMessage("Parameter named 'opt' should be not null!"); }
@Test public void copyIn2PathNull3() { assertThatThrownBy(() -> Files.copy(Files.newInputStream(Files.createTempFile("foo", "bar")), newTempDir().resolve("my_new_file.txt"), null)) .isInstanceOf(IllegalArgumentException.class) .hasMessage("Parameter named 'options' should be not null!"); }
@Test public void copyIn2PathInvalidOption() { assertThatThrownBy(() -> Files.copy(Files.newInputStream(Files.createTempFile("foo", "bar")), newTempDir().resolve("my_new_file.txt"), NOFOLLOW_LINKS)) .isInstanceOf(UnsupportedOperationException.class) .hasMessage("NOFOLLOW_LINKS not supported"); }
@Test public void copyIn2PathReplaceExistingNotExists() throws IOException { final Path dir = newTempDir(); try (final BufferedWriter writer = Files.newBufferedWriter(dir.resolve("myfile.txt"), Charset.defaultCharset())) { assertThat(writer).isNotNull(); writer.write("content"); } try (final InputStream is = Files.newInputStream(dir.resolve("myfile.txt"))) { Files.copy(is, dir.resolve("my_new_file.txt"), REPLACE_EXISTING); } try (final BufferedReader reader = Files.newBufferedReader(dir.resolve("my_new_file.txt"), Charset.defaultCharset())) { assertThat(reader).isNotNull(); assertThat(reader.readLine()).isEqualTo("content"); assertThat(reader.readLine()).isNull(); } }
@Test public void copyIn2Path() throws IOException { final Path dir = newTempDir(); try (final BufferedWriter writer = Files.newBufferedWriter(dir.resolve("myfile.txt"), Charset.defaultCharset())) { assertThat(writer).isNotNull(); writer.write("content"); } try (final InputStream is = Files.newInputStream(dir.resolve("myfile.txt"))) { Files.copy(is, dir.resolve("my_new_file.txt")); } try (final BufferedReader reader = Files.newBufferedReader(dir.resolve("my_new_file.txt"), Charset.defaultCharset())) { assertThat(reader).isNotNull(); assertThat(reader.readLine()).isNotNull().isEqualTo("content"); assertThat(reader.readLine()).isNull(); } }
@Test public void copyIn2PathReplaceExisting() throws IOException { final Path dir = newTempDir(); try (final BufferedWriter writer = Files.newBufferedWriter(dir.resolve("myfile.txt"), Charset.defaultCharset())) { assertThat(writer).isNotNull(); writer.write("content"); } try (final BufferedWriter writer2 = Files.newBufferedWriter(dir.resolve("my_new_file.txt"), Charset.defaultCharset())) { assertThat(writer2).isNotNull(); writer2.write("empty_content"); } try (final InputStream is = Files.newInputStream(dir.resolve("myfile.txt"))) { Files.copy(is, dir.resolve("my_new_file.txt"), REPLACE_EXISTING); } try (final BufferedReader reader = Files.newBufferedReader(dir.resolve("my_new_file.txt"), Charset.defaultCharset())) { assertThat(reader).isNotNull(); assertThat(reader.readLine()).isNotNull().isEqualTo("content"); assertThat(reader.readLine()).isNull(); } }
@Test public void newIOStreams() throws IOException { final Path dir = newTempDir(); try (final OutputStream out = Files.newOutputStream(dir.resolve("file.txt"))) { assertThat(out).isNotNull(); out.write("content".getBytes()); } try (final InputStream in = Files.newInputStream(dir.resolve("file.txt"))) { assertThat(in).isNotNull(); final StringBuilder sb = new StringBuilder(); while (true) { int i = in.read(); if (i == -1) { break; } sb.append((char) i); } assertThat(sb.toString()).isEqualTo("content"); } }