jdk/src/share/classes/sun/nio/ch/Util.java
author martin
Mon, 10 Mar 2008 15:07:09 -0700
changeset 51 6fe31bc95bbc
parent 2 90ce3da70b43
child 715 f16baef3a20e
child 787 637b139ce684
permissions -rw-r--r--
6600143: Remove another 450 unnecessary casts Reviewed-by: alanb, iris, lmalvent, bristor, peterjones, darcy, wetmore
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
90ce3da70b43 Initial load
duke
parents:
diff changeset
     2
 * Copyright 2000-2007 Sun Microsystems, Inc.  All Rights Reserved.
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
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
    static {
51
6fe31bc95bbc 6600143: Remove another 450 unnecessary casts
martin
parents: 2
diff changeset
    55
        bufferPool = (ThreadLocal<SoftReference<ByteBuffer>>[])
6fe31bc95bbc 6600143: Remove another 450 unnecessary casts
martin
parents: 2
diff changeset
    56
            new ThreadLocal[TEMP_BUF_POOL_SIZE];
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
        for (int i=0; i<TEMP_BUF_POOL_SIZE; i++)
51
6fe31bc95bbc 6600143: Remove another 450 unnecessary casts
martin
parents: 2
diff changeset
    58
            bufferPool[i] = new ThreadLocal<SoftReference<ByteBuffer>>();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
    static ByteBuffer getTemporaryDirectBuffer(int size) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
        ByteBuffer buf = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
        // Grab a buffer if available
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
        for (int i=0; i<TEMP_BUF_POOL_SIZE; i++) {
51
6fe31bc95bbc 6600143: Remove another 450 unnecessary casts
martin
parents: 2
diff changeset
    65
            SoftReference<ByteBuffer> ref = bufferPool[i].get();
6fe31bc95bbc 6600143: Remove another 450 unnecessary casts
martin
parents: 2
diff changeset
    66
            if ((ref != null) && ((buf = ref.get()) != null) &&
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
                (buf.capacity() >= size)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
                buf.rewind();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
                buf.limit(size);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
                bufferPool[i].set(null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
                return buf;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
        // Make a new one
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
        return ByteBuffer.allocateDirect(size);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
    static void releaseTemporaryDirectBuffer(ByteBuffer buf) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
        if (buf == null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
            return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
        // Put it in an empty slot if such exists
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
        for (int i=0; i<TEMP_BUF_POOL_SIZE; i++) {
51
6fe31bc95bbc 6600143: Remove another 450 unnecessary casts
martin
parents: 2
diff changeset
    84
            SoftReference<ByteBuffer> ref = bufferPool[i].get();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
            if ((ref == null) || (ref.get() == null)) {
51
6fe31bc95bbc 6600143: Remove another 450 unnecessary casts
martin
parents: 2
diff changeset
    86
                bufferPool[i].set(new SoftReference<ByteBuffer>(buf));
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
                return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
        // Otherwise replace a smaller one in the cache if such exists
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
        for (int i=0; i<TEMP_BUF_POOL_SIZE; i++) {
51
6fe31bc95bbc 6600143: Remove another 450 unnecessary casts
martin
parents: 2
diff changeset
    92
            SoftReference<ByteBuffer> ref = bufferPool[i].get();
6fe31bc95bbc 6600143: Remove another 450 unnecessary casts
martin
parents: 2
diff changeset
    93
            ByteBuffer inCacheBuf = ref.get();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
            if ((inCacheBuf == null) || (buf.capacity() > inCacheBuf.capacity())) {
51
6fe31bc95bbc 6600143: Remove another 450 unnecessary casts
martin
parents: 2
diff changeset
    95
                bufferPool[i].set(new SoftReference<ByteBuffer>(buf));
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
                return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
    private static class SelectorWrapper {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
        private Selector sel;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
        private SelectorWrapper (Selector sel) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
            this.sel = sel;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
            Cleaner.create(this, new Closer(sel));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
        private static class Closer implements Runnable {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
            private Selector sel;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
            private Closer (Selector sel) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
                this.sel = sel;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
            public void run () {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
                try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
                    sel.close();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
                } catch (Throwable th) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
                    throw new Error(th);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
        public Selector get() { return sel;}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
    // Per-thread cached selector
51
6fe31bc95bbc 6600143: Remove another 450 unnecessary casts
martin
parents: 2
diff changeset
   124
    private static ThreadLocal<SoftReference<SelectorWrapper>> localSelector
6fe31bc95bbc 6600143: Remove another 450 unnecessary casts
martin
parents: 2
diff changeset
   125
        = new ThreadLocal<SoftReference<SelectorWrapper>>();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
    // Hold a reference to the selWrapper object to prevent it from
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
    // being cleaned when the temporary selector wrapped is on lease.
51
6fe31bc95bbc 6600143: Remove another 450 unnecessary casts
martin
parents: 2
diff changeset
   128
    private static ThreadLocal<SelectorWrapper> localSelectorWrapper
6fe31bc95bbc 6600143: Remove another 450 unnecessary casts
martin
parents: 2
diff changeset
   129
        = new ThreadLocal<SelectorWrapper>();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
    // When finished, invoker must ensure that selector is empty
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
    // by cancelling any related keys and explicitly releasing
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
    // the selector by invoking releaseTemporarySelector()
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
    static Selector getTemporarySelector(SelectableChannel sc)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
        throws IOException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
    {
51
6fe31bc95bbc 6600143: Remove another 450 unnecessary casts
martin
parents: 2
diff changeset
   137
        SoftReference<SelectorWrapper> ref = localSelector.get();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
        SelectorWrapper selWrapper = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
        Selector sel = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
        if (ref == null
51
6fe31bc95bbc 6600143: Remove another 450 unnecessary casts
martin
parents: 2
diff changeset
   141
            || ((selWrapper = ref.get()) == null)
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
            || ((sel = selWrapper.get()) == null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
            || (sel.provider() != sc.provider())) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
            sel = sc.provider().openSelector();
51
6fe31bc95bbc 6600143: Remove another 450 unnecessary casts
martin
parents: 2
diff changeset
   145
            localSelector.set(new SoftReference<SelectorWrapper>(
6fe31bc95bbc 6600143: Remove another 450 unnecessary casts
martin
parents: 2
diff changeset
   146
                                  new SelectorWrapper(sel)));
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
            localSelectorWrapper.set(selWrapper);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
        return sel;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
    static void releaseTemporarySelector(Selector sel)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
        throws IOException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
        // Selector should be empty
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
        sel.selectNow();                // Flush cancelled keys
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
        assert sel.keys().isEmpty() : "Temporary selector not empty";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
        localSelectorWrapper.set(null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
    // -- Random stuff --
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
    static ByteBuffer[] subsequence(ByteBuffer[] bs, int offset, int length) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
        if ((offset == 0) && (length == bs.length))
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
            return bs;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
        int n = length;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
        ByteBuffer[] bs2 = new ByteBuffer[n];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
        for (int i = 0; i < n; i++)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
            bs2[i] = bs[offset + i];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   172
        return bs2;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   173
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   174
90ce3da70b43 Initial load
duke
parents:
diff changeset
   175
    static <E> Set<E> ungrowableSet(final Set<E> s) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   176
        return new Set<E>() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   177
90ce3da70b43 Initial load
duke
parents:
diff changeset
   178
                public int size()                 { return s.size(); }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   179
                public boolean isEmpty()          { return s.isEmpty(); }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   180
                public boolean contains(Object o) { return s.contains(o); }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   181
                public Object[] toArray()         { return s.toArray(); }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   182
                public <T> T[] toArray(T[] a)     { return s.toArray(a); }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   183
                public String toString()          { return s.toString(); }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   184
                public Iterator<E> iterator()     { return s.iterator(); }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   185
                public boolean equals(Object o)   { return s.equals(o); }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   186
                public int hashCode()             { return s.hashCode(); }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   187
                public void clear()               { s.clear(); }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   188
                public boolean remove(Object o)   { return s.remove(o); }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   189
90ce3da70b43 Initial load
duke
parents:
diff changeset
   190
                public boolean containsAll(Collection<?> coll) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   191
                    return s.containsAll(coll);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   192
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   193
                public boolean removeAll(Collection<?> coll) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   194
                    return s.removeAll(coll);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   195
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   196
                public boolean retainAll(Collection<?> coll) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   197
                    return s.retainAll(coll);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   198
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   199
90ce3da70b43 Initial load
duke
parents:
diff changeset
   200
                public boolean add(E o){
90ce3da70b43 Initial load
duke
parents:
diff changeset
   201
                    throw new UnsupportedOperationException();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   202
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   203
                public boolean addAll(Collection<? extends E> coll) {
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
90ce3da70b43 Initial load
duke
parents:
diff changeset
   207
        };
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
    // -- Unsafe access --
90ce3da70b43 Initial load
duke
parents:
diff changeset
   212
90ce3da70b43 Initial load
duke
parents:
diff changeset
   213
    private static Unsafe unsafe = Unsafe.getUnsafe();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   214
90ce3da70b43 Initial load
duke
parents:
diff changeset
   215
    private static byte _get(long a) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   216
        return unsafe.getByte(a);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   217
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   218
90ce3da70b43 Initial load
duke
parents:
diff changeset
   219
    private static void _put(long a, byte b) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   220
        unsafe.putByte(a, b);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   221
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   222
90ce3da70b43 Initial load
duke
parents:
diff changeset
   223
    static void erase(ByteBuffer bb) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   224
        unsafe.setMemory(((DirectBuffer)bb).address(), bb.capacity(), (byte)0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   225
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   226
90ce3da70b43 Initial load
duke
parents:
diff changeset
   227
    static Unsafe unsafe() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   228
        return unsafe;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   229
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   230
90ce3da70b43 Initial load
duke
parents:
diff changeset
   231
    private static int pageSize = -1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   232
90ce3da70b43 Initial load
duke
parents:
diff changeset
   233
    static int pageSize() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   234
        if (pageSize == -1)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   235
            pageSize = unsafe().pageSize();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   236
        return pageSize;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   237
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   238
90ce3da70b43 Initial load
duke
parents:
diff changeset
   239
    private static volatile Constructor directByteBufferConstructor = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   240
90ce3da70b43 Initial load
duke
parents:
diff changeset
   241
    private static void initDBBConstructor() {
51
6fe31bc95bbc 6600143: Remove another 450 unnecessary casts
martin
parents: 2
diff changeset
   242
        AccessController.doPrivileged(new PrivilegedAction<Void>() {
6fe31bc95bbc 6600143: Remove another 450 unnecessary casts
martin
parents: 2
diff changeset
   243
                public Void run() {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   244
                    try {
51
6fe31bc95bbc 6600143: Remove another 450 unnecessary casts
martin
parents: 2
diff changeset
   245
                        Class<?> cl = Class.forName("java.nio.DirectByteBuffer");
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   246
                        Constructor ctor = cl.getDeclaredConstructor(
90ce3da70b43 Initial load
duke
parents:
diff changeset
   247
                            new Class[] { int.class,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   248
                                          long.class,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   249
                                          Runnable.class });
90ce3da70b43 Initial load
duke
parents:
diff changeset
   250
                        ctor.setAccessible(true);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   251
                        directByteBufferConstructor = ctor;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   252
                    } catch (ClassNotFoundException x) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   253
                        throw new InternalError();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   254
                    } catch (NoSuchMethodException x) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   255
                        throw new InternalError();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   256
                    } catch (IllegalArgumentException x) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   257
                        throw new InternalError();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   258
                    } catch (ClassCastException x) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   259
                        throw new InternalError();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   260
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   261
                    return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   262
                }});
90ce3da70b43 Initial load
duke
parents:
diff changeset
   263
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   264
90ce3da70b43 Initial load
duke
parents:
diff changeset
   265
    static MappedByteBuffer newMappedByteBuffer(int size, long addr,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   266
                                                Runnable unmapper)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   267
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   268
        MappedByteBuffer dbb;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   269
        if (directByteBufferConstructor == null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   270
            initDBBConstructor();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   271
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   272
            dbb = (MappedByteBuffer)directByteBufferConstructor.newInstance(
90ce3da70b43 Initial load
duke
parents:
diff changeset
   273
              new Object[] { new Integer(size),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   274
                             new Long(addr),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   275
                             unmapper });
90ce3da70b43 Initial load
duke
parents:
diff changeset
   276
        } catch (InstantiationException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   277
            throw new InternalError();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   278
        } catch (IllegalAccessException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   279
            throw new InternalError();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   280
        } catch (InvocationTargetException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   281
            throw new InternalError();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   282
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   283
        return dbb;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   284
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   285
90ce3da70b43 Initial load
duke
parents:
diff changeset
   286
    private static volatile Constructor directByteBufferRConstructor = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   287
90ce3da70b43 Initial load
duke
parents:
diff changeset
   288
    private static void initDBBRConstructor() {
51
6fe31bc95bbc 6600143: Remove another 450 unnecessary casts
martin
parents: 2
diff changeset
   289
        AccessController.doPrivileged(new PrivilegedAction<Void>() {
6fe31bc95bbc 6600143: Remove another 450 unnecessary casts
martin
parents: 2
diff changeset
   290
                public Void run() {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   291
                    try {
51
6fe31bc95bbc 6600143: Remove another 450 unnecessary casts
martin
parents: 2
diff changeset
   292
                        Class<?> cl = Class.forName("java.nio.DirectByteBufferR");
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   293
                        Constructor ctor = cl.getDeclaredConstructor(
90ce3da70b43 Initial load
duke
parents:
diff changeset
   294
                            new Class[] { int.class,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   295
                                          long.class,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   296
                                          Runnable.class });
90ce3da70b43 Initial load
duke
parents:
diff changeset
   297
                        ctor.setAccessible(true);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   298
                        directByteBufferRConstructor = ctor;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   299
                    } catch (ClassNotFoundException x) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   300
                        throw new InternalError();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   301
                    } catch (NoSuchMethodException x) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   302
                        throw new InternalError();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   303
                    } catch (IllegalArgumentException x) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   304
                        throw new InternalError();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   305
                    } catch (ClassCastException x) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   306
                        throw new InternalError();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   307
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   308
                    return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   309
                }});
90ce3da70b43 Initial load
duke
parents:
diff changeset
   310
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   311
90ce3da70b43 Initial load
duke
parents:
diff changeset
   312
    static MappedByteBuffer newMappedByteBufferR(int size, long addr,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   313
                                                 Runnable unmapper)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   314
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   315
        MappedByteBuffer dbb;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   316
        if (directByteBufferRConstructor == null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   317
            initDBBRConstructor();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   318
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   319
            dbb = (MappedByteBuffer)directByteBufferRConstructor.newInstance(
90ce3da70b43 Initial load
duke
parents:
diff changeset
   320
              new Object[] { new Integer(size),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   321
                             new Long(addr),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   322
                             unmapper });
90ce3da70b43 Initial load
duke
parents:
diff changeset
   323
        } catch (InstantiationException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   324
            throw new InternalError();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   325
        } catch (IllegalAccessException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   326
            throw new InternalError();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   327
        } catch (InvocationTargetException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   328
            throw new InternalError();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   329
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   330
        return dbb;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   331
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   332
90ce3da70b43 Initial load
duke
parents:
diff changeset
   333
90ce3da70b43 Initial load
duke
parents:
diff changeset
   334
    // -- Bug compatibility --
90ce3da70b43 Initial load
duke
parents:
diff changeset
   335
90ce3da70b43 Initial load
duke
parents:
diff changeset
   336
    private static volatile String bugLevel = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   337
90ce3da70b43 Initial load
duke
parents:
diff changeset
   338
    static boolean atBugLevel(String bl) {              // package-private
90ce3da70b43 Initial load
duke
parents:
diff changeset
   339
        if (bugLevel == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   340
            if (!sun.misc.VM.isBooted())
90ce3da70b43 Initial load
duke
parents:
diff changeset
   341
                return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   342
            String value = AccessController.doPrivileged(
90ce3da70b43 Initial load
duke
parents:
diff changeset
   343
                new GetPropertyAction("sun.nio.ch.bugLevel"));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   344
            bugLevel = (value != null) ? value : "";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   345
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   346
        return bugLevel.equals(bl);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   347
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   348
90ce3da70b43 Initial load
duke
parents:
diff changeset
   349
90ce3da70b43 Initial load
duke
parents:
diff changeset
   350
90ce3da70b43 Initial load
duke
parents:
diff changeset
   351
    // -- Initialization --
90ce3da70b43 Initial load
duke
parents:
diff changeset
   352
90ce3da70b43 Initial load
duke
parents:
diff changeset
   353
    private static boolean loaded = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   354
90ce3da70b43 Initial load
duke
parents:
diff changeset
   355
    static void load() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   356
        synchronized (Util.class) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   357
            if (loaded)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   358
                return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   359
            loaded = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   360
            java.security.AccessController
90ce3da70b43 Initial load
duke
parents:
diff changeset
   361
                .doPrivileged(new sun.security.action.LoadLibraryAction("net"));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   362
            java.security.AccessController
90ce3da70b43 Initial load
duke
parents:
diff changeset
   363
                .doPrivileged(new sun.security.action.LoadLibraryAction("nio"));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   364
            // IOUtil must be initialized; Its native methods are called from
90ce3da70b43 Initial load
duke
parents:
diff changeset
   365
            // other places in native nio code so they must be set up.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   366
            IOUtil.initIDs();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   367
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   368
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   369
90ce3da70b43 Initial load
duke
parents:
diff changeset
   370
}