forked from clearw5/Auto.js
-
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.
- Loading branch information
hyb1996
committed
Jan 21, 2019
1 parent
5cf08a9
commit 91365c6
Showing
3 changed files
with
79 additions
and
15 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
30 changes: 30 additions & 0 deletions
30
common/src/main/java/com/stardust/io/ByteBufferBackedInputStream.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 com.stardust.io | ||
|
||
import java.io.IOException | ||
import java.io.InputStream | ||
import java.nio.ByteBuffer | ||
|
||
class ByteBufferBackedInputStream(private var buf: ByteBuffer) : InputStream() { | ||
|
||
@Throws(IOException::class) | ||
override fun read(): Int { | ||
return if (!buf.hasRemaining()) { | ||
-1 | ||
} else buf.get().toInt() and 0xFF | ||
} | ||
|
||
@Throws(IOException::class) | ||
override fun read(bytes: ByteArray, off: Int, len: Int): Int { | ||
if (!buf.hasRemaining()) { | ||
return -1 | ||
} | ||
val read = Math.min(len, available()) | ||
buf.get(bytes, off, read) | ||
buf.position(buf.position() - read) | ||
return read | ||
} | ||
|
||
override fun available(): Int { | ||
return buf.position() | ||
} | ||
} |
19 changes: 19 additions & 0 deletions
19
common/src/main/java/com/stardust/io/ByteBufferBackedOutputStream.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,19 @@ | ||
package com.stardust.io | ||
|
||
import java.io.IOException | ||
import java.io.OutputStream | ||
import java.nio.ByteBuffer | ||
|
||
class ByteBufferBackedOutputStream(private var buf: ByteBuffer) : OutputStream() { | ||
|
||
@Throws(IOException::class) | ||
override fun write(b: Int) { | ||
buf.put(b.toByte()) | ||
} | ||
|
||
@Throws(IOException::class) | ||
override fun write(bytes: ByteArray, off: Int, len: Int) { | ||
buf.put(bytes, off, len) | ||
} | ||
|
||
} |