001: /*
002: * Licensed to the Apache Software Foundation (ASF) under one or more
003: * contributor license agreements. See the NOTICE file distributed with
004: * this work for additional information regarding copyright ownership.
005: * The ASF licenses this file to You under the Apache License, Version 2.0
006: * (the "License"); you may not use this file except in compliance with
007: * the License. You may obtain a copy of the License at
008: *
009: * http://www.apache.org/licenses/LICENSE-2.0
010: *
011: * Unless required by applicable law or agreed to in writing, software
012: * distributed under the License is distributed on an "AS IS" BASIS,
013: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: * See the License for the specific language governing permissions and
015: * limitations under the License.
016: */
017:
018: package java.nio;
019:
020: import org.apache.harmony.luni.platform.PlatformAddress;
021:
022: /**
023: * DirectByteBuffer, ReadWriteDirectByteBuffer and ReadOnlyDirectByteBuffer
024: * compose the implementation of platform memory based byte buffers.
025: * <p>
026: * ReadOnlyDirectByteBuffer extends DirectByteBuffer with all the write methods
027: * throwing read only exception.
028: * </p>
029: * <p>
030: * This class is marked final for runtime performance.
031: * </p>
032: *
033: */
034: final class ReadOnlyDirectByteBuffer extends DirectByteBuffer {
035:
036: static ReadOnlyDirectByteBuffer copy(DirectByteBuffer other,
037: int markOfOther) {
038: ReadOnlyDirectByteBuffer buf = new ReadOnlyDirectByteBuffer(
039: other.safeAddress, other.capacity(), other.offset);
040: buf.limit = other.limit();
041: buf.position = other.position();
042: buf.mark = markOfOther;
043: buf.order(other.order());
044: return buf;
045: }
046:
047: protected ReadOnlyDirectByteBuffer(SafeAddress address,
048: int capacity, int offset) {
049: super (address, capacity, offset);
050: }
051:
052: protected ReadOnlyDirectByteBuffer(PlatformAddress address,
053: int capacity, int offset) {
054: super (new SafeAddress(address), capacity, offset);
055: }
056:
057: @Override
058: public ByteBuffer asReadOnlyBuffer() {
059: return copy(this , mark);
060: }
061:
062: @Override
063: public ByteBuffer compact() {
064: throw new ReadOnlyBufferException();
065: }
066:
067: @Override
068: public ByteBuffer duplicate() {
069: return copy(this , mark);
070: }
071:
072: @Override
073: public boolean isReadOnly() {
074: return true;
075: }
076:
077: @Override
078: public ByteBuffer put(byte value) {
079: throw new ReadOnlyBufferException();
080: }
081:
082: @Override
083: public ByteBuffer put(int index, byte value) {
084: throw new ReadOnlyBufferException();
085: }
086:
087: @Override
088: public ByteBuffer put(byte[] src, int off, int len) {
089: throw new ReadOnlyBufferException();
090: }
091:
092: @Override
093: public ByteBuffer putDouble(double value) {
094: throw new ReadOnlyBufferException();
095: }
096:
097: @Override
098: public ByteBuffer putDouble(int index, double value) {
099: throw new ReadOnlyBufferException();
100: }
101:
102: @Override
103: public ByteBuffer putFloat(float value) {
104: throw new ReadOnlyBufferException();
105: }
106:
107: @Override
108: public ByteBuffer putFloat(int index, float value) {
109: throw new ReadOnlyBufferException();
110: }
111:
112: @Override
113: public ByteBuffer putInt(int value) {
114: throw new ReadOnlyBufferException();
115: }
116:
117: @Override
118: public ByteBuffer putInt(int index, int value) {
119: throw new ReadOnlyBufferException();
120: }
121:
122: @Override
123: public ByteBuffer putLong(int index, long value) {
124: throw new ReadOnlyBufferException();
125: }
126:
127: @Override
128: public ByteBuffer putLong(long value) {
129: throw new ReadOnlyBufferException();
130: }
131:
132: @Override
133: public ByteBuffer putShort(int index, short value) {
134: throw new ReadOnlyBufferException();
135: }
136:
137: @Override
138: public ByteBuffer putShort(short value) {
139: throw new ReadOnlyBufferException();
140: }
141:
142: @Override
143: public ByteBuffer put(ByteBuffer buf) {
144: throw new ReadOnlyBufferException();
145: }
146:
147: @Override
148: public ByteBuffer slice() {
149: ReadOnlyDirectByteBuffer buf = new ReadOnlyDirectByteBuffer(
150: safeAddress, remaining(), offset + position);
151: buf.order = order;
152: return buf;
153: }
154:
155: }
|