From 16a15ea6ac329b70d8fb1efb232143a032bf0f32 Mon Sep 17 00:00:00 2001 From: Gavin King Date: Wed, 30 Oct 2024 11:21:08 +0100 Subject: [PATCH] cleanups in descriptor.java package - delete a duplicate class - fix some incorrect usage of that class - continue using more flow typing Signed-off-by: Gavin King --- .../type/descriptor/java/ArrayJavaType.java | 4 +- .../java/BooleanPrimitiveArrayJavaType.java | 4 +- .../descriptor/java/ByteArrayJavaType.java | 16 +- .../type/descriptor/java/ClobJavaType.java | 2 +- .../type/descriptor/java/DataHelper.java | 45 ++- .../java/DoublePrimitiveArrayJavaType.java | 4 +- .../java/FloatPrimitiveArrayJavaType.java | 4 +- .../java/IntegerPrimitiveArrayJavaType.java | 4 +- .../descriptor/java/LobStreamDataHelper.java | 299 ------------------ .../java/LongPrimitiveArrayJavaType.java | 4 +- .../type/descriptor/java/NClobJavaType.java | 4 +- .../java/PrimitiveByteArrayJavaType.java | 12 +- .../descriptor/java/SerializableJavaType.java | 12 +- .../java/ShortPrimitiveArrayJavaType.java | 4 +- .../type/descriptor/java/StringJavaType.java | 9 +- .../java/spi/BasicCollectionJavaType.java | 4 +- 16 files changed, 63 insertions(+), 368 deletions(-) delete mode 100644 hibernate-core/src/main/java/org/hibernate/type/descriptor/java/LobStreamDataHelper.java diff --git a/hibernate-core/src/main/java/org/hibernate/type/descriptor/java/ArrayJavaType.java b/hibernate-core/src/main/java/org/hibernate/type/descriptor/java/ArrayJavaType.java index b802b48c68..106d2ef614 100644 --- a/hibernate-core/src/main/java/org/hibernate/type/descriptor/java/ArrayJavaType.java +++ b/hibernate-core/src/main/java/org/hibernate/type/descriptor/java/ArrayJavaType.java @@ -285,10 +285,10 @@ public class ArrayJavaType extends AbstractArrayJavaType { return null; } - if ( value instanceof java.sql.Array ) { + if ( value instanceof java.sql.Array array ) { try { //noinspection unchecked - value = (X) ( (java.sql.Array) value ).getArray(); + value = (X) array.getArray(); } catch ( SQLException ex ) { // This basically shouldn't happen unless you've lost connection to the database. diff --git a/hibernate-core/src/main/java/org/hibernate/type/descriptor/java/BooleanPrimitiveArrayJavaType.java b/hibernate-core/src/main/java/org/hibernate/type/descriptor/java/BooleanPrimitiveArrayJavaType.java index e37003deb2..e12f78a3a5 100644 --- a/hibernate-core/src/main/java/org/hibernate/type/descriptor/java/BooleanPrimitiveArrayJavaType.java +++ b/hibernate-core/src/main/java/org/hibernate/type/descriptor/java/BooleanPrimitiveArrayJavaType.java @@ -138,10 +138,10 @@ public class BooleanPrimitiveArrayJavaType extends AbstractArrayJavaType { if ( value == null ) { return null; } - if (value instanceof Byte[]) { - return (Byte[]) value; + if (value instanceof Byte[] bytes) { + return bytes; } - if (value instanceof byte[]) { - return wrapBytes( (byte[]) value ); + if (value instanceof byte[] bytes) { + return wrapBytes( bytes ); } - if (value instanceof InputStream) { - return wrapBytes( DataHelper.extractBytes( (InputStream) value ) ); + if (value instanceof InputStream inputStream) { + return wrapBytes( DataHelper.extractBytes( inputStream ) ); } - if ( value instanceof Blob || DataHelper.isNClob( value.getClass() ) ) { + if ( value instanceof Blob blob ) { try { - return wrapBytes( DataHelper.extractBytes( ( (Blob) value ).getBinaryStream() ) ); + return wrapBytes( DataHelper.extractBytes( blob.getBinaryStream() ) ); } catch ( SQLException e ) { throw new HibernateException( "Unable to access lob stream", e ); diff --git a/hibernate-core/src/main/java/org/hibernate/type/descriptor/java/ClobJavaType.java b/hibernate-core/src/main/java/org/hibernate/type/descriptor/java/ClobJavaType.java index b4b8687403..d3e8265b4d 100644 --- a/hibernate-core/src/main/java/org/hibernate/type/descriptor/java/ClobJavaType.java +++ b/hibernate-core/src/main/java/org/hibernate/type/descriptor/java/ClobJavaType.java @@ -99,7 +99,7 @@ public class ClobJavaType extends AbstractClassJavaType { } else { // otherwise extract the bytes from the stream manually - return (X) LobStreamDataHelper.extractString( value.getCharacterStream() ); + return (X) DataHelper.extractString( value.getCharacterStream() ); } } else if ( Clob.class.isAssignableFrom( type ) ) { diff --git a/hibernate-core/src/main/java/org/hibernate/type/descriptor/java/DataHelper.java b/hibernate-core/src/main/java/org/hibernate/type/descriptor/java/DataHelper.java index d1fdbfaf94..3b27172043 100644 --- a/hibernate-core/src/main/java/org/hibernate/type/descriptor/java/DataHelper.java +++ b/hibernate-core/src/main/java/org/hibernate/type/descriptor/java/DataHelper.java @@ -15,6 +15,7 @@ import java.sql.SQLException; import java.sql.SQLFeatureNotSupportedException; import org.hibernate.HibernateException; +import org.hibernate.Internal; import org.hibernate.engine.jdbc.BinaryStream; import org.hibernate.engine.jdbc.internal.BinaryStreamImpl; import org.hibernate.internal.CoreMessageLogger; @@ -22,10 +23,11 @@ import org.hibernate.internal.CoreMessageLogger; import org.jboss.logging.Logger; /** - * A help for dealing with BLOB and CLOB data + * A helper for dealing with {@code BLOB} and {@code CLOB} data * * @author Steve Ebersole */ +@Internal public final class DataHelper { private DataHelper() { } @@ -35,10 +37,6 @@ public final class DataHelper { private static final CoreMessageLogger LOG = Logger.getMessageLogger( MethodHandles.lookup(), CoreMessageLogger.class, DataHelper.class.getName() ); - public static boolean isNClob(final Class type) { - return java.sql.NClob.class.isAssignableFrom( type ); - } - /** * Extract the contents of the given reader/stream as a string. * The reader will be closed. @@ -65,7 +63,7 @@ public final class DataHelper { final int bufferSize = getSuggestedBufferSize( lengthHint ); final StringBuilder stringBuilder = new StringBuilder( bufferSize ); try { - char[] buffer = new char[bufferSize]; + final char[] buffer = new char[bufferSize]; while (true) { int amountRead = reader.read( buffer, 0, bufferSize ); if ( amountRead == -1 ) { @@ -101,17 +99,17 @@ public final class DataHelper { if ( length == 0 ) { return ""; } - StringBuilder stringBuilder = new StringBuilder( length ); + final StringBuilder stringBuilder = new StringBuilder( length ); try { - long skipped = characterStream.skip( start ); + final long skipped = characterStream.skip( start ); if ( skipped != start ) { throw new HibernateException( "Unable to skip needed bytes" ); } final int bufferSize = getSuggestedBufferSize( length ); - char[] buffer = new char[bufferSize]; + final char[] buffer = new char[bufferSize]; int charsRead = 0; while ( true ) { - int amountRead = characterStream.read( buffer, 0, bufferSize ); + final int amountRead = characterStream.read( buffer, 0, bufferSize ); if ( amountRead == -1 ) { break; } @@ -153,16 +151,16 @@ public final class DataHelper { * @return The contents as a {@code byte[]} */ public static byte[] extractBytes(InputStream inputStream) { - if ( inputStream instanceof BinaryStream ) { - return ( (BinaryStream ) inputStream ).getBytes(); + if ( inputStream instanceof BinaryStream binaryStream ) { + return binaryStream.getBytes(); } // read the stream contents into a buffer and return the complete byte[] - ByteArrayOutputStream outputStream = new ByteArrayOutputStream(BUFFER_SIZE); + final ByteArrayOutputStream outputStream = new ByteArrayOutputStream(BUFFER_SIZE); try { - byte[] buffer = new byte[BUFFER_SIZE]; + final byte[] buffer = new byte[BUFFER_SIZE]; while (true) { - int amountRead = inputStream.read( buffer ); + final int amountRead = inputStream.read( buffer ); if ( amountRead == -1 ) { break; } @@ -199,24 +197,25 @@ public final class DataHelper { * @return The extracted bytes */ public static byte[] extractBytes(InputStream inputStream, long start, int length) { - if ( inputStream instanceof BinaryStream && Integer.MAX_VALUE > start ) { - byte[] data = ( (BinaryStream ) inputStream ).getBytes(); - int size = Math.min( length, data.length ); - byte[] result = new byte[size]; + if ( inputStream instanceof BinaryStream binaryStream + && Integer.MAX_VALUE > start ) { + final byte[] data = binaryStream.getBytes(); + final int size = Math.min( length, data.length ); + final byte[] result = new byte[size]; System.arraycopy( data, (int) start, result, 0, size ); return result; } - ByteArrayOutputStream outputStream = new ByteArrayOutputStream( length ); + final ByteArrayOutputStream outputStream = new ByteArrayOutputStream( length ); try { - long skipped = inputStream.skip( start ); + final long skipped = inputStream.skip( start ); if ( skipped != start ) { throw new HibernateException( "Unable to skip needed bytes" ); } - byte[] buffer = new byte[BUFFER_SIZE]; + final byte[] buffer = new byte[BUFFER_SIZE]; int bytesRead = 0; while ( true ) { - int amountRead = inputStream.read( buffer ); + final int amountRead = inputStream.read( buffer ); if ( amountRead == -1 ) { break; } diff --git a/hibernate-core/src/main/java/org/hibernate/type/descriptor/java/DoublePrimitiveArrayJavaType.java b/hibernate-core/src/main/java/org/hibernate/type/descriptor/java/DoublePrimitiveArrayJavaType.java index f792d0b53c..bbeedcde33 100644 --- a/hibernate-core/src/main/java/org/hibernate/type/descriptor/java/DoublePrimitiveArrayJavaType.java +++ b/hibernate-core/src/main/java/org/hibernate/type/descriptor/java/DoublePrimitiveArrayJavaType.java @@ -138,10 +138,10 @@ public class DoublePrimitiveArrayJavaType extends AbstractArrayJavaType= length ) { - break; - } - } - } - catch ( IOException ioe ) { - throw new HibernateException( "IOException occurred reading a binary value", ioe ); - } - return stringBuilder.toString(); - } - - /** - * Extract a portion of a reader, wrapping the portion in a new reader. - * - * @param characterStream The reader for the content - * @param start The start position/offset (0-based, per general stream/reader contracts). - * @param length The amount to extract - * - * @return The content portion as a reader - */ - public static Object subStream(Reader characterStream, long start, int length) { - return new StringReader( extractString( characterStream, start, length ) ); - } - - /** - * Extract by bytes from the given stream. - * - * @param inputStream The stream of bytes. - * - * @return The contents as a {@code byte[]} - */ - public static byte[] extractBytes(InputStream inputStream) { - if ( inputStream instanceof BinaryStream ) { - return ( (BinaryStream) inputStream ).getBytes(); - } - - // read the stream contents into a buffer and return the complete byte[] - ByteArrayOutputStream outputStream = new ByteArrayOutputStream(BUFFER_SIZE); - try { - byte[] buffer = new byte[BUFFER_SIZE]; - while (true) { - int amountRead = inputStream.read( buffer ); - if ( amountRead == -1 ) { - break; - } - outputStream.write( buffer, 0, amountRead ); - } - } - catch ( IOException ioe ) { - throw new HibernateException( "IOException occurred reading a binary value", ioe ); - } - finally { - try { - inputStream.close(); - } - catch ( IOException e ) { - LOG.unableToCloseInputStream( e ); - } - try { - outputStream.close(); - } - catch ( IOException e ) { - LOG.unableToCloseOutputStream( e ); - } - } - return outputStream.toByteArray(); - } - - /** - * Extract a portion of the bytes from the given stream. - * - * @param inputStream The stream of bytes. - * @param start The start position/offset (0-based, per general stream/reader contracts). - * @param length The amount to extract - * - * @return The extracted bytes - */ - public static byte[] extractBytes(InputStream inputStream, long start, int length) { - if ( inputStream instanceof BinaryStream && Integer.MAX_VALUE > start ) { - byte[] data = ( (BinaryStream) inputStream ).getBytes(); - int size = Math.min( length, data.length ); - byte[] result = new byte[size]; - System.arraycopy( data, (int) start, result, 0, size ); - return result; - } - - ByteArrayOutputStream outputStream = new ByteArrayOutputStream( length ); - try { - long skipped = inputStream.skip( start ); - if ( skipped != start ) { - throw new HibernateException( "Unable to skip needed bytes" ); - } - byte[] buffer = new byte[BUFFER_SIZE]; - int bytesRead = 0; - while ( true ) { - int amountRead = inputStream.read( buffer ); - if ( amountRead == -1 ) { - break; - } - outputStream.write( buffer, 0, amountRead ); - if ( amountRead < buffer.length ) { - // we have read up to the end of stream - break; - } - bytesRead += amountRead; - if ( bytesRead >= length ) { - break; - } - } - } - catch ( IOException ioe ) { - throw new HibernateException( "IOException occurred reading a binary value", ioe ); - } - return outputStream.toByteArray(); - } - - /** - * Extract a portion of the bytes from the given stream., wrapping them in a new stream. - * - * @param inputStream The stream of bytes. - * @param start The start position/offset (0-based, per general stream/reader contracts). - * @param length The amount to extract - * - * @return The extracted bytes as a stream - */ - 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 value The clob to to be extracted from - * - * @return The content as string - */ - public static String extractString(final Clob value) { - try { - final Reader characterStream = value.getCharacterStream(); - final long length = determineLengthForBufferSizing( value ); - return length > Integer.MAX_VALUE - ? extractString( characterStream, Integer.MAX_VALUE ) - : extractString( characterStream, (int) length ); - } - catch ( SQLException e ) { - throw new HibernateException( "Unable to access lob stream", e ); - } - } - - /** - * Determine a buffer size for reading the underlying character stream. - * - * @param value The Clob value - * - * @return The appropriate buffer size ({@link Clob#length()} by default. - * - */ - private static long determineLengthForBufferSizing(Clob value) throws SQLException { - try { - return value.length(); - } - catch ( SQLFeatureNotSupportedException e ) { - return BUFFER_SIZE; - } - } - - /** - * 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 int getSuggestedBufferSize(final int lengthHint) { - return Math.max( 1, Math.min( lengthHint , BUFFER_SIZE ) ); - } -} diff --git a/hibernate-core/src/main/java/org/hibernate/type/descriptor/java/LongPrimitiveArrayJavaType.java b/hibernate-core/src/main/java/org/hibernate/type/descriptor/java/LongPrimitiveArrayJavaType.java index d0aa6b069b..7a28cc69d6 100644 --- a/hibernate-core/src/main/java/org/hibernate/type/descriptor/java/LongPrimitiveArrayJavaType.java +++ b/hibernate-core/src/main/java/org/hibernate/type/descriptor/java/LongPrimitiveArrayJavaType.java @@ -138,10 +138,10 @@ public class LongPrimitiveArrayJavaType extends AbstractArrayJavaType { try { if ( CharacterStream.class.isAssignableFrom( type ) ) { - if (value instanceof NClobImplementer) { + if (value instanceof NClobImplementer clobImplementer) { // if the incoming NClob is a wrapper, just pass along its BinaryStream - return (X) ( (NClobImplementer) value ).getUnderlyingStream(); + return (X) clobImplementer.getUnderlyingStream(); } else { // otherwise we need to build a BinaryStream... diff --git a/hibernate-core/src/main/java/org/hibernate/type/descriptor/java/PrimitiveByteArrayJavaType.java b/hibernate-core/src/main/java/org/hibernate/type/descriptor/java/PrimitiveByteArrayJavaType.java index f10416ce9c..e62159b47e 100644 --- a/hibernate-core/src/main/java/org/hibernate/type/descriptor/java/PrimitiveByteArrayJavaType.java +++ b/hibernate-core/src/main/java/org/hibernate/type/descriptor/java/PrimitiveByteArrayJavaType.java @@ -120,15 +120,15 @@ public class PrimitiveByteArrayJavaType extends AbstractClassJavaType if ( value == null ) { return null; } - if (value instanceof byte[]) { - return (byte[]) value; + if (value instanceof byte[] bytes) { + return bytes; } - if (value instanceof InputStream) { - return DataHelper.extractBytes( (InputStream) value ); + if (value instanceof InputStream inputStream) { + return DataHelper.extractBytes( inputStream ); } - if ( value instanceof Blob || DataHelper.isNClob( value.getClass() ) ) { + if ( value instanceof Blob blob ) { try { - return DataHelper.extractBytes( ( (Blob) value ).getBinaryStream() ); + return DataHelper.extractBytes( blob.getBinaryStream() ); } catch ( SQLException e ) { throw new HibernateException( "Unable to access lob stream", e ); diff --git a/hibernate-core/src/main/java/org/hibernate/type/descriptor/java/SerializableJavaType.java b/hibernate-core/src/main/java/org/hibernate/type/descriptor/java/SerializableJavaType.java index f336377a30..5785eaa969 100644 --- a/hibernate-core/src/main/java/org/hibernate/type/descriptor/java/SerializableJavaType.java +++ b/hibernate-core/src/main/java/org/hibernate/type/descriptor/java/SerializableJavaType.java @@ -121,15 +121,15 @@ public class SerializableJavaType extends AbstractClassJ if ( value == null ) { return null; } - else if (value instanceof byte[]) { - return fromBytes( (byte[]) value ); + else if (value instanceof byte[] bytes) { + return fromBytes( bytes ); } - else if (value instanceof InputStream) { - return fromBytes( DataHelper.extractBytes( (InputStream) value ) ); + else if (value instanceof InputStream inputStream) { + return fromBytes( DataHelper.extractBytes( inputStream ) ); } - else if (value instanceof Blob) { + else if (value instanceof Blob blob) { try { - return fromBytes( DataHelper.extractBytes( ((Blob) value).getBinaryStream() ) ); + return fromBytes( DataHelper.extractBytes( blob.getBinaryStream() ) ); } catch ( SQLException e ) { throw new HibernateException( e ); diff --git a/hibernate-core/src/main/java/org/hibernate/type/descriptor/java/ShortPrimitiveArrayJavaType.java b/hibernate-core/src/main/java/org/hibernate/type/descriptor/java/ShortPrimitiveArrayJavaType.java index bd55333e46..ab2fa7336a 100644 --- a/hibernate-core/src/main/java/org/hibernate/type/descriptor/java/ShortPrimitiveArrayJavaType.java +++ b/hibernate-core/src/main/java/org/hibernate/type/descriptor/java/ShortPrimitiveArrayJavaType.java @@ -138,10 +138,10 @@ public class ShortPrimitiveArrayJavaType extends AbstractArrayJavaType { // Since NClob extends Clob, we need to check if type is an NClob // before checking if type is a Clob. That will ensure that // the correct type is returned. - if ( DataHelper.isNClob( type ) ) { - return (X) options.getLobCreator().createNClob( value ); - } if ( NClob.class.isAssignableFrom( type ) ) { return (X) options.getLobCreator().createNClob( value ); } @@ -87,12 +84,10 @@ public class StringJavaType extends AbstractClassJavaType { return (X) options.getLobCreator().createClob( value ); } if ( Integer.class.isAssignableFrom( type ) ) { - Integer parsed = Integer.parseInt( value ); - return (X) parsed; + return (X) (Integer) Integer.parseInt( value ); } if ( Long.class.isAssignableFrom( type ) ) { - Long parsed = Long.parseLong( value ); - return (X) parsed; + return (X) (Long) Long.parseLong( value ); } throw unknownUnwrap( type ); diff --git a/hibernate-core/src/main/java/org/hibernate/type/descriptor/java/spi/BasicCollectionJavaType.java b/hibernate-core/src/main/java/org/hibernate/type/descriptor/java/spi/BasicCollectionJavaType.java index 7217fe9c62..df73561914 100644 --- a/hibernate-core/src/main/java/org/hibernate/type/descriptor/java/spi/BasicCollectionJavaType.java +++ b/hibernate-core/src/main/java/org/hibernate/type/descriptor/java/spi/BasicCollectionJavaType.java @@ -379,10 +379,10 @@ public class BasicCollectionJavaType, E> extends Abstrac return null; } - if ( value instanceof java.sql.Array ) { + if ( value instanceof java.sql.Array array ) { try { //noinspection unchecked - value = (X) ( (java.sql.Array) value ).getArray(); + value = (X) array.getArray(); } catch ( SQLException ex ) { // This basically shouldn't happen unless you've lost connection to the database.