String to Blob Conversion in Java
Working with BLOB (Binary Large Object) data is common when storing large binary files (e.g., PDFs, images, documents) in relational databases. However, applications often need to convert that binary data into a String (for readability, logs, or transmission) and back into a Blob for persistence. This article demonstrates how to do that in Java.
1. Converting String to Blob
We transform text into binary data using UTF-8 encoding. UTF-8 is widely recognised as the standard for text encoding because it supports all Unicode characters, including emojis and multilingual content, unlike default platform encodings, which can differ and potentially corrupt data.
public class StringToBlobConverter {
public Blob convert(String data) throws SQLException {
if (data == null) {
return null;
}
if (data.isEmpty()) {
return new SerialBlob(new byte[0]);
}
byte[] bytes = data.getBytes(StandardCharsets.UTF_8);
return new SerialBlob(bytes);
}
}
Inside the convert method, the input string is checked for special cases to prevent errors: if the string is null, the method returns null, and if the string is empty, it returns an empty Blob. For valid text, the string is transformed into a byte array using UTF-8 to ensure safe handling of all characters, including international text and emojis, and then wrapped in a SerialBlob, which represents a Blob stored in memory.
2. Converting Blob to String
To safely convert a Blob back into a String, we need to handle several scenarios: the Blob might be null, empty, or exceptionally large. Using UTF-8 encoding ensures that all characters, including international text and emojis, are preserved accurately.
Additionally, we check the Blob size to prevent errors, since Blob.getBytes() requires the length as an integer and cannot handle Blobs larger than Integer.MAX_VALUE bytes.
public class BlobToStringConverter {
public String convert(Blob blob) throws Exception {
if (blob == null) {
return null;
}
long length = blob.length();
if (length == 0) {
return "";
}
if (length > Integer.MAX_VALUE) {
throw new IllegalArgumentException(
"Blob is too large to convert — exceeds max int capacity");
}
byte[] bytes = blob.getBytes(1, (int) length);
return new String(bytes, StandardCharsets.UTF_8);
}
}
This method converts a Blob object into a String while handling various edge cases to ensure safe and reliable conversion. It first checks if the Blob is null and returns null immediately. Then it retrieves the Blob’s length and returns an empty string if the Blob contains no data.
To prevent errors with oversized Blobs, it throws an IllegalArgumentException if the length exceeds Integer.MAX_VALUE, since Blob.getBytes() requires an integer length. For valid Blobs, it reads the bytes starting at position 1 and constructs a new String using UTF-8 encoding, preserving all characters, including international and special characters.
3. Bi-Directional Conversion Tests
Now we verify that both conversion directions work, especially ensuring the original string survives the round trip from String to Blob to String.
public class BlobConversionTest {
private final StringToBlobConverter s2b = new StringToBlobConverter();
private final BlobToStringConverter b2s = new BlobToStringConverter();
@Test
void testNullHandling() throws Exception {
Assertions.assertNull(s2b.convert(null));
Assertions.assertNull(b2s.convert(null));
}
@Test
void testEmptyConversion() throws Exception {
Blob blob = s2b.convert("");
String text = b2s.convert(blob);
Assertions.assertEquals("", text);
}
@Test
void testRoundTrip() throws Exception {
String original = "Café 🌟 naïve résumé 🎉 München 🍕 こんにちは 😀";
Blob blob = s2b.convert(original);
String result = b2s.convert(blob);
Assertions.assertEquals(original, result);
}
}
These tests confirm that the conversion methods handle null inputs without runtime errors, process empty BLOBs safely, and preserve data integrity during UTF-8 round-trip conversions from String to Blob and back.
4. Conclusion
In this article, we explored how to safely convert between String and Blob in Java, handling null and empty values, preventing errors with oversized Blobs, and preserving text integrity using UTF-8 encoding. The examples and tests demonstrate a reliable approach for storing and retrieving textual data in binary form for database applications.
5. Download the Source Code
This article covered how Java can convert between Blob and String.
You can download the full source code of this example here: convert between blob and string

