Skip to content

Commit adce458

Browse files
committed
Fix for EXPOSED-86, allowing blobs with streams of unknown size
1 parent 1999d3f commit adce458

File tree

2 files changed

+30
-2
lines changed

2 files changed

+30
-2
lines changed

exposed-jdbc/src/main/kotlin/org/jetbrains/exposed/sql/statements/jdbc/JdbcPreparedStatementImpl.kt

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,12 +5,15 @@ import org.jetbrains.exposed.sql.BlobColumnType
55
import org.jetbrains.exposed.sql.IColumnType
66
import org.jetbrains.exposed.sql.statements.StatementResult
77
import org.jetbrains.exposed.sql.statements.api.PreparedStatementApi
8+
import java.io.ByteArrayInputStream
9+
import java.io.FileInputStream
810
import org.jetbrains.exposed.sql.vendors.SQLiteDialect
911
import org.jetbrains.exposed.sql.vendors.currentDialect
1012
import java.io.InputStream
1113
import java.sql.PreparedStatement
1214
import java.sql.ResultSet
1315
import java.sql.Statement
16+
import java.sql.SQLFeatureNotSupportedException
1417
import java.sql.Types
1518

1619
/**
@@ -80,7 +83,25 @@ class JdbcPreparedStatementImpl(
8083
}
8184

8285
override fun setInputStream(index: Int, inputStream: InputStream) {
83-
statement.setBinaryStream(index, inputStream, inputStream.available())
86+
try {
87+
when {
88+
// streams with known length where available matches the actual length
89+
inputStream is ByteArrayInputStream ->
90+
statement.setBinaryStream(index, inputStream, inputStream.available())
91+
92+
// FileInputStream.available() returns returns Int.MAX_VALUE
93+
// if the underlying file is larger than 2GB
94+
inputStream is FileInputStream && inputStream.available() < Int.MAX_VALUE ->
95+
statement.setBinaryStream(index, inputStream, inputStream.available())
96+
97+
// default handling for unknown length
98+
else -> statement.setBinaryStream(index, inputStream)
99+
100+
}
101+
} catch (e: SQLFeatureNotSupportedException) {
102+
// fallback to bytes
103+
statement.setBytes(index, inputStream.readBytes())
104+
}
84105
}
85106

86107
override fun setArray(index: Int, type: String, array: Array<*>) {

exposed-tests/src/test/kotlin/org/jetbrains/exposed/sql/tests/shared/DDLTests.kt

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,8 @@ import org.jetbrains.exposed.sql.vendors.SQLiteDialect
2525
import org.junit.Assume
2626
import org.junit.Test
2727
import org.postgresql.util.PGobject
28+
import java.io.ByteArrayInputStream
29+
import java.io.SequenceInputStream
2830
import java.util.*
2931
import kotlin.random.Random
3032
import kotlin.test.assertNotNull
@@ -612,7 +614,12 @@ class DDLTests : DatabaseTestsBase() {
612614
val shortBytes = "Hello there!".toByteArray()
613615
val longBytes = Random.nextBytes(1024)
614616
val shortBlob = ExposedBlob(shortBytes)
615-
val longBlob = ExposedBlob(longBytes)
617+
val longBlob = ExposedBlob(
618+
inputStream = SequenceInputStream(
619+
ByteArrayInputStream(longBytes, 0, 512),
620+
ByteArrayInputStream(longBytes, 512, 512)
621+
)
622+
)
616623

617624
val id1 = t.insert {
618625
it[t.b] = shortBlob

0 commit comments

Comments
 (0)