The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2006 The Android Open Source Project |
| 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
| 16 | |
| 17 | package android.os; |
Jeff Sharkey | e861b42 | 2012-03-01 20:59:22 -0800 | [diff] [blame] | 18 | import java.io.Closeable; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 19 | import java.io.File; |
| 20 | import java.io.FileDescriptor; |
| 21 | import java.io.FileInputStream; |
| 22 | import java.io.FileNotFoundException; |
| 23 | import java.io.FileOutputStream; |
| 24 | import java.io.IOException; |
Chia-chi Yeh | 47f8f0f | 2011-05-12 16:23:44 -0700 | [diff] [blame] | 25 | import java.net.DatagramSocket; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 26 | import java.net.Socket; |
| 27 | |
| 28 | /** |
| 29 | * The FileDescriptor returned by {@link Parcel#readFileDescriptor}, allowing |
| 30 | * you to close it when done with it. |
| 31 | */ |
Jeff Sharkey | e861b42 | 2012-03-01 20:59:22 -0800 | [diff] [blame] | 32 | public class ParcelFileDescriptor implements Parcelable, Closeable { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 33 | private final FileDescriptor mFileDescriptor; |
| 34 | private boolean mClosed; |
| 35 | //this field is to create wrapper for ParcelFileDescriptor using another |
| 36 | //PartialFileDescriptor but avoid invoking close twice |
| 37 | //consider ParcelFileDescriptor A(fileDescriptor fd), ParcelFileDescriptor B(A) |
| 38 | //in this particular case fd.close might be invoked twice. |
| 39 | private final ParcelFileDescriptor mParcelDescriptor; |
Elliott Hughes | 4390758 | 2011-04-12 14:25:23 -0700 | [diff] [blame] | 40 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 41 | /** |
| 42 | * For use with {@link #open}: if {@link #MODE_CREATE} has been supplied |
| 43 | * and this file doesn't already exist, then create the file with |
| 44 | * permissions such that any application can read it. |
| 45 | */ |
| 46 | public static final int MODE_WORLD_READABLE = 0x00000001; |
Elliott Hughes | 4390758 | 2011-04-12 14:25:23 -0700 | [diff] [blame] | 47 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 48 | /** |
| 49 | * For use with {@link #open}: if {@link #MODE_CREATE} has been supplied |
| 50 | * and this file doesn't already exist, then create the file with |
| 51 | * permissions such that any application can write it. |
| 52 | */ |
| 53 | public static final int MODE_WORLD_WRITEABLE = 0x00000002; |
Elliott Hughes | 4390758 | 2011-04-12 14:25:23 -0700 | [diff] [blame] | 54 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 55 | /** |
| 56 | * For use with {@link #open}: open the file with read-only access. |
| 57 | */ |
| 58 | public static final int MODE_READ_ONLY = 0x10000000; |
Elliott Hughes | 4390758 | 2011-04-12 14:25:23 -0700 | [diff] [blame] | 59 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 60 | /** |
| 61 | * For use with {@link #open}: open the file with write-only access. |
| 62 | */ |
| 63 | public static final int MODE_WRITE_ONLY = 0x20000000; |
Elliott Hughes | 4390758 | 2011-04-12 14:25:23 -0700 | [diff] [blame] | 64 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 65 | /** |
| 66 | * For use with {@link #open}: open the file with read and write access. |
| 67 | */ |
| 68 | public static final int MODE_READ_WRITE = 0x30000000; |
Elliott Hughes | 4390758 | 2011-04-12 14:25:23 -0700 | [diff] [blame] | 69 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 70 | /** |
| 71 | * For use with {@link #open}: create the file if it doesn't already exist. |
| 72 | */ |
| 73 | public static final int MODE_CREATE = 0x08000000; |
Elliott Hughes | 4390758 | 2011-04-12 14:25:23 -0700 | [diff] [blame] | 74 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 75 | /** |
| 76 | * For use with {@link #open}: erase contents of file when opening. |
| 77 | */ |
| 78 | public static final int MODE_TRUNCATE = 0x04000000; |
Elliott Hughes | 4390758 | 2011-04-12 14:25:23 -0700 | [diff] [blame] | 79 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 80 | /** |
| 81 | * For use with {@link #open}: append to end of file while writing. |
| 82 | */ |
| 83 | public static final int MODE_APPEND = 0x02000000; |
Elliott Hughes | 4390758 | 2011-04-12 14:25:23 -0700 | [diff] [blame] | 84 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 85 | /** |
| 86 | * Create a new ParcelFileDescriptor accessing a given file. |
Elliott Hughes | 4390758 | 2011-04-12 14:25:23 -0700 | [diff] [blame] | 87 | * |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 88 | * @param file The file to be opened. |
| 89 | * @param mode The desired access mode, must be one of |
| 90 | * {@link #MODE_READ_ONLY}, {@link #MODE_WRITE_ONLY}, or |
| 91 | * {@link #MODE_READ_WRITE}; may also be any combination of |
| 92 | * {@link #MODE_CREATE}, {@link #MODE_TRUNCATE}, |
| 93 | * {@link #MODE_WORLD_READABLE}, and {@link #MODE_WORLD_WRITEABLE}. |
Elliott Hughes | 4390758 | 2011-04-12 14:25:23 -0700 | [diff] [blame] | 94 | * |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 95 | * @return Returns a new ParcelFileDescriptor pointing to the given |
| 96 | * file. |
Elliott Hughes | 4390758 | 2011-04-12 14:25:23 -0700 | [diff] [blame] | 97 | * |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 98 | * @throws FileNotFoundException Throws FileNotFoundException if the given |
| 99 | * file does not exist or can not be opened with the requested mode. |
| 100 | */ |
| 101 | public static ParcelFileDescriptor open(File file, int mode) |
| 102 | throws FileNotFoundException { |
| 103 | String path = file.getPath(); |
| 104 | SecurityManager security = System.getSecurityManager(); |
| 105 | if (security != null) { |
| 106 | security.checkRead(path); |
| 107 | if ((mode&MODE_WRITE_ONLY) != 0) { |
| 108 | security.checkWrite(path); |
| 109 | } |
| 110 | } |
Elliott Hughes | 4390758 | 2011-04-12 14:25:23 -0700 | [diff] [blame] | 111 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 112 | if ((mode&MODE_READ_WRITE) == 0) { |
| 113 | throw new IllegalArgumentException( |
| 114 | "Must specify MODE_READ_ONLY, MODE_WRITE_ONLY, or MODE_READ_WRITE"); |
| 115 | } |
Elliott Hughes | 4390758 | 2011-04-12 14:25:23 -0700 | [diff] [blame] | 116 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 117 | FileDescriptor fd = Parcel.openFileDescriptor(path, mode); |
Dianne Hackborn | 1866839 | 2010-03-23 22:10:55 -0700 | [diff] [blame] | 118 | return fd != null ? new ParcelFileDescriptor(fd) : null; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 119 | } |
| 120 | |
| 121 | /** |
Dianne Hackborn | e17aeb3 | 2011-04-07 15:11:57 -0700 | [diff] [blame] | 122 | * Create a new ParcelFileDescriptor that is a dup of an existing |
| 123 | * FileDescriptor. This obeys standard POSIX semantics, where the |
| 124 | * new file descriptor shared state such as file position with the |
| 125 | * original file descriptor. |
| 126 | */ |
| 127 | public static ParcelFileDescriptor dup(FileDescriptor orig) throws IOException { |
| 128 | FileDescriptor fd = Parcel.dupFileDescriptor(orig); |
| 129 | return fd != null ? new ParcelFileDescriptor(fd) : null; |
| 130 | } |
| 131 | |
| 132 | /** |
Dianne Hackborn | 62f20ec | 2011-08-15 17:40:28 -0700 | [diff] [blame] | 133 | * Create a new ParcelFileDescriptor that is a dup of the existing |
| 134 | * FileDescriptor. This obeys standard POSIX semantics, where the |
| 135 | * new file descriptor shared state such as file position with the |
| 136 | * original file descriptor. |
| 137 | */ |
| 138 | public ParcelFileDescriptor dup() throws IOException { |
| 139 | return dup(getFileDescriptor()); |
| 140 | } |
| 141 | |
| 142 | /** |
Dianne Hackborn | ea2117bd | 2011-05-20 10:37:34 -0700 | [diff] [blame] | 143 | * Create a new ParcelFileDescriptor from a raw native fd. The new |
| 144 | * ParcelFileDescriptor holds a dup of the original fd passed in here, |
| 145 | * so you must still close that fd as well as the new ParcelFileDescriptor. |
| 146 | * |
| 147 | * @param fd The native fd that the ParcelFileDescriptor should dup. |
| 148 | * |
| 149 | * @return Returns a new ParcelFileDescriptor holding a FileDescriptor |
| 150 | * for a dup of the given fd. |
| 151 | */ |
| 152 | public static ParcelFileDescriptor fromFd(int fd) throws IOException { |
| 153 | FileDescriptor fdesc = getFileDescriptorFromFd(fd); |
| 154 | return new ParcelFileDescriptor(fdesc); |
| 155 | } |
| 156 | |
| 157 | // Extracts the file descriptor from the specified socket and returns it untouched |
| 158 | private static native FileDescriptor getFileDescriptorFromFd(int fd) throws IOException; |
| 159 | |
| 160 | /** |
| 161 | * Take ownership of a raw native fd in to a new ParcelFileDescriptor. |
| 162 | * The returned ParcelFileDescriptor now owns the given fd, and will be |
| 163 | * responsible for closing it. You must not close the fd yourself. |
| 164 | * |
| 165 | * @param fd The native fd that the ParcelFileDescriptor should adopt. |
| 166 | * |
| 167 | * @return Returns a new ParcelFileDescriptor holding a FileDescriptor |
| 168 | * for the given fd. |
| 169 | */ |
| 170 | public static ParcelFileDescriptor adoptFd(int fd) { |
| 171 | FileDescriptor fdesc = getFileDescriptorFromFdNoDup(fd); |
| 172 | return new ParcelFileDescriptor(fdesc); |
| 173 | } |
| 174 | |
| 175 | // Extracts the file descriptor from the specified socket and returns it untouched |
| 176 | private static native FileDescriptor getFileDescriptorFromFdNoDup(int fd); |
| 177 | |
| 178 | /** |
| 179 | * Create a new ParcelFileDescriptor from the specified Socket. The new |
| 180 | * ParcelFileDescriptor holds a dup of the original FileDescriptor in |
| 181 | * the Socket, so you must still close the Socket as well as the new |
| 182 | * ParcelFileDescriptor. |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 183 | * |
| 184 | * @param socket The Socket whose FileDescriptor is used to create |
| 185 | * a new ParcelFileDescriptor. |
| 186 | * |
| 187 | * @return A new ParcelFileDescriptor with the FileDescriptor of the |
| 188 | * specified Socket. |
| 189 | */ |
| 190 | public static ParcelFileDescriptor fromSocket(Socket socket) { |
Elliott Hughes | 4390758 | 2011-04-12 14:25:23 -0700 | [diff] [blame] | 191 | FileDescriptor fd = socket.getFileDescriptor$(); |
Dianne Hackborn | 1866839 | 2010-03-23 22:10:55 -0700 | [diff] [blame] | 192 | return fd != null ? new ParcelFileDescriptor(fd) : null; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 193 | } |
| 194 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 195 | /** |
Chia-chi Yeh | 47f8f0f | 2011-05-12 16:23:44 -0700 | [diff] [blame] | 196 | * Create a new ParcelFileDescriptor from the specified DatagramSocket. |
| 197 | * |
| 198 | * @param datagramSocket The DatagramSocket whose FileDescriptor is used |
| 199 | * to create a new ParcelFileDescriptor. |
| 200 | * |
| 201 | * @return A new ParcelFileDescriptor with the FileDescriptor of the |
| 202 | * specified DatagramSocket. |
| 203 | */ |
| 204 | public static ParcelFileDescriptor fromDatagramSocket(DatagramSocket datagramSocket) { |
| 205 | FileDescriptor fd = datagramSocket.getFileDescriptor$(); |
| 206 | return fd != null ? new ParcelFileDescriptor(fd) : null; |
| 207 | } |
| 208 | |
| 209 | /** |
Dianne Hackborn | 23fdaf6 | 2010-08-06 12:16:55 -0700 | [diff] [blame] | 210 | * Create two ParcelFileDescriptors structured as a data pipe. The first |
| 211 | * ParcelFileDescriptor in the returned array is the read side; the second |
| 212 | * is the write side. |
| 213 | */ |
| 214 | public static ParcelFileDescriptor[] createPipe() throws IOException { |
| 215 | FileDescriptor[] fds = new FileDescriptor[2]; |
Dianne Hackborn | ea2117bd | 2011-05-20 10:37:34 -0700 | [diff] [blame] | 216 | createPipeNative(fds); |
| 217 | ParcelFileDescriptor[] pfds = new ParcelFileDescriptor[2]; |
| 218 | pfds[0] = new ParcelFileDescriptor(fds[0]); |
| 219 | pfds[1] = new ParcelFileDescriptor(fds[1]); |
| 220 | return pfds; |
Dianne Hackborn | 23fdaf6 | 2010-08-06 12:16:55 -0700 | [diff] [blame] | 221 | } |
| 222 | |
Dianne Hackborn | ea2117bd | 2011-05-20 10:37:34 -0700 | [diff] [blame] | 223 | private static native void createPipeNative(FileDescriptor[] outFds) throws IOException; |
Dianne Hackborn | 23fdaf6 | 2010-08-06 12:16:55 -0700 | [diff] [blame] | 224 | |
| 225 | /** |
Dianne Hackborn | 540f86a | 2011-01-11 17:52:22 -0800 | [diff] [blame] | 226 | * @hide Please use createPipe() or ContentProvider.openPipeHelper(). |
Bjorn Bringert | a006b472 | 2010-04-14 14:43:26 +0100 | [diff] [blame] | 227 | * Gets a file descriptor for a read-only copy of the given data. |
| 228 | * |
| 229 | * @param data Data to copy. |
| 230 | * @param name Name for the shared memory area that may back the file descriptor. |
| 231 | * This is purely informative and may be {@code null}. |
| 232 | * @return A ParcelFileDescriptor. |
| 233 | * @throws IOException if there is an error while creating the shared memory area. |
| 234 | */ |
Dianne Hackborn | a2ea747 | 2010-12-20 12:10:01 -0800 | [diff] [blame] | 235 | @Deprecated |
Bjorn Bringert | a006b472 | 2010-04-14 14:43:26 +0100 | [diff] [blame] | 236 | public static ParcelFileDescriptor fromData(byte[] data, String name) throws IOException { |
| 237 | if (data == null) return null; |
| 238 | MemoryFile file = new MemoryFile(name, data.length); |
| 239 | if (data.length > 0) { |
| 240 | file.writeBytes(data, 0, 0, data.length); |
| 241 | } |
| 242 | file.deactivate(); |
| 243 | FileDescriptor fd = file.getFileDescriptor(); |
| 244 | return fd != null ? new ParcelFileDescriptor(fd) : null; |
| 245 | } |
| 246 | |
| 247 | /** |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 248 | * Retrieve the actual FileDescriptor associated with this object. |
Elliott Hughes | 4390758 | 2011-04-12 14:25:23 -0700 | [diff] [blame] | 249 | * |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 250 | * @return Returns the FileDescriptor associated with this object. |
| 251 | */ |
| 252 | public FileDescriptor getFileDescriptor() { |
| 253 | return mFileDescriptor; |
| 254 | } |
Elliott Hughes | 4390758 | 2011-04-12 14:25:23 -0700 | [diff] [blame] | 255 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 256 | /** |
| 257 | * Return the total size of the file representing this fd, as determined |
| 258 | * by stat(). Returns -1 if the fd is not a file. |
| 259 | */ |
| 260 | public native long getStatSize(); |
Elliott Hughes | 4390758 | 2011-04-12 14:25:23 -0700 | [diff] [blame] | 261 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 262 | /** |
| 263 | * This is needed for implementing AssetFileDescriptor.AutoCloseOutputStream, |
| 264 | * and I really don't think we want it to be public. |
| 265 | * @hide |
| 266 | */ |
| 267 | public native long seekTo(long pos); |
Elliott Hughes | 4390758 | 2011-04-12 14:25:23 -0700 | [diff] [blame] | 268 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 269 | /** |
Dianne Hackborn | c9119f5 | 2011-02-28 18:03:26 -0800 | [diff] [blame] | 270 | * Return the native fd int for this ParcelFileDescriptor. The |
| 271 | * ParcelFileDescriptor still owns the fd, and it still must be closed |
| 272 | * through this API. |
| 273 | */ |
| 274 | public int getFd() { |
| 275 | if (mClosed) { |
| 276 | throw new IllegalStateException("Already closed"); |
| 277 | } |
| 278 | return getFdNative(); |
| 279 | } |
Elliott Hughes | 4390758 | 2011-04-12 14:25:23 -0700 | [diff] [blame] | 280 | |
Dianne Hackborn | c9119f5 | 2011-02-28 18:03:26 -0800 | [diff] [blame] | 281 | private native int getFdNative(); |
Elliott Hughes | 4390758 | 2011-04-12 14:25:23 -0700 | [diff] [blame] | 282 | |
Dianne Hackborn | c9119f5 | 2011-02-28 18:03:26 -0800 | [diff] [blame] | 283 | /** |
| 284 | * Return the native fd int for this ParcelFileDescriptor and detach it |
| 285 | * from the object here. You are now responsible for closing the fd in |
| 286 | * native code. |
| 287 | */ |
| 288 | public int detachFd() { |
| 289 | if (mClosed) { |
| 290 | throw new IllegalStateException("Already closed"); |
| 291 | } |
| 292 | if (mParcelDescriptor != null) { |
| 293 | int fd = mParcelDescriptor.detachFd(); |
| 294 | mClosed = true; |
| 295 | return fd; |
| 296 | } |
| 297 | int fd = getFd(); |
| 298 | mClosed = true; |
| 299 | Parcel.clearFileDescriptor(mFileDescriptor); |
| 300 | return fd; |
| 301 | } |
Elliott Hughes | 4390758 | 2011-04-12 14:25:23 -0700 | [diff] [blame] | 302 | |
Dianne Hackborn | c9119f5 | 2011-02-28 18:03:26 -0800 | [diff] [blame] | 303 | /** |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 304 | * Close the ParcelFileDescriptor. This implementation closes the underlying |
| 305 | * OS resources allocated to represent this stream. |
Elliott Hughes | 4390758 | 2011-04-12 14:25:23 -0700 | [diff] [blame] | 306 | * |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 307 | * @throws IOException |
| 308 | * If an error occurs attempting to close this ParcelFileDescriptor. |
| 309 | */ |
| 310 | public void close() throws IOException { |
Dianne Hackborn | 1866839 | 2010-03-23 22:10:55 -0700 | [diff] [blame] | 311 | synchronized (this) { |
| 312 | if (mClosed) return; |
| 313 | mClosed = true; |
| 314 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 315 | if (mParcelDescriptor != null) { |
| 316 | // If this is a proxy to another file descriptor, just call through to its |
| 317 | // close method. |
| 318 | mParcelDescriptor.close(); |
| 319 | } else { |
| 320 | Parcel.closeFileDescriptor(mFileDescriptor); |
| 321 | } |
| 322 | } |
Elliott Hughes | 4390758 | 2011-04-12 14:25:23 -0700 | [diff] [blame] | 323 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 324 | /** |
| 325 | * An InputStream you can create on a ParcelFileDescriptor, which will |
| 326 | * take care of calling {@link ParcelFileDescriptor#close |
Christopher Tate | fa9e7c0 | 2010-05-06 12:07:10 -0700 | [diff] [blame] | 327 | * ParcelFileDescriptor.close()} for you when the stream is closed. |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 328 | */ |
| 329 | public static class AutoCloseInputStream extends FileInputStream { |
| 330 | private final ParcelFileDescriptor mFd; |
Elliott Hughes | 4390758 | 2011-04-12 14:25:23 -0700 | [diff] [blame] | 331 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 332 | public AutoCloseInputStream(ParcelFileDescriptor fd) { |
| 333 | super(fd.getFileDescriptor()); |
| 334 | mFd = fd; |
| 335 | } |
| 336 | |
| 337 | @Override |
| 338 | public void close() throws IOException { |
Brian Carlstrom | fd9ddd1 | 2010-11-04 11:24:58 -0700 | [diff] [blame] | 339 | try { |
| 340 | mFd.close(); |
| 341 | } finally { |
| 342 | super.close(); |
| 343 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 344 | } |
| 345 | } |
Elliott Hughes | 4390758 | 2011-04-12 14:25:23 -0700 | [diff] [blame] | 346 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 347 | /** |
| 348 | * An OutputStream you can create on a ParcelFileDescriptor, which will |
| 349 | * take care of calling {@link ParcelFileDescriptor#close |
Christopher Tate | fa9e7c0 | 2010-05-06 12:07:10 -0700 | [diff] [blame] | 350 | * ParcelFileDescriptor.close()} for you when the stream is closed. |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 351 | */ |
| 352 | public static class AutoCloseOutputStream extends FileOutputStream { |
| 353 | private final ParcelFileDescriptor mFd; |
Elliott Hughes | 4390758 | 2011-04-12 14:25:23 -0700 | [diff] [blame] | 354 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 355 | public AutoCloseOutputStream(ParcelFileDescriptor fd) { |
| 356 | super(fd.getFileDescriptor()); |
| 357 | mFd = fd; |
| 358 | } |
| 359 | |
| 360 | @Override |
| 361 | public void close() throws IOException { |
Brian Carlstrom | fd9ddd1 | 2010-11-04 11:24:58 -0700 | [diff] [blame] | 362 | try { |
| 363 | mFd.close(); |
| 364 | } finally { |
| 365 | super.close(); |
| 366 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 367 | } |
| 368 | } |
Elliott Hughes | 4390758 | 2011-04-12 14:25:23 -0700 | [diff] [blame] | 369 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 370 | @Override |
| 371 | public String toString() { |
| 372 | return "{ParcelFileDescriptor: " + mFileDescriptor + "}"; |
| 373 | } |
Elliott Hughes | 4390758 | 2011-04-12 14:25:23 -0700 | [diff] [blame] | 374 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 375 | @Override |
| 376 | protected void finalize() throws Throwable { |
| 377 | try { |
| 378 | if (!mClosed) { |
| 379 | close(); |
| 380 | } |
| 381 | } finally { |
| 382 | super.finalize(); |
| 383 | } |
| 384 | } |
Elliott Hughes | 4390758 | 2011-04-12 14:25:23 -0700 | [diff] [blame] | 385 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 386 | public ParcelFileDescriptor(ParcelFileDescriptor descriptor) { |
| 387 | super(); |
| 388 | mParcelDescriptor = descriptor; |
| 389 | mFileDescriptor = mParcelDescriptor.mFileDescriptor; |
| 390 | } |
Elliott Hughes | 4390758 | 2011-04-12 14:25:23 -0700 | [diff] [blame] | 391 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 392 | /*package */ParcelFileDescriptor(FileDescriptor descriptor) { |
| 393 | super(); |
Dianne Hackborn | 1866839 | 2010-03-23 22:10:55 -0700 | [diff] [blame] | 394 | if (descriptor == null) { |
| 395 | throw new NullPointerException("descriptor must not be null"); |
| 396 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 397 | mFileDescriptor = descriptor; |
| 398 | mParcelDescriptor = null; |
| 399 | } |
Elliott Hughes | 4390758 | 2011-04-12 14:25:23 -0700 | [diff] [blame] | 400 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 401 | /* Parcelable interface */ |
| 402 | public int describeContents() { |
| 403 | return Parcelable.CONTENTS_FILE_DESCRIPTOR; |
| 404 | } |
| 405 | |
Dan Egnor | b3e4ef3 | 2010-07-20 09:03:35 -0700 | [diff] [blame] | 406 | /** |
| 407 | * {@inheritDoc} |
| 408 | * If {@link Parcelable#PARCELABLE_WRITE_RETURN_VALUE} is set in flags, |
| 409 | * the file descriptor will be closed after a copy is written to the Parcel. |
| 410 | */ |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 411 | public void writeToParcel(Parcel out, int flags) { |
| 412 | out.writeFileDescriptor(mFileDescriptor); |
| 413 | if ((flags&PARCELABLE_WRITE_RETURN_VALUE) != 0 && !mClosed) { |
| 414 | try { |
| 415 | close(); |
| 416 | } catch (IOException e) { |
| 417 | // Empty |
| 418 | } |
| 419 | } |
| 420 | } |
| 421 | |
| 422 | public static final Parcelable.Creator<ParcelFileDescriptor> CREATOR |
| 423 | = new Parcelable.Creator<ParcelFileDescriptor>() { |
| 424 | public ParcelFileDescriptor createFromParcel(Parcel in) { |
| 425 | return in.readFileDescriptor(); |
| 426 | } |
| 427 | public ParcelFileDescriptor[] newArray(int size) { |
| 428 | return new ParcelFileDescriptor[size]; |
| 429 | } |
| 430 | }; |
| 431 | |
| 432 | } |