diff --git a/exposed-core/src/main/kotlin/org/jetbrains/exposed/sql/statements/api/ExposedBlob.kt b/exposed-core/src/main/kotlin/org/jetbrains/exposed/sql/statements/api/ExposedBlob.kt index 5a12cf2197..2ea25210cb 100644 --- a/exposed-core/src/main/kotlin/org/jetbrains/exposed/sql/statements/api/ExposedBlob.kt +++ b/exposed-core/src/main/kotlin/org/jetbrains/exposed/sql/statements/api/ExposedBlob.kt @@ -2,18 +2,24 @@ package org.jetbrains.exposed.sql.statements.api import java.io.InputStream -class ExposedBlob(val inputStream: InputStream) { +class ExposedBlob(inputStream: InputStream) { constructor(bytes: ByteArray) : this (bytes.inputStream()) - val bytes get() = inputStream.readBytes().also { inputStream.reset() } + var inputStream = inputStream + private set + + val bytes get() = inputStream.readBytes().also { + if (inputStream.markSupported()) + inputStream.reset() + else + inputStream = it.inputStream() + } override fun equals(other: Any?): Boolean { if (this === other) return true if (other !is ExposedBlob) return false - if (!bytes.contentEquals(other.bytes)) return false - - return true + return bytes.contentEquals(other.bytes) } override fun hashCode(): Int = bytes.contentHashCode() diff --git a/exposed-tests/src/test/kotlin/org/jetbrains/exposed/sql/tests/shared/DDLTests.kt b/exposed-tests/src/test/kotlin/org/jetbrains/exposed/sql/tests/shared/DDLTests.kt index e05bdb84a4..214dfda6c2 100644 --- a/exposed-tests/src/test/kotlin/org/jetbrains/exposed/sql/tests/shared/DDLTests.kt +++ b/exposed-tests/src/test/kotlin/org/jetbrains/exposed/sql/tests/shared/DDLTests.kt @@ -20,6 +20,7 @@ import org.jetbrains.exposed.sql.vendors.SQLiteDialect import org.junit.Test import org.postgresql.util.PGobject import java.util.* +import kotlin.random.Random import kotlin.test.assertNotNull class DDLTests : DatabaseTestsBase() { @@ -337,24 +338,37 @@ class DDLTests : DatabaseTestsBase() { } withTables(t) { - val bytes = "Hello there!".toByteArray() - val blob = ExposedBlob(bytes) + val shortBytes = "Hello there!".toByteArray() + val longBytes = Random.nextBytes(1024) + val shotBlob = ExposedBlob(shortBytes) + val longBlob = ExposedBlob(longBytes) // if (currentDialectTest.dataTypeProvider.blobAsStream) { // SerialBlob(bytes) // } else connection.createBlob().apply { // setBytes(1, bytes) // } - val id = t.insert { - it[t.b] = blob + val id1 = t.insert { + it[t.b] = shotBlob } get (t.id) - val readOn = t.select { t.id eq id }.first()[t.b] - val text1 = String(readOn.bytes) - val text2 = readOn.inputStream.bufferedReader().readText() + val id2 = t.insert { + it[t.b] = longBlob + } get (t.id) + + val readOn1 = t.select { t.id eq id1 }.first()[t.b] + val text1 = String(readOn1.bytes) + val text2 = readOn1.inputStream.bufferedReader().readText() assertEquals("Hello there!", text1) assertEquals("Hello there!", text2) + + val readOn2 = t.select { t.id eq id2 }.first()[t.b] + val bytes1 = readOn2.bytes + val bytes2 = readOn2.inputStream.readBytes() + + assertTrue(longBytes.contentEquals(bytes1)) + assertTrue(longBytes.contentEquals(bytes2)) } }