当前位置: 首页>>代码示例>>Java>>正文


Java Os类代码示例

本文整理汇总了Java中android.system.Os的典型用法代码示例。如果您正苦于以下问题:Java Os类的具体用法?Java Os怎么用?Java Os使用的例子?那么, 这里精选的类代码示例或许可以为您提供帮助。


Os类属于android.system包,在下文中一共展示了Os类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。

示例1: setLength

import android.system.Os; //导入依赖的package包/类
@Override
public void setLength(long newLength) throws IOException {
    final String tag = "DownloadUriOutputStream";
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
        try {
            Os.ftruncate(pdf.getFileDescriptor(), newLength);
        } catch (ErrnoException e) {
            Util.w(tag, "It can't pre-allocate length(" + newLength + ") on the sdk"
                    + " version(" + Build.VERSION.SDK_INT + "), because of " + e);
            e.printStackTrace();
        }
    } else {
        Util.w(tag,
                "It can't pre-allocate length(" + newLength + ") on the sdk "
                        + "version(" + Build.VERSION.SDK_INT + ")");
    }
}
 
开发者ID:lingochamp,项目名称:okdownload,代码行数:18,代码来源:DownloadUriOutputStream.java

示例2: chmod

import android.system.Os; //导入依赖的package包/类
/**
 * @param path
 * @param mode {@link FileMode}
 * @throws Exception
 */
public static void chmod(String path, int mode) throws Exception {
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
        try {
            Os.chmod(path, mode);
            return;
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    File file = new File(path);
    String cmd = "chmod ";
    if (file.isDirectory()) {
        cmd += " -R ";
    }
    String cmode = String.format("%o", mode);
    Runtime.getRuntime().exec(cmd + cmode + " " + path).waitFor();
}
 
开发者ID:7763sea,项目名称:VirtualHook,代码行数:24,代码来源:FileUtils.java

示例3: openReadInternal

import android.system.Os; //导入依赖的package包/类
private ParcelFileDescriptor openReadInternal(String name) throws IOException {
    assertPreparedAndNotSealed("openRead");

    try {
        if (!FileUtils.isValidExtFilename(name)) {
            throw new IllegalArgumentException("Invalid name: " + name);
        }
        final File target = new File(resolveStageDir(), name);

        final FileDescriptor targetFd = Os.open(target.getAbsolutePath(), O_RDONLY, 0);
        return ParcelFileDescriptor.dup(targetFd);

    } catch (ErrnoException e) {
        throw new IOException(e);
    }
}
 
开发者ID:7763sea,项目名称:VirtualHook,代码行数:17,代码来源:PackageInstallerSession.java

示例4: read

import android.system.Os; //导入依赖的package包/类
/**
 * 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);
    }
}
 
开发者ID:7763sea,项目名称:VirtualHook,代码行数:24,代码来源:FileBridge.java

示例5: write

import android.system.Os; //导入依赖的package包/类
/**
 * java.io always writes every byte it's asked to, or fails with an error. (That is, unlike
 * Unix it never just writes as many bytes as happens to be convenient.)
 */
public static void write(FileDescriptor fd, byte[] bytes, int byteOffset, int byteCount) throws IOException {
    ArrayUtils.checkOffsetAndCount(bytes.length, byteOffset, byteCount);
    if (byteCount == 0) {
        return;
    }
    try {
        while (byteCount > 0) {
            int bytesWritten = Os.write(fd, bytes, byteOffset, byteCount);
            byteCount -= bytesWritten;
            byteOffset += bytesWritten;
        }
    } catch (ErrnoException errnoException) {
        throw new IOException(errnoException);
    }
}
 
开发者ID:7763sea,项目名称:VirtualHook,代码行数:20,代码来源:FileBridge.java

示例6: chmod

import android.system.Os; //导入依赖的package包/类
/**
 * @param path
 * @param mode {@link FileMode}
 * @throws Exception
 */
public static void chmod(String path, int mode) throws Exception {
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
        try {
            Os.chmod(path, mode);
            return;
        } catch (Exception e) {
            // ignore
        }
    }

    File file = new File(path);
    String cmd = "chmod ";
    if (file.isDirectory()) {
        cmd += " -R ";
    }
    String cmode = String.format("%o", mode);
    Runtime.getRuntime().exec(cmd + cmode + " " + path).waitFor();
}
 
