@@ -124,14 +124,8 @@ RawBsonDocument encrypt(final String databaseName, final RawBsonDocument command
124124 return command ;
125125 }
126126
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 );
135129 } catch (MongoCryptException e ) {
136130 throw wrapInClientException (e );
137131 }
@@ -146,14 +140,8 @@ RawBsonDocument encrypt(final String databaseName, final RawBsonDocument command
146140 RawBsonDocument decrypt (final RawBsonDocument commandResponse ) {
147141 notNull ("commandResponse" , commandResponse );
148142
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 );
157145 } catch (MongoCryptException e ) {
158146 throw wrapInClientException (e );
159147 }
@@ -170,18 +158,12 @@ BsonDocument createDataKey(final String kmsProvider, final DataKeyOptions option
170158 notNull ("kmsProvider" , kmsProvider );
171159 notNull ("options" , options );
172160
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 );
185167 } catch (MongoCryptException e ) {
186168 throw wrapInClientException (e );
187169 }
@@ -219,12 +201,9 @@ BsonBinary encryptExplicitly(final BsonValue value, final EncryptOptions options
219201 encryptOptionsBuilder .queryType (MongoExplicitEncryptOptions .QueryType .valueOf (queryType .name ()));
220202 }
221203
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 ())) {
225206 return executeStateMachine (encryptionContext , null ).getBinary ("v" );
226- } finally {
227- encryptionContext .close ();
228207 }
229208 } catch (MongoCryptException e ) {
230209 throw wrapInClientException (e );
@@ -240,14 +219,8 @@ BsonBinary encryptExplicitly(final BsonValue value, final EncryptOptions options
240219 BsonValue decryptExplicitly (final BsonBinary value ) {
241220 notNull ("value" , value );
242221
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" );
251224 } catch (MongoCryptException e ) {
252225 throw wrapInClientException (e );
253226 }
@@ -269,15 +242,15 @@ public String getCryptSharedLibVersionString() {
269242 return mongoCrypt .getCryptSharedLibVersionString ();
270243 }
271244
272- private RawBsonDocument executeStateMachine (final MongoCryptContext cryptContext , final String databaseName ) {
245+ private RawBsonDocument executeStateMachine (final MongoCryptContext cryptContext , @ Nullable final String databaseName ) {
273246 while (true ) {
274247 State state = cryptContext .getState ();
275248 switch (state ) {
276249 case NEED_MONGO_COLLINFO :
277- collInfo (cryptContext , databaseName );
250+ collInfo (cryptContext , notNull ( " databaseName" , databaseName ) );
278251 break ;
279252 case NEED_MONGO_MARKINGS :
280- mark (cryptContext , databaseName );
253+ mark (cryptContext , notNull ( " databaseName" , databaseName ) );
281254 break ;
282255 case NEED_KMS_CREDENTIALS :
283256 fetchCredentials (cryptContext );
@@ -347,9 +320,8 @@ private void decryptKeys(final MongoCryptContext cryptContext) {
347320 }
348321
349322 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 ())) {
353325 int bytesNeeded = keyDecryptor .bytesNeeded ();
354326
355327 while (bytesNeeded > 0 ) {
@@ -358,12 +330,8 @@ private void decryptKey(final MongoKeyDecryptor keyDecryptor) throws IOException
358330 keyDecryptor .feed (ByteBuffer .wrap (bytes , 0 , bytesRead ));
359331 bytesNeeded = keyDecryptor .bytesNeeded ();
360332 }
361- } finally {
362- try {
363- inputStream .close ();
364- } catch (IOException e ) {
365- // ignore
366- }
333+ } catch (IOException e ) {
334+ // ignore
367335 }
368336 }
369337
0 commit comments