Package one.microstream.files
Class XFiles
- java.lang.Object
-
- one.microstream.files.XFiles
-
public final class XFiles extends Object
-
-
Constructor Summary
Constructors Constructor Description XFiles()
-
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 File
buildFile(File parent, String... items)
static File
buildFile(String... items)
static String
buildFilePath(String... items)
static void
closeSilent(Closeable closable)
static void
copyFile(File in, File out)
static FileChannel
createReadingFileChannel(File file)
static FileChannel
createWritingFileChannel(File file)
static FileChannel
createWritingFileChannel(File file, boolean append)
static Charset
defaultCharset()
Alias forCharset.defaultCharset()
, which returns sometimes the one thing and sometimes another.static ByteBuffer
determineLastNonEmpty(ByteBuffer[] byteBuffers)
static File
ensureDirectory(File directory)
static File
ensureDirectoryAndFile(File file)
static File
ensureFile(File file)
static String
ensureNormalizedPathSeperators(String path)
static String
ensureTrailingSlash(String path)
static File
ensureWriteableFile(File file)
static void
mergeBinary(Iterable<File> sourceFiles, File targetFile)
static void
mergeBinary(Iterable<File> sourceFiles, File targetFile, Predicate<? super File> selector)
static void
move(File sourceFile, File targetFile)
static byte[]
readBytesFromFile(File file)
static char[]
readCharsFromFile(File file, Charset charset)
static char[]
readCharsFromFile(File file, Charset charset, Consumer<? super IOException> exceptionHandler)
static char[]
readCharsFromFileDefaultCharset(File file)
static char[]
readCharsFromFileDefaultCharset(File file, Consumer<? super IOException> exceptionHandler)
static char[]
readCharsFromFileUtf8(File file)
static char[]
readCharsFromFileUtf8(File file, Consumer<? super IOException> exceptionHandler)
static String
readStringFromFile(File file, Charset charset)
static <E extends Exception>
StringreadStringFromFile(File file, Charset charset, Function<IOException,E> exceptionMapper)
static String
readStringFromFileDefaultCharset(File file)
static Charset
standardCharset()
ReturnsStandardCharsets.UTF_8
, because any other one out there is nonsense as a standard.static Charset
utf8()
static void
writeStringToFile(File file, String string)
static void
writeStringToFile(File file, String string, Charset charset)
static <E extends Exception>
voidwriteStringToFile(File file, String string, Charset charset, Function<IOException,E> exceptionMapper)
static void
writeStringToFileDefaultCharset(File file, String string)
static void
writeStringToFileUtf8(File file, String string)
-
-
-
Method Detail
-
utf8
public static final Charset utf8()
-
standardCharset
public static final Charset standardCharset()
ReturnsStandardCharsets.UTF_8
, because any other one out there is nonsense as a standard.- Returns:
java.nio.charset.Charset.forName("UTF-8")
.
-
defaultCharset
public static final Charset defaultCharset()
Alias forCharset.defaultCharset()
, which returns sometimes the one thing and sometimes another. It is strongly advised to use a reliableCharset
querying method, likeutf8()
, which also is the only reasonable choice for thestandardCharset()
.- Returns:
Charset.defaultCharset()
-
ensureNormalizedPathSeperators
public static final String ensureNormalizedPathSeperators(String path)
-
ensureDirectory
public static final File ensureDirectory(File directory) throws DirectoryException
- Throws:
DirectoryException
-
ensureDirectoryAndFile
public static final File ensureDirectoryAndFile(File file) throws FileException
- Throws:
FileException
-
ensureFile
public static final File ensureFile(File file) throws FileException
- Throws:
FileException
-
ensureWriteableFile
public static final File ensureWriteableFile(File file) throws FileException
- Throws:
FileException
-
readCharsFromFileDefaultCharset
public static final char[] readCharsFromFileDefaultCharset(File file) throws IOException
- Throws:
IOException
-
readCharsFromFileDefaultCharset
public static final char[] readCharsFromFileDefaultCharset(File file, Consumer<? super IOException> exceptionHandler)
-
readCharsFromFile
public static final char[] readCharsFromFile(File file, Charset charset, Consumer<? super IOException> exceptionHandler)
-
readCharsFromFileUtf8
public static final char[] readCharsFromFileUtf8(File file)
-
readCharsFromFileUtf8
public static final char[] readCharsFromFileUtf8(File file, Consumer<? super IOException> exceptionHandler)
-
readStringFromFileDefaultCharset
public static final String readStringFromFileDefaultCharset(File file) throws IOException
- Throws:
IOException
-
readCharsFromFile
public static final char[] readCharsFromFile(File file, Charset charset) throws IOException
- Throws:
IOException
-
readStringFromFile
public static final String readStringFromFile(File file, Charset charset) throws IOException
- Throws:
IOException
-
readStringFromFile
public static final <E extends Exception> String readStringFromFile(File file, Charset charset, Function<IOException,E> exceptionMapper) throws E extends Exception
- Throws:
E extends Exception
-
readBytesFromFile
public static final byte[] readBytesFromFile(File file) throws IOException
- Throws:
IOException
-
writeStringToFile
public static final void writeStringToFile(File file, String string) throws IOException
- Throws:
IOException
-
writeStringToFileUtf8
public static final void writeStringToFileUtf8(File file, String string) throws IOException
- Throws:
IOException
-
writeStringToFileDefaultCharset
public static final void writeStringToFileDefaultCharset(File file, String string) throws IOException
- Throws:
IOException
-
writeStringToFile
public static final void writeStringToFile(File file, String string, Charset charset) throws IOException
- Throws:
IOException
-
writeStringToFile
public static final <E extends Exception> void writeStringToFile(File file, String string, Charset charset, Function<IOException,E> exceptionMapper) throws E extends Exception
- Throws:
E extends Exception
-
copyFile
public static void copyFile(File in, File out) throws IOException
- Throws:
IOException
-
createWritingFileChannel
public static final FileChannel createWritingFileChannel(File file) throws FileException, IOException
- Throws:
FileException
IOException
-
createWritingFileChannel
public static final FileChannel createWritingFileChannel(File file, boolean append) throws FileException, IOException
- Throws:
FileException
IOException
-
createReadingFileChannel
public static final FileChannel createReadingFileChannel(File file) throws IOException
- Throws:
IOException
-
mergeBinary
public static final void mergeBinary(Iterable<File> sourceFiles, File targetFile, Predicate<? super File> selector)
-
closeSilent
public static void closeSilent(Closeable closable)
-
move
public static void move(File sourceFile, File targetFile) throws IORuntimeException, RuntimeException
- Throws:
IORuntimeException
RuntimeException
-
determineLastNonEmpty
public static ByteBuffer determineLastNonEmpty(ByteBuffer[] byteBuffers)
-
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
-
-