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

The following examples show how to use android.system.Os#posix_fallocate() . 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: DownloadUriOutputStream.java    From okdownload with Apache License 2.0 5 votes vote down vote up
@Override
public void setLength(long newLength) {
    final String tag = "DownloadUriOutputStream";
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
        try {
            Os.posix_fallocate(pdf.getFileDescriptor(), 0, newLength);
        } catch (Throwable e) {
            if (e instanceof ErrnoException) {
                if (((ErrnoException) e).errno == OsConstants.ENOSYS
                        || ((ErrnoException) e).errno == OsConstants.ENOTSUP) {
                    Util.w(tag, "fallocate() not supported; falling back to ftruncate()");
                    try {
                        Os.ftruncate(pdf.getFileDescriptor(), newLength);
                    } catch (Throwable e1) {
                        Util.w(tag, "It can't pre-allocate length(" + newLength + ") on the sdk"
                                + " version(" + Build.VERSION.SDK_INT + "), because of " + e1);
                    }
                 }
            } else {
                Util.w(tag, "It can't pre-allocate length(" + newLength + ") on the sdk"
                        + " version(" + Build.VERSION.SDK_INT + "), because of " + e);
            }
        }
    } else {
        Util.w(tag,
                "It can't pre-allocate length(" + newLength + ") on the sdk "
                        + "version(" + Build.VERSION.SDK_INT + ")");
    }
}
 
Example 2
Source File: PackageInstallerSession.java    From container with GNU General Public License v3.0 5 votes vote down vote up
private ParcelFileDescriptor openWriteInternal(String name, long offsetBytes, long lengthBytes)
        throws IOException {
    // Quick sanity check of state, and allocate a pipe for ourselves. We
    // then do heavy disk allocation outside the lock, but this open pipe
    // will block any attempted install transitions.
    final FileBridge bridge;
    synchronized (mLock) {
        assertPreparedAndNotSealed("openWrite");

        bridge = new FileBridge();
        mBridges.add(bridge);
    }
    try {
        final File target = new File(resolveStageDir(), name);
        // TODO: this should delegate to DCS so the system process avoids
        // holding open FDs into containers.
        final FileDescriptor targetFd = Os.open(target.getAbsolutePath(),
                O_CREAT | O_WRONLY, 0644);
        // If caller specified a total length, allocate it for them. Free up
        // cache space to grow, if needed.
        if (lengthBytes > 0) {
            Os.posix_fallocate(targetFd, 0, lengthBytes);
        }
        if (offsetBytes > 0) {
            Os.lseek(targetFd, offsetBytes, OsConstants.SEEK_SET);
        }
        bridge.setTargetFile(targetFd);
        bridge.start();
        return ParcelFileDescriptor.dup(bridge.getClientSocket());

    } catch (ErrnoException e) {
        throw new IOException(e);
    }
}
 
Example 3
Source File: SysUtil.java    From SoLoader with Apache License 2.0 5 votes vote down vote up
@DoNotOptimize
public static void fallocateIfSupported(FileDescriptor fd, long length) throws IOException {
  try {
    Os.posix_fallocate(fd, 0, length);
  } catch (ErrnoException ex) {
    if (ex.errno != OsConstants.EOPNOTSUPP
        && ex.errno != OsConstants.ENOSYS
        && ex.errno != OsConstants.EINVAL) {
      throw new IOException(ex.toString(), ex);
    }
  }
}