Skip to content

Commit

Permalink
Test InputStream.transferTo() (#1526)
Browse files Browse the repository at this point in the history
* Test InputStream.transferTo()

* Don't crash on Java 8
  • Loading branch information
squarejesse authored Oct 1, 2024
1 parent d5a18d9 commit cebc032
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 3 deletions.
3 changes: 0 additions & 3 deletions okio/src/jvmMain/kotlin/okio/RealBufferedSource.kt
Original file line number Diff line number Diff line change
Expand Up @@ -185,9 +185,6 @@ internal actual class RealBufferedSource actual constructor(
if (read == -1L) break
}
count += buffer.size
if (count < 0) {
count = Long.MAX_VALUE
}
buffer.writeTo(out)
}
return count
Expand Down
23 changes: 23 additions & 0 deletions okio/src/jvmTest/kotlin/okio/BufferedSourceTest.kt
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,11 @@
*/
package okio

import assertk.assertThat
import assertk.assertions.isEqualTo
import assertk.assertions.isTrue
import java.io.ByteArrayInputStream
import java.io.ByteArrayOutputStream
import java.io.EOFException
import java.nio.ByteBuffer
import java.nio.charset.Charset
Expand Down Expand Up @@ -991,6 +996,24 @@ class BufferedSourceTest(
}
}

@Test
fun inputStreamTransferTo() {
try {
ByteArrayInputStream(byteArrayOf(1)).transferTo(ByteArrayOutputStream())
} catch (e: NoSuchMethodError) {
return // This JDK doesn't have transferTo(). Skip this test.
}

val data = "a".repeat(SEGMENT_SIZE * 3 + 1)
sink.writeUtf8(data)
sink.emit()
val inputStream = source.inputStream()
val outputStream = ByteArrayOutputStream()
inputStream.transferTo(outputStream)
assertThat(source.exhausted()).isTrue()
assertThat(outputStream.toByteArray().toUtf8String()).isEqualTo(data)
}

@Test
fun longHexString() {
assertLongHexString("8000000000000000", -0x7fffffffffffffffL - 1L)
Expand Down

0 comments on commit cebc032

Please sign in to comment.