### File System Operations with File Class in Kotlin Source: https://github.com/skolson/kmpio/blob/master/ReadMe.md This example showcases various file system operations using the `File` class, including listing files in a directory (shallow and deep), resolving subdirectories, checking file existence, and accessing properties like name, extension, path, size, and last modified time. It demonstrates common file and directory management tasks. ```Kotlin val directory = File("/var/tmp/anydir") val shallowList = directory.listFiles() // lists files in sub-directories in directory, no subdirectory content val deepList = directory.listFilesTree() // lists ALL files in directory and all of its sub-directories val subDirectory = directory.resolve("anysubdir") // finds a subdirectory of directory, makes it if it doesn't exist if (subDirectory.exists) { File(subDirectory, "anyfile.txt").apply { /* some of the properties available name: String nameWithoutExtension: String extension: String path: String // include file name directoryPath: String // just the path of the directory containing this file isDirectory: Boolean listNames: List exists: Boolean size: ULong lastModified: LocalDateTime // internal time converted to kotlinx equivalent using system TimeZone */ } } ``` -------------------------------- ### Base64 Encoding and Decoding in Kotlin Source: https://github.com/skolson/kmpio/blob/master/ReadMe.md This snippet illustrates the basic usage of the `Base64` class for encoding a UTF-8 string into a Base64-encoded byte array and subsequently decoding that byte array back into a string. It provides a straightforward example of Base64 operations. ```Kotlin Base64().apply { val bytes = encode("any UTF-8 text) // resulting bytes are base-64 encoded val text = decode(bytes) } ``` -------------------------------- ### Reading and Writing Text Files with TextFile in Kotlin Source: https://github.com/skolson/kmpio/blob/master/ReadMe.md This example illustrates how to create a text file with a specified character encoding (ISO-8859-1), write a line of text to it, and then read that line back. It highlights the `TextFile` class's functionality for handling text content with automatic encoding/decoding, similar to `RawFile` but tailored for text. ```Kotlin val fil = File(subDir, "Text${charset.charset.charsetName}.txt") TextFile( fil, Charset(Charsets.Iso8859_1), FileMode.Write ).use { it.writeLine(textContent) } TextFile( fil, Charset(Charsets.Iso8859_1) ).use { val textContent = it.readLine() } ``` -------------------------------- ### Encoding and Decoding Strings with Charset in Kotlin Source: https://github.com/skolson/kmpio/blob/master/ReadMe.md This example demonstrates how to use the `Charset` class to encode a string into a byte array and then decode the byte array back into a string, specifying the character set (e.g., UTF-16LE). It shows the basic workflow for character set conversions. ```Kotlin val utf16 = Charset(Charsets.Utf16LE) // See Charsets enum for supported character sets val test = "anytext" val bytes = utf16.encode(test) assertEquals(test, utf16.decode(bytes) ``` -------------------------------- ### Creating a Zip File with KMP-IO in Kotlin Source: https://github.com/skolson/kmpio/blob/master/ReadMe.md This snippet demonstrates how to create a new zip file, add individual files, add a directory tree with a filter, and add an entry with custom data and metadata using the KMP-IO library. It also shows how to set Zip64 support and custom last modified times. ```Kotlin val dir = File("/anydir") val sourceDirectory = File("/anyPathToaDirectoryTreeToZip") val zip = File(dir, "TestFile.zip") val dataFile = File("SomethingToZip.dat") ZipFile(zip, FileMode.Write).use { it.isZip64 = true // default is false, but true doesn't require large content or LOTS of entries, just supports them if needed. it.zipFile(dataFile) it.zipFile(dataFile, "Copy${dataFile.name}") it.zipDirectory(sourceDirectory, shallow = false, ) { name -> name.endsWith(".txt") } ZipEntry( name = "someData.dat", comment = "Anything legal here, up to Int.MAX_VALUE length if isZip64 = false", lastModTime = LocalDateTime(year = 2022, monthNumber = 1, dayOfMonth = 1, hour = 12 ).apply { var count = 0 val dataLine = Charset(Charsets.Utf16Le).encode("Any old stuff from anywhere") it.addEntry(this) { // this lambda will be called repeatedly until it returns an empty ByteArray() // provide uncompressed data in ByteArray instances of any size until done if (count++ < 100) dataline else ByteArray(0) } } } ``` -------------------------------- ### Initializing and Accessing BitSet in Kotlin Source: https://github.com/skolson/kmpio/blob/master/ReadMe.md This snippet demonstrates how to initialize a BitSet instance in Kotlin, either from an existing ByteArray or by specifying the total number of bits. It also illustrates how to define a custom property with getter and setter to conveniently access and modify individual bits within the BitSet. ```Kotlin // Create a 2 byte BitSet, can be any number of bits val bitset = BitSet(byteArrayOf(0x80, 0x80) // or val bitset2 = BitSet(16) var isBit0 get() = bitSet[0] set(value) { bitSet[0] = value } ``` -------------------------------- ### Reading and Writing Binary Files with RawFile in Kotlin Source: https://github.com/skolson/kmpio/blob/master/ReadMe.md This snippet demonstrates how to create and write binary data to a `RawFile` using a `ByteBuffer`, and then read from it. It also shows how to change the file position for random access, allowing specific portions of the file to be read, such as the last 12 bytes. ```Kotlin val buf = ByteBuffer(4096) val fil = File(subDir, "Somefile.dat") RawFile(fil, FileMode.Write).use { it.write(ByteBuffer(hexContent)) } RawFile(fil).use { var count = it.read(buf) it.position = it.size - 12u // position to last 12 bytes of file buf.clear() count = it.read(buf) // read last 12 bytes } ``` -------------------------------- ### Reading and Filtering Zip File Entries with KMP-IO in Kotlin Source: https://github.com/skolson/kmpio/blob/master/ReadMe.md This snippet illustrates how to open an existing zip file, access its entries, filter them (e.g., by file extension), and read the uncompressed content of a specific entry using a callback mechanism. It also mentions the availability of extra record data. ```Kotlin ZipFile(File("anything.zip").use { zip -> // zip.map is a map keyed by name of all ZipEntry instances // zip.entries is the map values as a List zip.entries .filter { it.name.contains(".txt"} .apply { val extraRecords: List = it.extras zip.readEntry(it) { _, content, count -> // this block is called repeatedly until all uncompressed data has been passed, then ends // first arg is ZipEntry, second is a ByteArray of content, third arg is byteCount, will be same as content size. // Max content size for any one call is zip.bufferSize } } } ``` -------------------------------- ### Encoding and Decoding Basic Types with ByteBuffer in Kotlin Source: https://github.com/skolson/kmpio/blob/master/ReadMe.md This snippet demonstrates how to initialize ByteBuffer with different byte orders (little-endian and big-endian), encode various primitive types (byte, short, int, long, ulong, uint, float, double) and strings, retrieve the encoded bytes, and then decode values back from the buffer. It highlights the use of `flip()` to reset the buffer's position for reading and `getBytes()` to extract the data. ```Kotlin val littleEndianBuf = ByteBuffer(ByteArray(1024)) // little endian is default val bigEndianBuf = ByteBuffer(ByteArray(1024), order = ByteOrder.BigEndian) val test = "anytext" val utf8 = Charset(Charsets.Utf8) // encode various basic types into the ByteBuffer, littleEndianBuf.apply { byte = 0x1.toByte() // position = 1 here, limit is 1024, remaining is 1023 short = 0x0101 // position = 3 here, limit is 1024, remaining is 1021 int = 0x01010101 // position = 7 here, limit is 1024, remaining is 1017 long = 0x0101010101010101L ulong = 0x0101010101010101UL uint = 0x0101 float = 1.01f double = 1.01 put(utf8.encode(test)) val x = remaining // number of bytes left before limit is reached flip() // sets position to 0, limit to former position. var encodedBytes = getBytes() // little endian encoded byte array with data above } bigEndianBuf.apply { byte = 0x1 // position = 1 here, limit is 1024, remaining is 1023 short = 0x0101 // position = 3 here, limit is 1024, remaining is 1021 int = 0x01010101 // position = 7 here, limit is 1024, remaining is 1017 long = 0x0101010101010101L float = 1.01f double = 1.01 put(utf8.encode(test)) ... flip() // sets position to 0, limit to former position. var encodedBytes = getBytes() // big endian encoded byte array with data above } littleEndianBuf.apply { assertEquals(0x1.toByte(), byte) position = 7 assertEquals(0x0101010101010101L, long) assertEquals(0x0101010101010101UL, ulong) } ``` -------------------------------- ### Adding KmpIO Gradle Dependency Source: https://github.com/skolson/kmpio/blob/master/ReadMe.md This snippet demonstrates how to add the KmpIO library as a dependency in a Gradle build script. It assumes that mavenCentral() is configured as a repository in the project's build.gradle scripts, allowing Gradle to resolve the library from the specified coordinates. ```Gradle dependencies { implementation("io.github.skolson:kmp-io:0.1.5") } ``` === COMPLETE CONTENT === This response contains all available snippets from this library. No additional content exists. Do not make further requests.