Skip to content

Commit 16a15ea

Browse files
committed
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 <[email protected]>
1 parent 2924fe8 commit 16a15ea

16 files changed

+63
-368
lines changed

hibernate-core/src/main/java/org/hibernate/type/descriptor/java/ArrayJavaType.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -285,10 +285,10 @@ public <X> T[] wrap(X value, WrapperOptions options) {
285285
return null;
286286
}
287287

288-
if ( value instanceof java.sql.Array ) {
288+
if ( value instanceof java.sql.Array array ) {
289289
try {
290290
//noinspection unchecked
291-
value = (X) ( (java.sql.Array) value ).getArray();
291+
value = (X) array.getArray();
292292
}
293293
catch ( SQLException ex ) {
294294
// This basically shouldn't happen unless you've lost connection to the database.

hibernate-core/src/main/java/org/hibernate/type/descriptor/java/BooleanPrimitiveArrayJavaType.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -138,10 +138,10 @@ public <X> boolean[] wrap(X value, WrapperOptions options) {
138138
return null;
139139
}
140140

141-
if ( value instanceof java.sql.Array ) {
141+
if ( value instanceof java.sql.Array array ) {
142142
try {
143143
//noinspection unchecked
144-
value = (X) ( (java.sql.Array) value ).getArray();
144+
value = (X) array.getArray();
145145
}
146146
catch ( SQLException ex ) {
147147
// This basically shouldn't happen unless you've lost connection to the database.

hibernate-core/src/main/java/org/hibernate/type/descriptor/java/ByteArrayJavaType.java

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -112,18 +112,18 @@ public <X> Byte[] wrap(X value, WrapperOptions options) {
112112
if ( value == null ) {
113113
return null;
114114
}
115-
if (value instanceof Byte[]) {
116-
return (Byte[]) value;
115+
if (value instanceof Byte[] bytes) {
116+
return bytes;
117117
}
118-
if (value instanceof byte[]) {
119-
return wrapBytes( (byte[]) value );
118+
if (value instanceof byte[] bytes) {
119+
return wrapBytes( bytes );
120120
}
121-
if (value instanceof InputStream) {
122-
return wrapBytes( DataHelper.extractBytes( (InputStream) value ) );
121+
if (value instanceof InputStream inputStream) {
122+
return wrapBytes( DataHelper.extractBytes( inputStream ) );
123123
}
124-
if ( value instanceof Blob || DataHelper.isNClob( value.getClass() ) ) {
124+
if ( value instanceof Blob blob ) {
125125
try {
126-
return wrapBytes( DataHelper.extractBytes( ( (Blob) value ).getBinaryStream() ) );
126+
return wrapBytes( DataHelper.extractBytes( blob.getBinaryStream() ) );
127127
}
128128
catch ( SQLException e ) {
129129
throw new HibernateException( "Unable to access lob stream", e );

hibernate-core/src/main/java/org/hibernate/type/descriptor/java/ClobJavaType.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -99,7 +99,7 @@ else if ( String.class.isAssignableFrom( type ) ) {
9999
}
100100
else {
101101
// otherwise extract the bytes from the stream manually
102-
return (X) LobStreamDataHelper.extractString( value.getCharacterStream() );
102+
return (X) DataHelper.extractString( value.getCharacterStream() );
103103
}
104104
}
105105
else if ( Clob.class.isAssignableFrom( type ) ) {

hibernate-core/src/main/java/org/hibernate/type/descriptor/java/DataHelper.java

Lines changed: 22 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -15,17 +15,19 @@
1515
import java.sql.SQLFeatureNotSupportedException;
1616

1717
import org.hibernate.HibernateException;
18+
import org.hibernate.Internal;
1819
import org.hibernate.engine.jdbc.BinaryStream;
1920
import org.hibernate.engine.jdbc.internal.BinaryStreamImpl;
2021
import org.hibernate.internal.CoreMessageLogger;
2122

2223
import org.jboss.logging.Logger;
2324

2425
/**
25-
* A help for dealing with BLOB and CLOB data
26+
* A helper for dealing with {@code BLOB} and {@code CLOB} data
2627
*
2728
* @author Steve Ebersole
2829
*/
30+
@Internal
2931
public final class DataHelper {
3032
private DataHelper() {
3133
}
@@ -35,10 +37,6 @@ private DataHelper() {
3537

3638
private static final CoreMessageLogger LOG = Logger.getMessageLogger( MethodHandles.lookup(), CoreMessageLogger.class, DataHelper.class.getName() );
3739

38-
public static boolean isNClob(final Class type) {
39-
return java.sql.NClob.class.isAssignableFrom( type );
40-
}
41-
4240
/**
4341
* Extract the contents of the given reader/stream as a string.
4442
* The reader will be closed.
@@ -65,7 +63,7 @@ public static String extractString(Reader reader, int lengthHint) {
6563
final int bufferSize = getSuggestedBufferSize( lengthHint );
6664
final StringBuilder stringBuilder = new StringBuilder( bufferSize );
6765
try {
68-
char[] buffer = new char[bufferSize];
66+
final char[] buffer = new char[bufferSize];
6967
while (true) {
7068
int amountRead = reader.read( buffer, 0, bufferSize );
7169
if ( amountRead == -1 ) {
@@ -101,17 +99,17 @@ private static String extractString(Reader characterStream, long start, int leng
10199
if ( length == 0 ) {
102100
return "";
103101
}
104-
StringBuilder stringBuilder = new StringBuilder( length );
102+
final StringBuilder stringBuilder = new StringBuilder( length );
105103
try {
106-
long skipped = characterStream.skip( start );
104+
final long skipped = characterStream.skip( start );
107105
if ( skipped != start ) {
108106
throw new HibernateException( "Unable to skip needed bytes" );
109107
}
110108
final int bufferSize = getSuggestedBufferSize( length );
111-
char[] buffer = new char[bufferSize];
109+
final char[] buffer = new char[bufferSize];
112110
int charsRead = 0;
113111
while ( true ) {
114-
int amountRead = characterStream.read( buffer, 0, bufferSize );
112+
final int amountRead = characterStream.read( buffer, 0, bufferSize );
115113
if ( amountRead == -1 ) {
116114
break;
117115
}
@@ -153,16 +151,16 @@ public static Object subStream(Reader characterStream, long start, int length) {
153151
* @return The contents as a {@code byte[]}
154152
*/
155153
public static byte[] extractBytes(InputStream inputStream) {
156-
if ( inputStream instanceof BinaryStream ) {
157-
return ( (BinaryStream ) inputStream ).getBytes();
154+
if ( inputStream instanceof BinaryStream binaryStream ) {
155+
return binaryStream.getBytes();
158156
}
159157

160158
// read the stream contents into a buffer and return the complete byte[]
161-
ByteArrayOutputStream outputStream = new ByteArrayOutputStream(BUFFER_SIZE);
159+
final ByteArrayOutputStream outputStream = new ByteArrayOutputStream(BUFFER_SIZE);
162160
try {
163-
byte[] buffer = new byte[BUFFER_SIZE];
161+
final byte[] buffer = new byte[BUFFER_SIZE];
164162
while (true) {
165-
int amountRead = inputStream.read( buffer );
163+
final int amountRead = inputStream.read( buffer );
166164
if ( amountRead == -1 ) {
167165
break;
168166
}
@@ -199,24 +197,25 @@ public static byte[] extractBytes(InputStream inputStream) {
199197
* @return The extracted bytes
200198
*/
201199
public static byte[] extractBytes(InputStream inputStream, long start, int length) {
202-
if ( inputStream instanceof BinaryStream && Integer.MAX_VALUE > start ) {
203-
byte[] data = ( (BinaryStream ) inputStream ).getBytes();
204-
int size = Math.min( length, data.length );
205-
byte[] result = new byte[size];
200+
if ( inputStream instanceof BinaryStream binaryStream
201+
&& Integer.MAX_VALUE > start ) {
202+
final byte[] data = binaryStream.getBytes();
203+
final int size = Math.min( length, data.length );
204+
final byte[] result = new byte[size];
206205
System.arraycopy( data, (int) start, result, 0, size );
207206
return result;
208207
}
209208

210-
ByteArrayOutputStream outputStream = new ByteArrayOutputStream( length );
209+
final ByteArrayOutputStream outputStream = new ByteArrayOutputStream( length );
211210
try {
212-
long skipped = inputStream.skip( start );
211+
final long skipped = inputStream.skip( start );
213212
if ( skipped != start ) {
214213
throw new HibernateException( "Unable to skip needed bytes" );
215214
}
216-
byte[] buffer = new byte[BUFFER_SIZE];
215+
final byte[] buffer = new byte[BUFFER_SIZE];
217216
int bytesRead = 0;
218217
while ( true ) {
219-
int amountRead = inputStream.read( buffer );
218+
final int amountRead = inputStream.read( buffer );
220219
if ( amountRead == -1 ) {
221220
break;
222221
}

hibernate-core/src/main/java/org/hibernate/type/descriptor/java/DoublePrimitiveArrayJavaType.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -138,10 +138,10 @@ public <X> double[] wrap(X value, WrapperOptions options) {
138138
return null;
139139
}
140140

141-
if ( value instanceof java.sql.Array ) {
141+
if ( value instanceof java.sql.Array array ) {
142142
try {
143143
//noinspection unchecked
144-
value = (X) ( (java.sql.Array) value ).getArray();
144+
value = (X) array.getArray();
145145
}
146146
catch ( SQLException ex ) {
147147
// This basically shouldn't happen unless you've lost connection to the database.

hibernate-core/src/main/java/org/hibernate/type/descriptor/java/FloatPrimitiveArrayJavaType.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -138,10 +138,10 @@ public <X> float[] wrap(X value, WrapperOptions options) {
138138
return null;
139139
}
140140

141-
if ( value instanceof java.sql.Array ) {
141+
if ( value instanceof java.sql.Array array ) {
142142
try {
143143
//noinspection unchecked
144-
value = (X) ( (java.sql.Array) value ).getArray();
144+
value = (X) array.getArray();
145145
}
146146
catch ( SQLException ex ) {
147147
// This basically shouldn't happen unless you've lost connection to the database.

hibernate-core/src/main/java/org/hibernate/type/descriptor/java/IntegerPrimitiveArrayJavaType.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -138,10 +138,10 @@ public <X> int[] wrap(X value, WrapperOptions options) {
138138
return null;
139139
}
140140

141-
if ( value instanceof java.sql.Array ) {
141+
if ( value instanceof java.sql.Array array ) {
142142
try {
143143
//noinspection unchecked
144-
value = (X) ( (java.sql.Array) value ).getArray();
144+
value = (X) array.getArray();
145145
}
146146
catch ( SQLException ex ) {
147147
// This basically shouldn't happen unless you've lost connection to the database.

0 commit comments

Comments
 (0)