开发者ID:coding-dream,项目名称:TPlayer,代码行数:24,代码来源:FileUtils.java

示例7: stop

import android.system.Os; //导入依赖的package包/类
@TargetApi(Build.VERSION_CODES.LOLLIPOP)
public void stop() {
    try {
        if (mInterruptFd != null) {
            Os.close(mInterruptFd);
        }
        if (mBlockFd != null) {
            Os.close(mBlockFd);
        }
        if (this.descriptor != null) {
            this.descriptor.close();
            this.descriptor = null;
        }
    } catch (Exception ignored) {
    }
}
 
开发者ID:iTXTech,项目名称:Daedalus,代码行数:17,代码来源:UdpProvider.java

示例8: runVpn

import android.system.Os; //导入依赖的package包/类
private void runVpn() throws InterruptedException, ErrnoException, IOException, VpnNetworkException {
    // Allocate the buffer for a single packet.
    byte[] packet = new byte[32767];

    // A pipe we can interrupt the poll() call with by closing the interruptFd end
    FileDescriptor[] pipes = Os.pipe();
    mInterruptFd = pipes[0];
    mBlockFd = pipes[1];

    // Authenticate and configure the virtual network interface.
    try (ParcelFileDescriptor pfd = configure()) {
        // Read and write views of the tun device
        FileInputStream inputStream = new FileInputStream(pfd.getFileDescriptor());
        FileOutputStream outFd = new FileOutputStream(pfd.getFileDescriptor());

        // Now we are connected. Set the flag and show the message.
        if (notify != null)
            notify.run(AdVpnService.VPN_STATUS_RUNNING);

        // We keep forwarding packets till something goes wrong.
        while (doOne(inputStream, outFd, packet))
            ;
    } finally {
        mBlockFd = FileHelper.closeOrWarn(mBlockFd, TAG, "runVpn: Could not close blockFd");
    }
}
 
开发者ID:julian-klode,项目名称:dns66,代码行数:27,代码来源:AdVpnThread.java

示例9: testPoll_retryInterrupt

import android.system.Os; //导入依赖的package包/类
@Test
@PrepareForTest({Log.class, Os.class})
public void testPoll_retryInterrupt() throws Exception {
    mockStatic(Log.class);
    mockStatic(Os.class);
    when(Os.poll(any(StructPollfd[].class), anyInt())).then(new Answer<Integer>() {
        @Override
        public Integer answer(InvocationOnMock invocation) throws Throwable {
            // First try fails with EINTR, seconds returns success.
            if (testResult++ == 0) {
                // Android actually sets all OsConstants to 0 when running the
                // unit tests, so this works, but another constant would have
                // exactly the same result.
                throw new ErrnoException("poll", OsConstants.EINTR);
            }
            return 0;
        }
    });

    // poll() will be interrupted first time, so called a second time.
    assertEquals(0, FileHelper.poll(null, 0));
    assertEquals(2, testResult);
}
 
开发者ID:julian-klode,项目名称:dns66,代码行数:24,代码来源:FileHelperTest.java

示例10: testCloseOrWarn_fileDescriptor

import android.system.Os; //导入依赖的package包/类
@Test
@PrepareForTest({Log.class, Os.class})
public void testCloseOrWarn_fileDescriptor() throws Exception {
    FileDescriptor fd = mock(FileDescriptor.class);
    mockStatic(Log.class);
    mockStatic(Os.class);
    when(Log.e(anyString(), anyString(), any(Throwable.class))).then(new CountingAnswer(null));

    // Closing null should work just fine
    testResult = 0;
    assertNull(FileHelper.closeOrWarn((FileDescriptor) null, "tag", "msg"));
    assertEquals(0, testResult);

    // Successfully closing the file should not log.
    testResult = 0;
    assertNull(FileHelper.closeOrWarn(fd, "tag", "msg"));
    assertEquals(0, testResult);

    // If closing fails, it should log.
    testResult = 0;
    doThrow(new ErrnoException("close", 0)).when(Os.class, "close", any(FileDescriptor.class));
    assertNull(FileHelper.closeOrWarn(fd, "tag", "msg"));
    assertEquals(1, testResult);
}
 
