Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

EXPOSED-86 allowing blobs with streams of unknown size #1777

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,15 @@ import org.jetbrains.exposed.sql.BlobColumnType
import org.jetbrains.exposed.sql.IColumnType
import org.jetbrains.exposed.sql.statements.StatementResult
import org.jetbrains.exposed.sql.statements.api.PreparedStatementApi
import java.io.ByteArrayInputStream
import java.io.FileInputStream
import org.jetbrains.exposed.sql.vendors.SQLiteDialect
import org.jetbrains.exposed.sql.vendors.currentDialect
import java.io.InputStream
import java.sql.PreparedStatement
import java.sql.ResultSet
import java.sql.Statement
import java.sql.SQLFeatureNotSupportedException
import java.sql.Types

/**
Expand Down Expand Up @@ -80,7 +83,25 @@ class JdbcPreparedStatementImpl(
}

override fun setInputStream(index: Int, inputStream: InputStream) {
statement.setBinaryStream(index, inputStream, inputStream.available())
try {
when {
// streams with known length where available matches the actual length
inputStream is ByteArrayInputStream ->
statement.setBinaryStream(index, inputStream, inputStream.available())

// FileInputStream.available() returns returns Int.MAX_VALUE
// if the underlying file is larger than 2GB
inputStream is FileInputStream && inputStream.available() < Int.MAX_VALUE ->
statement.setBinaryStream(index, inputStream, inputStream.available())

// default handling for unknown length
else -> statement.setBinaryStream(index, inputStream)

}
} catch (e: SQLFeatureNotSupportedException) {
// fallback to bytes
statement.setBytes(index, inputStream.readBytes())
}
}

override fun setArray(index: Int, type: String, array: Array<*>) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@ import org.jetbrains.exposed.sql.vendors.SQLiteDialect
import org.junit.Assume
import org.junit.Test
import org.postgresql.util.PGobject
import java.io.ByteArrayInputStream
import java.io.SequenceInputStream
import java.util.*
import kotlin.random.Random
import kotlin.test.assertNotNull
Expand Down Expand Up @@ -612,7 +614,12 @@ class DDLTests : DatabaseTestsBase() {
val shortBytes = "Hello there!".toByteArray()
val longBytes = Random.nextBytes(1024)
val shortBlob = ExposedBlob(shortBytes)
val longBlob = ExposedBlob(longBytes)
val longBlob = ExposedBlob(
inputStream = SequenceInputStream(
ByteArrayInputStream(longBytes, 0, 512),
ByteArrayInputStream(longBytes, 512, 512)
)
)

val id1 = t.insert {
it[t.b] = shortBlob
Expand Down