| | |
| | | */ |
| | | |
| | | // J2SE dependencies |
| | | |
| | | import java.lang.reflect.Method; |
| | | import java.nio.ByteBuffer; |
| | | import java.security.AccessController; |
| | |
| | | /** |
| | | * 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. |
| | | */ |
| | |
| | | * 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() |
| | | { |
| | | } |
| | | |
| | | /** |
| | |
| | | * 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); |
| | | } |
| | | } |
| | |
| | | /** |
| | | * 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; |
| | |
| | | /** |
| | | * 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); |
| | | } |
| | | } |