Deprecate readBytes with estimatedSize parameter, add overload without parameters

Add a test for readBytes.

#KT-19305 Fixed
This commit is contained in:
Ilya Gorbunov
2018-08-06 22:22:53 +03:00
parent cbc92bc9a1
commit 17e04b2665
3 changed files with 38 additions and 1 deletions
@@ -117,8 +117,22 @@ public fun InputStream.copyTo(out: OutputStream, bufferSize: Int = DEFAULT_BUFFE
*
* **Note**: It is the caller's responsibility to close this stream.
*/
@Deprecated("Use readBytes() overload without estimatedSize parameter", ReplaceWith("readBytes()"))
// TODO: Add DeprecatedSince when available
public fun InputStream.readBytes(estimatedSize: Int = DEFAULT_BUFFER_SIZE): ByteArray {
val buffer = ByteArrayOutputStream(Math.max(estimatedSize, this.available()))
val buffer = ByteArrayOutputStream(maxOf(estimatedSize, this.available()))
copyTo(buffer)
return buffer.toByteArray()
}
/**
* Reads this stream completely into a byte array.
*
* **Note**: It is the caller's responsibility to close this stream.
*/
@SinceKotlin("1.3")
public fun InputStream.readBytes(): ByteArray {
val buffer = ByteArrayOutputStream(maxOf(DEFAULT_BUFFER_SIZE, this.available()))
copyTo(buffer)
return buffer.toByteArray()
}
+22
View File
@@ -8,6 +8,7 @@ package test.io
import kotlin.test.*
import java.io.Writer
import java.io.BufferedReader
import kotlin.random.Random
class IOStreamsTest {
@Test fun testGetStreamOfFile() {
@@ -43,4 +44,25 @@ class IOStreamsTest {
assertEquals(x.asList(), result)
}
@Test fun readWriteBytes() {
val file = createTempFile("temp", Random.nextLong().toString())
try {
val bytes = Random.nextBytes(256_000)
file.outputStream().use { outStream ->
outStream.write(bytes)
}
val inBytes = file.inputStream().use { inStream ->
inStream.readBytes()
}
assertTrue(inBytes contentEquals bytes, "Expected to read the same content back, read bytes of length ${inBytes.size}")
} finally {
file.delete()
}
}
}
@@ -2913,6 +2913,7 @@ public final class kotlin/io/ByteStreamsKt {
public static final fun copyTo (Ljava/io/InputStream;Ljava/io/OutputStream;I)J
public static synthetic fun copyTo$default (Ljava/io/InputStream;Ljava/io/OutputStream;IILjava/lang/Object;)J
public static final fun iterator (Ljava/io/BufferedInputStream;)Lkotlin/collections/ByteIterator;
public static final fun readBytes (Ljava/io/InputStream;)[B
public static final fun readBytes (Ljava/io/InputStream;I)[B
public static synthetic fun readBytes$default (Ljava/io/InputStream;IILjava/lang/Object;)[B
}