jdk/src/share/classes/sun/nio/ch/Util.java
author alanb
Thu, 24 Jul 2008 12:46:41 +0100
changeset 895 67f1dc69ad10
parent 798 75b3a644ef91
child 2057 3acf8e5e2ca0
permissions -rw-r--r--
6726309: Compiler warnings in nio code Reviewed-by: sherman, iris
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
715
f16baef3a20e 6719955: Update copyright year
xdono
parents: 51
diff changeset
     2
 * Copyright 2000-2008 Sun Microsystems, Inc.  All Rights Reserved.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
90ce3da70b43 Initial load
duke
parents:
diff changeset
     4
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
90ce3da70b43 Initial load
duke
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
90ce3da70b43 Initial load
duke
parents:
diff changeset
     7
 * published by the Free Software Foundation.  Sun designates this
90ce3da70b43 Initial load
duke
parents:
diff changeset
     8
 * particular file as subject to the "Classpath" exception as provided
90ce3da70b43 Initial load
duke
parents:
diff changeset
     9
 * by Sun in the LICENSE file that accompanied this code.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    10
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    11
 * This code is distributed in the hope that it will be useful, but WITHOUT
90ce3da70b43 Initial load
duke
parents:
diff changeset
    12
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
90ce3da70b43 Initial load
duke
parents:
diff changeset
    13
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
90ce3da70b43 Initial load
duke
parents:
diff changeset
    14
 * version 2 for more details (a copy is included in the LICENSE file that
90ce3da70b43 Initial load
duke
parents:
diff changeset
    15
 * accompanied this code).
90ce3da70b43 Initial load
duke
parents:
diff changeset
    16
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    17
 * You should have received a copy of the GNU General Public License version
90ce3da70b43 Initial load
duke
parents:
diff changeset
    18
 * 2 along with this work; if not, write to the Free Software Foundation,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    19
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    20
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    21
 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    22
 * CA 95054 USA or visit www.sun.com if you need additional information or
90ce3da70b43 Initial load
duke
parents:
diff changeset
    23
 * have any questions.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    24
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    25
90ce3da70b43 Initial load
duke
parents:
diff changeset
    26
package sun.nio.ch;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    27
90ce3da70b43 Initial load
duke
parents:
diff changeset
    28
