use of libcore.io.ErrnoException in project XobotOS by xamarin.
the class FileChannelImpl method basicLock.
private FileLock basicLock(long position, long size, boolean shared, boolean wait) throws IOException {
int accessMode = (mode & O_ACCMODE);
if (accessMode == O_RDONLY) {
if (!shared) {
throw new NonWritableChannelException();
}
} else if (accessMode == O_WRONLY) {
if (shared) {
throw new NonReadableChannelException();
}
}
if (position < 0 || size < 0) {
throw new IllegalArgumentException("position=" + position + " size=" + size);
}
FileLock pendingLock = new FileLockImpl(this, position, size, shared);
addLock(pendingLock);
StructFlock flock = new StructFlock();
flock.l_type = (short) (shared ? F_RDLCK : F_WRLCK);
flock.l_whence = (short) SEEK_SET;
flock.l_start = position;
flock.l_len = translateLockLength(size);
boolean success = false;
try {
success = (Libcore.os.fcntlFlock(fd, wait ? F_SETLKW64 : F_SETLK64, flock) != -1);
} catch (ErrnoException errnoException) {
throw errnoException.rethrowAsIOException();
} finally {
if (!success) {
removeLock(pendingLock);
}
}
return success ? pendingLock : null;
}
use of libcore.io.ErrnoException in project XobotOS by xamarin.
the class InetAddress method isReachable.
private boolean isReachable(InetAddress destination, InetAddress source, int timeout) throws IOException {
// TODO: try ICMP first (http://code.google.com/p/android/issues/detail?id=20106)
FileDescriptor fd = IoBridge.socket(true);
boolean reached = false;
try {
if (source != null) {
IoBridge.bind(fd, source, 0);
}
IoBridge.connect(fd, destination, 7, timeout);
reached = true;
} catch (IOException e) {
if (e.getCause() instanceof ErrnoException) {
// "Connection refused" means the IP address was reachable.
reached = (((ErrnoException) e.getCause()).errno == ECONNREFUSED);
}
}
IoBridge.closeSocket(fd);
return reached;
}
use of libcore.io.ErrnoException in project j2objc by google.
the class FileChannelImpl method release.
/**
* Non-API method to release a given lock on a file channel. Assumes that
* the lock will mark itself invalid after successful unlocking.
*/
public void release(FileLock lock) throws IOException {
checkOpen();
StructFlock flock = new StructFlock();
flock.l_type = (short) F_UNLCK;
flock.l_whence = (short) SEEK_SET;
flock.l_start = lock.position();
flock.l_len = translateLockLength(lock.size());
try {
Libcore.os.fcntlFlock(fd, F_SETLKW64, flock);
} catch (ErrnoException errnoException) {
throw errnoException.rethrowAsIOException();
}
removeLock(lock);
}
use of libcore.io.ErrnoException in project j2objc by google.
the class SocketChannelImpl method onBind.
/**
* Initialise the isBound, localAddress and localPort state from the file descriptor. Used when
* some or all of the bound state has been left to the OS to decide, or when the Socket handled
* bind() or connect().
*
* @param updateSocketState
* if the associated socket (if present) needs to be updated
* @hide package visible for other nio classes
*/
void onBind(boolean updateSocketState) {
SocketAddress sa;
try {
sa = NetworkOs.getsockname(fd);
} catch (ErrnoException errnoException) {
throw new AssertionError(errnoException);
}
isBound = true;
InetSocketAddress localSocketAddress = (InetSocketAddress) sa;
localAddress = localSocketAddress.getAddress();
localPort = localSocketAddress.getPort();
if (updateSocketState && socket != null) {
socket.onBind(localAddress, localPort);
}
}
use of libcore.io.ErrnoException in project j2objc by google.
the class InetAddress method lookupHostByName.
/**
* Resolves a hostname to its IP addresses using a cache.
*
* @param host the hostname to resolve.
* @return the IP addresses of the host.
*/
private static InetAddress[] lookupHostByName(String host) throws UnknownHostException {
BlockGuard.getThreadPolicy().onNetwork();
// Do we have a result cached?
Object cachedResult = addressCache.get(host);
if (cachedResult != null) {
if (cachedResult instanceof InetAddress[]) {
// A cached positive result.
return (InetAddress[]) cachedResult;
} else {
// A cached negative result.
throw new UnknownHostException((String) cachedResult);
}
}
try {
StructAddrinfo hints = new StructAddrinfo();
hints.ai_flags = AI_ADDRCONFIG;
hints.ai_family = AF_UNSPEC;
// If we don't specify a socket type, every address will appear twice, once
// for SOCK_STREAM and one for SOCK_DGRAM. Since we do not return the family
// anyway, just pick one.
hints.ai_socktype = SOCK_STREAM;
InetAddress[] addresses = NetworkOs.getaddrinfo(host, hints);
// TODO: should getaddrinfo set the hostname of the InetAddresses it returns?
for (InetAddress address : addresses) {
address.hostName = host;
}
addressCache.put(host, addresses);
return addresses;
} catch (GaiException gaiException) {
// http://code.google.com/p/android/issues/detail?id=15722
if (gaiException.getCause() instanceof ErrnoException) {
if (((ErrnoException) gaiException.getCause()).errno == EACCES) {
throw new SecurityException("Permission denied (missing INTERNET permission?)", gaiException);
}
}
// Otherwise, throw an UnknownHostException.
String detailMessage = "Unable to resolve host \"" + host + "\": " + NetworkOs.gai_strerror(gaiException.error);
addressCache.putUnknownHost(host, detailMessage);
throw gaiException.rethrowAsUnknownHostException(detailMessage);
}
}
Aggregations