Home | History | Annotate | Download | only in nio
      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 {
     26     private NioUtils() {
     27     }
     28 
     29     public static void freeDirectBuffer(ByteBuffer buffer) {
     30         if (buffer == null) {
     31             return;
     32         }
     33         ((DirectByteBuffer) buffer).free();
     34     }
     35 
     36     /**
     37      * Returns the int file descriptor from within the given FileChannel 'fc'.
     38      */
     39     public static FileDescriptor getFD(FileChannel fc) {
     40         return ((FileChannelImpl) fc).getFD();
     41     }
     42 
     43     /**
     44      * Helps bridge between io and nio.
     45      */
     46     public static FileChannel newFileChannel(Object stream, FileDescriptor fd, int mode) {
     47         return new FileChannelImpl(stream, fd, mode);
     48     }
     49 
     50     /**
     51      * Exposes the array backing a non-direct ByteBuffer, even if the ByteBuffer is read-only.
     52      * Normally, attempting to access the array backing a read-only buffer throws.
     53      */
     54     public static byte[] unsafeArray(ByteBuffer b) {
     55         return ((ByteArrayBuffer) b).backingArray;
     56     }
     57 
     58     /**
     59      * Exposes the array offset for the array backing a non-direct ByteBuffer,
     60      * even if the ByteBuffer is read-only.
     61      */
     62     public static int unsafeArrayOffset(ByteBuffer b) {
     63         return ((ByteArrayBuffer) b).arrayOffset;
     64     }
     65 }
     66