All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.freedesktop.dbus.FileDescriptor Maven / Gradle / Ivy

Go to download

Improved version of the DBus-Java library provided by freedesktop.org (https://dbus.freedesktop.org/doc/dbus-java/).

There is a newer version: 5.1.0
Show newest version
package org.freedesktop.dbus;

import java.lang.reflect.Constructor;
import java.lang.reflect.Field;
import java.lang.reflect.InvocationTargetException;
import org.freedesktop.dbus.exceptions.MarshallingException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 * Represents a FileDescriptor to be passed over the bus.  Can be created from
 * either an integer(gotten through some JNI/JNA/JNR call) or from a 
 * java.io.FileDescriptor.
 * 
 */
public class FileDescriptor {
    
    private final Logger      logger          = LoggerFactory.getLogger(getClass());
    
    private int m_fd;
    
    public FileDescriptor(int _fd){
        m_fd = _fd;
    }
    
    // TODO this should have a better exception?
    public FileDescriptor(java.io.FileDescriptor _data) throws MarshallingException {
        m_fd = getFileDescriptor(_data);
    }
    
    // TODO this should have a better exception?
    public java.io.FileDescriptor toJavaFileDescriptor() throws MarshallingException {
        return createFileDescriptorByReflection(m_fd);
    }
    
    public int getIntFileDescriptor(){
        return m_fd;
    }
    
    private int getFileDescriptor(java.io.FileDescriptor _data) throws MarshallingException {
        Field declaredField;
        try {
            declaredField = _data.getClass().getDeclaredField("fd");
            declaredField.setAccessible(true);
            return declaredField.getInt(_data);
        } catch (NoSuchFieldException | SecurityException | IllegalArgumentException | IllegalAccessException _ex) {
            logger.error("Could not get filedescriptor by reflection.", _ex);
            throw new MarshallingException("Could not get member 'fd' of FileDescriptor by reflection!", _ex);
        }
    }

    private java.io.FileDescriptor createFileDescriptorByReflection(long _demarshallint) throws MarshallingException {
        try {
            Constructor constructor = java.io.FileDescriptor.class.getDeclaredConstructor(int.class);
            constructor.setAccessible(true);
            return constructor.newInstance((int) _demarshallint);
        } catch (NoSuchMethodException | SecurityException | InstantiationException | IllegalAccessException
                | IllegalArgumentException | InvocationTargetException _ex) {
            logger.error("Could not create new FileDescriptor instance by reflection.", _ex);
            throw new MarshallingException("Could not create new FileDescriptor instance by reflection", _ex);
        }
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy