Skip to content

Commit

Permalink
Java heap space error when writing > 2 GB file content as Blob into O…
Browse files Browse the repository at this point in the history
…racle Table #1617 / Fix for non-repeatable streams
  • Loading branch information
Tapac committed Dec 12, 2022
1 parent c6cf379 commit 367babc
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 12 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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() {
Expand Down Expand Up @@ -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))
}
}

Expand Down

0 comments on commit 367babc

Please sign in to comment.