/** * This implementation opens a FileChannel for the underlying file. * @see java.nio.channels.FileChannel */ @Override public WritableByteChannel writableChannel() throws IOException { return FileChannel.open(this.filePath, StandardOpenOption.WRITE); }
/** * @param filePath the path of the file to write to. * @param charset the charset to use when writing characters. * @param forceOnFlush set to true if you want {@link FileChannel#force(boolean)} to be called on {@link #flush()}. * @param options the options for opening the file. * @throws IOException if something went wrong. */ FileChannelWriter(Path filePath, Charset charset, boolean forceOnFlush, boolean forceOnClose, OpenOption... options) throws IOException { this.charset = charset; this.forceOnFlush = forceOnFlush; this.forceOnClose = forceOnClose; channel = FileChannel.open(filePath, options); }
/** * This implementation opens a FileChannel for the underlying file. * @see java.nio.channels.FileChannel */ @Override public ReadableByteChannel readableChannel() throws IOException { try { return FileChannel.open(this.filePath, StandardOpenOption.READ); } catch (NoSuchFileException ex) { throw new FileNotFoundException(ex.getMessage()); } }
@Override public WritableByteChannel writableChannel() throws IOException { return FileChannel.open(getFile().toPath(), StandardOpenOption.WRITE); }
/** * Attempt to read a file as a string * @throws IOException */ public static String readFileAsString(String path, Charset charset) throws IOException { if (charset == null) charset = Charset.defaultCharset(); try (FileChannel fc = FileChannel.open(Paths.get(path))) { MappedByteBuffer bb = fc.map(FileChannel.MapMode.READ_ONLY, 0, fc.size()); return charset.decode(bb).toString(); } }
@Override public void truncate( File path, long size ) throws IOException { try ( FileChannel channel = FileChannel.open( path( path ) ) ) { channel.truncate( size ); } }
public static FileChannel open( Path path, OpenMode openMode ) throws IOException { return FileChannel.open( path, convertOpenMode( openMode ) ); }
/** * This implementation returns a FileChannel for the given URI-identified * resource, provided that it refers to a file in the file system. * @since 5.0 * @see #getFile() */ @Override public ReadableByteChannel readableChannel() throws IOException { try { // Try file system channel return FileChannel.open(getFile().toPath(), StandardOpenOption.READ); } catch (FileNotFoundException | NoSuchFileException ex) { // Fall back to InputStream adaptation in superclass return super.readableChannel(); } }
@Override protected void doCommandLine() throws Exception { if (remainingArgs.length < 1) { throw new HelpException("<core.xxxx> is required."); } Path core = new File(remainingArgs[0]).toPath(); try (SeekableByteChannel channel = FileChannel.open(core, StandardOpenOption.READ);) { List<Long> possibleOdexs = findPossibleOdexLocation(channel); extractDex(channel, possibleOdexs, core.getFileName().toString()); } }
@Override public WriteOutBytes makeWriteOutBytes() throws IOException { File file = File.createTempFile("filePeon", null, dir); FileChannel ch = FileChannel.open( file.toPath(), StandardOpenOption.READ, StandardOpenOption.WRITE ); closer.register(file::delete); closer.register(ch); return new FileWriteOutBytes(file, ch); }
LocalRecoverableFsDataOutputStream(LocalRecoverable resumable) throws IOException { this.targetFile = checkNotNull(resumable.targetFile()); this.tempFile = checkNotNull(resumable.tempFile()); if (!tempFile.exists()) { throw new FileNotFoundException("File Not Found: " + tempFile.getAbsolutePath()); } this.fileChannel = FileChannel.open(tempFile.toPath(), StandardOpenOption.WRITE, StandardOpenOption.APPEND); if (this.fileChannel.position() < resumable.offset()) { throw new IOException("Missing data in tmp file: " + tempFile.getAbsolutePath()); } this.fileChannel.truncate(resumable.offset()); this.fos = Channels.newOutputStream(fileChannel); }
LocalRecoverableFsDataOutputStream(File targetFile, File tempFile) throws IOException { this.targetFile = checkNotNull(targetFile); this.tempFile = checkNotNull(tempFile); this.fileChannel = FileChannel.open(tempFile.toPath(), StandardOpenOption.WRITE, StandardOpenOption.CREATE_NEW); this.fos = Channels.newOutputStream(fileChannel); }
public static void main( String[] args ) throws IOException { Path path = Paths.get( args[0] ); try ( FileChannel channel = FileChannel.open( path, StandardOpenOption.READ, StandardOpenOption.WRITE ); java.nio.channels.FileLock lock = channel.lock() ) { System.out.println( LOCKED_OUTPUT ); System.out.flush(); System.in.read(); } } }
@Override public Mono<Void> writeWith(Path file, long position, long count) { return doCommit(() -> Mono.defer(() -> { try (FileChannel source = FileChannel.open(file, StandardOpenOption.READ)) { StreamSinkChannel destination = this.exchange.getResponseChannel(); Channels.transferBlocking(destination, source, position, count); return Mono.empty(); } catch (IOException ex) { return Mono.error(ex); } })); }
/** * Checks that unmap doesn't throw exceptions. */ @Test public void testUnmap() throws Exception { File file = TestUtils.tempFile(); try (FileChannel channel = FileChannel.open(file.toPath())) { MappedByteBuffer map = channel.map(FileChannel.MapMode.READ_ONLY, 0, 0); MappedByteBuffers.unmap(file.getAbsolutePath(), map); } }
@Test public void readByteChannel() throws Exception { URI uri = this.resource.getURI(); Flux<DataBuffer> result = DataBufferUtils.readByteChannel(() -> FileChannel.open(Paths.get(uri), StandardOpenOption.READ), this.bufferFactory, 3); verifyReadData(result); }
@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 writableChannelNioFile() throws Exception { File file = temporaryFolder.newFile(); FileChannel fileChannel = FileChannel.open(file.toPath(), StandardOpenOption.WRITE); testWritableByteChannel(fileChannel); BufferedSource emitted = Okio.buffer(Okio.source(file)); assertEquals("defghijklmnopqrstuvw", emitted.readUtf8()); emitted.close(); }
@Test public void readableChannelNioFile() throws Exception { File file = temporaryFolder.newFile(); BufferedSink initialData = Okio.buffer(Okio.sink(file)); initialData.writeUtf8("abcdefghijklmnopqrstuvwxyz"); initialData.close(); FileChannel fileChannel = FileChannel.open(file.toPath(), StandardOpenOption.READ); testReadableByteChannel(fileChannel); }
@Test public void readByteChannelCancel() throws Exception { URI uri = this.resource.getURI(); Flux<DataBuffer> result = DataBufferUtils.readByteChannel(() -> FileChannel.open(Paths.get(uri), StandardOpenOption.READ), this.bufferFactory, 3); StepVerifier.create(result) .consumeNextWith(stringConsumer("foo")) .thenCancel() .verify(); }