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