HHH-7066 make sure buffer size is at least 1

This commit is contained in:
Sanne Grinovero 2012-02-13 19:11:25 +00:00
parent f51779d2f7
commit 18185e58f0
1 changed files with 13 additions and 2 deletions

View File

@ -73,7 +73,7 @@ public class DataHelper {
*/
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 @@ public class DataHelper {
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 class DataHelper {
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 ) );
}
}