public final class XIO extends Object
| Modifier and Type | Class and Description |
|---|---|
static class |
XIO.unchecked |
| Modifier and Type | Method and Description |
|---|---|
static String |
addFileSuffix(String fileName,
String fileSuffix) |
static long |
appendAll(FileChannel fileChannel,
ByteBuffer[] byteBuffers)
Sets the passed
FileChannel's position to its current length and repeatedly calls
FileChannel.write(ByteBuffer[]) until the last non-empty buffer has no remaining bytes.This is necessary because JDK's FileChannel.write(ByteBuffer[]) seems to arbitrarily stop processing
the passed ByteBuffers even though they have remaining bytes left to be written. |
static long |
appendAllGuaranteed(FileChannel fileChannel,
ByteBuffer[] byteBuffers)
Calls
appendAll(FileChannel, ByteBuffer[]), then FileChannel.force(boolean), then validates
if the actual new file size is really exactely what it should be based on old file size and the amount of bytes
written. |
static VarString |
assemblePath(VarString vs,
CharSequence... elements) |
static String |
buildFilePath(String... items) |
static <C extends Closeable> |
close(C closable,
Throwable suppressed) |
static <C extends AutoCloseable> |
close(C closable,
Throwable suppressed) |
static long |
copyFile(FileChannel sourceChannel,
FileChannel targetChannel)
Alias for
targetChannel.transferFrom(sourceChannel, 0, sourceChannel.size()).(Once again a method that is missing in the JDK.) |
static long |
copyFile(FileChannel sourceChannel,
FileChannel targetChannel,
long targetPosition)
Uses the sourceChannel's current position!
|
static long |
copyFile(FileChannel sourceChannel,
FileChannel targetChannel,
long targetPosition,
long length) |
static long |
copyFile(FileChannel sourceChannel,
long sourcePosition,
FileChannel targetChannel) |
static long |
copyFile(FileChannel sourceChannel,
long sourcePosition,
long length,
FileChannel targetChannel) |
static long |
copyFile(Path sourceFile,
Path targetFile,
OpenOption... targetChannelOpenOptions)
Uses
openFileChannelReading(Path), openFileChannelWriting(Path, OpenOption...)
and copyFile(FileChannel, FileChannel) to copy the contents of the specified sourceFile
to the specified targetFile.ensureDirectoryAndFile(Path) is intentionally NOT called in order to not swallow problems
in the calling context's logic. |
static boolean |
delete(Path path) |
static ByteBuffer |
determineLastNonEmpty(ByteBuffer[] byteBuffers) |
static <P extends Path> |
ensureDirectory(P directory) |
static <P extends Path> |
ensureDirectoryAndFile(P file) |
static <P extends Path> |
ensureFile(P file) |
static String |
ensureNormalizedPathSeperators(String path) |
static String |
ensureTrailingSlash(String path) |
static <P extends Path> |
ensureWriteableFile(P file) |
static boolean |
exists(Path path) |
static char |
filePathSeparator() |
static char |
fileSuffixSeparator() |
static String |
getFileName(Path file) |
static String |
getFilePath(Path file) |
static String |
getFilePrefix(Path file) |
static String |
getFilePrefix(String fileName) |
static String |
getFileSuffix(Path file) |
static String |
getFileSuffix(String fileName) |
static boolean |
hasNoFiles(Path directory) |
static boolean |
isDirectory(Path path) |
static <C extends Consumer<? super Path>> |
iterateEntries(Path directory,
C logic)
Warning: this (because of using Files.newDirectoryStream) does some weird file opening/locking stuff.
|
static <C extends Consumer<? super Path>> |
iterateEntries(Path directory,
C logic,
Predicate<? super Path> selector)
Warning: this (because of using Files.newDirectoryStream) does some weird file opening/locking stuff.
|
static long |
lastModified(Path file) |
static Path[] |
listEntries(Path directory) |
static <C extends Consumer<? super Path>> |
listEntries(Path directory,
C target) |
static <C extends Consumer<? super Path>> |
listEntries(Path directory,
C target,
Predicate<? super Path> selector) |
static Path[] |
listEntries(Path directory,
Predicate<? super Path> selector) |
static void |
mergeBinary(Iterable<Path> sourceFiles,
Path targetFile) |
static void |
mergeBinary(Iterable<Path> sourceFiles,
Path targetFile,
Predicate<? super Path> selector) |
static void |
move(Path sourceFile,
Path targetFile) |
static FileChannel |
openFileChannel(Path file,
OpenOption... options) |
static FileChannel |
openFileChannelReading(Path file) |
static FileChannel |
openFileChannelReading(Path file,
OpenOption... options) |
static FileChannel |
openFileChannelRW(Path file) |
static FileChannel |
openFileChannelRW(Path file,
OpenOption... options) |
static FileChannel |
openFileChannelWriting(Path file) |
static FileChannel |
openFileChannelWriting(Path file,
OpenOption... options) |
static Path |
Path(FileSystem fileSystem,
String... items) |
static Path |
Path(FileSystem fileSystem,
String path) |
static Path |
Path(Path parent,
String... items)
|
static Path |
Path(String... items) |
static Path |
Path(String path) |
static <T> T |
performClosingOperation(FileChannel fileChannel,
IoOperationSR<FileChannel,T> operation) |
static byte[] |
read_bytes(FileChannel fileChannel) |
static byte[] |
read_bytes(Path file) |
static ByteBuffer |
read(FileChannel fileChannel) |
static long |
read(FileChannel fileChannel,
ByteBuffer targetBuffer) |
static long |
read(FileChannel fileChannel,
ByteBuffer targetBuffer,
long filePosition) |
static long |
read(FileChannel fileChannel,
ByteBuffer targetBuffer,
long filePosition,
long length) |
static ByteBuffer |
read(FileChannel fileChannel,
long filePosition) |
static ByteBuffer |
read(FileChannel fileChannel,
long filePosition,
long length) |
static ByteBuffer |
read(Path file) |
static <T> T |
readOneShot(Path file,
IoOperationSR<FileChannel,T> operation) |
static String |
readString(FileChannel fileChannel) |
static String |
readString(FileChannel fileChannel,
Charset charSet) |
static String |
readString(Path file) |
static String |
readString(Path file,
Charset charSet) |
static String |
readString(String filePath)
Extreme convenience method.
|
static String |
readString(String filePath,
Charset charSet)
Extreme convenience method.
|
static long |
size(Path file) |
static String[] |
splitPath(Path path) |
static String |
toAbsoluteNormalizedPath(Path file) |
static void |
truncate(FileChannel fileChannel,
long newSize) |
static void |
truncate(Path file,
long newSize)
Truncates the file to the given size
|
static void |
unchecked(IoOperation operation) |
static <T> T |
unchecked(IoOperationR<T> operation) |
static <S> void |
unchecked(IoOperationS<S> operation,
S subject) |
static <S,R> R |
unchecked(IoOperationSR<S,R> operation,
S subject) |
static ByteBuffer |
wrapInDirectByteBuffer(byte[] bytes) |
static long |
write(FileChannel fileChannel,
ByteBuffer buffer) |
static long |
write(FileChannel fileChannel,
Iterable<? extends ByteBuffer> buffers) |
static long |
write(Path file,
byte[] bytes)
Writes the contents of the array to the file.
|
static long |
write(Path file,
ByteBuffer buffer)
Writes the contents of the buffer to the file.
|
static long |
write(Path file,
String string)
Writes the contents of the string to the file.
|
static long |
write(Path file,
String string,
Charset charset)
Writes the contents of the string to the file.
|
static long |
writeAppending(FileChannel fileChannel,
ByteBuffer buffer) |
static long |
writeAppending(Path file,
byte[] bytes) |
static long |
writeAppending(Path file,
ByteBuffer buffer) |
static long |
writeAppending(Path file,
String string) |
static long |
writeAppending(Path file,
String string,
Charset charset) |
static <T> T |
writeOneShot(Path file,
IoOperationSR<FileChannel,T> operation) |
static long |
writePositioned(FileChannel fileChannel,
long filePosition,
ByteBuffer buffer) |
static long |
writePositioned(Path file,
long filePosition,
byte[] bytes) |
static long |
writePositioned(Path file,
long filePosition,
ByteBuffer buffer) |
static long |
writePositioned(Path file,
long filePosition,
String string) |
static long |
writePositioned(Path file,
long filePosition,
String string,
Charset charset) |
public static char fileSuffixSeparator()
public static char filePathSeparator()
public static void unchecked(IoOperation operation) throws IORuntimeException
IORuntimeExceptionpublic static <T> T unchecked(IoOperationR<T> operation) throws IORuntimeException
IORuntimeExceptionpublic static <S> void unchecked(IoOperationS<S> operation, S subject) throws IORuntimeException
IORuntimeExceptionpublic static <S,R> R unchecked(IoOperationSR<S,R> operation, S subject) throws IORuntimeException
IORuntimeExceptionpublic static final <C extends Closeable> C close(C closable, Throwable suppressed) throws IOException
IOExceptionpublic static final <C extends AutoCloseable> C close(C closable, Throwable suppressed) throws Exception
Exceptionpublic static final String ensureNormalizedPathSeperators(String path)
public static final Path Path(FileSystem fileSystem, String path)
public static final Path Path(FileSystem fileSystem, String... items)
public static final Path Path(Path parent, String... items)
parent Path inside the same FileSystem.
Note that this is fundamentally different to Path(String...) or Paths.get(String, String...)
since those two end up using FileSystems.getDefault(), no matter the FileSystem that the passed
parent Path is associated with.
public static final VarString assemblePath(VarString vs, CharSequence... elements)
public static boolean isDirectory(Path path) throws IOException
IOExceptionpublic static boolean exists(Path path) throws IOException
IOExceptionpublic static long size(Path file) throws IOException
IOExceptionpublic static final boolean delete(Path path) throws IOException
IOExceptionpublic static Path[] listEntries(Path directory) throws IOException
IOExceptionpublic static Path[] listEntries(Path directory, Predicate<? super Path> selector) throws IOException
IOExceptionpublic static <C extends Consumer<? super Path>> C listEntries(Path directory, C target) throws IOException
IOExceptionpublic static <C extends Consumer<? super Path>> C listEntries(Path directory, C target, Predicate<? super Path> selector) throws IOException
IOExceptionpublic static <C extends Consumer<? super Path>> C iterateEntries(Path directory, C logic) throws IOException
Also see: https://stackoverflow.com/questions/48311252/a-bit-strange-behaviour-of-files-delete-and-files-deleteifexists
C - the consumer typedirectory - the directory to iteratelogic - the itaration logicIOException - if an IO error occurspublic static <C extends Consumer<? super Path>> C iterateEntries(Path directory, C logic, Predicate<? super Path> selector) throws IOException
Also see: https://stackoverflow.com/questions/48311252/a-bit-strange-behaviour-of-files-delete-and-files-deleteifexists
C - the consumer typedirectory - the directory to iteratelogic - the itaration logicselector - filter predicateIOException - if an IO error occurspublic static boolean hasNoFiles(Path directory) throws IOException
IOExceptionpublic static final long lastModified(Path file) throws IOException
IOExceptionpublic static final <P extends Path> P ensureDirectory(P directory) throws IOException
IOExceptionpublic static final <P extends Path> P ensureDirectoryAndFile(P file) throws IOException
IOExceptionpublic static final <P extends Path> P ensureFile(P file) throws IOException
IOExceptionpublic static final <P extends Path> P ensureWriteableFile(P file) throws IOException, FilePathException
IOExceptionFilePathExceptionpublic static FileChannel openFileChannelReading(Path file) throws IOException
IOExceptionpublic static FileChannel openFileChannelWriting(Path file) throws IOException
IOExceptionpublic static FileChannel openFileChannelRW(Path file) throws IOException
IOExceptionpublic static FileChannel openFileChannelReading(Path file, OpenOption... options) throws IOException
IOExceptionpublic static FileChannel openFileChannelWriting(Path file, OpenOption... options) throws IOException
IOExceptionpublic static FileChannel openFileChannelRW(Path file, OpenOption... options) throws IOException
IOExceptionpublic static FileChannel openFileChannel(Path file, OpenOption... options) throws IOException
IOExceptionpublic static final <T> T readOneShot(Path file, IoOperationSR<FileChannel,T> operation) throws IOException
IOExceptionpublic static String readString(String filePath) throws IOException
Path.
However, for a convenience method, there is not much safety won writing
readString(Path("./my/path/myFile.txt")), only verbosity.filePath - the source file pathIOException - if an IO error occurspublic static String readString(String filePath, Charset charSet) throws IOException
Path.
However, for a convenience method, there is not much safety won writing
readString(Path("./my/path/myFile.txt")), only verbosity.filePath - the source file pathcharSet - the charset to useIOException - if an IO error occurspublic static String readString(Path file) throws IOException
IOExceptionpublic static String readString(Path file, Charset charSet) throws IOException
IOExceptionpublic static String readString(FileChannel fileChannel) throws IOException
IOExceptionpublic static String readString(FileChannel fileChannel, Charset charSet) throws IOException
IOExceptionpublic static byte[] read_bytes(Path file) throws IOException
IOExceptionpublic static byte[] read_bytes(FileChannel fileChannel) throws IOException
IOExceptionpublic static ByteBuffer read(Path file) throws IOException
IOExceptionpublic static final <T> T writeOneShot(Path file, IoOperationSR<FileChannel,T> operation) throws IOException
IOExceptionpublic static final long write(Path file, String string) throws IOException
Attention: Internally this method opens a new FileChannel to operate on!
file - the file to write tostring - the string to writeIOException - if an IO error occurspublic static final long write(Path file, String string, Charset charset) throws IOException
Attention: Internally this method opens a new FileChannel to operate on!
file - the file to write tostring - the string to writecharset - the charset which is used to decode the stringIOException - if an IO error occurspublic static final long write(Path file, byte[] bytes) throws IOException
Attention: Internally this method opens a new FileChannel to operate on!
file - the file to write tobytes - the bytes to writeIOException - if an IO error occurspublic static long write(Path file, ByteBuffer buffer) throws IOException
Attention: Internally this method opens a new FileChannel to operate on!
file - the file to write tobuffer - the buffer to writeIOException - if an IO error occurspublic static void truncate(Path file, long newSize) throws IOException
Attention: Internally this method opens a new FileChannel to operate on!
file - file to be truncatednewSize - new Size, must be zero or greaterIOException - if an IO error occurspublic static final long writePositioned(Path file, long filePosition, String string) throws IOException
IOExceptionpublic static final long writePositioned(Path file, long filePosition, String string, Charset charset) throws IOException
IOExceptionpublic static final long writePositioned(Path file, long filePosition, byte[] bytes) throws IOException
IOExceptionpublic static long writePositioned(Path file, long filePosition, ByteBuffer buffer) throws IOException
IOExceptionpublic static final long writeAppending(Path file, String string) throws IOException
IOExceptionpublic static final long writeAppending(Path file, String string, Charset charset) throws IOException
IOExceptionpublic static final long writeAppending(Path file, byte[] bytes) throws IOException
IOExceptionpublic static long writeAppending(Path file, ByteBuffer buffer) throws IOException
IOExceptionpublic static final void mergeBinary(Iterable<Path> sourceFiles, Path targetFile, Predicate<? super Path> selector)
public static void move(Path sourceFile, Path targetFile) throws IOException, RuntimeException
IOExceptionRuntimeExceptionpublic static ByteBuffer determineLastNonEmpty(ByteBuffer[] byteBuffers)
public static final ByteBuffer wrapInDirectByteBuffer(byte[] bytes)
public static long appendAll(FileChannel fileChannel, ByteBuffer[] byteBuffers) throws IOException
FileChannel's position to its current length and repeatedly calls
FileChannel.write(ByteBuffer[]) until the last non-empty buffer has no remaining bytes.FileChannel.write(ByteBuffer[]) seems to arbitrarily stop processing
the passed ByteBuffers even though they have remaining bytes left to be written.
The reason for this behavior is unknown, but looking at countless other issues in the JDK code, one might guess... .
fileChannel - the target file channelbyteBuffers - the source data buffersIOException - if an IO error occurspublic static long appendAllGuaranteed(FileChannel fileChannel, ByteBuffer[] byteBuffers) throws IOException
appendAll(FileChannel, ByteBuffer[]), then FileChannel.force(boolean), then validates
if the actual new file size is really exactely what it should be based on old file size and the amount of bytes
written.
In short: this method "guarantees" that every byte contained in the passed ByteBuffers was appended
to the passed FileChannel and actually reached the physical file.
fileChannel - the target file channelbyteBuffers - the source data buffersIOException - if an IO error occurspublic static long writeAppending(FileChannel fileChannel, ByteBuffer buffer) throws IOException
IOExceptionpublic static long writePositioned(FileChannel fileChannel, long filePosition, ByteBuffer buffer) throws IOException
IOExceptionpublic static long write(FileChannel fileChannel, ByteBuffer buffer) throws IOException
IOExceptionpublic static long write(FileChannel fileChannel, Iterable<? extends ByteBuffer> buffers) throws IOException
IOExceptionpublic static void truncate(FileChannel fileChannel, long newSize) throws IOException
IOExceptionpublic static final <T> T performClosingOperation(FileChannel fileChannel, IoOperationSR<FileChannel,T> operation) throws IOException
IOExceptionpublic static ByteBuffer read(FileChannel fileChannel) throws IOException
IOExceptionpublic static ByteBuffer read(FileChannel fileChannel, long filePosition) throws IOException
IOExceptionpublic static ByteBuffer read(FileChannel fileChannel, long filePosition, long length) throws IOException
IOExceptionpublic static long read(FileChannel fileChannel, ByteBuffer targetBuffer) throws IOException
IOExceptionpublic static long read(FileChannel fileChannel, ByteBuffer targetBuffer, long filePosition, long length) throws IOException
IOExceptionpublic static long read(FileChannel fileChannel, ByteBuffer targetBuffer, long filePosition) throws IOException
IOExceptionpublic static long copyFile(Path sourceFile, Path targetFile, OpenOption... targetChannelOpenOptions) throws IOException
openFileChannelReading(Path), openFileChannelWriting(Path, OpenOption...)
and copyFile(FileChannel, FileChannel) to copy the contents of the specified sourceFile
to the specified targetFile.ensureDirectoryAndFile(Path) is intentionally NOT called in order to not swallow problems
in the calling context's logic.
Important note:
This method is a fix for the bugged JDK method Files.copy(Path, Path, java.nio.file.CopyOption...),
which throws an incorrect exception about another process having locked "the file" (without specifying
which one it means) if the process owns a lock on the source file. Since this means the process locks
itself out of using the source file if it has secured the source file for its exclusive use, this is nothing
but a bug. As a consequence, the JDK method cannot be used if a file is locked and should generally not be
trusted. Once again, one has to write a proper solution by oneself since the quality in the JDK code is just
too low.
For any special needs like copying from and/or to a position and/or only a part of the file and/or using custom OpenOptions and/or modifying file timestamps and or performing pre- or post-actions, it is strongly suggested to write a custom tailored version of a copying method. Covering all conceivable cases would result in an overly complicated one-size-fits-all attempt and we all know how well those work in practice.
sourceFile - the source file whose content shall be copied.targetFile - the target file that shall receive the copied content. Must already exist!targetChannelOpenOptions - the OpenOptions (see StandardOpenOption) to be passed to
openFileChannelWriting(Path, OpenOption...). May be null / empty.FileChannel.transferFrom(java.nio.channels.ReadableByteChannel, long, long).IOException - if an IO error occursensureFile(Path),
ensureDirectoryAndFile(Path),
StandardOpenOption,
openFileChannelReading(Path),
openFileChannelWriting(Path),
copyFile(FileChannel, FileChannel),
FileChannel.transferFrom(java.nio.channels.ReadableByteChannel, long, long)public static long copyFile(FileChannel sourceChannel, FileChannel targetChannel) throws IOException
targetChannel.transferFrom(sourceChannel, 0, sourceChannel.size()).sourceChannel - an open and readable channel to the source file whose content shall be copied.targetChannel - an open and writeable channel to the target file that shall receive the copied content.IOException - as specified by FileChannel.transferFrom(java.nio.channels.ReadableByteChannel, long, long)FileChannel.transferFrom(java.nio.channels.ReadableByteChannel, long, long),
copyFile(Path, Path, OpenOption...)public static long copyFile(FileChannel sourceChannel, FileChannel targetChannel, long targetPosition) throws IOException
sourceChannel - an open and readable channel to the source file whose content shall be copied.targetChannel - an open and writeable channel to the target file that shall receive the copied content.targetPosition - the position to write to in the target channelIOException - as specified by FileChannel.transferFrom(java.nio.channels.ReadableByteChannel, long, long)public static long copyFile(FileChannel sourceChannel, long sourcePosition, FileChannel targetChannel) throws IOException
IOExceptionpublic static long copyFile(FileChannel sourceChannel, long sourcePosition, long length, FileChannel targetChannel) throws IOException
IOExceptionpublic static long copyFile(FileChannel sourceChannel, FileChannel targetChannel, long targetPosition, long length) throws IOException
IOExceptionCopyright © 2022 MicroStream Software. All rights reserved.