@@ -124,14 +124,8 @@ RawBsonDocument encrypt(final String databaseName, final RawBsonDocument command
124
124
return command ;
125
125
}
126
126
127
- try {
128
- MongoCryptContext encryptionContext = mongoCrypt .createEncryptionContext (databaseName , command );
129
-
130
- try {
131
- return executeStateMachine (encryptionContext , databaseName );
132
- } finally {
133
- encryptionContext .close ();
134
- }
127
+ try (MongoCryptContext encryptionContext = mongoCrypt .createEncryptionContext (databaseName , command )) {
128
+ return executeStateMachine (encryptionContext , databaseName );
135
129
} catch (MongoCryptException e ) {
136
130
throw wrapInClientException (e );
137
131
}
@@ -146,14 +140,8 @@ RawBsonDocument encrypt(final String databaseName, final RawBsonDocument command
146
140
RawBsonDocument decrypt (final RawBsonDocument commandResponse ) {
147
141
notNull ("commandResponse" , commandResponse );
148
142
149
- try {
150
- MongoCryptContext decryptionContext = mongoCrypt .createDecryptionContext (commandResponse );
151
-
152
- try {
153
- return executeStateMachine (decryptionContext , null );
154
- } finally {
155
- decryptionContext .close ();
156
- }
143
+ try (MongoCryptContext decryptionContext = mongoCrypt .createDecryptionContext (commandResponse )) {
144
+ return executeStateMachine (decryptionContext , null );
157
145
} catch (MongoCryptException e ) {
158
146
throw wrapInClientException (e );
159
147
}
@@ -170,18 +158,12 @@ BsonDocument createDataKey(final String kmsProvider, final DataKeyOptions option
170
158
notNull ("kmsProvider" , kmsProvider );
171
159
notNull ("options" , options );
172
160
173
- try {
174
- MongoCryptContext dataKeyCreationContext = mongoCrypt .createDataKeyContext (kmsProvider ,
175
- MongoDataKeyOptions .builder ()
176
- .keyAltNames (options .getKeyAltNames ())
177
- .masterKey (options .getMasterKey ())
178
- .build ());
179
-
180
- try {
181
- return executeStateMachine (dataKeyCreationContext , null );
182
- } finally {
183
- dataKeyCreationContext .close ();
184
- }
161
+ try (MongoCryptContext dataKeyCreationContext = mongoCrypt .createDataKeyContext (kmsProvider ,
162
+ MongoDataKeyOptions .builder ()
163
+ .keyAltNames (options .getKeyAltNames ())
164
+ .masterKey (options .getMasterKey ())
165
+ .build ())) {
166
+ return executeStateMachine (dataKeyCreationContext , null );
185
167
} catch (MongoCryptException e ) {
186
168
throw wrapInClientException (e );
187
169
}
@@ -219,12 +201,9 @@ BsonBinary encryptExplicitly(final BsonValue value, final EncryptOptions options
219
201
encryptOptionsBuilder .queryType (MongoExplicitEncryptOptions .QueryType .valueOf (queryType .name ()));
220
202
}
221
203
222
- MongoCryptContext encryptionContext = mongoCrypt .createExplicitEncryptionContext (
223
- new BsonDocument ("v" , value ), encryptOptionsBuilder .build ());
224
- try {
204
+ try (MongoCryptContext encryptionContext = mongoCrypt .createExplicitEncryptionContext (
205
+ new BsonDocument ("v" , value ), encryptOptionsBuilder .build ())) {
225
206
return executeStateMachine (encryptionContext , null ).getBinary ("v" );
226
- } finally {
227
- encryptionContext .close ();
228
207
}
229
208
} catch (MongoCryptException e ) {
230
209
throw wrapInClientException (e );
@@ -240,14 +219,8 @@ BsonBinary encryptExplicitly(final BsonValue value, final EncryptOptions options
240
219
BsonValue decryptExplicitly (final BsonBinary value ) {
241
220
notNull ("value" , value );
242
221
243
- try {
244
- MongoCryptContext decryptionContext = mongoCrypt .createExplicitDecryptionContext (new BsonDocument ("v" , value ));
245
-
246
- try {
247
- return executeStateMachine (decryptionContext , null ).get ("v" );
248
- } finally {
249
- decryptionContext .close ();
250
- }
222
+ try (MongoCryptContext decryptionContext = mongoCrypt .createExplicitDecryptionContext (new BsonDocument ("v" , value ))) {
223
+ return executeStateMachine (decryptionContext , null ).get ("v" );
251
224
} catch (MongoCryptException e ) {
252
225
throw wrapInClientException (e );
253
226
}
@@ -269,15 +242,15 @@ public String getCryptSharedLibVersionString() {
269
242
return mongoCrypt .getCryptSharedLibVersionString ();
270
243
}
271
244
272
- private RawBsonDocument executeStateMachine (final MongoCryptContext cryptContext , final String databaseName ) {
245
+ private RawBsonDocument executeStateMachine (final MongoCryptContext cryptContext , @ Nullable final String databaseName ) {
273
246
while (true ) {
274
247
State state = cryptContext .getState ();
275
248
switch (state ) {
276
249
case NEED_MONGO_COLLINFO :
277
- collInfo (cryptContext , databaseName );
250
+ collInfo (cryptContext , notNull ( " databaseName" , databaseName ) );
278
251
break ;
279
252
case NEED_MONGO_MARKINGS :
280
- mark (cryptContext , databaseName );
253
+ mark (cryptContext , notNull ( " databaseName" , databaseName ) );
281
254
break ;
282
255
case NEED_KMS_CREDENTIALS :
283
256
fetchCredentials (cryptContext );
@@ -347,9 +320,8 @@ private void decryptKeys(final MongoCryptContext cryptContext) {
347
320
}
348
321
349
322
private void decryptKey (final MongoKeyDecryptor keyDecryptor ) throws IOException {
350
- InputStream inputStream = keyManagementService .stream (keyDecryptor .getKmsProvider (), keyDecryptor .getHostName (),
351
- keyDecryptor .getMessage ());
352
- try {
323
+ try (InputStream inputStream = keyManagementService .stream (keyDecryptor .getKmsProvider (), keyDecryptor .getHostName (),
324
+ keyDecryptor .getMessage ())) {
353
325
int bytesNeeded = keyDecryptor .bytesNeeded ();
354
326
355
327
while (bytesNeeded > 0 ) {
@@ -358,12 +330,8 @@ private void decryptKey(final MongoKeyDecryptor keyDecryptor) throws IOException
358
330
keyDecryptor .feed (ByteBuffer .wrap (bytes , 0 , bytesRead ));
359
331
bytesNeeded = keyDecryptor .bytesNeeded ();
360
332
}
361
- } finally {
362
- try {
363
- inputStream .close ();
364
- } catch (IOException e ) {
365
- // ignore
366
- }
333
+ } catch (IOException e ) {
334
+ // ignore
367
335
}
368
336
}
369
337
0 commit comments