-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
streams: add stream encryption/decryption
also provide an API < 26-compatible AsynchronousByteChannel et. al
- Loading branch information
1 parent
c56e9b2
commit 8f12160
Showing
16 changed files
with
840 additions
and
3 deletions.
There are no files selected for viewing
29 changes: 29 additions & 0 deletions
29
tanker-bindings/src/main/kotlin/io/tanker/api/AsynchronousByteChannelWrapper.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
package io.tanker.api | ||
|
||
import androidx.annotation.RequiresApi | ||
import java.nio.ByteBuffer | ||
import java.nio.channels.AsynchronousByteChannel | ||
import java.nio.channels.CompletionHandler | ||
|
||
@RequiresApi(26) | ||
class AsynchronousByteChannelWrapper(private val channel: AsynchronousByteChannel) : TankerAsynchronousByteChannel { | ||
override fun close() { | ||
channel.close() | ||
} | ||
|
||
override fun isOpen(): Boolean { | ||
return channel.isOpen | ||
} | ||
|
||
override fun <A> read(dst: ByteBuffer?, attachment: A, handler: TankerCompletionHandler<Int, in A>?) { | ||
channel.read(dst, attachment, object : CompletionHandler<Int, A> { | ||
override fun completed(result: Int, attachment: A) { | ||
handler!!.completed(result, attachment) | ||
} | ||
|
||
override fun failed(exc: Throwable, attachment: A) { | ||
handler!!.failed(exc, attachment) | ||
} | ||
}) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
34 changes: 34 additions & 0 deletions
34
tanker-bindings/src/main/kotlin/io/tanker/api/InputStreamWrapper.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
package io.tanker.api | ||
|
||
import java.io.InputStream | ||
import java.nio.ByteBuffer | ||
import java.util.concurrent.Future | ||
|
||
class InputStreamWrapper(private val inputStream: InputStream) : TankerAsynchronousByteChannel { | ||
companion object { | ||
private var isClosed = false | ||
} | ||
|
||
override fun <A> read(dst: ByteBuffer?, attachment: A, handler: TankerCompletionHandler<Int, in A>?) { | ||
TankerFuture.threadPool.execute { | ||
try { | ||
val b = ByteArray(dst!!.remaining()) | ||
val nbRead = inputStream.read(b) | ||
if (nbRead != -1) { | ||
dst.put(b, 0, nbRead) | ||
} | ||
handler!!.completed(nbRead, attachment) | ||
} catch (e: Throwable) { | ||
handler!!.failed(e, attachment) | ||
} | ||
} | ||
} | ||
override fun isOpen(): Boolean { | ||
return !isClosed | ||
} | ||
|
||
override fun close() { | ||
inputStream.close() | ||
isClosed = true | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
13 changes: 13 additions & 0 deletions
13
tanker-bindings/src/main/kotlin/io/tanker/api/TankerAsynchronousByteChannel.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
package io.tanker.api | ||
|
||
import java.io.InputStream | ||
import java.nio.ByteBuffer | ||
import java.nio.channels.Channel | ||
import java.util.concurrent.Future | ||
|
||
// nio.channels.AsynchronousByteChannel requires API 26 | ||
// provide our own interface as a replacement | ||
|
||
interface TankerAsynchronousByteChannel : Channel { | ||
public abstract fun <A : Any?> read(dst: ByteBuffer?, attachment: A, handler: TankerCompletionHandler<Int, in A>?) | ||
} |
13 changes: 13 additions & 0 deletions
13
tanker-bindings/src/main/kotlin/io/tanker/api/TankerChannels.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
package io.tanker.api | ||
|
||
import java.io.InputStream | ||
|
||
class TankerChannels { | ||
|
||
companion object { | ||
@JvmStatic | ||
public fun newInputStream(channel: TankerStreamChannel): InputStream { | ||
return TankerInputStream(channel) | ||
} | ||
} | ||
} |
9 changes: 9 additions & 0 deletions
9
tanker-bindings/src/main/kotlin/io/tanker/api/TankerCompletionHandler.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
package io.tanker.api | ||
|
||
// nio.channels.CompletionHandler requires API 26 | ||
// provide our own interface as a replacement | ||
|
||
interface TankerCompletionHandler<V, A> { | ||
public abstract fun completed(result: V, attachment: A): Unit | ||
public abstract fun failed(exc: Throwable, attachment: A): Unit | ||
} |
68 changes: 68 additions & 0 deletions
68
tanker-bindings/src/main/kotlin/io/tanker/api/TankerInputStream.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,68 @@ | ||
package io.tanker.api | ||
|
||
import io.tanker.bindings.TankerError | ||
import io.tanker.bindings.TankerLib | ||
import java.io.IOException | ||
import java.io.InputStream | ||
import java.nio.ByteBuffer | ||
import java.nio.channels.ClosedChannelException | ||
import java.util.concurrent.Callable | ||
import java.util.concurrent.FutureTask | ||
import java.util.concurrent.ThreadPoolExecutor | ||
|
||
|
||
class TankerInputStream internal constructor(private val channel: TankerStreamChannel) : InputStream() { | ||
val resourceID = channel.resourceID | ||
|
||
override fun read(): Int { | ||
val buffer = ByteArray(1) | ||
if (read(buffer, 0, 1) == -1) | ||
return -1 | ||
return buffer[0].toInt() | ||
} | ||
|
||
override fun read(b: ByteArray): Int { | ||
return read(b, 0, b.size) | ||
|
||
} | ||
|
||
override fun read(b: ByteArray, off: Int, len: Int): Int { | ||
val fut = FutureTask {} | ||
var nbRead = 0 | ||
var err : Throwable? = null | ||
|
||
val buffer = ByteBuffer.wrap(b, off, len) | ||
channel.read(buffer, Unit, object : TankerCompletionHandler<Int, Unit> { | ||
override fun completed(result: Int, attachment: Unit) { | ||
nbRead = result | ||
fut.run() | ||
} | ||
|
||
override fun failed(exc: Throwable, attachment: Unit) { | ||
err = exc | ||
fut.run() | ||
} | ||
}) | ||
fut.get() | ||
if (err != null) { | ||
if (err is ClosedChannelException) { | ||
throw IOException("Stream is closed", err) | ||
} | ||
throw err!! | ||
} | ||
return nbRead | ||
} | ||
|
||
override fun markSupported(): Boolean { | ||
return false | ||
} | ||
|
||
override fun available(): Int { | ||
return 0 | ||
} | ||
|
||
override fun close() { | ||
channel.close() | ||
} | ||
|
||
} |
6 changes: 6 additions & 0 deletions
6
tanker-bindings/src/main/kotlin/io/tanker/api/TankerReadPendingException.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
package io.tanker.api | ||
|
||
import java.lang.IllegalStateException | ||
|
||
open class TankerReadPendingException : IllegalStateException() { | ||
} |
Oops, something went wrong.