001: /* Licensed to the Apache Software Foundation (ASF) under one or more
002: * contributor license agreements. See the NOTICE file distributed with
003: * this work for additional information regarding copyright ownership.
004: * The ASF licenses this file to You under the Apache License, Version 2.0
005: * (the "License"); you may not use this file except in compliance with
006: * the License. You may obtain a copy of the License at
007: *
008: * http://www.apache.org/licenses/LICENSE-2.0
009: *
010: * Unless required by applicable law or agreed to in writing, software
011: * distributed under the License is distributed on an "AS IS" BASIS,
012: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013: * See the License for the specific language governing permissions and
014: * limitations under the License.
015: */
016:
017: package java.nio;
018:
019: import org.apache.harmony.luni.platform.IMemorySystem;
020: import org.apache.harmony.luni.platform.MappedPlatformAddress;
021: import org.apache.harmony.luni.platform.PlatformAddress;
022: import org.apache.harmony.nio.internal.DirectBuffer;
023:
024: /**
025: * <code>MappedByteBuffer</code> is a special kind of direct byte buffer,
026: * which maps a region of file to memory.
027: * <p>
028: * <code>MappedByteBuffer</code> can be created by calling
029: * {@link java.nio.channels.FileChannel#map(java.nio.channels.FileChannel.MapMode, long, long) FileChannel.map}.
030: * Once created, the mapping between the byte buffer and the file region remains
031: * valid until the byte buffer is garbage collected.
032: * </p>
033: * <p>
034: * All or part of a <code>MappedByteBuffer</code>'s content may change or
035: * become inaccessible at any time, since the mapped file region can be modified
036: * by another thread or process at any time. If this happens, the behavior of
037: * the <code>MappedByteBuffer</code> is undefined.
038: * </p>
039: *
040: */
041: public abstract class MappedByteBuffer extends ByteBuffer {
042:
043: final DirectByteBuffer wrapped;
044:
045: private int mapMode;
046:
047: MappedByteBuffer(ByteBuffer directBuffer) {
048: super (directBuffer.capacity);
049: if (!directBuffer.isDirect()) {
050: throw new IllegalArgumentException();
051: }
052: this .wrapped = (DirectByteBuffer) directBuffer;
053:
054: }
055:
056: MappedByteBuffer(PlatformAddress addr, int capa, int offset,
057: int mode) {
058: super (capa);
059: mapMode = mode;
060: switch (mapMode) {
061: case IMemorySystem.MMAP_READ_ONLY:
062: wrapped = new ReadOnlyDirectByteBuffer(addr, capa, offset);
063: break;
064: case IMemorySystem.MMAP_READ_WRITE:
065: case IMemorySystem.MMAP_WRITE_COPY:
066: wrapped = new ReadWriteDirectByteBuffer(addr, capa, offset);
067: break;
068: default:
069: throw new IllegalArgumentException();
070: }
071: addr.autoFree();
072: }
073:
074: /**
075: * Returns true if this buffer's content is loaded.
076: *
077: * @return True if this buffer's content is loaded.
078: */
079: public final boolean isLoaded() {
080: return ((MappedPlatformAddress) ((DirectBuffer) wrapped)
081: .getBaseAddress()).mmapIsLoaded();
082: }
083:
084: /**
085: * Loads this buffer's content into memory.
086: *
087: * @return This buffer
088: */
089: public final MappedByteBuffer load() {
090: ((MappedPlatformAddress) ((DirectBuffer) wrapped)
091: .getBaseAddress()).mmapLoad();
092: return this ;
093: }
094:
095: /**
096: * Writes all changes of the buffer to the mapped file.
097: *
098: * All changes must be written by invoking this method if the mapped file
099: * exists on the local device, otherwise the action can not be specified.
100: *
101: * @return This buffer
102: */
103: public final MappedByteBuffer force() {
104: if (mapMode == IMemorySystem.MMAP_READ_WRITE) {
105: ((MappedPlatformAddress) ((DirectBuffer) wrapped)
106: .getBaseAddress()).mmapFlush();
107: }
108: return this;
109: }
110: }
|