|
| 1 | +// |
| 2 | +// Asset.swift |
| 3 | +// SwiftAndroid |
| 4 | +// |
| 5 | +// Created by Alsey Coleman Miller on 2/27/26. |
| 6 | +// |
| 7 | + |
| 8 | +#if os(Android) |
| 9 | +import Android |
| 10 | +import AndroidNDK |
| 11 | +#endif |
| 12 | + |
| 13 | +/// A handle to an `AAsset`. |
| 14 | +/// |
| 15 | +/// Asset values own their pointer and close it during deinitialization. |
| 16 | +public struct Asset: ~Copyable { |
| 17 | + |
| 18 | + internal let pointer: OpaquePointer |
| 19 | + |
| 20 | + internal init(_ pointer: OpaquePointer) { |
| 21 | + self.pointer = pointer |
| 22 | + } |
| 23 | + |
| 24 | + deinit { |
| 25 | + AAsset_close(pointer) |
| 26 | + } |
| 27 | +} |
| 28 | + |
| 29 | +// MARK: - Properties |
| 30 | + |
| 31 | +public extension Asset { |
| 32 | + |
| 33 | + /// Total uncompressed length of this asset in bytes. |
| 34 | + var length: Int64 { |
| 35 | + AAsset_getLength64(pointer) |
| 36 | + } |
| 37 | + |
| 38 | + /// Remaining unread bytes in this asset. |
| 39 | + var remainingLength: Int64 { |
| 40 | + AAsset_getRemainingLength64(pointer) |
| 41 | + } |
| 42 | + |
| 43 | + /// Whether the asset is backed by a memory allocation. |
| 44 | + var isAllocated: Bool { |
| 45 | + AAsset_isAllocated(pointer) != 0 |
| 46 | + } |
| 47 | +} |
| 48 | + |
| 49 | +// MARK: - Methods |
| 50 | + |
| 51 | +public extension Asset { |
| 52 | + |
| 53 | + enum SeekOrigin: Int32, Sendable { |
| 54 | + case start = 0 |
| 55 | + case current = 1 |
| 56 | + case end = 2 |
| 57 | + } |
| 58 | + |
| 59 | + /// Reads up to `maxCount` bytes from the current cursor position. |
| 60 | + func read(maxCount: Int = 4096) throws(AndroidFileManagerError) -> [UInt8] { |
| 61 | + guard maxCount > 0 else { |
| 62 | + return [] |
| 63 | + } |
| 64 | + var bytes = [UInt8](repeating: 0, count: maxCount) |
| 65 | + let count = AAsset_read(pointer, &bytes, maxCount) |
| 66 | + guard count >= 0 else { |
| 67 | + throw .readAsset(count) |
| 68 | + } |
| 69 | + return Array(bytes.prefix(Int(count))) |
| 70 | + } |
| 71 | + |
| 72 | + /// Reads and returns all remaining bytes. |
| 73 | + func readAll(chunkSize: Int = 4096) throws(AndroidFileManagerError) -> [UInt8] { |
| 74 | + guard chunkSize > 0 else { |
| 75 | + return [] |
| 76 | + } |
| 77 | + var output = [UInt8]() |
| 78 | + output.reserveCapacity(Int(max(remainingLength, 0))) |
| 79 | + while true { |
| 80 | + let chunk = try read(maxCount: chunkSize) |
| 81 | + if chunk.isEmpty { |
| 82 | + break |
| 83 | + } |
| 84 | + output.append(contentsOf: chunk) |
| 85 | + } |
| 86 | + return output |
| 87 | + } |
| 88 | + |
| 89 | + /// Seeks the asset cursor and returns the new absolute position. |
| 90 | + /// |
| 91 | + /// - Parameters: |
| 92 | + /// - offset: Signed offset. |
| 93 | + /// - whence: `SEEK_SET`, `SEEK_CUR`, or `SEEK_END`. |
| 94 | + func seek(offset: Int64, whence: SeekOrigin = .start) throws(AndroidFileManagerError) -> Int64 { |
| 95 | + let result = AAsset_seek64(pointer, offset, whence.rawValue) |
| 96 | + guard result >= 0 else { |
| 97 | + throw .seekAsset(result) |
| 98 | + } |
| 99 | + return result |
| 100 | + } |
| 101 | + |
| 102 | + /// Returns a file descriptor and byte range when available. |
| 103 | + func openFileDescriptor() -> (fd: Int32, start: Int64, length: Int64)? { |
| 104 | + var start: Int64 = 0 |
| 105 | + var length: Int64 = 0 |
| 106 | + let fd = AAsset_openFileDescriptor64(pointer, &start, &length) |
| 107 | + guard fd >= 0 else { |
| 108 | + return nil |
| 109 | + } |
| 110 | + return (fd, start, length) |
| 111 | + } |
| 112 | + |
| 113 | + /// Returns an in-memory buffer, if this asset exposes one. |
| 114 | + func withUnsafeBufferPointer<T>( |
| 115 | + _ body: (UnsafeRawBufferPointer) throws -> T |
| 116 | + ) rethrows -> T? { |
| 117 | + guard let baseAddress = AAsset_getBuffer(pointer) else { |
| 118 | + return nil |
| 119 | + } |
| 120 | + let count = Int(max(length, 0)) |
| 121 | + let buffer = UnsafeRawBufferPointer(start: baseAddress, count: count) |
| 122 | + return try body(buffer) |
| 123 | + } |
| 124 | +} |
0 commit comments