-
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.
Merge branch 'theo/stream_encryption' into 'master'
theo/stream_encryption See merge request Tanker/sdk-android!84
- Loading branch information
Showing
22 changed files
with
785 additions
and
12 deletions.
There are no files selected for viewing
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 |
---|---|---|
|
@@ -2,6 +2,7 @@ | |
*.apk | ||
*.ap_ | ||
*.dex | ||
*.log | ||
|
||
# Java build artifacts class files | ||
*.class | ||
|
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
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
This file was deleted.
Oops, something went wrong.
4 changes: 2 additions & 2 deletions
4
tanker-bindings/src/androidTest/java/io/tanker/api/TankerTest.java
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
30 changes: 30 additions & 0 deletions
30
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,30 @@ | ||
package io.tanker.api | ||
|
||
import androidx.annotation.RequiresApi | ||
import java.nio.ByteBuffer | ||
import java.nio.channels.AsynchronousByteChannel | ||
import java.nio.channels.CompletionHandler | ||
|
||
@RequiresApi(26) | ||
internal 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>) { | ||
// CompletionHandler is API 26 only, hence the boilerplate | ||
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
37 changes: 37 additions & 0 deletions
37
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,37 @@ | ||
package io.tanker.api | ||
|
||
import java.io.IOException | ||
import java.io.InputStream | ||
import java.nio.ByteBuffer | ||
|
||
class InputStreamWrapper(private var inputStream: InputStream?) : TankerAsynchronousByteChannel { | ||
override fun <A> read(dst: ByteBuffer, attachment: A, handler: TankerCompletionHandler<Int, in A>) { | ||
TankerFuture.threadPool.execute { | ||
if (!isOpen) | ||
handler.failed(IOException("Stream is closed"), attachment) | ||
else { | ||
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 inputStream != null | ||
} | ||
|
||
override fun close() { | ||
if (inputStream != null) { | ||
inputStream!!.close() | ||
inputStream = null | ||
} | ||
} | ||
} |
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
11 changes: 11 additions & 0 deletions
11
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,11 @@ | ||
package io.tanker.api | ||
|
||
import java.nio.ByteBuffer | ||
import java.nio.channels.Channel | ||
|
||
// nio.channels.AsynchronousByteChannel requires API 26 | ||
// provide our own interface as a replacement | ||
|
||
interface TankerAsynchronousByteChannel : Channel { | ||
fun <A : Any?> read(dst: ByteBuffer, attachment: A, handler: TankerCompletionHandler<Int, in A>) | ||
} |
51 changes: 51 additions & 0 deletions
51
tanker-bindings/src/main/kotlin/io/tanker/api/TankerAsynchronousByteChannelWrapper.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,51 @@ | ||
package io.tanker.api | ||
|
||
import androidx.annotation.RequiresApi | ||
import java.nio.ByteBuffer | ||
import java.nio.channels.AsynchronousByteChannel | ||
import java.nio.channels.CompletionHandler | ||
import java.nio.channels.ReadPendingException | ||
import java.util.concurrent.Future | ||
|
||
@RequiresApi(26) | ||
internal class TankerAsynchronousByteChannelWrapper(internal val streamChannel: TankerAsynchronousByteChannel) : AsynchronousByteChannel { | ||
override fun read(dst: ByteBuffer?): Future<Int> { | ||
throw UnsupportedOperationException() | ||
} | ||
|
||
override fun <A : Any?> read(dst: ByteBuffer, attachment: A, handler: CompletionHandler<Int, in A>) { | ||
try { | ||
return streamChannel.read(dst, attachment, object : TankerCompletionHandler<Int, A> { | ||
override fun completed(result: Int, attachment: A) { | ||
handler.completed(result, attachment) | ||
} | ||
|
||
override fun failed(exc: Throwable, attachment: A) { | ||
handler.failed(exc, attachment) | ||
} | ||
}) | ||
} catch (exc: Throwable) { | ||
if (exc is TankerPendingReadException) | ||
throw ReadPendingException() | ||
throw exc | ||
} | ||
} | ||
|
||
override fun close() { | ||
return streamChannel.close() | ||
} | ||
|
||
override fun write(src: ByteBuffer?): Future<Int> { | ||
throw UnsupportedOperationException() | ||
} | ||
|
||
override fun <A : Any?> write(src: ByteBuffer?, attachment: A, handler: CompletionHandler<Int, in A>?) { | ||
throw UnsupportedOperationException() | ||
} | ||
|
||
|
||
override fun isOpen(): Boolean { | ||
return streamChannel.isOpen | ||
} | ||
|
||
} |
32 changes: 32 additions & 0 deletions
32
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,32 @@ | ||
package io.tanker.api | ||
|
||
import androidx.annotation.RequiresApi | ||
import java.io.InputStream | ||
import java.nio.channels.AsynchronousByteChannel | ||
|
||
class TankerChannels { | ||
|
||
companion object { | ||
@JvmStatic | ||
fun toInputStream(channel: TankerAsynchronousByteChannel): InputStream { | ||
return TankerInputStream(channel) | ||
} | ||
|
||
@JvmStatic | ||
fun fromInputStream(stream: InputStream): TankerAsynchronousByteChannel { | ||
return InputStreamWrapper(stream) | ||
} | ||
|
||
@RequiresApi(26) | ||
@JvmStatic | ||
fun toAsynchronousByteChannel(channel: TankerAsynchronousByteChannel): AsynchronousByteChannel { | ||
return TankerAsynchronousByteChannelWrapper(channel) | ||
} | ||
|
||
@RequiresApi(26) | ||
@JvmStatic | ||
fun fromAsynchronousByteChannel(channel: AsynchronousByteChannel): TankerAsynchronousByteChannel { | ||
return AsynchronousByteChannelWrapper(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> { | ||
fun completed(result: V, attachment: A) | ||
fun failed(exc: Throwable, attachment: A) | ||
} |
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
66 changes: 66 additions & 0 deletions
66
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,66 @@ | ||
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 | ||
|
||
|
||
internal class TankerInputStream constructor(private val channel: TankerAsynchronousByteChannel) : InputStream() { | ||
|
||
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/TankerPendingReadException.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 TankerPendingReadException : IllegalStateException() { | ||
} |
Oops, something went wrong.