use of libcore.io.ErrnoException in project robovm by robovm.
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 robovm by robovm.
the class FileChannelImpl method map.
public final MappedByteBuffer map(MapMode mapMode, long position, long size) throws IOException {
checkOpen();
if (mapMode == null) {
throw new NullPointerException("mapMode == null");
}
if (position < 0 || size < 0 || size > Integer.MAX_VALUE) {
throw new IllegalArgumentException("position=" + position + " size=" + size);
}
int accessMode = (mode & O_ACCMODE);
if (accessMode == O_RDONLY) {
if (mapMode != MapMode.READ_ONLY) {
throw new NonWritableChannelException();
}
} else if (accessMode == O_WRONLY) {
throw new NonReadableChannelException();
}
if (position + size > size()) {
// and we only care about making our backing file longer here.
try {
Libcore.os.ftruncate(fd, position + size);
} catch (ErrnoException ftruncateException) {
// continue on.
try {
if (S_ISREG(Libcore.os.fstat(fd).st_mode) || ftruncateException.errno != EINVAL) {
throw ftruncateException.rethrowAsIOException();
}
} catch (ErrnoException fstatException) {
throw fstatException.rethrowAsIOException();
}
}
}
long alignment = position - position % Libcore.os.sysconf(_SC_PAGE_SIZE);
int offset = (int) (position - alignment);
MemoryBlock block = MemoryBlock.mmap(fd, alignment, size + offset, mapMode);
return new DirectByteBuffer(block, (int) size, offset, (mapMode == MapMode.READ_ONLY), mapMode);
}
use of libcore.io.ErrnoException in project robovm by robovm.
the class SocketChannelImpl method initLocalAddressAndPort.
private void initLocalAddressAndPort() {
SocketAddress sa;
try {
sa = Libcore.os.getsockname(fd);
} catch (ErrnoException errnoException) {
throw new AssertionError(errnoException);
}
InetSocketAddress isa = (InetSocketAddress) sa;
localAddress = isa.getAddress();
localPort = isa.getPort();
if (socket != null) {
socket.socketImpl().initLocalPort(localPort);
}
}
use of libcore.io.ErrnoException in project robovm by robovm.
the class System method initSystemProperties.
private static void initSystemProperties() {
VMRuntime runtime = VMRuntime.getRuntime();
Properties p = new Properties();
String projectUrl = "http://www.robovm.org/";
String projectName = "RoboVM";
p.put("java.boot.class.path", runtime.bootClassPath());
p.put("java.class.path", runtime.classPath());
// None of these four are meaningful on Android, but these keys are guaranteed
// to be present for System.getProperty. For java.class.version, we use the maximum
// class file version that dx currently supports.
p.put("java.class.version", "50.0");
p.put("java.compiler", "");
p.put("java.ext.dirs", "");
p.put("java.version", "0");
// RoboVM note: Android uses getenv("JAVA_HOME") here with "/system" as fallback.
p.put("java.home", VM.resourcesPath());
// RoboVM note: Use value of $TMPDIR if set. Otherwise use /tmp as Android does.
String tmpdir = getenv("TMPDIR");
p.put("java.io.tmpdir", tmpdir != null ? tmpdir : "/tmp");
String ldLibraryPath = getenv("LD_LIBRARY_PATH");
if (ldLibraryPath != null) {
p.put("java.library.path", ldLibraryPath);
}
p.put("java.specification.name", "RoboVM Core Library");
p.put("java.specification.vendor", projectName);
p.put("java.specification.version", "0.9");
p.put("java.vendor", projectName);
p.put("java.vendor.url", projectUrl);
p.put("java.vm.name", "RoboVM");
p.put("java.vm.specification.name", "RoboVM Virtual Machine Specification");
p.put("java.vm.specification.vendor", projectName);
p.put("java.vm.specification.version", "0.9");
p.put("java.vm.vendor", projectName);
p.put("java.vm.version", runtime.vmVersion());
p.put("file.separator", "/");
p.put("line.separator", "\n");
p.put("path.separator", ":");
p.put("java.runtime.name", "RoboVM Runtime");
p.put("java.runtime.version", "0.9");
p.put("java.vm.vendor.url", projectUrl);
p.put("file.encoding", "UTF-8");
p.put("user.language", "en");
p.put("user.region", "US");
try {
StructPasswd passwd = Libcore.os.getpwuid(Libcore.os.getuid());
p.put("user.home", passwd.pw_dir);
p.put("user.name", passwd.pw_name);
} catch (ErrnoException exception) {
// RoboVM note: Start change. Fall back to environment variables. getpwuid() fails on the iOS simulator.
String home = getenv("HOME");
String user = getenv("USER");
p.put("user.home", home != null ? home : "");
p.put("user.name", user != null ? user : "");
// RoboVM note: End change.
}
StructUtsname info = Libcore.os.uname();
p.put("os.arch", info.machine);
p.put("os.name", info.sysname);
p.put("os.version", info.release);
// Undocumented Android-only properties.
p.put("android.icu.library.version", ICU.getIcuVersion());
p.put("android.icu.unicode.version", ICU.getUnicodeVersion());
p.put("android.icu.cldr.version", ICU.getCldrVersion());
parsePropertyAssignments(p, specialProperties());
parsePropertyAssignments(p, robovmSpecialProperties());
// user.home, user.dir and user.name values on iOS.
if (p.getProperty("os.name").contains("iOS")) {
// On iOS we want user.home and user.dir to point to the app's data
// container root dir. This is the dir $HOME points to. We also set
// user.name to $USER or hardcode 'mobile' if $USER isn't set (iOS
// simulator).
String home = getenv("HOME");
String user = getenv("USER");
p.put("user.home", home != null ? home : "");
p.put("user.dir", home != null ? home : "/");
p.put("user.name", user != null ? user : "mobile");
}
// RoboVM note: End change.
// Override built-in properties with settings from the command line.
parsePropertyAssignments(p, runtime.properties());
systemProperties = p;
}
use of libcore.io.ErrnoException in project robovm by robovm.
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 = Libcore.os.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 + "\": " + Libcore.os.gai_strerror(gaiException.error);
addressCache.putUnknownHost(host, detailMessage);
throw gaiException.rethrowAsUnknownHostException(detailMessage);
}
}
Aggregations