Class XIO
- java.lang.Object
-
- one.microstream.io.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 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 String
buildFilePath(String... items)
static <C extends Closeable>
Cclose(C closable, Throwable suppressed)
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 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 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 XAddingCollection<? super Path>>
ClistEntries(Path directory, C target)
static <C extends XAddingCollection<? 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
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(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, long length)
static long
read(FileChannel fileChannel, ByteBuffer targetBuffer)
static long
read(FileChannel fileChannel, ByteBuffer targetBuffer, long filePosition, long length)
static ByteBuffer
read(Path file)
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(Path file)
static String
readString(Path file, Charset charSet)
static String
toAbsoluteNormalizedPath(Path file)
static void
unchecked(IoOperation operation)
static <T> T
unchecked(IoOperationR<T> operation)
static <S> void
unchecked(IoOperationS<S> operation, S subject)
static <S,R>
Runchecked(IoOperationSR<S,R> operation, S subject)
static ByteBuffer
wrapInDirectByteBuffer(byte[] bytes)
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)
-
-
-
Method Detail
-
unchecked
public static void unchecked(IoOperation operation) throws IORuntimeException
- Throws:
IORuntimeException
-
unchecked
public static <T> T unchecked(IoOperationR<T> operation) throws IORuntimeException
- Throws:
IORuntimeException
-
unchecked
public static <S> void unchecked(IoOperationS<S> operation, S subject) throws IORuntimeException
- 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
public static final String ensureNormalizedPathSeperators(String path)
-
Path
public static final Path Path(Path parent, String... items)
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
public 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. Why are the JDK guys so horribly bad at designing clean Java code structure? Why?- Parameters:
file
-- Returns:
-
isDirectory
public static boolean isDirectory(Path path) throws IOException
- Throws:
IOException
-
exists
public static boolean exists(Path path) throws IOException
- Throws:
IOException
-
delete
public static final boolean delete(Path path) throws IOException
- Throws:
IOException
-
listEntries
public static Path[] listEntries(Path directory) throws IOException
- Throws:
IOException
-
listEntries
public static Path[] listEntries(Path directory, Predicate<? super Path> selector) throws IOException
- Throws:
IOException
-
listEntries
public static <C extends XAddingCollection<? super Path>> C listEntries(Path directory, C target) throws IOException
- Throws:
IOException
-
listEntries
public static <C extends XAddingCollection<? 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 IOException
Warning: 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 IOException
Warning: 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
public static boolean hasNoFiles(Path directory) throws IOException
- Throws:
IOException
-
lastModified
public static final long lastModified(Path file) throws IOException
- Throws:
IOException
-
ensureDirectory
public static final <P extends Path> P ensureDirectory(P directory) throws IOException
- Throws:
IOException
-
ensureDirectoryAndFile
public static final <P extends Path> P ensureDirectoryAndFile(P file) throws IOException
- Throws:
IOException
-
ensureFile
public static final <P extends Path> P ensureFile(P file) throws IOException
- Throws:
IOException
-
ensureWriteableFile
public static final <P extends Path> P ensureWriteableFile(P file) throws IOException, FilePathException
- Throws:
IOException
FilePathException
-
openFileChannelReading
public static FileChannel openFileChannelReading(Path file) throws IOException
- Throws:
IOException
-
openFileChannelWriting
public static FileChannel openFileChannelWriting(Path file) throws IOException
- Throws:
IOException
-
openFileChannelRW
public static FileChannel openFileChannelRW(Path file) throws IOException
- 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
public static FileChannel openFileChannelRW(Path file, OpenOption... options) throws IOException
- Throws:
IOException
-
readOneShot
public static final <T> T readOneShot(Path file, IoOperationSR<FileChannel,T> operation) throws IOException
- Throws:
IOException
-
readString
public static String readString(String filePath) throws IOException
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
public static String readString(String filePath, Charset charSet) throws IOException
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
public static String readString(Path file) throws IOException
- Throws:
IOException
-
readString
public static String readString(Path file, Charset charSet) throws IOException
- Throws:
IOException
-
read_bytes
public static byte[] read_bytes(Path file) throws IOException
- Throws:
IOException
-
read
public static ByteBuffer read(Path file) throws IOException
- Throws:
IOException
-
writeOneShot
public static final <T> T writeOneShot(Path file, IoOperationSR<FileChannel,T> operation) throws IOException
- Throws:
IOException
-
write
public static final long write(Path file, String string) throws IOException
- Throws:
IOException
-
write
public static final long write(Path file, String string, Charset charset) throws IOException
- Throws:
IOException
-
write
public static final long write(Path file, byte[] bytes) throws IOException
- Throws:
IOException
-
write
public static long write(Path file, ByteBuffer buffer) throws IOException
- 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
public static final long writeAppending(Path file, String string) throws IOException
- Throws:
IOException
-
writeAppending
public static final long writeAppending(Path file, String string, Charset charset) throws IOException
- Throws:
IOException
-
writeAppending
public static final long writeAppending(Path file, byte[] bytes) throws IOException
- Throws:
IOException
-
writeAppending
public static long writeAppending(Path file, ByteBuffer buffer) throws IOException
- Throws:
IOException
-
mergeBinary
public static final void mergeBinary(Iterable<Path> sourceFiles, Path targetFile, Predicate<? super Path> selector)
-
move
public static void move(Path sourceFile, Path targetFile) throws IOException, RuntimeException
- Throws:
IOException
RuntimeException
-
determineLastNonEmpty
public static ByteBuffer determineLastNonEmpty(ByteBuffer[] byteBuffers)
-
wrapInDirectByteBuffer
public static final ByteBuffer wrapInDirectByteBuffer(byte[] bytes) throws IOException
- Throws:
IOException
-
appendAll
public static long appendAll(FileChannel fileChannel, ByteBuffer[] byteBuffers) throws IOException
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 IOException
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.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
public static long writeAppending(FileChannel fileChannel, ByteBuffer buffer) throws IOException
- Throws:
IOException
-
writePositioned
public static long writePositioned(FileChannel fileChannel, long filePosition, ByteBuffer buffer) throws IOException
- Throws:
IOException
-
write
public static long write(FileChannel fileChannel, ByteBuffer buffer) throws IOException
- Throws:
IOException
-
performClosingOperation
public static final <T> T performClosingOperation(FileChannel fileChannel, IoOperationSR<FileChannel,T> operation) throws IOException
- Throws:
IOException
-
read
public static ByteBuffer read(FileChannel fileChannel) throws IOException
- Throws:
IOException
-
read
public static ByteBuffer read(FileChannel fileChannel, long filePosition, long length) throws IOException
- Throws:
IOException
-
read
public static long read(FileChannel fileChannel, ByteBuffer targetBuffer) throws IOException
- Throws:
IOException
-
read
public static long read(FileChannel fileChannel, ByteBuffer targetBuffer, long filePosition, long length) throws IOException
- Throws:
IOException
-
-