|
| 1 | +package kotlinx.io.internal |
| 2 | + |
| 3 | +import kotlinx.io.* |
| 4 | +import kotlinx.io.buffer.* |
| 5 | +import java.io.* |
| 6 | + |
| 7 | +internal class InputStreamFromInput(private val input: Input) : InputStream() { |
| 8 | + override fun read(): Int { |
| 9 | + if (input.exhausted()) { |
| 10 | + return -1 |
| 11 | + } |
| 12 | + return input.readByte().toInt() and 0xFF |
| 13 | + } |
| 14 | + |
| 15 | + override fun read(b: ByteArray): Int { |
| 16 | + if (b.isEmpty()) return 0 |
| 17 | + val result = input.readAvailableTo(bufferOf(b)) |
| 18 | + if (result == 0) return -1 |
| 19 | + return result |
| 20 | + } |
| 21 | + |
| 22 | + override fun read(b: ByteArray, off: Int, len: Int): Int { |
| 23 | + if (len == 0) return 0 |
| 24 | + val result = input.readAvailableTo(bufferOf(b), off, off + len) |
| 25 | + if (result == 0) return -1 |
| 26 | + return result |
| 27 | + } |
| 28 | + |
| 29 | + override fun close() { |
| 30 | + input.close() |
| 31 | + } |
| 32 | +} |
| 33 | + |
| 34 | +internal class InputFromInputStream(private val inputStream: InputStream) : Input() { |
| 35 | + override fun closeSource() { |
| 36 | + inputStream.close() |
| 37 | + } |
| 38 | + |
| 39 | + override fun fill(buffer: Buffer, startIndex: Int, endIndex: Int): Int { |
| 40 | + // Zero-copy attempt |
| 41 | + if (buffer.buffer.hasArray()) { |
| 42 | + val result = inputStream.read(buffer.buffer.array(), startIndex, endIndex - startIndex) |
| 43 | + return result.coerceAtLeast(0) // -1 when IS is closed |
| 44 | + } |
| 45 | + |
| 46 | + for (i in startIndex until endIndex) { |
| 47 | + val byte = inputStream.read() |
| 48 | + if (byte == -1) return (i - startIndex) |
| 49 | + buffer[i] = byte.toByte() |
| 50 | + } |
| 51 | + return endIndex - startIndex |
| 52 | + } |
| 53 | +} |
| 54 | + |
| 55 | +internal class OutputStreamFromOutput(private val output: Output) : OutputStream() { |
| 56 | + override fun write(b: Int) { |
| 57 | + output.writeByte(b.toByte()) |
| 58 | + } |
| 59 | + |
| 60 | + override fun write(b: ByteArray) { |
| 61 | + output.writeBuffer(bufferOf(b)) |
| 62 | + } |
| 63 | + |
| 64 | + override fun write(b: ByteArray, off: Int, len: Int) { |
| 65 | + output.writeBuffer(bufferOf(b), off, off + len) |
| 66 | + } |
| 67 | + |
| 68 | + override fun flush() { |
| 69 | + output.flush() |
| 70 | + } |
| 71 | + |
| 72 | + override fun close() { |
| 73 | + output.close() |
| 74 | + } |
| 75 | +} |
| 76 | + |
| 77 | +internal class OutputFromOutputStream(private val outputStream: OutputStream) : Output() { |
| 78 | + |
| 79 | + override fun closeSource() { |
| 80 | + outputStream.close() |
| 81 | + } |
| 82 | + |
| 83 | + override fun flush(source: Buffer, startIndex: Int, endIndex: Int) { |
| 84 | + if (source.buffer.hasArray()) { |
| 85 | + return outputStream.write(source.buffer.array(), startIndex, endIndex - startIndex) |
| 86 | + } |
| 87 | + |
| 88 | + for (i in startIndex until endIndex) { |
| 89 | + outputStream.write(source[i].toInt()) |
| 90 | + } |
| 91 | + } |
| 92 | +} |
0 commit comments