@Override public OutputStream newOutputStream(final Path path, final OpenOption... options) throws IllegalArgumentException, UnsupportedOperationException, IOException, SecurityException { return Files.newOutputStream(path, options); }
/** * @throws IllegalArgumentException * @throws IOException * @throws UnsupportedOperationException * @throws SecurityException * @see <a href="http://docs.oracle.com/javase/7/docs/api/java/nio/file/Files.html#newBufferedWriter(java.nio.file.Path, java.nio.charset.Charset, java.nio.file.OpenOption...)">Original JavaDoc</a> */ public static BufferedWriter newBufferedWriter(final Path path, final Charset cs, final OpenOption... options) throws IllegalArgumentException, IOException, UnsupportedOperationException, SecurityException { checkNotNull("path", path); checkNotNull("cs", cs); return new BufferedWriter(new OutputStreamWriter(newOutputStream(path, options), cs)); }
/** * @throws IllegalArgumentException * @throws IOException * @throws UnsupportedOperationException * @throws SecurityException * @see <a href="http://docs.oracle.com/javase/7/docs/api/java/nio/file/Files.html#newBufferedWriter(java.nio.file.Path, java.nio.charset.Charset, java.nio.file.OpenOption...)">Original JavaDoc</a> */ public static BufferedWriter newBufferedWriter(final Path path, final Charset cs, final OpenOption... options) throws IllegalArgumentException, IOException, UnsupportedOperationException, SecurityException { checkNotNull("path", path); checkNotNull("cs", cs); return new BufferedWriter(new OutputStreamWriter(newOutputStream(path, options), cs)); }
/** * @throws IllegalArgumentException * @throws IOException * @throws UnsupportedOperationException * @throws SecurityException * @see <a href="http://docs.oracle.com/javase/7/docs/api/java/nio/file/Files.html#newBufferedWriter(java.nio.file.Path, java.nio.charset.Charset, java.nio.file.OpenOption...)">Original JavaDoc</a> */ public static BufferedWriter newBufferedWriter(final Path path, final Charset cs, final OpenOption... options) throws IllegalArgumentException, IOException, UnsupportedOperationException, SecurityException { checkNotNull("path", path); checkNotNull("cs", cs); return new BufferedWriter(new OutputStreamWriter(newOutputStream(path, options), cs)); }
@Test public void newOutpurStreamNull() { assertThatThrownBy(() -> Files.newOutputStream(null)) .isInstanceOf(IllegalArgumentException.class) .hasMessage("Parameter named 'path' should be not null!"); }
public static Path write(final Path path, final byte[] bytes, final OpenOption... options) throws IOException, UnsupportedOperationException, SecurityException { checkNotNull("path", path); checkNotNull("bytes", bytes); final OutputStream out = newOutputStream(path, options); int len = bytes.length; int rem = len; try { while (rem > 0) { int n = (rem <= BUFFER_SIZE) ? rem : BUFFER_SIZE; out.write(bytes, (len - rem), n); rem -= n; } return path; } catch (java.io.IOException e) { throw new IOException(e); } finally { try { out.close(); } catch (java.io.IOException e) { throw new IOException(e); } } }
@Test public void newOutputStreamOnExistent() { final Path dir = newTempDir(); assertThatThrownBy(() -> Files.newOutputStream(dir)) .isInstanceOf(org.uberfire.java.nio.IOException.class) .hasMessage("Could not open output stream."); }
bytes); final OutputStream out = newOutputStream(path, options); int len = bytes.length;
@Override public org.uberfire.backend.vfs.Path exportDataSetExcel(DataSet dataSet) { try { SXSSFWorkbook wb = dataSetToWorkbook(dataSet); // Write workbook to Path String tempXlsFile = uuidGenerator.newUuid() + ".xlsx"; Path tempXlsPath = gitStorage.createTempFile(tempXlsFile); try (OutputStream os = Files.newOutputStream(tempXlsPath)) { wb.write(os); os.flush(); } // Dispose of temporary files backing this workbook on disk if (!wb.dispose()) { log.warn("Could not dispose of temporary file associated to data export!"); } return Paths.convert(tempXlsPath); } catch (Exception e) { throw exceptionManager.handleException(e); } }
@Override public org.uberfire.backend.vfs.Path exportDataSetExcel(DataSet dataSet) { try { SXSSFWorkbook wb = dataSetToWorkbook(dataSet); // Write workbook to Path String tempXlsFile = uuidGenerator.newUuid() + ".xlsx"; Path tempXlsPath = gitStorage.createTempFile(tempXlsFile); try (OutputStream os = Files.newOutputStream(tempXlsPath)) { wb.write(os); os.flush(); } // Dispose of temporary files backing this workbook on disk if (!wb.dispose()) { log.warn("Could not dispose of temporary file associated to data export!"); } return Paths.convert(tempXlsPath); } catch (Exception e) { throw exceptionManager.handleException(e); } }
@Test public void copyPath2OutNull1() { assertThatThrownBy(() -> { try (OutputStream os = Files.newOutputStream(newTempDir().resolve("my_new_file.txt"))) { Files.copy(null, os); } }) .isInstanceOf(IllegalArgumentException.class) .hasMessage("Parameter named 'source' should be not null!"); }
@Test public void newBufferedReader() throws IOException { final Path dir = newTempDir(); try (final OutputStream out = Files.newOutputStream(dir.resolve("file.txt"))) { assertThat(out).isNotNull(); out.write("content".getBytes()); } final BufferedReader reader = Files.newBufferedReader(dir.resolve("file.txt"), Charset.defaultCharset()); assertThat(reader).isNotNull(); assertThat(reader.readLine()).isNotNull().isEqualTo("content"); assertThat(reader.readLine()).isNull(); reader.close(); try { reader.read(); fail("can't read closed stream"); } catch (Exception ignored) { } }
@Test public void copyPath2Out() 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 OutputStream os = Files.newOutputStream(dir.resolve("my_new_file.txt"))) { Files.copy(dir.resolve("myfile.txt"), os); } 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 size() throws IOException { final Path path = Files.createTempFile("foo", "bar"); assertThat(Files.size(path)).isEqualTo(0L); final Path sourceFile = newTempDir().resolve("file.txt"); final OutputStream stream = Files.newOutputStream(sourceFile); stream.write('a'); stream.close(); assertThat(Files.size(sourceFile)).isEqualTo(1L); }
@Test public void copyPath2OutNotExists() { assertThatThrownBy(() -> { try (OutputStream os = Files.newOutputStream(newTempDir().resolve("my_new_file.txt"))) { Files.copy(newTempDir().resolve("myfile.txt"), os); } }).isInstanceOf(NoSuchFileException.class); }
@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"); } }
@Test public void moveFileInvalidSourceAndTarget() throws IOException { final Path source = newTempDir(); final Path dest = newTempDir().resolve("other"); final Path sourceFile = source.resolve("file.txt"); try (final OutputStream stream = Files.newOutputStream(sourceFile)) { stream.write('a'); } assertThatThrownBy(() -> Files.move(source, dest)) .isInstanceOf(DirectoryNotEmptyException.class); sourceFile.toFile().delete(); Files.copy(source, dest); assertThatThrownBy(() -> Files.move(source, dest)) .isInstanceOf(FileAlreadyExistsException.class); dest.toFile().delete(); source.toFile().delete(); assertThatThrownBy(() -> Files.move(source, dest)) .isInstanceOf(IllegalStateException.class) .hasMessage("Condition 'source must exist' is invalid!"); }
@Test public void copyFileInvalidSourceAndTarget() throws IOException { final Path source = newTempDir(); final Path dest = newTempDir().resolve("other"); final Path sourceFile = source.resolve("file.txt"); try (final OutputStream stream = Files.newOutputStream(sourceFile)) { stream.write('a'); } assertThatThrownBy(() -> Files.copy(source, dest)) .isInstanceOf(DirectoryNotEmptyException.class); sourceFile.toFile().delete(); Files.copy(source, dest); assertThatThrownBy(() -> Files.copy(source, dest)) .isInstanceOf(FileAlreadyExistsException.class); dest.toFile().delete(); source.toFile().delete(); assertThatThrownBy(() -> Files.copy(source, dest)) .isInstanceOf(IllegalStateException.class) .hasMessage("Condition 'source must exist' is invalid!"); }
@Test public void copyFile() throws IOException { final Path dir = newTempDir(); final Path source = dir.resolve("temp.txt"); final Path dest = dir.resolve("result.txt"); try (final OutputStream stream = Files.newOutputStream(source)) { stream.write('a'); } Files.copy(source, dest); assertThat(dest.toFile()).exists(); assertThat(source.toFile()).exists(); assertThat(dest.toFile().length()).isEqualTo(source.toFile().length()); }
@Test public void moveFile() throws IOException { final Path dir = newTempDir(); final Path source = dir.resolve("fileSource.txt"); final Path dest = dir.resolve("fileDest.txt"); try (final OutputStream stream = Files.newOutputStream(source)) { stream.write('a'); } long lenght = source.toFile().length(); Files.move(source, dest); assertThat(dest.toFile()).exists(); assertThat(source.toFile()).doesNotExist(); assertThat(dest.toFile().length()).isEqualTo(lenght); }