-
Notifications
You must be signed in to change notification settings - Fork 70
Introduce interoperability with java.io #95
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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 hidden or 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
File renamed without changes.
This file contains hidden or 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 hidden or 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 hidden or 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,16 @@ | ||
| package kotlinx.io | ||
|
|
||
| import kotlinx.io.internal.* | ||
| import java.io.* | ||
|
|
||
| /** | ||
| * Returns an [InputStream] that uses the current [Input] as an underlying source of data. | ||
| * Closing the resulting [InputStream] will close the input. | ||
| */ | ||
| public fun Input.asInputStream(): InputStream = InputStreamFromInput(this) | ||
|
|
||
| /** | ||
| * Returns an [Input] that uses the current [InputStream] as an underlying source of data. | ||
| * Closing the resulting [Input] will close the input stream. | ||
| */ | ||
| public fun InputStream.asInput(): Input = InputFromInputStream(this) |
This file contains hidden or 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,16 @@ | ||
| package kotlinx.io | ||
|
|
||
| import kotlinx.io.internal.* | ||
| import java.io.* | ||
|
|
||
| /** | ||
| * Returns an [OutputStream] that uses the current [Output] as the destination. | ||
| * Closing the resulting [OutputStream] will close the input. | ||
| */ | ||
| public fun Output.asOutputStream(): OutputStream = OutputStreamFromOutput(this) | ||
|
|
||
| /** | ||
| * Returns an [Output] that uses the current [OutputStream] as the destination. | ||
| * Closing the resulting [Output] will close the input stream. | ||
| */ | ||
| public fun OutputStream.asOutput(): Output = OutputFromOutputStream(this) |
This file was deleted.
Oops, something went wrong.
This file contains hidden or 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,92 @@ | ||
| package kotlinx.io.internal | ||
|
|
||
| import kotlinx.io.* | ||
| import kotlinx.io.buffer.* | ||
| import java.io.* | ||
|
|
||
| internal class InputStreamFromInput(private val input: Input) : InputStream() { | ||
| override fun read(): Int { | ||
| if (input.exhausted()) { | ||
| return -1 | ||
| } | ||
| return input.readByte().toInt() and 0xFF | ||
| } | ||
|
|
||
| override fun read(b: ByteArray): Int { | ||
| if (b.isEmpty()) return 0 | ||
| val result = input.readAvailableTo(bufferOf(b)) | ||
| if (result == 0) return -1 | ||
| return result | ||
| } | ||
|
|
||
| override fun read(b: ByteArray, off: Int, len: Int): Int { | ||
| if (len == 0) return 0 | ||
| val result = input.readAvailableTo(bufferOf(b), off, off + len) | ||
| if (result == 0) return -1 | ||
| return result | ||
| } | ||
|
|
||
| override fun close() { | ||
| input.close() | ||
| } | ||
| } | ||
|
|
||
| internal class InputFromInputStream(private val inputStream: InputStream) : Input() { | ||
| override fun closeSource() { | ||
| inputStream.close() | ||
| } | ||
|
|
||
| override fun fill(buffer: Buffer, startIndex: Int, endIndex: Int): Int { | ||
| // Zero-copy attempt | ||
| if (buffer.buffer.hasArray()) { | ||
| val result = inputStream.read(buffer.buffer.array(), startIndex, endIndex - startIndex) | ||
| return result.coerceAtLeast(0) // -1 when IS is closed | ||
| } | ||
|
|
||
| for (i in startIndex until endIndex) { | ||
| val byte = inputStream.read() | ||
qwwdfsad marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| if (byte == -1) return (i - startIndex) | ||
| buffer[i] = byte.toByte() | ||
| } | ||
| return endIndex - startIndex | ||
| } | ||
| } | ||
|
|
||
| internal class OutputStreamFromOutput(private val output: Output) : OutputStream() { | ||
| override fun write(b: Int) { | ||
| output.writeByte(b.toByte()) | ||
| } | ||
|
|
||
| override fun write(b: ByteArray) { | ||
| output.writeBuffer(bufferOf(b)) | ||
| } | ||
|
|
||
| override fun write(b: ByteArray, off: Int, len: Int) { | ||
| output.writeBuffer(bufferOf(b), off, off + len) | ||
| } | ||
|
|
||
| override fun flush() { | ||
| output.flush() | ||
| } | ||
|
|
||
| override fun close() { | ||
| output.close() | ||
| } | ||
| } | ||
|
|
||
| internal class OutputFromOutputStream(private val outputStream: OutputStream) : Output() { | ||
|
|
||
| override fun closeSource() { | ||
| outputStream.close() | ||
| } | ||
|
|
||
| override fun flush(source: Buffer, startIndex: Int, endIndex: Int) { | ||
| if (source.buffer.hasArray()) { | ||
| return outputStream.write(source.buffer.array(), startIndex, endIndex - startIndex) | ||
| } | ||
|
|
||
| for (i in startIndex until endIndex) { | ||
| outputStream.write(source[i].toInt()) | ||
qwwdfsad marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| } | ||
| } | ||
| } | ||
This file contains hidden or 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 hidden or 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
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.