Skip to content

Commit

Permalink
Bugfix with breaking change at java.nio.ByteBuffer
Browse files Browse the repository at this point in the history
  • Loading branch information
bastie committed Sep 13, 2024
1 parent a1bb277 commit 3c82f1d
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 11 deletions.
51 changes: 42 additions & 9 deletions Sources/JavApi/nio/ByteBuffer.swift
Original file line number Diff line number Diff line change
Expand Up @@ -10,19 +10,52 @@ extension java.nio {
}

/// Abstract type for working with bytes
public protocol ByteBuffer {
associatedtype ByteBuffer: java.nio.ByteBuffer

public class ByteBuffer {

/// byte buffer
var content : [UInt8] { get set }

}

extension ByteBuffer {
internal var content : [UInt8] = []

public init (){}
/// Note: Implementation did not allocate space at this time
public static func allocate (_ count : Int) throws -> ByteBuffer {
guard count > -1 else {
throw Throwable.IllegalArgumentException("sorry for my failure master, I cant not create a ByteBuffer with negative count of bytes")
}
let buffer = ByteBuffer()
//buffer.content = Array(repeating: 0, count: count)
return buffer
}

/// Return the underlying byte buffer as byte array
/// - Returns byte buffer
public func array () throws -> [UInt8] {
open func array () throws -> [UInt8] {
return self.content
}

open func put (_ byte : UInt8) throws -> ByteBuffer {
self.content.append(byte)
return self
}

open func put (_ bytes : [UInt8]) throws -> ByteBuffer {
for byte in bytes {
_ = try self.put(byte)
}
return self
}

open func put (_ bytes : [UInt8], _ offset : Int, _ length : Int) throws -> ByteBuffer {
guard offset > -1 && offset < bytes.count else {
throw Throwable.IndexOutOufBoundsException(offset, "illegal start position \(offset)")
}
guard length > -1 && length <= (bytes.count-offset) else {
throw Throwable.IndexOutOufBoundsException(offset, "illegal length \(length)")
}
for i in offset..<(offset+length) {
try _ = self.put(bytes[i])
}

return self
}
}

4 changes: 2 additions & 2 deletions Sources/JavApi/util/zip/Checksum.swift
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ public protocol Checksum {
func update(_ b : Int)

/// Update the checksum with the given ByteBuffer content
func update (_ buffer : any java.nio.ByteBuffer)
func update (_ buffer : java.nio.ByteBuffer)

}

Expand All @@ -97,7 +97,7 @@ extension Checksum {
throw Throwable.NullPointerException()
}
}
public func update (_ buffer : any java.nio.ByteBuffer) {
public func update (_ buffer : java.nio.ByteBuffer) {
try! self.update(buffer.array())
}
}
Expand Down

0 comments on commit 3c82f1d

Please sign in to comment.