Skip to content

Commit

Permalink
HHH-7066 make sure buffer size is at least 1
Browse files Browse the repository at this point in the history
  • Loading branch information
Sanne committed Feb 15, 2012
1 parent f51779d commit 18185e5
Showing 1 changed file with 13 additions and 2 deletions.
Expand Up @@ -73,7 +73,7 @@ public static String extractString(Reader reader) {
*/ */
public static String extractString(Reader reader, int lengthHint) { public static String extractString(Reader reader, int lengthHint) {
// read the Reader contents into a buffer and return the complete string // 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 ); final StringBuilder stringBuilder = new StringBuilder( bufferSize );
try { try {
char[] buffer = new char[bufferSize]; char[] buffer = new char[bufferSize];
Expand Down Expand Up @@ -115,7 +115,7 @@ private static String extractString(Reader characterStream, long start, int leng
if ( skipped != start ) { if ( skipped != start ) {
throw new HibernateException( "Unable to skip needed bytes" ); 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]; char[] buffer = new char[bufferSize];
int charsRead = 0; int charsRead = 0;
while ( true ) { while ( true ) {
Expand Down Expand Up @@ -280,4 +280,15 @@ public static String extractString(final Clob value) {
throw new HibernateException( "Unable to access lob stream", e ); 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.