开发者ID:julian-klode,项目名称:dns66,代码行数:25,代码来源:FileHelperTest.java

示例11: calculateSize

import android.system.Os; //导入依赖的package包/类
/**
 * Calculate size of the entry reading directory tree
 * @param file is file corresponding to this entry
 * @return size of entry in blocks
 */
private final long calculateSize(LegacyFile file) {
  if (file.isLink()) return 0;

  if (file.isFile()) {
    try {
      StructStat res = Os.stat(file.getCannonicalPath());
      return res.st_blocks;
    } catch (ErrnoException|IOException e) {
      return 0;
    }
  }

  LegacyFile[] list = null;
  try {
    list = file.listFiles();
  } catch (SecurityException io) {
    Log.e("diskusage", "list files", io);
  }
  if (list == null) return 0;
  long size = 1;

  for (int i = 0; i < list.length; i++)
    size += calculateSize(list[i]);
  return size;
}
 
开发者ID:IvanVolosyuk,项目名称:diskusage,代码行数:31,代码来源:Scanner.java

示例12: copyFile

import android.system.Os; //导入依赖的package包/类
private void copyFile() throws IOException, ErrnoException {
    try (InputStream in = assetManager.open(assetNameBuilder.toString(), AssetManager.ACCESS_BUFFER))
    {
        final FileDescriptor fd = Os.open(
                directoryNameBuilder.toString(),
                OsConstants.O_CREAT | OsConstants.O_TRUNC | OsConstants.O_WRONLY,
                OsConstants.S_IROTH | OsConstants.S_IWUSR);

        try (OutputStream out = new FileOutputStream(fd)) {
            int read;
            while ((read = in.read(buffer)) != -1) {
                out.write(buffer, 0, read);
            }
        } finally {
            Os.close(fd);
        }
    }
}
 
开发者ID:chdir,项目名称:aria2-android,代码行数:19,代码来源:WebUiExtractor.java

示例13: dumpProcStatus

import android.system.Os; //导入依赖的package包/类
/**
 * {@code ps}
 */
public static String dumpProcStatus() {
    String[] stat = myProcStat();
    if (stat.length < 27) {
        return "unknown stat string:" + Arrays.toString(stat);
    }
    StringBuilder builder = new StringBuilder(128);
    builder.append("uptime=").append(SystemClock.elapsedRealtime()).append('\t');
    if (Build.VERSION.SDK_INT >= 21) {
        builder.append("CLK_TCK=").append(Os.sysconf(OsConstants._SC_CLK_TCK)).append("HZ\t");
    }
    builder.append("pid=").append(stat[0]).append(',')
            .append("utime=").append(stat[13]).append(',')
            .append("stime=").append(stat[14]).append(',')
            .append("num_threads=").append(stat[19]).append(',')
            .append("starttime=").append(stat[21]);
    return builder.toString();
}
 
开发者ID:succlz123,项目名称:S1-Go,代码行数:21,代码来源:ProcessUtils.java

示例14: deleteIfOld

import android.system.Os; //导入依赖的package包/类
static void deleteIfOld(File file, long olderThan) {
    try {
        StructStat stat = Os.lstat(file.getAbsolutePath());
        if ((stat.st_atime * 1000L) < olderThan) {
            file.delete();
        }
    } catch (ErrnoException e) {
        e.printStackTrace();
    }
}
 
开发者ID:uhuru-mobile,项目名称:mobile-store,代码行数:11,代码来源:CleanCacheService21.java

示例15: createSymlink

import android.system.Os; //导入依赖的package包/类
public static void createSymlink(String oldPath, String newPath) throws Exception {
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
        Os.symlink(oldPath, newPath);
    } else {
        Runtime.getRuntime().exec("ln -s " + oldPath + " " + newPath).waitFor();
    }
}
 
开发者ID:7763sea,项目名称:VirtualHook,代码行数:8,代码来源:FileUtils.java


注:本文中的android.system.Os类示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。