package org.robolectric.shadows;

import android.os.ParcelFileDescriptor;
import java.io.File;
import java.io.FileDescriptor;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.RandomAccessFile;
import java.lang.reflect.Constructor;
import org.robolectric.Shadows;
import org.robolectric.annotation.Implementation;
import org.robolectric.annotation.Implements;

@Implements(ParcelFileDescriptor.class)
public class ShadowParcelFileDescriptor {
  private RandomAccessFile file;

  @Implementation
  public static ParcelFileDescriptor open(File file, int mode) throws FileNotFoundException {
    ParcelFileDescriptor pfd;
    try {
      Constructor<ParcelFileDescriptor> constructor = ParcelFileDescriptor.class.getDeclaredConstructor(FileDescriptor.class);
      pfd = constructor.newInstance(new FileDescriptor());
    } catch (Exception e) {
      throw new RuntimeException(e);
    }
    Shadows.shadowOf(pfd).file = new RandomAccessFile(file, mode == ParcelFileDescriptor.MODE_READ_ONLY ? "r" : "rw");
    return pfd;
  }

  @Implementation
  public FileDescriptor getFileDescriptor() {
    try {
      return file.getFD();
    } catch (IOException e) {
      throw new RuntimeException(e);
    }
  }

  @Implementation
  public long getStatSize() {
    try {
      return file.length();
    } catch (IOException e) {
      throw new RuntimeException(e);
    }
  }

  @Implementation
  public void close() throws IOException {
    file.close();
  }
}

@@ -27,16 +27,77 @@
 adows;%0A%0A
+import static org.robolectric.shadow.api.Shadow.directlyOn;%0A%0A
 import a
@@ -120,32 +120,32 @@
 FileDescriptor;%0A
-
 import java.io.F
@@ -444,16 +444,62 @@
 lements;
+%0Aimport org.robolectric.annotation.RealObject;
 %0A%0A@Imple
@@ -606,16 +606,65 @@
 le file;
+%0A  @RealObject ParcelFileDescriptor realParcelFd;
 %0A%0A  @Imp
@@ -1266,24 +1266,71 @@
 criptor() %7B%0A
+    if (ShadowArscAssetManager.USE_LEGACY) %7B%0A  
     try %7B%0A  
@@ -1319,32 +1319,34 @@
 ) %7B%0A      try %7B%0A
+  
       return fil
@@ -1352,24 +1352,26 @@
 le.getFD();%0A
+  
     %7D catch 
@@ -1386,32 +1386,34 @@
 tion e) %7B%0A      
+  
 throw new Runtim
@@ -1419,32 +1419,141 @@
 meException(e);%0A
+      %7D%0A    %7D else %7B%0A      return directlyOn(realParcelFd, ParcelFileDescriptor.class, %22getFileDescriptor%22);%0A
     %7D%0A  %7D%0A%0A  @Im

Fix ShadowAssetManagerTest#openFd_shouldProvideFileDescriptorForAsset in binary mode.