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 java.nio;
18:
19: import org.apache.harmony.luni.platform.PlatformAddress;
20:
21: /**
22: * Helper class for operations on direct ByteBuffer
23: *
24: * @see java.nio.ByteBuffer
25: */
26: class DirectByteBuffers {
27:
28: /**
29: * Explicitly frees the memory used by the given direct byte buffer.
30: * <p>
31: * If the memory is known to already have been freed then this is a no-op.
32: * Once the memory has been freed then operations requiring access to the
33: * memory will throw an <code>IllegalStateException</code>.
34: * </p>
35: * <p>
36: * Note this is is possible that the memory is freed by code that reaches
37: * into the address and explicitly frees it 'beneith' us -- this is bad
38: * form.
39: * </p>
40: *
41: * @param directBuffer
42: * the direct byte buffer memory to free
43: * @throws IllegalArgumentException
44: * if the buffer is <code>null</code> or is not a
45: * <em>direct</em> byte buffer.
46: */
47: public static void free(ByteBuffer directBuffer) {
48: if ((directBuffer == null) || (!directBuffer.isDirect())) {
49: throw new IllegalArgumentException();
50: }
51: DirectByteBuffer buf = (DirectByteBuffer) directBuffer;
52: buf.free();
53: }
54:
55: /**
56: * Answers the platform address of the start of this buffer instance.
57: * <em>You must not attempt to free the returned address!!</em> It may not
58: * be an address that was explicitly malloc'ed (i.e. if this buffer is the
59: * result of a split); and it may be memory shared by multiple buffers.
60: * <p>
61: * If you can guarantee that you want to free the underlying memory call the
62: * #free() method on this instance -- generally applications will rely on
63: * the garbage collector to autofree this memory.
64: * </p>
65: *
66: * @param directBuffer
67: * the direct byte buffer
68: * @return the effective address of the start of the buffer.
69: * @throws IllegalStateException
70: * if this buffer address is known to have been freed
71: * previously.
72: */
73: public static PlatformAddress getEffectiveAddress(
74: ByteBuffer directBuffer) {
75: return toDirectBuffer(directBuffer).getEffectiveAddress();
76: }
77:
78: private static DirectByteBuffer toDirectBuffer(
79: ByteBuffer directBuffer) {
80: if ((directBuffer == null) || (!directBuffer.isDirect())) {
81: throw new IllegalArgumentException();
82: }
83: return (DirectByteBuffer) directBuffer;
84: }
85:
86: }
|