Merge "Switch to AutoCloseable"

This commit is contained in:
Treehugger Robot 2023-02-15 16:38:39 +00:00 committed by Gerrit Code Review
commit f95cacf1f1
4 changed files with 16 additions and 11 deletions

View File

@ -85,8 +85,8 @@ package android.system.virtualmachine {
method @NonNull public android.system.virtualmachine.VirtualMachineConfig.Builder setVmOutputCaptured(boolean);
}
public final class VirtualMachineDescriptor implements java.io.Closeable android.os.Parcelable {
method public void close() throws java.io.IOException;
public final class VirtualMachineDescriptor implements java.lang.AutoCloseable android.os.Parcelable {
method public void close();
method public int describeContents();
method public void writeToParcel(@NonNull android.os.Parcel, int);
field @NonNull public static final android.os.Parcelable.Creator<android.system.virtualmachine.VirtualMachineDescriptor> CREATOR;

View File

@ -377,7 +377,7 @@ public class VirtualMachine implements AutoCloseable {
}
/**
* Builds a virtual machine from an {@link VirtualMachineDescriptor} object and associates it
* Creates a virtual machine from an {@link VirtualMachineDescriptor} object and associates it
* with the given name.
*
* <p>The new virtual machine will be in the same state as the descriptor indicates.
@ -416,8 +416,6 @@ public class VirtualMachine implements AutoCloseable {
}
vm.importEncryptedStoreFrom(vmDescriptor.getEncryptedStoreFd());
}
} catch (IOException e) {
throw new VirtualMachineException(e);
}
return vm;
} catch (VirtualMachineException | RuntimeException e) {

View File

@ -25,7 +25,6 @@ import android.os.Parcel;
import android.os.ParcelFileDescriptor;
import android.os.Parcelable;
import java.io.Closeable;
import java.io.IOException;
/**
@ -37,9 +36,8 @@ import java.io.IOException;
*
* @hide
*/
// TODO(b/268613460): should implement autocloseable.
@SystemApi
public final class VirtualMachineDescriptor implements Parcelable, Closeable {
public final class VirtualMachineDescriptor implements Parcelable, AutoCloseable {
private volatile boolean mClosed = false;
@NonNull private final ParcelFileDescriptor mConfigFd;
@NonNull private final ParcelFileDescriptor mInstanceImgFd;
@ -120,13 +118,21 @@ public final class VirtualMachineDescriptor implements Parcelable, Closeable {
ParcelFileDescriptor.class.getClassLoader(), ParcelFileDescriptor.class);
}
/**
* Release any resources held by this descriptor. Calling {@code close} on an already-closed
* descriptor has no effect.
*/
@Override
public void close() throws IOException {
public void close() {
mClosed = true;
// Let the compiler do the work: close everything, throw if any of them fail, skipping null.
try (mConfigFd;
mInstanceImgFd;
mEncryptedStoreFd) {}
mEncryptedStoreFd) {
} catch (IOException ignored) {
// PFD already swallows exceptions from closing the fd. There's no reason to propagate
// this to the caller.
}
}
private void checkNotClosed() {

View File

@ -196,7 +196,8 @@ public class VirtualMachineManager {
*
* <p>NOTE: This method may block and should not be called on the main thread.
*
* @throws VirtualMachineException if the VM cannot be imported.
* @throws VirtualMachineException if the VM cannot be imported or the {@code
* VirtualMachineDescriptor} has already been closed.
* @hide
*/
@NonNull