Search in sources :

Example 41 with ErrnoException

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);
}
Also used : ErrnoException(libcore.io.ErrnoException) StructFlock(libcore.io.StructFlock)

Example 42 with ErrnoException

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);
}
Also used : NonReadableChannelException(java.nio.channels.NonReadableChannelException) ErrnoException(libcore.io.ErrnoException) NonWritableChannelException(java.nio.channels.NonWritableChannelException)

Example 43 with ErrnoException

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);
    }
}
Also used : ErrnoException(libcore.io.ErrnoException) InetSocketAddress(java.net.InetSocketAddress) SocketAddress(java.net.SocketAddress) InetSocketAddress(java.net.InetSocketAddress)

Example 44 with ErrnoException

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;
}
Also used : VMRuntime(dalvik.system.VMRuntime) ErrnoException(libcore.io.ErrnoException) StructUtsname(libcore.io.StructUtsname) Properties(java.util.Properties) StructPasswd(libcore.io.StructPasswd)

Example 45 with ErrnoException

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);
    }
}
Also used : ErrnoException(libcore.io.ErrnoException) StructAddrinfo(libcore.io.StructAddrinfo) GaiException(libcore.io.GaiException)

Aggregations

ErrnoException (libcore.io.ErrnoException)46 IOException (java.io.IOException)15 FileDescriptor (java.io.FileDescriptor)13 StructStat (libcore.io.StructStat)9 File (java.io.File)7 InetSocketAddress (java.net.InetSocketAddress)7 NonReadableChannelException (java.nio.channels.NonReadableChannelException)6 NonWritableChannelException (java.nio.channels.NonWritableChannelException)6 StructFlock (libcore.io.StructFlock)6 FileOutputStream (java.io.FileOutputStream)5 SocketAddress (java.net.SocketAddress)4 FileInputStream (java.io.FileInputStream)3 SocketTimeoutException (java.net.SocketTimeoutException)3 ClosedChannelException (java.nio.channels.ClosedChannelException)3 FileLock (java.nio.channels.FileLock)3 MutableLong (libcore.util.MutableLong)3 PrintStream (java.io.PrintStream)2 SocketException (java.net.SocketException)2 LinkedList (java.util.LinkedList)2 GaiException (libcore.io.GaiException)2