Skip to content

Commit 15f6bbf

Browse files
authored
fix: allow empty I/O content (#859)
1 parent b67d7d7 commit 15f6bbf

File tree

5 files changed

+36
-6
lines changed

5 files changed

+36
-6
lines changed
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
{
2+
"id": "2d39ef91-f11a-4f2a-af23-9331b90926f2",
3+
"type": "bugfix",
4+
"description": "Allow empty I/O content"
5+
}

runtime/runtime-core/jvm/src/aws/smithy/kotlin/runtime/content/ByteStreamJVM.kt

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -24,8 +24,8 @@ public fun ByteStream.Companion.fromFile(file: File): ByteStream = file.asByteSt
2424
*/
2525
public fun File.asByteStream(start: Long = 0, endInclusive: Long = length() - 1): ByteStream {
2626
require(start >= 0) { "start index $start cannot be negative" }
27-
require(endInclusive >= start) {
28-
"end index $endInclusive must be greater than or equal to start index $start"
27+
require(endInclusive >= start - 1) {
28+
"end index $endInclusive must be greater than or equal to start index minus one (${start - 1})"
2929
}
3030

3131
val len = length()
@@ -46,9 +46,10 @@ public fun Path.asByteStream(start: Long = 0, endInclusive: Long = -1): ByteStre
4646
val f = toFile()
4747
require(f.exists()) { "cannot create ByteStream, file does not exist: $this" }
4848
require(f.isFile) { "cannot create a ByteStream from a directory: $this" }
49-
require(endInclusive >= -1L) { "endInclusive must be -1 or greater" }
5049

5150
val calculatedEndInclusive = if (endInclusive == -1L) f.length() - 1L else endInclusive
51+
require(calculatedEndInclusive >= start - 1) { "end index $calculatedEndInclusive must be greater or equal to start index minus one (${start - 1})" }
52+
5253
return f.asByteStream(start, calculatedEndInclusive)
5354
}
5455

runtime/runtime-core/jvm/src/aws/smithy/kotlin/runtime/io/JavaIO.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ public fun Path.source(range: LongRange): SdkSource = source(range.first, range.
2222
* Create a [SdkSource] from the given path and range
2323
*/
2424
public fun Path.source(start: Long = 0L, endInclusive: Long = -1): SdkSource {
25-
require(endInclusive >= -1L) { "endInclusive must be -1 or greater" }
25+
require(endInclusive >= start - 1) { "end index $endInclusive must be greater or equal to start index minus one (${start - 1})" }
2626

2727
val f = toFile()
2828
val calculatedEndInclusive = if (endInclusive == -1L) f.length() - 1L else endInclusive

runtime/runtime-core/jvm/src/aws/smithy/kotlin/runtime/io/RandomAccessFileSource.kt

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,10 @@ internal class RandomAccessFileSource(
2222
require(fileObject.exists()) { "cannot create SdkSource, file does not exist: $this" }
2323
require(fileObject.isFile) { "cannot create a SdkSource from a directory: $this" }
2424
require(start >= 0L) { "start position should be >= 0, found $start" }
25-
require(endInclusive >= 0 && endInclusive <= fileObject.length() - 1) {
25+
require(endInclusive >= start - 1) {
26+
"end index $endInclusive must be greater than or equal to the start index minus one (${start - 1})"
27+
}
28+
require(endInclusive <= fileObject.length() - 1) {
2629
"endInclusive should be less than or equal to the length of the file, was $endInclusive"
2730
}
2831

runtime/runtime-core/jvm/test/aws/smithy/kotlin/runtime/content/ByteStreamJVMTest.kt

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ package aws.smithy.kotlin.runtime.content
88
import aws.smithy.kotlin.runtime.testing.RandomTempFile
99
import kotlinx.coroutines.ExperimentalCoroutinesApi
1010
import kotlinx.coroutines.test.runTest
11+
import java.nio.file.Files
1112
import kotlin.test.*
1213

1314
@OptIn(ExperimentalCoroutinesApi::class)
@@ -36,7 +37,7 @@ class ByteStreamJVMTest {
3637
val e = assertFailsWith<Throwable> {
3738
file.asByteStream(5, 1)
3839
}
39-
assertEquals("end index 1 must be greater than or equal to start index 5", e.message)
40+
assertEquals("end index 1 must be greater than or equal to start index minus one (4)", e.message)
4041
}
4142

4243
@Test
@@ -130,4 +131,24 @@ class ByteStreamJVMTest {
130131

131132
assertEquals(1024, stream.contentLength)
132133
}
134+
135+
@Test
136+
fun `can create byte stream from empty file and path using createTempFile`() = runTest {
137+
val file = Files.createTempFile(null, null)
138+
val byteStream = file.asByteStream()
139+
assertEquals(0, byteStream.contentLength)
140+
141+
val byteStreamFromPath = file.toAbsolutePath().asByteStream()
142+
assertEquals(0, byteStreamFromPath.contentLength)
143+
}
144+
145+
@Test
146+
fun `can create byte stream from empty file and path using RandomTempFile`() = runTest {
147+
val file = RandomTempFile(sizeInBytes = 0)
148+
val byteStream = file.asByteStream()
149+
assertEquals(0, byteStream.contentLength)
150+
151+
val byteStreamFromPath = file.toPath().asByteStream()
152+
assertEquals(0, byteStreamFromPath.contentLength)
153+
}
133154
}

0 commit comments

Comments
 (0)