13
13
import java .io .ObjectStreamClass ;
14
14
import java .io .OutputStream ;
15
15
import java .io .Serializable ;
16
+ import java .util .Objects ;
16
17
17
18
import org .hibernate .Hibernate ;
18
19
import org .hibernate .internal .CoreLogging ;
@@ -105,25 +106,12 @@ public static void serialize(Serializable obj, OutputStream outputStream) throws
105
106
}
106
107
}
107
108
108
- ObjectOutputStream out = null ;
109
- try {
110
- // stream closed in the finally
111
- out = new ObjectOutputStream ( outputStream );
109
+ try ( var out = new ObjectOutputStream ( outputStream ) ) {
112
110
out .writeObject ( obj );
113
-
114
111
}
115
112
catch (IOException ex ) {
116
113
throw new SerializationException ( "could not serialize" , ex );
117
114
}
118
- finally {
119
- try {
120
- if ( out != null ) {
121
- out .close ();
122
- }
123
- }
124
- catch (IOException ignored ) {
125
- }
126
- }
127
115
}
128
116
129
117
/**
@@ -137,7 +125,7 @@ public static void serialize(Serializable obj, OutputStream outputStream) throws
137
125
* @throws SerializationException (runtime) if the serialization fails
138
126
*/
139
127
public static byte [] serialize (Serializable obj ) throws SerializationException {
140
- ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream ( 512 );
128
+ final var byteArrayOutputStream = new ByteArrayOutputStream ( 512 );
141
129
serialize ( obj , byteArrayOutputStream );
142
130
return byteArrayOutputStream .toByteArray ();
143
131
}
@@ -199,7 +187,6 @@ public static Object deserialize(InputStream inputStream, ClassLoader loader) th
199
187
return doDeserialize ( inputStream , loader , defaultClassLoader (), hibernateClassLoader () );
200
188
}
201
189
202
- @ SuppressWarnings ("unchecked" )
203
190
public static <T > T doDeserialize (
204
191
InputStream inputStream ,
205
192
ClassLoader loader ,
@@ -211,32 +198,11 @@ public static <T> T doDeserialize(
211
198
212
199
LOG .trace ( "Starting deserialization of object" );
213
200
214
- try {
215
- CustomObjectInputStream in = new CustomObjectInputStream (
216
- inputStream ,
217
- loader ,
218
- fallbackLoader1 ,
219
- fallbackLoader2
220
- );
221
- try {
222
- return (T ) in .readObject ();
223
- }
224
- catch (ClassNotFoundException e ) {
225
- throw new SerializationException ( "could not deserialize" , e );
226
- }
227
- catch (IOException e ) {
228
- throw new SerializationException ( "could not deserialize" , e );
229
- }
230
- finally {
231
- try {
232
- in .close ();
233
- }
234
- catch (IOException ignore ) {
235
- // ignore
236
- }
237
- }
201
+ try ( var in = new CustomObjectInputStream ( inputStream , loader , fallbackLoader1 , fallbackLoader2 ) ) {
202
+ //noinspection unchecked
203
+ return (T ) in .readObject ();
238
204
}
239
- catch (IOException e ) {
205
+ catch (ClassNotFoundException | IOException e ) {
240
206
throw new SerializationException ( "could not deserialize" , e );
241
207
}
242
208
}
@@ -310,7 +276,7 @@ private CustomObjectInputStream(
310
276
}
311
277
312
278
@ Override
313
- protected Class resolveClass (ObjectStreamClass v ) throws IOException , ClassNotFoundException {
279
+ protected Class <?> resolveClass (ObjectStreamClass v ) throws IOException , ClassNotFoundException {
314
280
final String className = v .getName ();
315
281
LOG .tracev ( "Attempting to locate class [{0}]" , className );
316
282
@@ -321,7 +287,7 @@ protected Class resolveClass(ObjectStreamClass v) throws IOException, ClassNotFo
321
287
LOG .trace ( "Unable to locate class using given classloader" );
322
288
}
323
289
324
- if ( different ( loader1 , loader2 ) ) {
290
+ if ( ! Objects . equals ( loader1 , loader2 ) ) {
325
291
try {
326
292
return Class .forName ( className , false , loader2 );
327
293
}
@@ -330,7 +296,7 @@ protected Class resolveClass(ObjectStreamClass v) throws IOException, ClassNotFo
330
296
}
331
297
}
332
298
333
- if ( different ( loader1 , loader3 ) && different ( loader2 , loader3 ) ) {
299
+ if ( ! Objects . equals ( loader1 , loader3 ) && ! Objects . equals ( loader2 , loader3 ) ) {
334
300
try {
335
301
return Class .forName ( className , false , loader3 );
336
302
}
@@ -339,16 +305,9 @@ protected Class resolveClass(ObjectStreamClass v) throws IOException, ClassNotFo
339
305
}
340
306
}
341
307
342
- // By default delegate to normal JDK deserialization which will use the class loader
343
- // of the class which is calling this deserialization.
308
+ // By default, delegate to normal JDK deserialization which will use the
309
+ // class loader of the class which is calling this deserialization.
344
310
return super .resolveClass ( v );
345
311
}
346
-
347
- private boolean different (ClassLoader one , ClassLoader other ) {
348
- if ( one == null ) {
349
- return other != null ;
350
- }
351
- return !one .equals ( other );
352
- }
353
312
}
354
313
}
0 commit comments