本文整理汇总了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 + ")");
}
}
示例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();
}
示例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);
}
}
示例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);
}
}
示例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);
}
}
示例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();
}
示例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) {
}
}
示例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");
}
}
示例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);
}
示例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);
}
示例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;
}
示例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);
}
}
}
示例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();
}
示例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();
}
}
示例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();
}
}