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 17package java.nio; 18 19import java.io.Closeable; 20import java.io.FileDescriptor; 21import java.io.IOException; 22import java.nio.channels.ClosedChannelException; 23import java.nio.channels.FileChannel; 24import java.util.Set; 25 26/** 27 * @hide internal use only 28 */ 29public final class NioUtils { 30 private NioUtils() { 31 } 32 33 public static void freeDirectBuffer(ByteBuffer buffer) { 34 if (buffer == null) { 35 return; 36 } 37 ((DirectByteBuffer) buffer).free(); 38 } 39 40 /** 41 * Returns the int file descriptor from within the given FileChannel 'fc'. 42 */ 43 public static FileDescriptor getFD(FileChannel fc) { 44 return ((FileChannelImpl) fc).getFD(); 45 } 46 47 /** 48 * Helps bridge between io and nio. 49 */ 50 public static FileChannel newFileChannel(Closeable ioObject, FileDescriptor fd, int mode) { 51 return new FileChannelImpl(ioObject, fd, mode); 52 } 53 54 /** 55 * Exposes the array backing a non-direct ByteBuffer, even if the ByteBuffer is read-only. 56 * Normally, attempting to access the array backing a read-only buffer throws. 57 */ 58 public static byte[] unsafeArray(ByteBuffer b) { 59 return ((ByteArrayBuffer) b).backingArray; 60 } 61 62 /** 63 * Exposes the array offset for the array backing a non-direct ByteBuffer, 64 * even if the ByteBuffer is read-only. 65 */ 66 public static int unsafeArrayOffset(ByteBuffer b) { 67 return ((ByteArrayBuffer) b).arrayOffset; 68 } 69} 70