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 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 FilebuildFile(File parent, String... items)static FilebuildFile(String... items)static StringbuildFilePath(String... items)static voidcloseSilent(Closeable closable)static voidcopyFile(File in, File out)static FileChannelcreateReadingFileChannel(File file)static FileChannelcreateWritingFileChannel(File file)static FileChannelcreateWritingFileChannel(File file, boolean append)static CharsetdefaultCharset()Alias forCharset.defaultCharset(), which returns sometimes the one thing and sometimes another.static ByteBufferdetermineLastNonEmpty(ByteBuffer[] byteBuffers)static FileensureDirectory(File directory)static FileensureDirectoryAndFile(File file)static FileensureFile(File file)static StringensureNormalizedPathSeperators(String path)static StringensureTrailingSlash(String path)static FileensureWriteableFile(File file)static voidmergeBinary(Iterable<File> sourceFiles, File targetFile)static voidmergeBinary(Iterable<File> sourceFiles, File targetFile, Predicate<? super File> selector)static voidmove(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 StringreadStringFromFile(File file, Charset charset)static <E extends Exception>
StringreadStringFromFile(File file, Charset charset, Function<IOException,E> exceptionMapper)static StringreadStringFromFileDefaultCharset(File file)static CharsetstandardCharset()ReturnsStandardCharsets.UTF_8, because any other one out there is nonsense as a standard.static Charsetutf8()static voidwriteStringToFile(File file, String string)static voidwriteStringToFile(File file, String string, Charset charset)static <E extends Exception>
voidwriteStringToFile(File file, String string, Charset charset, Function<IOException,E> exceptionMapper)static voidwriteStringToFileDefaultCharset(File file, String string)static voidwriteStringToFileUtf8(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 reliableCharsetquerying 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:
FileExceptionIOException
-
createWritingFileChannel
public static final FileChannel createWritingFileChannel(File file, boolean append) throws FileException, IOException
- Throws:
FileExceptionIOException
-
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:
IORuntimeExceptionRuntimeException
-
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 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 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
ByteBuffers was appended to the passedFileChanneland actually reached the physical file.- Parameters:
fileChannel-byteBuffers-- Throws:
IOException
-
-