Permalink
Browse files

HHH-7066 make sure buffer size is at least 1

  • Loading branch information...
Sanne committed Feb 13, 2012
1 parent f51779d commit 18185e58f0dee5d364870c9f16acc0124dc7a78a
Showing with 13 additions and 2 deletions.
  1. +13 −2 hibernate-core/src/main/java/org/hibernate/type/descriptor/java/DataHelper.java
@@ -73,7 +73,7 @@ public static String extractString(Reader reader) {
*/
public static String extractString(Reader reader, int lengthHint) {
// read the Reader contents into a buffer and return the complete string
- final int bufferSize = Math.min( lengthHint, 2048 );
+ final int bufferSize = getSuggestedBufferSize( lengthHint );
final StringBuilder stringBuilder = new StringBuilder( bufferSize );
try {
char[] buffer = new char[bufferSize];
@@ -115,7 +115,7 @@ private static String extractString(Reader characterStream, long start, int leng
if ( skipped != start ) {
throw new HibernateException( "Unable to skip needed bytes" );
}
- final int bufferSize = Math.min( length, 2048 );
+ final int bufferSize = getSuggestedBufferSize( length );
char[] buffer = new char[bufferSize];
int charsRead = 0;
while ( true ) {
@@ -280,4 +280,15 @@ public static String extractString(final Clob value) {
throw new HibernateException( "Unable to access lob stream", e );
}
}
+
+ /**
+ * Make sure we allocate a buffer sized not bigger than 2048,
+ * not higher than what is actually needed, and at least one.
+ *
+ * @param lengthHint the expected size of the full value
+ * @return the buffer size
+ */
+ private static final int getSuggestedBufferSize(final int lengthHint) {
+ return Math.max( 1, Math.min( lengthHint , 2048 ) );
+ }
}

0 comments on commit 18185e5

Please sign in to comment.