• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /* Licensed to the Apache Software Foundation (ASF) under one or more
2  * contributor license agreements.  See the NOTICE file distributed with
3  * this work for additional information regarding copyright ownership.
4  * The ASF licenses this file to You under the Apache License, Version 2.0
5  * (the "License"); you may not use this file except in compliance with
6  * the License.  You may obtain a copy of the License at
7  *
8  *     http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 package java.nio;
18 
19 import java.io.FileDescriptor;
20 import java.nio.channels.FileChannel;
21 
22 /**
23  * @hide internal use only
24  */
25 public final class NioUtils {
NioUtils()26     private NioUtils() {
27     }
28 
29     /**
30      * Gets the start address of a direct buffer.
31      * <p>
32      * This method corresponds to the JNI function:
33      *
34      * <pre>
35      *    void* GetDirectBufferAddress(JNIEnv* env, jobject buf);
36      * </pre>
37      *
38      * @param buf
39      *            the direct buffer whose address shall be returned must not be
40      *            <code>null</code>.
41      * @return the address of the buffer given, or zero if the buffer is not a
42      *         direct Buffer.
43      */
getDirectBufferAddress(Buffer buffer)44     public static int getDirectBufferAddress(Buffer buffer) {
45         return buffer.effectiveDirectAddress;
46     }
47 
freeDirectBuffer(ByteBuffer buffer)48     public static void freeDirectBuffer(ByteBuffer buffer) {
49         if (buffer == null) {
50             return;
51         }
52         if (buffer instanceof DirectByteBuffer) {
53             ((DirectByteBuffer) buffer).free();
54         } else if (buffer instanceof MappedByteBuffer) {
55             ((MappedByteBufferAdapter) buffer).free();
56         } else {
57             throw new AssertionError();
58         }
59     }
60 
61     /**
62      * Returns the int file descriptor from within the given FileChannel 'fc'.
63      */
getFD(FileChannel fc)64     public static FileDescriptor getFD(FileChannel fc) {
65         return ((FileChannelImpl) fc).getFD();
66     }
67 
68     /**
69      * Helps bridge between io and nio.
70      */
newFileChannel(Object stream, FileDescriptor fd, int mode)71     public static FileChannel newFileChannel(Object stream, FileDescriptor fd, int mode) {
72         return new FileChannelImpl(stream, fd, mode);
73     }
74 
75     /**
76      * Exposes the array backing a non-direct ByteBuffer, even if the ByteBuffer is read-only.
77      * Normally, attempting to access the array backing a read-only buffer throws.
78      */
unsafeArray(ByteBuffer b)79     public static byte[] unsafeArray(ByteBuffer b) {
80         return ((HeapByteBuffer) b).backingArray;
81     }
82 
83     /**
84      * Exposes the array offset for the array backing a non-direct ByteBuffer,
85      * even if the ByteBuffer is read-only.
86      */
unsafeArrayOffset(ByteBuffer b)87     public static int unsafeArrayOffset(ByteBuffer b) {
88         return ((HeapByteBuffer) b).offset;
89     }
90 }
91