Class XIO
public final class XIO extends Object
-
Nested Class Summary
Nested Classes Modifier and Type Class Description static classXIO.unchecked -
Method Summary
Modifier and Type Method Description static StringaddFileSuffix(String fileName, String fileSuffix)static longappendAll(FileChannel fileChannel, ByteBuffer[] byteBuffers)Sets the passedFileChannel's position to its current length and repeatedly callsFileChannel.write(ByteBuffer[])until the last non-empty buffer has no remaining bytes.
This is necessary because JDK'sFileChannel.write(ByteBuffer[])seems to arbitrarily stop processing the passedByteBuffers even though they have remaining bytes left to be written.static longappendAllGuaranteed(FileChannel fileChannel, ByteBuffer[] byteBuffers)CallsappendAll(FileChannel, ByteBuffer[]), thenFileChannel.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 VarStringassemblePath(VarString vs, CharSequence... elements)static StringbuildFilePath(String... items)static <C extends Closeable>
Cclose(C closable, Throwable suppressed)static longcopyFile(FileChannel sourceChannel, long sourcePosition, long length, FileChannel targetChannel)static longcopyFile(FileChannel sourceChannel, long sourcePosition, FileChannel targetChannel)static longcopyFile(FileChannel sourceChannel, FileChannel targetChannel)Alias fortargetChannel.transferFrom(sourceChannel, 0, sourceChannel.size()).
(Once again a method that is missing in the JDK.)static longcopyFile(FileChannel sourceChannel, FileChannel targetChannel, long targetPosition)Uses the sourceChannel's current position!static longcopyFile(FileChannel sourceChannel, FileChannel targetChannel, long targetPosition, long length)static longcopyFile(Path sourceFile, Path targetFile, OpenOption... targetChannelOpenOptions)UsesopenFileChannelReading(Path),openFileChannelWriting(Path, OpenOption...)andcopyFile(FileChannel, FileChannel)to copy the contents of the specifiedsourceFileto the specifiedtargetFile.
ensureDirectoryAndFile(Path)is intentionally NOT called in order to not swallow problems in the calling context's logic.static booleandelete(Path path)static ByteBufferdetermineLastNonEmpty(ByteBuffer[] byteBuffers)static <P extends Path>
PensureDirectory(P directory)static <P extends Path>
PensureDirectoryAndFile(P file)static <P extends Path>
PensureFile(P file)static StringensureNormalizedPathSeperators(String path)static StringensureTrailingSlash(String path)static <P extends Path>
PensureWriteableFile(P file)static booleanexists(Path path)static charfilePathSeparator()static charfileSuffixSeparator()static StringgetFileName(Path file)static StringgetFilePath(Path file)static StringgetFilePrefix(String fileName)static StringgetFilePrefix(Path file)static StringgetFileSuffix(String fileName)static StringgetFileSuffix(Path file)static booleanhasNoFiles(Path directory)static booleanisDirectory(Path path)static <C extends Consumer<? super Path>>
CiterateEntries(Path directory, C logic)Warning: this (because of using Files.newDirectoryStream) does some weird file opening/locking stuff.static <C extends Consumer<? super Path>>
CiterateEntries(Path directory, C logic, Predicate<? super Path> selector)Warning: this (because of using Files.newDirectoryStream) does some weird file opening/locking stuff.static longlastModified(Path file)static Path[]listEntries(Path directory)static <C extends Consumer<? super Path>>
ClistEntries(Path directory, C target)static <C extends Consumer<? super Path>>
ClistEntries(Path directory, C target, Predicate<? super Path> selector)static Path[]listEntries(Path directory, Predicate<? super Path> selector)static voidmergeBinary(Iterable<Path> sourceFiles, Path targetFile)static voidmergeBinary(Iterable<Path> sourceFiles, Path targetFile, Predicate<? super Path> selector)static voidmove(Path sourceFile, Path targetFile)static FileChannelopenFileChannel(Path file, OpenOption... options)static FileChannelopenFileChannelReading(Path file)static FileChannelopenFileChannelReading(Path file, OpenOption... options)static FileChannelopenFileChannelRW(Path file)static FileChannelopenFileChannelRW(Path file, OpenOption... options)static FileChannelopenFileChannelWriting(Path file)static FileChannelopenFileChannelWriting(Path file, OpenOption... options)static PathPath(String path)static PathPath(String... items)static PathPath(FileSystem fileSystem, String path)static PathPath(FileSystem fileSystem, String... items)static PathPath(Path parent, String... items)static <T> TperformClosingOperation(FileChannel fileChannel, IoOperationSR<FileChannel,T> operation)static ByteBufferread(FileChannel fileChannel)static ByteBufferread(FileChannel fileChannel, long filePosition)static ByteBufferread(FileChannel fileChannel, long filePosition, long length)static longread(FileChannel fileChannel, ByteBuffer targetBuffer)static longread(FileChannel fileChannel, ByteBuffer targetBuffer, long filePosition)static longread(FileChannel fileChannel, ByteBuffer targetBuffer, long filePosition, long length)static ByteBufferread(Path file)static byte[]read_bytes(FileChannel fileChannel)static byte[]read_bytes(Path file)static <T> TreadOneShot(Path file, IoOperationSR<FileChannel,T> operation)static StringreadString(String filePath)Extreme convenience method.static StringreadString(String filePath, Charset charSet)Extreme convenience method.static StringreadString(FileChannel fileChannel)static StringreadString(FileChannel fileChannel, Charset charSet)static StringreadString(Path file)static StringreadString(Path file, Charset charSet)static longsize(Path file)static String[]splitPath(Path path)static StringtoAbsoluteNormalizedPath(Path file)static voidtruncate(FileChannel fileChannel, long newSize)static voidtruncate(Path file, long newSize)Truncates the file to the given sizestatic voidunchecked(IoOperation operation)static <T> Tunchecked(IoOperationR<T> operation)static <S> voidunchecked(IoOperationS<S> operation, S subject)static <S, R> Runchecked(IoOperationSR<S,R> operation, S subject)static ByteBufferwrapInDirectByteBuffer(byte[] bytes)static longwrite(FileChannel fileChannel, Iterable<? extends ByteBuffer> buffers)static longwrite(FileChannel fileChannel, ByteBuffer buffer)static longwrite(Path file, byte[] bytes)Writes the contents of the array to the file.static longwrite(Path file, String string)Writes the contents of the string to the file.static longwrite(Path file, String string, Charset charset)Writes the contents of the string to the file.static longwrite(Path file, ByteBuffer buffer)Writes the contents of the buffer to the file.static longwriteAppending(FileChannel fileChannel, ByteBuffer buffer)static longwriteAppending(Path file, byte[] bytes)static longwriteAppending(Path file, String string)static longwriteAppending(Path file, String string, Charset charset)static longwriteAppending(Path file, ByteBuffer buffer)static <T> TwriteOneShot(Path file, IoOperationSR<FileChannel,T> operation)static longwritePositioned(FileChannel fileChannel, long filePosition, ByteBuffer buffer)static longwritePositioned(Path file, long filePosition, byte[] bytes)static longwritePositioned(Path file, long filePosition, String string)static longwritePositioned(Path file, long filePosition, String string, Charset charset)static longwritePositioned(Path file, long filePosition, ByteBuffer buffer)
-
Method Details
-
fileSuffixSeparator
public static char fileSuffixSeparator() -
filePathSeparator
public static char filePathSeparator() -
addFileSuffix
-
getFileSuffix
-
getFileSuffix
-
getFilePrefix
-
getFilePrefix
-
unchecked
- Throws:
IORuntimeException
-
unchecked
- Throws:
IORuntimeException
-
unchecked
- Throws:
IORuntimeException
-
unchecked
public static <S, R> R unchecked(IoOperationSR<S,R> operation, S subject) throws IORuntimeException- Throws:
IORuntimeException
-
close
public static final <C extends Closeable> C close(C closable, Throwable suppressed) throws IOException- Throws:
IOException
-
close
public static final <C extends AutoCloseable> C close(C closable, Throwable suppressed) throws Exception- Throws:
Exception
-
ensureNormalizedPathSeperators
-
ensureTrailingSlash
-
buildFilePath
-
Path
-
Path
-
Path
-
Path
-
Path
Creates a sub-path under the passedparentPathinside the sameFileSystem.Note that this is fundamentally different to
Path(String...)orPaths.get(String, String...)since those two end up usingFileSystems.getDefault(), no matter theFileSystemthat the passed parentPathis associated with. -
getFilePath
-
getFileName
-
splitPath
-
assemblePath
-
isDirectory
- Throws:
IOException
-
exists
- Throws:
IOException
-
size
- Throws:
IOException
-
delete
- Throws:
IOException
-
listEntries
- Throws:
IOException
-
listEntries
public static Path[] listEntries(Path directory, Predicate<? super Path> selector) throws IOException- Throws:
IOException
-
listEntries
public static <C extends Consumer<? super Path>> C listEntries(Path directory, C target) throws IOException- Throws:
IOException
-
listEntries
public static <C extends Consumer<? super Path>> C listEntries(Path directory, C target, Predicate<? super Path> selector) throws IOException- Throws:
IOException
-
iterateEntries
public static <C extends Consumer<? super Path>> C iterateEntries(Path directory, C logic) throws IOExceptionWarning: this (because of using Files.newDirectoryStream) does some weird file opening/locking stuff.Also see: https://stackoverflow.com/questions/48311252/a-bit-strange-behaviour-of-files-delete-and-files-deleteifexists
- Type Parameters:
C-- Parameters:
directory-logic-- Throws:
IOException
-
iterateEntries
public static <C extends Consumer<? super Path>> C iterateEntries(Path directory, C logic, Predicate<? super Path> selector) throws IOExceptionWarning: this (because of using Files.newDirectoryStream) does some weird file opening/locking stuff.Also see: https://stackoverflow.com/questions/48311252/a-bit-strange-behaviour-of-files-delete-and-files-deleteifexists
- Type Parameters:
C-- Parameters:
directory-logic-selector-- Throws:
IOException
-
hasNoFiles
- Throws:
IOException
-
lastModified
- Throws:
IOException
-
toAbsoluteNormalizedPath
-
ensureDirectory
- Throws:
IOException
-
ensureDirectoryAndFile
- Throws:
IOException
-
ensureFile
- Throws:
IOException
-
ensureWriteableFile
public static final <P extends Path> P ensureWriteableFile(P file) throws IOException, FilePathException- Throws:
IOExceptionFilePathException
-
openFileChannelReading
- Throws:
IOException
-
openFileChannelWriting
- Throws:
IOException
-
openFileChannelRW
- Throws:
IOException
-
openFileChannelReading
public static FileChannel openFileChannelReading(Path file, OpenOption... options) throws IOException- Throws:
IOException
-
openFileChannelWriting
public static FileChannel openFileChannelWriting(Path file, OpenOption... options) throws IOException- Throws:
IOException
-
openFileChannelRW
- Throws:
IOException
-
openFileChannel
- Throws:
IOException
-
readOneShot
public static final <T> T readOneShot(Path file, IoOperationSR<FileChannel,T> operation) throws IOException- Throws:
IOException
-
readString
Extreme convenience method. Normally, methods handling files should not accept file path strings, but only properly typed file instances likePath. However, for a convenience method, there is not much safety won writingreadString(Path("./my/path/myFile.txt")), only verbosity.
So when already using a convenience method, anyway, why not make it really convienent and accept file path strings right away?- Parameters:
filePath-- Throws:
IOException
-
readString
Extreme convenience method. Normally, methods handling files should not accept file path strings, but only properly typed file instances likePath. However, for a convenience method, there is not much safety won writingreadString(Path("./my/path/myFile.txt")), only verbosity.
So when already using a convenience method, anyway, why not make it really convienent and accept file path strings right away?- Parameters:
filePath-charSet-- Throws:
IOException
-
readString
- Throws:
IOException
-
readString
- Throws:
IOException
-
readString
- Throws:
IOException
-
readString
- Throws:
IOException
-
read_bytes
- Throws:
IOException
-
read_bytes
- Throws:
IOException
-
read
- Throws:
IOException
-
writeOneShot
public static final <T> T writeOneShot(Path file, IoOperationSR<FileChannel,T> operation) throws IOException- Throws:
IOException
-
write
Writes the contents of the string to the file.Attention: Internally this method opens a new FileChannel to operate on!
- Parameters:
file- the file to write tostring- the string to write- Returns:
- number of actual written bytes
- Throws:
IOException
-
write
Writes the contents of the string to the file.Attention: Internally this method opens a new FileChannel to operate on!
- Parameters:
file- the file to write tostring- the string to writecharset- the charset which is used to decode the string- Returns:
- number of actual written bytes
- Throws:
IOException
-
write
Writes the contents of the array to the file.Attention: Internally this method opens a new FileChannel to operate on!
- Parameters:
file- the file to write tobytes- the bytes to write- Returns:
- number of actual written bytes
- Throws:
IOException
-
write
Writes the contents of the buffer to the file.Attention: Internally this method opens a new FileChannel to operate on!
- Parameters:
file- the file to write tobuffer- the buffer to write- Returns:
- number of actual written bytes
- Throws:
IOException
-
truncate
Truncates the file to the given sizeAttention: Internally this method opens a new FileChannel to operate on!
- Parameters:
file- file to be truncatednewSize- new Size, must be zero or greater- Throws:
IOException
-
writePositioned
public static final long writePositioned(Path file, long filePosition, String string) throws IOException- Throws:
IOException
-
writePositioned
public static final long writePositioned(Path file, long filePosition, String string, Charset charset) throws IOException- Throws:
IOException
-
writePositioned
public static final long writePositioned(Path file, long filePosition, byte[] bytes) throws IOException- Throws:
IOException
-
writePositioned
public static long writePositioned(Path file, long filePosition, ByteBuffer buffer) throws IOException- Throws:
IOException
-
writeAppending
- Throws:
IOException
-
writeAppending
public static final long writeAppending(Path file, String string, Charset charset) throws IOException- Throws:
IOException
-
writeAppending
- Throws:
IOException
-
writeAppending
- Throws:
IOException
-
mergeBinary
-
mergeBinary
-
move
- Throws:
IOExceptionRuntimeException
-
determineLastNonEmpty
-
wrapInDirectByteBuffer
-
appendAll
Sets the passedFileChannel's position to its current length and repeatedly callsFileChannel.write(ByteBuffer[])until the last non-empty buffer has no remaining bytes.
This is necessary because JDK'sFileChannel.write(ByteBuffer[])seems to arbitrarily stop processing the passedByteBuffers 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... .
- Parameters:
fileChannel-byteBuffers-- Throws:
IOException
-
appendAllGuaranteed
public static long appendAllGuaranteed(FileChannel fileChannel, ByteBuffer[] byteBuffers) throws IOExceptionCallsappendAll(FileChannel, ByteBuffer[]), thenFileChannel.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 passedFileChanneland actually reached the physical file.- Parameters:
fileChannel-byteBuffers-- Throws:
IOException
-
writeAppending
- Throws:
IOException
-
writePositioned
public static long writePositioned(FileChannel fileChannel, long filePosition, ByteBuffer buffer) throws IOException- Throws:
IOException
-
write
- Throws:
IOException
-
write
public static long write(FileChannel fileChannel, Iterable<? extends ByteBuffer> buffers) throws IOException- Throws:
IOException
-
truncate
- Throws:
IOException
-
performClosingOperation
public static final <T> T performClosingOperation(FileChannel fileChannel, IoOperationSR<FileChannel,T> operation) throws IOException- Throws:
IOException
-
read
- Throws:
IOException
-
read
- Throws:
IOException
-
read
public static ByteBuffer read(FileChannel fileChannel, long filePosition, long length) throws IOException- Throws:
IOException
-
read
- Throws:
IOException
-
read
public static long read(FileChannel fileChannel, ByteBuffer targetBuffer, long filePosition, long length) throws IOException- Throws:
IOException
-
read
public static long read(FileChannel fileChannel, ByteBuffer targetBuffer, long filePosition) throws IOException- Throws:
IOException
-
copyFile
public static long copyFile(Path sourceFile, Path targetFile, OpenOption... targetChannelOpenOptions) throws IOExceptionUsesopenFileChannelReading(Path),openFileChannelWriting(Path, OpenOption...)andcopyFile(FileChannel, FileChannel)to copy the contents of the specifiedsourceFileto the specifiedtargetFile.
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 methodFiles.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. *
- Parameters:
sourceFile- the source file whose content shall be copied.targetFile- the target file that shall receive the copied content. Must already exist!targetChannelOpenOptions- theOpenOptions (seeStandardOpenOption) to be passed toopenFileChannelWriting(Path, OpenOption...). May be null / empty.- Returns:
- the number of bytes written by
FileChannel.transferFrom(java.nio.channels.ReadableByteChannel, long, long). - Throws:
IOException- See Also:
ensureFile(Path),ensureDirectoryAndFile(Path),StandardOpenOption,openFileChannelReading(Path),openFileChannelWriting(Path),copyFile(FileChannel, FileChannel),FileChannel.transferFrom(java.nio.channels.ReadableByteChannel, long, long)
-
copyFile
public static long copyFile(FileChannel sourceChannel, FileChannel targetChannel) throws IOExceptionAlias fortargetChannel.transferFrom(sourceChannel, 0, sourceChannel.size()).
(Once again a method that is missing in the JDK.)- Parameters:
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.- Returns:
- The number of bytes, possibly zero, that were actually transferred.
- Throws:
IOException- as specified byFileChannel.transferFrom(java.nio.channels.ReadableByteChannel, long, long)- See Also:
FileChannel.transferFrom(java.nio.channels.ReadableByteChannel, long, long),copyFile(Path, Path, OpenOption...)
-
copyFile
public static long copyFile(FileChannel sourceChannel, FileChannel targetChannel, long targetPosition) throws IOExceptionUses the sourceChannel's current position!- Parameters:
sourceChannel-targetChannel-targetPosition-- Throws:
IOException
-
copyFile
public static long copyFile(FileChannel sourceChannel, long sourcePosition, FileChannel targetChannel) throws IOException- Throws:
IOException
-
copyFile
public static long copyFile(FileChannel sourceChannel, long sourcePosition, long length, FileChannel targetChannel) throws IOException- Throws:
IOException
-
copyFile
public static long copyFile(FileChannel sourceChannel, FileChannel targetChannel, long targetPosition, long length) throws IOException- Throws:
IOException
-