Why is byte converted as Int8 and not UInt8?

A ran into this when creating a ByteArrayInputStream from a Data (Swift) instance, there's no easy conversion from Data to byte arrays in Java is there?

Java's byte type is explicitly defined to be a signed 8-bit integer.

It's likely that the java bridging support should provide some assistance here, but for the short-term, can you provide a snippet showing what you're trying to do, and maybe @glessard or @Andrew_Trick can help you with a more idiomatic way to do it?

1 Like

mmhmm, gotcha... the thing I'm trying to do is the following:

let data = try Data(contentsOf: URL(fileURLWithPath: "report.xls"))
let byteArray = ByteArrayInputStream(/* lost at how to convert here to [Int8]*/)
let workbook = try HSSFWorkbook(byteArray, true)

HSSFWorkbook supports an initializer with InputStream, thought ByteArrayInputStream would be the best option given I have a Data instance (for now it's a file, but later will be used in a network server).

Edit: tried data.map { Int8(bitPattern: $0) } and it worked, but not sure if there's a better way

We don't have built-in support for Data but we could look into some ability to use it with streams like that... Please file an issue and show all involved type signatures (on the Java side as well), please: GitHub - swiftlang/swift-java: Java interopability support for Swift What you did right now here is most likely the kinda best we can do right now.

E.g. JavaKit could include some helpers around such things.

1 Like

That is probably what I would have suggested, though obviously it's not great because of the extra copy. It seems like java.io.ByteArrayInputStream could readily take a RawSpan as input.

2 Likes

Created Feature Request: Better support for byte array conversions ยท Issue #288 ยท swiftlang/swift-java ยท GitHub to track this, thanks @ktoso !

4 Likes

Great, thank you :folded_hands: