Java Code Examples for android.system.Os#read()

The following examples show how to use android.system.Os#read() . 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: NativeCrashListener.java    From android_9.0.0_r45 with Apache License 2.0 6 votes vote down vote up
static int readExactly(FileDescriptor fd, byte[] buffer, int offset, int numBytes)
        throws ErrnoException, InterruptedIOException {
    int totalRead = 0;
    while (numBytes > 0) {
        int n = Os.read(fd, buffer, offset + totalRead, numBytes);
        if (n <= 0) {
            if (DEBUG) {
                Slog.w(TAG, "Needed " + numBytes + " but saw " + n);
            }
            return -1;  // premature EOF or timeout
        }
        numBytes -= n;
        totalRead += n;
    }
    return totalRead;
}
 
Example 2
Source File: FileUtils.java    From android_9.0.0_r45 with Apache License 2.0 6 votes vote down vote up
@Override
public void run() {
    final FileDescriptor fd = getInternalFD();
    try {
        int i = 0;
        while (i < data.length) {
            if (sink) {
                i += Os.read(fd, data, i, data.length - i);
            } else {
                i += Os.write(fd, data, i, data.length - i);
            }
        }
    } catch (IOException | ErrnoException e) {
        // Ignored
    } finally {
        if (sink) {
            SystemClock.sleep(TimeUnit.SECONDS.toMillis(1));
        }
        IoUtils.closeQuietly(fd);
    }
}
 
Example 3
Source File: FileBridge.java    From container with GNU General Public License v3.0 6 votes vote down vote up
/**
 * 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.checkOffsetAndCount(bytes.length, byteOffset, byteCount);
    if (byteCount == 0) {
        return 0;
    }
    try {
        int readCount = Os.read(fd, bytes, byteOffset, byteCount);
        if (readCount == 0) {
            return -1;
        }
        return readCount;
    } catch (ErrnoException errnoException) {
        if (errnoException.errno == OsConstants.EAGAIN) {
            // We return 0 rather than throw if we try to read from an empty non-blocking pipe.
            return 0;
        }
        throw new IOException(errnoException);
    }
}