import java.lang.ref.SoftReference;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    29
import java.lang.reflect.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
import java.io.IOException;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
import java.nio.ByteBuffer;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
import java.nio.MappedByteBuffer;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
import java.nio.channels.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
import java.nio.channels.spi.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
import java.security.AccessController;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
import java.security.PrivilegedAction;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
import java.util.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
import sun.misc.Unsafe;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
import sun.misc.Cleaner;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
import sun.security.action.GetPropertyAction;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
class Util {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
    // -- Caches --
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
    // The number of temp buffers in our pool
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
    private static final int TEMP_BUF_POOL_SIZE = 3;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
    // Per-thread soft cache of the last temporary direct buffer
51
6fe31bc95bbc 6600143: Remove another 450 unnecessary casts
martin
parents: 2
diff changeset
    52
    private static ThreadLocal<SoftReference<ByteBuffer>>[] bufferPool;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
895
67f1dc69ad10 6726309: Compiler warnings in nio code
alanb
parents: 798
diff changeset
    54
    @SuppressWarnings("unchecked")
67f1dc69ad10 6726309: Compiler warnings in nio code
alanb
parents: 798
diff changeset
    55
    static ThreadLocal<SoftReference<ByteBuffer>>[] createThreadLocalBufferPool() {
67f1dc69ad10 6726309: Compiler warnings in nio code
alanb
parents: 798
diff changeset
    56
        return new ThreadLocal[TEMP_BUF_POOL_SIZE];
67f1dc69ad10 6726309: Compiler warnings in nio code
alanb
parents: 798
diff changeset
    57
    }
67f1dc69ad10 6726309: Compiler warnings in nio code
alanb
parents: 798
diff changeset
    58
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
    static {
895
67f1dc69ad10 6726309: Compiler warnings in nio code
alanb
parents: 798
diff changeset
    60
        bufferPool = createThreadLocalBufferPool();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
        for (int i=0; i<TEMP_BUF_POOL_SIZE; i++)
51
6fe31bc95bbc 6600143: Remove another 450 unnecessary casts
martin
parents: 2
diff changeset
    62
            bufferPool[i] = new ThreadLocal<SoftReference<ByteBuffer>>();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
    static ByteBuffer getTemporaryDirectBuffer(int size) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
        ByteBuffer buf = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
        // Grab a buffer if available
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
        for (int i=0; i<TEMP_BUF_POOL_SIZE; i++) {
51
6fe31bc95bbc 6600143: Remove another 450 unnecessary casts
martin
parents: 2
diff changeset
    69
            SoftReference<ByteBuffer> ref = bufferPool[i].get();
6fe31bc95bbc 6600143: Remove another 450 unnecessary casts
martin
parents: 2
diff changeset
    70
            if ((ref != null) && ((buf = ref.get()) != null) &&
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
                (buf.capacity() >= size)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
                buf.rewind();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
                buf.limit(size);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
                bufferPool[i].set(null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
                return buf;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
        // Make a new one
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
        return ByteBuffer.allocateDirect(size);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
    static void releaseTemporaryDirectBuffer(ByteBuffer buf) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
        if (buf == null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
            return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
        // Put it in an empty slot if such exists
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
        for (int i=0; i<TEMP_BUF_POOL_SIZE; i++) {
51
6fe31bc95bbc 6600143: Remove another 450 unnecessary casts
martin
parents: 2
diff changeset
    88
            SoftReference<ByteBuffer> ref = bufferPool[i].get();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
            if ((ref == null) || (ref.get() == null)) {
51
6fe31bc95bbc 6600143: Remove another 450 unnecessary casts
martin
parents: 2
diff changeset
    90
                bufferPool[i].set(new SoftReference<ByteBuffer>(buf));
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
                return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
        // Otherwise replace a smaller one in the cache if such exists
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
        for (int i=0; i<TEMP_BUF_POOL_SIZE; i++) {
51
6fe31bc95bbc 6600143: Remove another 450 unnecessary casts
martin
parents: 2
diff changeset
    96
            SoftReference<ByteBuffer> ref = bufferPool[i].get();
6fe31bc95bbc 6600143: Remove another 450 unnecessary casts
martin
parents: 2
diff changeset
    97
            ByteBuffer inCacheBuf = ref.get();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
            if ((inCacheBuf == null) || (buf.capacity() > inCacheBuf.capacity())) {
51
6fe31bc95bbc 6600143: Remove another 450 unnecessary casts
martin
parents: 2
diff changeset
    99
                bufferPool[i].set(new SoftReference<ByteBuffer>(buf));
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
                return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
    private static class SelectorWrapper {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
        private Selector sel;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
        private SelectorWrapper (Selector sel) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
            this.sel = sel;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
            Cleaner.create(this, new Closer(sel));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
        private static class Closer implements Runnable {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
            private Selector sel;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
            private Closer (Selector sel) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
                this.sel = sel;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
            public void run () {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
                try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
                    sel.close();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
                } catch (Throwable th) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
                    throw new Error(th);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
        public Selector get() { return sel;}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
    // Per-thread cached selector
51
6fe31bc95bbc 6600143: Remove another 450 unnecessary casts
martin
parents: 2
diff changeset
   128
    private static ThreadLocal<SoftReference<SelectorWrapper>> localSelector
6fe31bc95bbc 6600143: Remove another 450 unnecessary casts
martin
parents: 2
diff changeset
   129
        = new ThreadLocal<SoftReference<SelectorWrapper>>();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
    // Hold a reference to the selWrapper object to prevent it from
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
    // being cleaned when the temporary selector wrapped is on lease.
51
6fe31bc95bbc 6600143: Remove another 450 unnecessary casts
martin
parents: 2
diff changeset
   132
    private static ThreadLocal<SelectorWrapper> localSelectorWrapper
6fe31bc95bbc 6600143: Remove another 450 unnecessary casts
martin
parents: 2
diff changeset
   133
        = new ThreadLocal<SelectorWrapper>();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
    // When finished, invoker must ensure that selector is empty
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
    // by cancelling any related keys and explicitly releasing
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
    // the selector by invoking releaseTemporarySelector()
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
    static Selector getTemporarySelector(SelectableChannel sc)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
        throws IOException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
    {
51
6fe31bc95bbc 6600143: Remove another 450 unnecessary casts
martin
parents: 2
diff changeset
   141
        SoftReference<SelectorWrapper> ref = localSelector.get();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
        SelectorWrapper selWrapper = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
        Selector sel = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
        if (ref == null
51
6fe31bc95bbc 6600143: Remove another 450 unnecessary casts
martin
parents: 2
diff changeset
   145
            || ((selWrapper = ref.get()) == null)
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
            || ((sel = selWrapper.get()) == null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
            || (sel.provider() != sc.provider())) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
            sel = sc.provider().openSelector();
787
637b139ce684 6645197: (so) Timed read with socket adaptor throws ClosedSelectorException if temporary selector GC'ed
sherman
parents: 51
diff changeset
   149
            selWrapper = new SelectorWrapper(sel);
637b139ce684 6645197: (so) Timed read with socket adaptor throws ClosedSelectorException if temporary selector GC'ed
sherman
parents: 51
diff changeset
   150
            localSelector.set(new SoftReference<SelectorWrapper>(selWrapper));
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
        }
787
637b139ce684 6645197: (so) Timed read with socket adaptor throws ClosedSelectorException if temporary selector GC'ed
sherman
parents: 51
diff changeset
   152
        localSelectorWrapper.set(selWrapper);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
        return sel;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
    static void releaseTemporarySelector(Selector sel)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
        throws IOException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
        // Selector should be empty
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
        sel.selectNow();                // Flush cancelled keys
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
        assert sel.keys().isEmpty() : "Temporary selector not empty";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
        localSelectorWrapper.set(null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
    // -- Random stuff --
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
    static ByteBuffer[] subsequence(ByteBuffer[] bs, int offset, int length) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
        if ((offset == 0) && (length == bs.length))
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
            return bs;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
        int n = length;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   172
        ByteBuffer[] bs2 = new ByteBuffer[n];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   173
        for (int i = 0; i < n; i++)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   174
            bs2[i] = bs[offset + i];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   175
        return bs2;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   176
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   177
90ce3da70b43 Initial load
duke
parents:
diff changeset
   178
    static <E> Set<E> ungrowableSet(final Set<E> s) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   179
        return new Set<E>() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   180
90ce3da70b43 Initial load
duke
parents:
diff changeset
   181
                public int size()                 { return s.size(); }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   182
                public boolean isEmpty()          { return s.isEmpty(); }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   183
                public boolean contains(Object o) { return s.contains(o); }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   184
                public Object[] toArray()         { return s.toArray(); }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   185
                public <T> T[] toArray(T[] a)     { return s.toArray(a); }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   186
                public String toString()          { return s.toString(); }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   187
                public Iterator<E> iterator()     { return s.iterator(); }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   188
                public boolean equals(Object o)   { return s.equals(o); }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   189
                public int hashCode()             { return s.hashCode(); }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   190
                public void clear()               { s.clear(); }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   191
                public boolean remove(Object o)   { return s.remove(o); }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   192
90ce3da70b43 Initial load
duke
parents:
diff changeset
   193
                public boolean containsAll(Collection<?> coll) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   194
                    return s.containsAll(coll);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   195
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   196
                public boolean removeAll(Collection<?> coll) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   197
                    return s.removeAll(coll);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   198
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   199
                public boolean retainAll(Collection<?> coll) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   200
                    return s.retainAll(coll);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   201
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   202
90ce3da70b43 Initial load
duke
parents:
diff changeset
   203
                public boolean add(E o){
90ce3da70b43 Initial load
duke
parents:
diff changeset
   204
                    throw new UnsupportedOperationException();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   205
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   206
                public boolean addAll(Collection<? extends E> coll) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   207
                    throw new UnsupportedOperationException();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   208
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   209
90ce3da70b43 Initial load
duke
parents:
diff changeset
   210
        };
90ce3da70b43 Initial load
duke
parents:
diff changeset
   211
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   212
90ce3da70b43 Initial load
duke
parents:
diff changeset
   213
90ce3da70b43 Initial load
duke
parents:
diff changeset
   214
    // -- Unsafe access --
90ce3da70b43 Initial load
duke
parents:
diff changeset
   215
90ce3da70b43 Initial load
duke
parents:
diff changeset
   216
    private static Unsafe unsafe = Unsafe.getUnsafe();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   217
90ce3da70b43 Initial load
duke
parents:
diff changeset
   218
    private static byte _get(long a) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   219
        return unsafe.getByte(a);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   220
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   221
90ce3da70b43 Initial load
duke
parents:
diff changeset
   222
    private static void _put(long a, byte b) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   223
        unsafe.putByte(a, b);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   224
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   225
90ce3da70b43 Initial load
duke
parents:
diff changeset
   226
    static void erase(ByteBuffer bb) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   227
        unsafe.setMemory(((DirectBuffer)bb).address(), bb.capacity(), (byte)0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   228
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   229
90ce3da70b43 Initial load
duke
parents:
diff changeset
   230
    static Unsafe unsafe() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   231
        return unsafe;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   232
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   233
90ce3da70b43 Initial load
duke
parents:
diff changeset
   234
    private static int pageSize = -1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   235
90ce3da70b43 Initial load
duke
parents:
diff changeset
   236
    static int pageSize() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   237
        if (pageSize == -1)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   238
            pageSize = unsafe().pageSize();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   239
        return pageSize;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   240
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   241
90ce3da70b43 Initial load
duke
parents:
diff changeset
   242
    private static volatile Constructor directByteBufferConstructor = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   243
90ce3da70b43 Initial load
duke
parents:
diff changeset
   244
    private static void initDBBConstructor() {
51
6fe31bc95bbc 6600143: Remove another 450 unnecessary casts
martin
parents: 2
diff changeset
   245
        AccessController.doPrivileged(new PrivilegedAction<Void>() {
6fe31bc95bbc 6600143: Remove another 450 unnecessary casts
martin
parents: 2
diff changeset
   246
                public Void run() {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   247
                    try {
51
6fe31bc95bbc 6600143: Remove another 450 unnecessary casts
martin
parents: 2
diff changeset
   248
                        Class<?> cl = Class.forName("java.nio.DirectByteBuffer");
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   249
                        Constructor ctor = cl.getDeclaredConstructor(
90ce3da70b43 Initial load
duke
parents:
diff changeset
   250
                            new Class[] { int.class,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   251
                                          long.class,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   252
                                          Runnable.class });
90ce3da70b43 Initial load
duke
parents:
diff changeset
   253
                        ctor.setAccessible(true);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   254
                        directByteBufferConstructor = ctor;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   255
                    } catch (ClassNotFoundException x) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   256
                        throw new InternalError();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   257
                    } catch (NoSuchMethodException x) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   258
                        throw new InternalError();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   259
                    } catch (IllegalArgumentException x) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   260
                        throw new InternalError();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   261
                    } catch (ClassCastException x) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   262
                        throw new InternalError();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   263
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   264
                    return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   265
                }});
90ce3da70b43 Initial load
duke
parents:
diff changeset
   266
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   267
90ce3da70b43 Initial load
duke
parents:
diff changeset
   268
    static MappedByteBuffer newMappedByteBuffer(int size, long addr,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   269
                                                Runnable unmapper)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   270
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   271
        MappedByteBuffer dbb;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   272
        if (directByteBufferConstructor == null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   273
            initDBBConstructor();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   274
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   275
            dbb = (MappedByteBuffer)directByteBufferConstructor.newInstance(
90ce3da70b43 Initial load
duke
parents:
diff changeset
   276
              new Object[] { new Integer(size),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   277
                             new Long(addr),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   278
                             unmapper });
90ce3da70b43 Initial load
duke
parents:
diff changeset
   279
        } catch (InstantiationException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   280
            throw new InternalError();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   281
        } catch (IllegalAccessException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   282
            throw new InternalError();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   283
        } catch (InvocationTargetException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   284
            throw new InternalError();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   285
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   286
        return dbb;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   287
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   288
90ce3da70b43 Initial load
duke
parents:
diff changeset
   289
    private static volatile Constructor directByteBufferRConstructor = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   290
90ce3da70b43 Initial load
duke
parents:
diff changeset
   291
    private static void initDBBRConstructor() {
51
6fe31bc95bbc 6600143: Remove another 450 unnecessary casts
martin
parents: 2
diff changeset
   292
        AccessController.doPrivileged(new PrivilegedAction<Void>() {
6fe31bc95bbc 6600143: Remove another 450 unnecessary casts
martin
parents: 2
diff changeset
   293
                public Void run() {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   294
                    try {
51
6fe31bc95bbc 6600143: Remove another 450 unnecessary casts
martin
parents: 2
diff changeset
   295
                        Class<?> cl = Class.forName("java.nio.DirectByteBufferR");
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   296
                        Constructor ctor = cl.getDeclaredConstructor(
90ce3da70b43 Initial load
duke
parents:
diff changeset
   297
                            new Class[] { int.class,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   298
                                          long.class,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   299
                                          Runnable.class });
90ce3da70b43 Initial load
duke
parents:
diff changeset
   300
                        ctor.setAccessible(true);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   301
                        directByteBufferRConstructor = ctor;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   302
                    } catch (ClassNotFoundException x) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   303
                        throw new InternalError();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   304
                    } catch (NoSuchMethodException x) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   305
                        throw new InternalError();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   306
                    } catch (IllegalArgumentException x) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   307
                        throw new InternalError();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   308
                    } catch (ClassCastException x) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   309
                        throw new InternalError();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   310
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   311
                    return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   312
                }});
90ce3da70b43 Initial load
duke
parents:
diff changeset
   313
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   314
90ce3da70b43 Initial load
duke
parents:
diff changeset
   315
    static MappedByteBuffer newMappedByteBufferR(int size, long addr,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   316
                                                 Runnable unmapper)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   317
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   318
        MappedByteBuffer dbb;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   319
        if (directByteBufferRConstructor == null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   320
            initDBBRConstructor();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   321
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   322
            dbb = (MappedByteBuffer)directByteBufferRConstructor.newInstance(
90ce3da70b43 Initial load
duke
parents:
diff changeset
   323
              new Object[] { new Integer(size),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   324
                             new Long(addr),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   325
                             unmapper });
90ce3da70b43 Initial load
duke
parents:
diff changeset
   326
        } catch (InstantiationException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   327
            throw new InternalError();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   328
        } catch (IllegalAccessException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   329
            throw new InternalError();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   330
        } catch (InvocationTargetException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   331
            throw new InternalError();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   332
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   333
        return dbb;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   334
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   335
90ce3da70b43 Initial load
duke
parents:
diff changeset
   336
90ce3da70b43 Initial load
duke
parents:
diff changeset
   337
    // -- Bug compatibility --
90ce3da70b43 Initial load
duke
parents:
diff changeset
   338
90ce3da70b43 Initial load
duke
parents:
diff changeset
   339
    private static volatile String bugLevel = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   340
90ce3da70b43 Initial load
duke
parents:
diff changeset
   341
    static boolean atBugLevel(String bl) {              // package-private
90ce3da70b43 Initial load
duke
parents:
diff changeset
   342
        if (bugLevel == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   343
            if (!sun.misc.VM.isBooted())
90ce3da70b43 Initial load
duke
parents:
diff changeset
   344
                return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   345
            String value = AccessController.doPrivileged(
90ce3da70b43 Initial load
duke
parents:
diff changeset
   346
                new GetPropertyAction("sun.nio.ch.bugLevel"));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   347
            bugLevel = (value != null) ? value : "";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   348
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   349
        return bugLevel.equals(bl);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   350
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   351
90ce3da70b43 Initial load
duke
parents:
diff changeset
   352
90ce3da70b43 Initial load
duke
parents:
diff changeset
   353
90ce3da70b43 Initial load
duke
parents:
diff changeset
   354
    // -- Initialization --
90ce3da70b43 Initial load
duke
parents:
diff changeset
   355
90ce3da70b43 Initial load
duke
parents:
diff changeset
   356
    private static boolean loaded = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   357
90ce3da70b43 Initial load
duke
parents:
diff changeset
   358
    static void load() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   359
        synchronized (Util.class) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   360
            if (loaded)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   361
                return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   362
            loaded = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   363
            java.security.AccessController
90ce3da70b43 Initial load
duke
parents:
diff changeset
   364
                .doPrivileged(new sun.security.action.LoadLibraryAction("net"));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   365
            java.security.AccessController
90ce3da70b43 Initial load
duke
parents:
diff changeset
   366
                .doPrivileged(new sun.security.action.LoadLibraryAction("nio"));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   367
            // IOUtil must be initialized; Its native methods are called from
90ce3da70b43 Initial load
duke
parents:
diff changeset
   368
            // other places in native nio code so they must be set up.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   369
            IOUtil.initIDs();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   370
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   371
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   372
90ce3da70b43 Initial load
duke
parents:
diff changeset
   373
}