Exposed 1.0.0-beta-3 Help

Binary

Exposed provides support for storing and handling binary data through the ExposedBlob type, which efficiently wraps binary content using InputStream. This approach ensures optimal memory usage when working with large binary objects.

Supported types

The exposed-core module provides the following binary types:

blob()

Maps to database BLOB (or equivalent type depending on the database). Used for storing binary objects of any size.

Database-specific mappings:

  • PostgreSQL: Maps to bytea

  • SQLServer: Maps to VARBINARY(MAX)

binary()

Maps to database VARBINARY. Available in two forms:

  • Simple version: Takes only the column name parameter and maps to VARBINARY with database-specific default length.

    val simpleData = binary("simple_data") // simple version without length

    Database-specific mappings:

    • PostgreSQL: Maps to bytea

    • SQLite: Maps to BLOB

    • SQLServer, MySQL, and Oracle: Not supported

  • Length-specified version: Takes both name and length parameters, maps to VARBINARY(MAX) where MAX is the specified maximum length. Used for storing fixed-size binary data.

    val thumbnail = binary("thumbnail", 1024) // length-specified version

    Database-specific mappings:

    • PostgreSQL: Maps to bytea

    • Oracle: Maps to RAW if the provided length is lower than 2000, otherwise not supported.

Basic usage

Here's a simple example of using binary types in a table definition:

object Files : Table() { val id = integer("id").autoIncrement() val name = varchar("name", 255) val content = blob("content") val simpleData = binary("simple_data") // simple version without length val thumbnail = binary("thumbnail", 1024) // length-specified version override val primaryKey = PrimaryKey(id) }
SchemaUtils.create(Files) // Store binary data Files.insert { it[name] = "example.txt" it[content] = ExposedBlob("Hello, World!".toByteArray()) it[simpleData] = "simple binary data".toByteArray() // using simple binary version it[thumbnail] = "thumbnail data".toByteArray() // using length-specified version } // Read binary data val file = Files.selectAll().first() val content = file[Files.content] val text = String(content.bytes) // for small files // or val text2 = content.inputStream.bufferedReader().readText() // for large files // Read both types of binary data val simpleBytes = file[Files.simpleData] // returns ByteArray from simple binary val thumbnailBytes = file[Files.thumbnail] // returns ByteArray from length-specified binary println("$text, $text2") }

PostgreSQL Object Identifiers

PostgreSQL supports storing large objects using Object Identifiers (OIDs):

val content = blob("content", useObjectIdentifier = true)
Last modified: 03 July 2025