/** * This implementation opens a Channel for the underlying file. * @see Files#newByteChannel(Path, OpenOption...) */ @Override public WritableByteChannel writableChannel() throws IOException { return Files.newByteChannel(this.path, StandardOpenOption.WRITE); }
/** * This implementation opens a Channel for the underlying file. * @see Files#newByteChannel(Path, OpenOption...) */ @Override public WritableByteChannel writableChannel() throws IOException { return Files.newByteChannel(this.path, StandardOpenOption.WRITE); }
@Override protected ByteChannel newStream() throws IOException { try { return Files.newByteChannel(path, StandardOpenOption.READ); } catch (NoSuchFileException e) { return null; } }
/** * This implementation opens a Channel for the underlying file. * @see Files#newByteChannel(Path, OpenOption...) */ @Override public ReadableByteChannel readableChannel() throws IOException { try { return Files.newByteChannel(this.path, StandardOpenOption.READ); } catch (NoSuchFileException ex) { throw new FileNotFoundException(ex.getMessage()); } }
/** * Opens file to write a 7z archive to. * * @param filename the file to write to * @throws IOException if opening the file fails */ public SevenZOutputFile(final File filename) throws IOException { this(Files.newByteChannel(filename.toPath(), EnumSet.of(StandardOpenOption.CREATE, StandardOpenOption.WRITE, StandardOpenOption.TRUNCATE_EXISTING))); }
@Override public byte[] read() throws IOException { try (SeekableByteChannel channel = Files.newByteChannel(path, options)) { return ByteStreams.toByteArray(Channels.newInputStream(channel), channel.size()); } }
/** * This implementation opens a Channel for the underlying file. * @see Files#newByteChannel(Path, OpenOption...) */ @Override public ReadableByteChannel readableChannel() throws IOException { try { return Files.newByteChannel(this.path, StandardOpenOption.READ); } catch (NoSuchFileException ex) { throw new FileNotFoundException(ex.getMessage()); } }
/** * Reads a file as 7z archive * * @param filename the file to read * @param password optional password if the archive is encrypted * @throws IOException if reading the archive fails * @since 1.17 */ public SevenZFile(final File filename, final char[] password) throws IOException { this(Files.newByteChannel(filename.toPath(), EnumSet.of(StandardOpenOption.READ)), filename.getAbsolutePath(), utf16Decode(password), true); }
@Override public byte[] read() throws IOException { try (SeekableByteChannel channel = Files.newByteChannel(path, options)) { return ByteStreams.toByteArray(Channels.newInputStream(channel), channel.size()); } }
@Override public byte[] read() throws IOException { try (SeekableByteChannel channel = Files.newByteChannel(path, options)) { return ByteStreams.toByteArray(Channels.newInputStream(channel), channel.size()); } }
@ExpectWarning("OS_OPEN_STREAM") void test4(File f, byte b) throws IOException { SeekableByteChannel c = Files.newByteChannel(Paths.get(""), StandardOpenOption.APPEND); c.position(); }
@ExpectWarning("OS_OPEN_STREAM") void test3(File f, byte b) throws IOException { SeekableByteChannel c = Files.newByteChannel(Paths.get("")); c.position(); }
@Override public byte[] read() throws IOException { try (SeekableByteChannel channel = Files.newByteChannel(path, options)) { return com.facebook.presto.jdbc.internal.guava.io.Files.readFile( Channels.newInputStream(channel), channel.size()); } }
/** Creates an IndexInput for the file with the given name. */ @Override public IndexInput openInput(String name, IOContext context) throws IOException { ensureOpen(); ensureCanRead(name); Path path = directory.resolve(name); SeekableByteChannel channel = Files.newByteChannel(path, StandardOpenOption.READ); return new SimpleFSIndexInput("SimpleFSIndexInput(path=\"" + path + "\")", channel, context); }
@Test public void testNewByteChannelRead_notFound() throws Exception { Path path = Paths.get(URI.create("gs://bucket/wednesday")); thrown.expect(NoSuchFileException.class); Files.newByteChannel(path); }
@Test public void readAndWriteByteChannel() throws Exception { Path source = Paths.get( DataBufferUtilsTests.class.getResource("DataBufferUtilsTests.txt").toURI()); Flux<DataBuffer> sourceFlux = DataBufferUtils .readByteChannel(() -> FileChannel.open(source, StandardOpenOption.READ), this.bufferFactory, 3); Path destination = Files.createTempFile("DataBufferUtilsTests", null); WritableByteChannel channel = Files.newByteChannel(destination, StandardOpenOption.WRITE); DataBufferUtils.write(sourceFlux, channel) .subscribe(DataBufferUtils.releaseConsumer(), throwable -> fail(throwable.getMessage()), () -> { try { String expected = String.join("", Files.readAllLines(source)); String result = String.join("", Files.readAllLines(destination)); assertEquals(expected, result); } catch (IOException e) { fail(e.getMessage()); } finally { DataBufferUtils.closeChannel(channel); } }); }
@Test public void writeWritableByteChannelCancel() throws Exception { DataBuffer foo = stringBuffer("foo"); DataBuffer bar = stringBuffer("bar"); Flux<DataBuffer> flux = Flux.just(foo, bar); WritableByteChannel channel = Files.newByteChannel(tempFile, StandardOpenOption.WRITE); Flux<DataBuffer> writeResult = DataBufferUtils.write(flux, channel); StepVerifier.create(writeResult, 1) .consumeNextWith(stringConsumer("foo")) .thenCancel() .verify(Duration.ofSeconds(5)); String result = String.join("", Files.readAllLines(tempFile)); assertEquals("foo", result); channel.close(); flux.subscribe(DataBufferUtils::release); }
@Test public void writeWritableByteChannel() throws Exception { DataBuffer foo = stringBuffer("foo"); DataBuffer bar = stringBuffer("bar"); DataBuffer baz = stringBuffer("baz"); DataBuffer qux = stringBuffer("qux"); Flux<DataBuffer> flux = Flux.just(foo, bar, baz, qux); WritableByteChannel channel = Files.newByteChannel(tempFile, StandardOpenOption.WRITE); Flux<DataBuffer> writeResult = DataBufferUtils.write(flux, channel); verifyWrittenData(writeResult); channel.close(); }
@Test public void writeWritableByteChannelErrorInFlux() throws Exception { DataBuffer foo = stringBuffer("foo"); DataBuffer bar = stringBuffer("bar"); Flux<DataBuffer> flux = Flux.just(foo, bar).concatWith(Flux.error(new RuntimeException())); WritableByteChannel channel = Files.newByteChannel(tempFile, StandardOpenOption.WRITE); Flux<DataBuffer> writeResult = DataBufferUtils.write(flux, channel); StepVerifier.create(writeResult) .consumeNextWith(stringConsumer("foo")) .consumeNextWith(stringConsumer("bar")) .expectError() .verify(Duration.ofSeconds(5)); String result = String.join("", Files.readAllLines(tempFile)); assertEquals("foobar", result); channel.close(); }
@Test public void testCloseWhilePrefetching() throws Exception { SeekableByteChannel chan = SeekableByteChannelPrefetcher.addPrefetcher(10, Files.newByteChannel(input)); // read just 1 byte, get the prefetching going ByteBuffer one = ByteBuffer.allocate(1); readFully(chan, one); // closing must not throw an exception, even if the prefetching // thread is active. chan.close(); }