01: /* Licensed to the Apache Software Foundation (ASF) under one or more
02: * contributor license agreements. See the NOTICE file distributed with
03: * this work for additional information regarding copyright ownership.
04: * The ASF licenses this file to You under the Apache License, Version 2.0
05: * (the "License"); you may not use this file except in compliance with
06: * the License. You may obtain a copy of the License at
07: *
08: * http://www.apache.org/licenses/LICENSE-2.0
09: *
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 org.apache.harmony.nio;
18:
19: import java.io.FileDescriptor;
20: import java.nio.Buffer;
21: import java.nio.channels.Channel;
22: import java.nio.channels.DatagramChannel;
23: import java.nio.channels.FileChannel;
24: import java.nio.channels.ServerSocketChannel;
25: import java.nio.channels.SocketChannel;
26:
27: import org.apache.harmony.luni.platform.FileDescriptorHandler;
28: import org.apache.harmony.nio.internal.DirectBuffer;
29: import org.apache.harmony.nio.internal.FileChannelImpl;
30:
31: public class AddressUtil {
32:
33: /**
34: * Gets the start address of a direct buffer.
35: * <p>
36: * This method corresponds to the JNI function:
37: *
38: * <pre>
39: * void* GetDirectBufferAddress(JNIEnv* env, jobject buf);
40: * </pre>
41: *
42: * @param buf
43: * the direct buffer whose address shall be returned must not be
44: * <code>null</code>.
45: * @return the address of the buffer given, or zero if the buffer is not a
46: * direct Buffer.
47: */
48: public static long getDirectBufferAddress(Buffer buf) {
49: if (!(buf instanceof DirectBuffer)) {
50: return 0;
51: }
52: return ((DirectBuffer) buf).getEffectiveAddress().toLong();
53: }
54:
55: /**
56: * Gets the address of native resource held by the given channel, if it has
57: * any.
58: *
59: * For network related channel, including {@link SocketChannel},
60: * {@link ServerSocketChannel} and {@link DatagramChannel}, this method
61: * returns the Socket handle (long) in Linux, and returns a SOCKET
62: * (UINT_PTR) in windows.
63: *
64: * For {@link FileChannel}, this method returns the native file descriptor.
65: *
66: * For other channels, this method return 0, which means unsupported
67: * operation.
68: *
69: * @param channel
70: * the given channel which may holds a native resource address
71: * @return the address of native resource held by the given channel, if any,
72: * otherwise return 0
73: */
74: public static long getChannelAddress(Channel channel) {
75: if (channel instanceof FileDescriptorHandler) {
76: return getFDAddress(((FileDescriptorHandler) channel)
77: .getFD());
78: } else if (channel instanceof FileChannelImpl) {
79: return ((FileChannelImpl) channel).getHandle();
80: }
81: return 0;
82: }
83:
84: private static native long getFDAddress(FileDescriptor fd);
85: }
|