forked from geodmms/xdgnjobs

?? ?
2008-06-09 bd210ee7438fd203c19d3e8080ea12b79fe56159
xdgnjobs/ximple-dgnio/src/main/java/com/ximple/io/dgn7/NIOUtilities.java
@@ -17,6 +17,7 @@
 */
// J2SE dependencies
import java.lang.reflect.Method;
import java.nio.ByteBuffer;
import java.security.AccessController;
@@ -28,12 +29,13 @@
/**
 * Utility class for managing memory mapped buffers.
 *
 * @since 2.0
 * @source $URL$
 * @version $Id$
 * @author Andres Aimes
 * @version $Id$
 * @source $URL$
 * @since 2.0
 */
public class NIOUtilities {
public class NIOUtilities
{
    /**
     * {@code true} if a warning has already been logged.
     */
@@ -43,9 +45,10 @@
     * Do not allows instantiation of this class.
     *
     * @todo This constructor will become private when {@code NIOBufferUtils}
     *       will have been removed.
     * will have been removed.
     */
    protected NIOUtilities() {
    protected NIOUtilities()
    {
    }
    /**
@@ -54,28 +57,34 @@
     * case) will be logged as {@code SEVERE} to the logger of the package name. To
     * force logging of errors, set the System property "org.geotools.io.debugBuffer" to "true".
     *
     * @param  buffer The buffer to close.
     * @param buffer The buffer to close.
     * @return true if the operation was successful, false otherwise.
     *
     * @see java.nio.MappedByteBuffer
     */
    public static boolean clean(final ByteBuffer buffer) {
        if (buffer == null || ! buffer.isDirect() ) {
    public static boolean clean(final ByteBuffer buffer)
    {
        if (buffer == null || !buffer.isDirect())
        {
            return false;
        }
        Boolean b = (Boolean) AccessController.doPrivileged(new PrivilegedAction() {
            public Object run() {
        Boolean b = (Boolean) AccessController.doPrivileged(new PrivilegedAction()
        {
            public Object run()
            {
                Boolean success = Boolean.FALSE;
                try {
                    Method getCleanerMethod = buffer.getClass().getMethod("cleaner", (Class[])null);
                try
                {
                    Method getCleanerMethod = buffer.getClass().getMethod("cleaner", (Class[]) null);
                    getCleanerMethod.setAccessible(true);
                    Object cleaner = getCleanerMethod.invoke(buffer,  (Object[])null);
                    Method clean = cleaner.getClass().getMethod("clean", (Class[])null);
                    clean.invoke(cleaner, (Object[])null);
                    Object cleaner = getCleanerMethod.invoke(buffer, (Object[]) null);
                    Method clean = cleaner.getClass().getMethod("clean", (Class[]) null);
                    clean.invoke(cleaner, (Object[]) null);
                    success = Boolean.TRUE;
                } catch (Exception e) {
                } catch (Exception e)
                {
                    // This really is a show stopper on windows
                    if (isLoggable()) {
                    if (isLoggable())
                    {
                        log(e, buffer);
                    }
                }
@@ -89,12 +98,15 @@
    /**
     * Check if a warning message should be logged.
     */
    private static synchronized boolean isLoggable() {
        try {
    private static synchronized boolean isLoggable()
    {
        try
        {
            return !warned && (
                    System.getProperty("org.geotools.io.debugBuffer", "false").equalsIgnoreCase("true") ||
                    System.getProperty("os.name").indexOf("Windows") >= 0 );
        } catch (SecurityException exception) {
                            System.getProperty("os.name").indexOf("Windows") >= 0);
        } catch (SecurityException exception)
        {
            // The utilities may be running in an Applet, in which case we
            // can't read properties. Assumes we are not in debugging mode.
            return false;
@@ -104,11 +116,12 @@
    /**
     * Log a warning message.
     */
    private static synchronized void log(final Exception e, final ByteBuffer buffer) {
    private static synchronized void log(final Exception e, final ByteBuffer buffer)
    {
        warned = true;
        String message = "Error attempting to close a mapped byte buffer : " + buffer.getClass().getName()
                       + "\n JVM : " + System.getProperty("java.version")
                       + ' '         + System.getProperty("java.vendor");
                + "\n JVM : " + System.getProperty("java.version")
                + ' ' + System.getProperty("java.vendor");
        Logger.getLogger("org.geotools.io").log(Level.SEVERE, message, e);
    }
}