|
30 | 30 | import java.io.StringReader;
|
31 | 31 | import java.sql.Clob;
|
32 | 32 | import java.sql.SQLException;
|
| 33 | +import java.sql.SQLFeatureNotSupportedException; |
33 | 34 |
|
34 | 35 | import org.jboss.logging.Logger;
|
35 | 36 |
|
@@ -268,34 +269,49 @@ public static InputStream subStream(InputStream inputStream, long start, int len
|
268 | 269 | /**
|
269 | 270 | * Extract the contents of the given Clob as a string.
|
270 | 271 | *
|
271 |
| - * @param reader The reader for the content |
| 272 | + * @param value The clob to to be extracted from |
272 | 273 | *
|
273 | 274 | * @return The content as string
|
274 | 275 | */
|
275 | 276 | public static String extractString(final Clob value) {
|
276 | 277 | try {
|
277 |
| - Reader characterStream = value.getCharacterStream(); |
278 |
| - long length = value.length(); |
279 |
| - if ( length > Integer.MAX_VALUE ) { |
280 |
| - return extractString( characterStream, Integer.MAX_VALUE ); |
281 |
| - } |
282 |
| - else { |
283 |
| - return extractString( characterStream, (int) length ); |
284 |
| - } |
| 278 | + final Reader characterStream = value.getCharacterStream(); |
| 279 | + final long length = determineLengthForBufferSizing( value ); |
| 280 | + return length > Integer.MAX_VALUE |
| 281 | + ? extractString( characterStream, Integer.MAX_VALUE ) |
| 282 | + : extractString( characterStream, (int) length ); |
285 | 283 | }
|
286 | 284 | catch ( SQLException e ) {
|
287 | 285 | throw new HibernateException( "Unable to access lob stream", e );
|
288 | 286 | }
|
289 | 287 | }
|
290 | 288 |
|
| 289 | + /** |
| 290 | + * Determine a buffer size for reading the underlying character stream. |
| 291 | + * |
| 292 | + * @param value The Clob value |
| 293 | + * |
| 294 | + * @return The appropriate buffer size ({@link java.sql.Clob#length()} by default. |
| 295 | + * |
| 296 | + * @throws SQLException |
| 297 | + */ |
| 298 | + private static long determineLengthForBufferSizing(Clob value) throws SQLException { |
| 299 | + try { |
| 300 | + return value.length(); |
| 301 | + } |
| 302 | + catch ( SQLFeatureNotSupportedException e ) { |
| 303 | + return BUFFER_SIZE; |
| 304 | + } |
| 305 | + } |
| 306 | + |
291 | 307 | /**
|
292 | 308 | * Make sure we allocate a buffer sized not bigger than 2048,
|
293 | 309 | * not higher than what is actually needed, and at least one.
|
294 | 310 | *
|
295 | 311 | * @param lengthHint the expected size of the full value
|
296 | 312 | * @return the buffer size
|
297 | 313 | */
|
298 |
| - private static final int getSuggestedBufferSize(final int lengthHint) { |
| 314 | + private static int getSuggestedBufferSize(final int lengthHint) { |
299 | 315 | return Math.max( 1, Math.min( lengthHint , BUFFER_SIZE ) );
|
300 | 316 | }
|
301 | 317 | }
|
0 commit comments