Java Code Examples for android.system.ErrnoException#rethrowAsIOException()
The following examples show how to use
android.system.ErrnoException#rethrowAsIOException() .
You can vote up the ones you like or vote down the ones you don't like,
and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: IpSecService.java From android_9.0.0_r45 with Apache License 2.0 | 6 votes |
/** * This function finds and forcibly binds to a random system port, ensuring that the port cannot * be unbound. * * <p>A socket cannot be un-bound from a port if it was bound to that port by number. To select * a random open port and then bind by number, this function creates a temp socket, binds to a * random port (specifying 0), gets that port number, and then uses is to bind the user's UDP * Encapsulation Socket forcibly, so that it cannot be un-bound by the user with the returned * FileHandle. * * <p>The loop in this function handles the inherent race window between un-binding to a port * and re-binding, during which the system could *technically* hand that port out to someone * else. */ private int bindToRandomPort(FileDescriptor sockFd) throws IOException { for (int i = MAX_PORT_BIND_ATTEMPTS; i > 0; i--) { try { FileDescriptor probeSocket = Os.socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP); Os.bind(probeSocket, INADDR_ANY, 0); int port = ((InetSocketAddress) Os.getsockname(probeSocket)).getPort(); Os.close(probeSocket); Log.v(TAG, "Binding to port " + port); Os.bind(sockFd, INADDR_ANY, port); return port; } catch (ErrnoException e) { // Someone miraculously claimed the port just after we closed probeSocket. if (e.errno == OsConstants.EADDRINUSE) { continue; } throw e.rethrowAsIOException(); } } throw new IOException("Failed " + MAX_PORT_BIND_ATTEMPTS + " attempts to bind to a port"); }
Example 2
Source File: RandomAccessFile.java From j2objc with Apache License 2.0 | 6 votes |
/** * Sets the length of this file to {@code newLength}. If the current file is * smaller, it is expanded but the contents from the previous end of the * file to the new end are undefined. The file is truncated if its current * size is bigger than {@code newLength}. If the current file pointer * position is in the truncated part, it is set to the end of the file. * * @param newLength * the new file length in bytes. * @throws IllegalArgumentException * if {@code newLength < 0}. * @throws IOException * if this file is closed or another I/O error occurs. */ public void setLength(long newLength) throws IOException { if (newLength < 0) { throw new IllegalArgumentException("newLength < 0"); } try { Libcore.os.ftruncate(fd, newLength); } catch (ErrnoException errnoException) { throw errnoException.rethrowAsIOException(); } long filePointer = getFilePointer(); if (filePointer > newLength) { seek(newLength); } // if we are in "rws" mode, attempt to sync file+metadata if (syncMetadata) { fd.sync(); } }
Example 3
Source File: PackageInstaller.java From android_9.0.0_r45 with Apache License 2.0 | 6 votes |
/** * Ensure that any outstanding data for given stream has been committed * to disk. This is only valid for streams returned from * {@link #openWrite(String, long, long)}. */ public void fsync(@NonNull OutputStream out) throws IOException { if (ENABLE_REVOCABLE_FD) { if (out instanceof ParcelFileDescriptor.AutoCloseOutputStream) { try { Os.fsync(((ParcelFileDescriptor.AutoCloseOutputStream) out).getFD()); } catch (ErrnoException e) { throw e.rethrowAsIOException(); } } else { throw new IllegalArgumentException("Unrecognized stream"); } } else { if (out instanceof FileBridge.FileBridgeOutputStream) { ((FileBridge.FileBridgeOutputStream) out).fsync(); } else { throw new IllegalArgumentException("Unrecognized stream"); } } }
Example 4
Source File: IoBridge.java From j2objc with Apache License 2.0 | 6 votes |
/** * java.io thinks that a read at EOF is an error and should return -1, contrary to traditional * Unix practice where you'd read until you got 0 bytes (and any future read would return -1). */ public static int read(FileDescriptor fd, byte[] bytes, int byteOffset, int byteCount) throws IOException { ArrayUtils.throwsIfOutOfBounds(bytes.length, byteOffset, byteCount); if (byteCount == 0) { return 0; } try { int readCount = Libcore.os.read(fd, bytes, byteOffset, byteCount); if (readCount == 0) { return -1; } return readCount; } catch (ErrnoException errnoException) { if (errnoException.errno == EAGAIN) { // We return 0 rather than throw if we try to read from an empty non-blocking pipe. return 0; } throw errnoException.rethrowAsIOException(); } }
Example 5
Source File: MemoryFile.java From android_9.0.0_r45 with Apache License 2.0 | 5 votes |
/** * Allocates a new ashmem region. The region is initially not purgable. * * @param name optional name for the file (can be null). * @param length of the memory file in bytes, must be positive. * @throws IOException if the memory file could not be created. */ public MemoryFile(String name, int length) throws IOException { try { mSharedMemory = SharedMemory.create(name, length); mMapping = mSharedMemory.mapReadWrite(); } catch (ErrnoException ex) { ex.rethrowAsIOException(); } }
Example 6
Source File: LocalSocketImpl.java From android_9.0.0_r45 with Apache License 2.0 | 5 votes |
/** * Accepts a new connection to the socket. Blocks until a new * connection arrives. * * @param s a socket that will be used to represent the new connection. * @throws IOException */ protected void accept(LocalSocketImpl s) throws IOException { if (fd == null) { throw new IOException("socket not created"); } try { s.fd = Os.accept(fd, null /* address */); s.mFdCreatedInternally = true; } catch (ErrnoException e) { throw e.rethrowAsIOException(); } }
Example 7
Source File: LocalSocketImpl.java From android_9.0.0_r45 with Apache License 2.0 | 5 votes |
protected void listen(int backlog) throws IOException { if (fd == null) { throw new IOException("socket not created"); } try { Os.listen(fd, backlog); } catch (ErrnoException e) { throw e.rethrowAsIOException(); } }
Example 8
Source File: ParcelFileDescriptor.java From android_9.0.0_r45 with Apache License 2.0 | 5 votes |
private static FileDescriptor[] createCommSocketPair() throws IOException { try { // Use SOCK_SEQPACKET so that we have a guarantee that the status // is written and read atomically as one unit and is not split // across multiple IO operations. final FileDescriptor comm1 = new FileDescriptor(); final FileDescriptor comm2 = new FileDescriptor(); Os.socketpair(AF_UNIX, SOCK_SEQPACKET, 0, comm1, comm2); IoUtils.setBlocking(comm1, false); IoUtils.setBlocking(comm2, false); return new FileDescriptor[] { comm1, comm2 }; } catch (ErrnoException e) { throw e.rethrowAsIOException(); } }
Example 9
Source File: ParcelFileDescriptor.java From android_9.0.0_r45 with Apache License 2.0 | 5 votes |
/** * @hide */ public static ParcelFileDescriptor[] createSocketPair(int type) throws IOException { try { final FileDescriptor fd0 = new FileDescriptor(); final FileDescriptor fd1 = new FileDescriptor(); Os.socketpair(AF_UNIX, type, 0, fd0, fd1); return new ParcelFileDescriptor[] { new ParcelFileDescriptor(fd0), new ParcelFileDescriptor(fd1) }; } catch (ErrnoException e) { throw e.rethrowAsIOException(); } }
Example 10
Source File: ParcelFileDescriptor.java From android_9.0.0_r45 with Apache License 2.0 | 5 votes |
/** * Create a new ParcelFileDescriptor from a raw native fd. The new * ParcelFileDescriptor holds a dup of the original fd passed in here, * so you must still close that fd as well as the new ParcelFileDescriptor. * * @param fd The native fd that the ParcelFileDescriptor should dup. * * @return Returns a new ParcelFileDescriptor holding a FileDescriptor * for a dup of the given fd. */ public static ParcelFileDescriptor fromFd(int fd) throws IOException { final FileDescriptor original = new FileDescriptor(); original.setInt$(fd); try { final FileDescriptor dup = Os.dup(original); return new ParcelFileDescriptor(dup); } catch (ErrnoException e) { throw e.rethrowAsIOException(); } }
Example 11
Source File: StorageManager.java From android_9.0.0_r45 with Apache License 2.0 | 5 votes |
/** {@hide} */ private static boolean isCacheBehavior(File path, String name) throws IOException { try { Os.getxattr(path.getAbsolutePath(), name); return true; } catch (ErrnoException e) { if (e.errno != OsConstants.ENODATA) { throw e.rethrowAsIOException(); } else { return false; } } }
Example 12
Source File: UserDataPreparer.java From android_9.0.0_r45 with Apache License 2.0 | 5 votes |
/** * Set serial number stored in user directory inode. * * @throws IOException if serial number was already set */ private static void setSerialNumber(File file, int serialNumber) throws IOException { try { final byte[] buf = Integer.toString(serialNumber).getBytes(StandardCharsets.UTF_8); Os.setxattr(file.getAbsolutePath(), XATTR_SERIAL, buf, OsConstants.XATTR_CREATE); } catch (ErrnoException e) { throw e.rethrowAsIOException(); } }
Example 13
Source File: FileUtils.java From android_9.0.0_r45 with Apache License 2.0 | 5 votes |
private MemoryPipe(byte[] data, boolean sink) throws IOException { try { this.pipe = Os.pipe(); } catch (ErrnoException e) { throw e.rethrowAsIOException(); } this.data = data; this.sink = sink; }
Example 14
Source File: LocalSocketImpl.java From android_9.0.0_r45 with Apache License 2.0 | 5 votes |
/** * Creates a socket in the underlying OS. * * @param sockType either {@link LocalSocket#SOCKET_DGRAM}, {@link LocalSocket#SOCKET_STREAM} * or {@link LocalSocket#SOCKET_SEQPACKET} * @throws IOException */ public void create(int sockType) throws IOException { if (fd != null) { throw new IOException("LocalSocketImpl already has an fd"); } int osType; switch (sockType) { case LocalSocket.SOCKET_DGRAM: osType = OsConstants.SOCK_DGRAM; break; case LocalSocket.SOCKET_STREAM: osType = OsConstants.SOCK_STREAM; break; case LocalSocket.SOCKET_SEQPACKET: osType = OsConstants.SOCK_SEQPACKET; break; default: throw new IllegalStateException("unknown sockType"); } try { fd = Os.socket(OsConstants.AF_UNIX, osType, 0); mFdCreatedInternally = true; } catch (ErrnoException e) { e.rethrowAsIOException(); } }
Example 15
Source File: IoUtils.java From j2objc with Apache License 2.0 | 5 votes |
/** * Sets 'fd' to be blocking or non-blocking, according to the state of 'blocking'. */ public static void setBlocking(FileDescriptor fd, boolean blocking) throws IOException { try { int flags = Libcore.os.fcntlVoid(fd, F_GETFL); if (!blocking) { flags |= O_NONBLOCK; } else { flags &= ~O_NONBLOCK; } Libcore.os.fcntlLong(fd, F_SETFL, flags); } catch (ErrnoException errnoException) { throw errnoException.rethrowAsIOException(); } }
Example 16
Source File: RevocableFileDescriptor.java From android_9.0.0_r45 with Apache License 2.0 | 5 votes |
/** * Create an instance that references the given {@link File}. */ public RevocableFileDescriptor(Context context, File file) throws IOException { try { init(context, Os.open(file.getAbsolutePath(), OsConstants.O_CREAT | OsConstants.O_RDWR, 0700)); } catch (ErrnoException e) { throw e.rethrowAsIOException(); } }
Example 17
Source File: PackageManagerServiceUtils.java From android_9.0.0_r45 with Apache License 2.0 | 5 votes |
/** * Returns the canonicalized path of {@code path} as per {@code realpath(3)} * semantics. */ public static String realpath(File path) throws IOException { try { return Os.realpath(path.getAbsolutePath()); } catch (ErrnoException ee) { throw ee.rethrowAsIOException(); } }
Example 18
Source File: LocalSocketImpl.java From android_9.0.0_r45 with Apache License 2.0 | 5 votes |
/** * Closes the socket. * * @throws IOException */ public void close() throws IOException { synchronized (LocalSocketImpl.this) { if ((fd == null) || (mFdCreatedInternally == false)) { fd = null; return; } try { Os.close(fd); } catch (ErrnoException e) { e.rethrowAsIOException(); } fd = null; } }
Example 19
Source File: PackageInstallerSession.java From android_9.0.0_r45 with Apache License 2.0 | 5 votes |
private void createRemoveSplitMarkerLocked(String splitName) throws IOException { try { final String markerName = splitName + REMOVE_SPLIT_MARKER_EXTENSION; if (!FileUtils.isValidExtFilename(markerName)) { throw new IllegalArgumentException("Invalid marker: " + markerName); } final File target = new File(resolveStageDirLocked(), markerName); target.createNewFile(); Os.chmod(target.getAbsolutePath(), 0 /*mode*/); } catch (ErrnoException e) { throw e.rethrowAsIOException(); } }
Example 20
Source File: RandomAccessFile.java From j2objc with Apache License 2.0 | 3 votes |
/** * Moves this file's file pointer to a new position, from where following * {@code read}, {@code write} or {@code skip} operations are done. The * position may be greater than the current length of the file, but the * file's length will only change if the moving of the pointer is followed * by a {@code write} operation. * * @param offset * the new file pointer position. * @throws IOException * if this file is closed, {@code pos < 0} or another I/O error * occurs. */ public void seek(long offset) throws IOException { if (offset < 0) { throw new IOException("offset < 0: " + offset); } try { Libcore.os.lseek(fd, offset, SEEK_SET); } catch (ErrnoException errnoException) { throw errnoException.rethrowAsIOException(); } }