Class XIO
public final class XIO extends Object
-
Nested Class Summary
Nested Classes Modifier and Type Class Description static class
XIO.unchecked
-
Constructor Summary
Constructors Constructor Description XIO()
-
Method Summary
Modifier and Type Method Description static String
addFileSuffix(String fileName, String fileSuffix)
static long
appendAll(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 passedByteBuffer
s even though they have remaining bytes left to be written.static long
appendAllGuaranteed(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 VarString
assemblePath(VarString vs, CharSequence... elements)
static String
buildFilePath(String... items)
static <C extends Closeable>
Cclose(C closable, Throwable suppressed)
static long
copyFile(FileChannel sourceChannel, long sourcePosition, long length, FileChannel targetChannel)
static long
copyFile(FileChannel sourceChannel, long sourcePosition, FileChannel targetChannel)
static long
copyFile(FileChannel sourceChannel, FileChannel targetChannel)
Alias fortargetChannel.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(Path sourceFile, Path targetFile, OpenOption... targetChannelOpenOptions)
UsesopenFileChannelReading(Path)
,openFileChannelWriting(Path,OpenOption...)
andcopyFile(FileChannel,FileChannel)
to copy the contents of the specifiedsourceFile
to the specifiedtargetFile
.
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>
PensureDirectory(P directory)
static <P extends Path>
PensureDirectoryAndFile(P file)
static <P extends Path>
PensureFile(P file)
static String
ensureNormalizedPathSeperators(String path)
static String
ensureTrailingSlash(String path)
static <P extends Path>
PensureWriteableFile(P file)
static boolean
exists(Path path)
static char
filePathSeparator()
static char
fileSuffixSeparator()
static String
getFileName(Path file)
static String
getFilePath(Path file)
Providing only the abused moreless debug-information method #toString is a horrible misconception since it conveys no clear message which string is desired.static String
getFilePrefix(String fileName)
static String
getFilePrefix(Path file)
static String
getFileSuffix(String fileName)
static String
getFileSuffix(Path file)
static boolean
hasNoFiles(Path directory)
static boolean
isDirectory(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 long
lastModified(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 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(String path)
static Path
Path(String... items)
static Path
Path(FileSystem fileSystem, String path)
static Path
Path(FileSystem fileSystem, String... items)
static Path
Path(Path parent, String... items)
static <T> T
performClosingOperation(FileChannel fileChannel, IoOperationSR<FileChannel,T> operation)
static ByteBuffer
read(FileChannel fileChannel)
static ByteBuffer
read(FileChannel fileChannel, long filePosition)
static ByteBuffer
read(FileChannel fileChannel, long filePosition, long length)
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(Path file)
static byte[]
read_bytes(FileChannel fileChannel)
static byte[]
read_bytes(Path file)
static <T> T
readOneShot(Path file, IoOperationSR<FileChannel,T> operation)
static String
readString(String filePath)
Extreme convenience method.static String
readString(String filePath, Charset charSet)
Extreme convenience method.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 long
size(Path file)
static String[]
splitPath(Path path)
static String
toAbsoluteNormalizedPath(Path file)
static void
truncate(Path file, long newSize)
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, Iterable<? extends ByteBuffer> buffers)
static long
write(FileChannel fileChannel, ByteBuffer buffer)
static long
write(Path file, byte[] bytes)
static long
write(Path file, String string)
static long
write(Path file, String string, Charset charset)
static long
write(Path file, ByteBuffer buffer)
static long
writeAppending(FileChannel fileChannel, ByteBuffer buffer)
static long
writeAppending(Path file, byte[] bytes)
static long
writeAppending(Path file, String string)
static long
writeAppending(Path file, String string, Charset charset)
static long
writeAppending(Path file, ByteBuffer buffer)
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, String string)
static long
writePositioned(Path file, long filePosition, String string, Charset charset)
static long
writePositioned(Path file, long filePosition, ByteBuffer buffer)
-
Constructor Details
-
XIO
public XIO()
-
-
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 passedparent
Path
inside the sameFileSystem
.Note that this is fundamentally different to
Path(String...)
orPaths.get(String,String...)
since those two end up usingFileSystems.getDefault()
, no matter theFileSystem
that the passed parentPath
is associated with. -
getFilePath
Providing only the abused moreless debug-information method #toString is a horrible misconception since it conveys no clear message which string is desired. Why are the JDK guys so horribly bad at designing clean Java code structure? Why?- Parameters:
file
-- Returns:
-
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
-- Returns:
- 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
-- Returns:
- 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:
IOException
FilePathException
-
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
-- Returns:
- 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
-- Returns:
- 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
- Throws:
IOException
-
write
- Throws:
IOException
-
write
- Throws:
IOException
-
write
- Throws:
IOException
-
truncate
- 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:
IOException
RuntimeException
-
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 passedByteBuffer
s 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
ByteBuffer
s was appended to the passedFileChannel
and 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
-
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 specifiedsourceFile
to 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
- theOpenOption
s (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)
-
copyFile
public static long copyFile(FileChannel sourceChannel, FileChannel targetChannel, long targetPosition) throws IOExceptionUses the sourceChannel's current position!- Parameters:
sourceChannel
-targetChannel
-targetPosition
-- Returns:
- 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
-