本文整理汇总了Java中android.system.ErrnoException类的典型用法代码示例。如果您正苦于以下问题:Java ErrnoException类的具体用法?Java ErrnoException怎么用?Java ErrnoException使用的例子?那么, 这里精选的类代码示例或许可以为您提供帮助。
ErrnoException类属于android.system包,在下文中一共展示了ErrnoException类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: setLength
import android.system.ErrnoException; //导入依赖的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: openReadInternal
import android.system.ErrnoException; //导入依赖的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);
}
}
示例3: read
import android.system.ErrnoException; //导入依赖的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);
}
}
示例4: write
import android.system.ErrnoException; //导入依赖的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);
}
}
示例5: onRead
import android.system.ErrnoException; //导入依赖的package包/类
@Override
public int onRead(long offset, int size, byte[] data) throws ErrnoException {
final ByteBuffer buffer = mBufferPool.obtainBuffer();
try {
mFile.seek(offset);
int readSize;
int total = 0;
while (size > total && (readSize = mFile.read(buffer, size - total)) > 0) {
buffer.get(data, total, readSize);
buffer.clear();
total += readSize;
}
return total;
} catch (IOException e) {
throwErrnoException(e);
} finally {
mBufferPool.recycleBuffer(buffer);
}
return 0;
}
示例6: onWrite
import android.system.ErrnoException; //导入依赖的package包/类
@Override
public int onWrite(long offset, int size, byte[] data) throws ErrnoException {
int written = 0;
final ByteBuffer buffer = mBufferPool.obtainBuffer();
try {
mFile.seek(offset);
while (written < size) {
int willWrite = Math.min(size - written, buffer.capacity());
buffer.put(data, written, willWrite);
int res = mFile.write(buffer, willWrite);
written += res;
buffer.clear();
}
} catch (IOException e) {
throwErrnoException(e);
} finally {
mBufferPool.recycleBuffer(buffer);
}
return written;
}
示例7: runVpn
import android.system.ErrnoException; //导入依赖的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");
}
}
示例8: forwardPacket
import android.system.ErrnoException; //导入依赖的package包/类
public void forwardPacket(DatagramPacket outPacket, IpPacket parsedPacket) throws VpnNetworkException {
DatagramSocket dnsSocket = null;
try {
// Packets to be sent to the real DNS server will need to be protected from the VPN
dnsSocket = new DatagramSocket();
vpnService.protect(dnsSocket);
dnsSocket.send(outPacket);
if (parsedPacket != null)
dnsIn.add(new WaitingOnSocketPacket(dnsSocket, parsedPacket));
else
FileHelper.closeOrWarn(dnsSocket, TAG, "handleDnsRequest: Cannot close socket in error");
} catch (IOException e) {
FileHelper.closeOrWarn(dnsSocket, TAG, "handleDnsRequest: Cannot close socket in error");
if (e.getCause() instanceof ErrnoException) {
ErrnoException errnoExc = (ErrnoException) e.getCause();
if ((errnoExc.errno == OsConstants.ENETUNREACH) || (errnoExc.errno == OsConstants.EPERM)) {
throw new VpnNetworkException("Cannot send message:", e);
}
}
Log.w(TAG, "handleDnsRequest: Could not send packet to upstream", e);
return;
}
}
示例9: testPoll_retryInterrupt
import android.system.ErrnoException; //导入依赖的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.ErrnoException; //导入依赖的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.ErrnoException; //导入依赖的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.ErrnoException; //导入依赖的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: isIOExceptionCausedByEPIPE
import android.system.ErrnoException; //导入依赖的package包/类
private static boolean isIOExceptionCausedByEPIPE(IOException e) {
if (android.os.Build.VERSION.SDK_INT < android.os.Build.VERSION_CODES.LOLLIPOP) {
return e.getMessage().contains("EPIPE");
}
Throwable cause = e.getCause();
return cause instanceof ErrnoException && ((ErrnoException) cause).errno == OsConstants.EPIPE;
}
示例14: sigtermCompat
import android.system.ErrnoException; //导入依赖的package包/类
@Deprecated // Use Process.destroy() since API 24
public static void sigtermCompat(Process process) throws Exception {
if (Build.VERSION.SDK_INT >= 24) throw new UnsupportedOperationException("Never call this method in OpenJDK!");
int errno = sigterm(process);
if (errno != 0) throw Build.VERSION.SDK_INT >= 21
? new ErrnoException("kill", errno) : new Exception("kill failed: " + errno);
}
示例15: symlink
import android.system.ErrnoException; //导入依赖的package包/类
@TargetApi(21)
void symlink(SanitizedFile source, SanitizedFile dest) {
try {
android.system.Os.symlink(source.getAbsolutePath(), dest.getAbsolutePath());
} catch (ErrnoException e) {
// Do nothing...
}
}