HHH-7066 Use known length from Clob to allocate StringBuffer

This commit is contained in:
Sanne Grinovero 2012-02-11 16:56:18 +00:00
parent 6497ff6a52
commit 98d0b8398d
2 changed files with 44 additions and 11 deletions

View File

@ -28,6 +28,8 @@ import java.io.IOException;
import java.io.InputStream;
import java.io.Reader;
import java.io.StringReader;
import java.sql.Clob;
import java.sql.SQLException;
import org.jboss.logging.Logger;
@ -50,18 +52,33 @@ public class DataHelper {
/**
* Extract the contents of the given reader/stream as a string.
* The reader will be closed.
*
* @param reader The reader for the content
*
* @return The content as string
*/
public static String extractString(Reader reader) {
return extractString( reader, 2048 );
}
/**
* Extract the contents of the given reader/stream as a string.
* The reader will be closed.
*
* @param reader The reader for the content
* @param lengthHint if the length is known in advance the implementation can be slightly more efficient
*
* @return The content as string
*/
public static String extractString(Reader reader, int lengthHint) {
// read the Reader contents into a buffer and return the complete string
final StringBuilder stringBuilder = new StringBuilder();
final StringBuilder stringBuilder = new StringBuilder( lengthHint );
try {
char[] buffer = new char[2048];
final int bufferSize = Math.min( lengthHint, 2048 );
char[] buffer = new char[bufferSize];
while (true) {
int amountRead = reader.read( buffer, 0, buffer.length );
int amountRead = reader.read( buffer, 0, bufferSize );
if ( amountRead == -1 ) {
break;
}
@ -240,4 +257,27 @@ public class DataHelper {
public static InputStream subStream(InputStream inputStream, long start, int length) {
return new BinaryStreamImpl( extractBytes( inputStream, start, length ) );
}
/**
* Extract the contents of the given Clob as a string.
*
* @param reader The reader for the content
*
* @return The content as string
*/
public static String extractString(final Clob value) {
try {
Reader characterStream = value.getCharacterStream();
long length = value.length();
if ( length > Integer.MAX_VALUE ) {
return extractString( characterStream, Integer.MAX_VALUE );
}
else {
return extractString( characterStream, (int) length );
}
}
catch ( SQLException e ) {
throw new HibernateException( "Unable to access lob stream", e );
}
}
}

View File

@ -26,9 +26,7 @@ package org.hibernate.type.descriptor.java;
import java.io.Reader;
import java.io.StringReader;
import java.sql.Clob;
import java.sql.SQLException;
import org.hibernate.HibernateException;
import org.hibernate.type.descriptor.CharacterStream;
import org.hibernate.type.descriptor.WrapperOptions;
@ -87,12 +85,7 @@ public class StringTypeDescriptor extends AbstractTypeDescriptor<String> {
return DataHelper.extractString( (Reader) value );
}
if ( Clob.class.isInstance( value ) || DataHelper.isNClob( value.getClass() ) ) {
try {
return DataHelper.extractString( ( (Clob) value ).getCharacterStream() );
}
catch ( SQLException e ) {
throw new HibernateException( "Unable to access lob stream", e );
}
return DataHelper.extractString( (Clob) value );
}
throw unknownWrap( value.getClass() );