这是indexloc提供的服务,不要输入任何密码
Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
package com.baeldung.blobstringconverter;

import java.sql.Blob;
import java.sql.SQLException;
import java.nio.charset.StandardCharsets;
import javax.sql.rowset.serial.SerialBlob;

public class BlobStringConverter {

/**
* Converts a java.sql.Blob object to a String using UTF-8 encoding.
* @param blob The Blob object to convert.
* @return The String representation of the Blob data.
* @throws SQLException If a database access error occurs.
*/
public static String blobToString(Blob blob) throws SQLException {
if (blob == null) {
return null;
}

long length = blob.length();

// Check for zero length to avoid SerialException: Invalid arguments.
if (length == 0) {
return "";
}

if (length > Integer.MAX_VALUE) {
throw new SQLException("Blob is too large for a single String conversion.");
}

// Get the entire Blob content as a byte array. The position starts at 1.
byte[] bytes = blob.getBytes(1, (int) length);

// Convert the byte array to a String using the UTF-8 charset
return new String(bytes, StandardCharsets.UTF_8);
}

/**
* Converts a String object to a java.sql.Blob using UTF-8 encoding.
* @param text The String to convert.
* @return A SerialBlob object containing the String data.
* @throws SQLException If a database access error occurs.
*/
public static Blob stringToBlob(String text) throws SQLException {
if (text == null) {
return null;
}

// Convert the String to a byte array using the UTF-8 charset.
// This is safe even for an empty string ("") which results in a zero-length byte array.
byte[] bytes = text.getBytes(StandardCharsets.UTF_8);

// Create a SerialBlob object from the byte array
return new SerialBlob(bytes);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
package com.baeldung.blobstringconverter;

import org.junit.jupiter.api.Test;
import java.sql.Blob;
import java.sql.SQLException;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertNull;
import static org.junit.jupiter.api.Assertions.assertNotNull;

public class BlobStringConverterUnitTest {

// Test a standard ASCII string
@Test
void givenStandardAsciiString_whenPerformingConversion_thenConversionSuccessful() throws SQLException {
String originalString = "Hello, world!";

// String to Blob
Blob blob = BlobStringConverter.stringToBlob(originalString);
assertNotNull(blob);

// Blob to String
String convertedString = BlobStringConverter.blobToString(blob);

// Assert the converted string is equal to the original
assertEquals(originalString, convertedString);
}

// Test a string with special characters (important for correct encoding)
@Test
void givenStringWithSpecialCharacters_whenPerformingConversion_thenConversionSuccessful() throws SQLException {
// String with non-ASCII characters (e.g., Japanese, German umlaut)
String originalString = "Test: こんにちは, äöü";

// String to Blob
Blob blob = BlobStringConverter.stringToBlob(originalString);

// Blob to String
String convertedString = BlobStringConverter.blobToString(blob);

assertEquals(originalString, convertedString);
}

// Test an empty string
@Test
void givenEmptyString_whenPerformingConversion_thenConversionSuccessful() throws SQLException {
String originalString = "";

// String to Blob
Blob blob = BlobStringConverter.stringToBlob(originalString);

// Blob to String
String convertedString = BlobStringConverter.blobToString(blob);

assertEquals(originalString, convertedString);
}

// Test null input for conversion methods
@Test
void givenNullString_whenPerformingConversion_thenConversionSuccessful() throws SQLException {
// Test String to Blob with null
assertNull(BlobStringConverter.stringToBlob(null),
"stringToBlob should return null for null input.");

// Test Blob to String with null
assertNull(BlobStringConverter.blobToString(null),
"blobToString should return null for null input.");
}
}