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: * ReadWriteDirectByteBuffer extends DirectByteBuffer with all the write
027: * methods.
028: * </p>
029: * <p>
030: * This class is marked final for runtime performance.
031: * </p>
032: *
033: */
034: final class ReadWriteDirectByteBuffer extends DirectByteBuffer {
035:
036: static ReadWriteDirectByteBuffer copy(DirectByteBuffer other,
037: int markOfOther) {
038: ReadWriteDirectByteBuffer buf = new ReadWriteDirectByteBuffer(
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: ReadWriteDirectByteBuffer(int capacity) {
048: super (capacity);
049: }
050:
051: ReadWriteDirectByteBuffer(SafeAddress address, int capacity,
052: int offset) {
053: super (address, capacity, offset);
054: }
055:
056: ReadWriteDirectByteBuffer(PlatformAddress address, int aCapacity,
057: int anOffset) {
058: super (new SafeAddress(address), aCapacity, anOffset);
059: }
060:
061: @Override
062: public ByteBuffer asReadOnlyBuffer() {
063: return ReadOnlyDirectByteBuffer.copy(this , mark);
064: }
065:
066: @Override
067: public ByteBuffer compact() {
068: PlatformAddress effectiveAddress = getEffectiveAddress();
069: effectiveAddress.offsetBytes(position).moveTo(effectiveAddress,
070: remaining());
071: position = limit - position;
072: limit = capacity;
073: mark = UNSET_MARK;
074: return this ;
075: }
076:
077: @Override
078: public ByteBuffer duplicate() {
079: return copy(this , mark);
080: }
081:
082: @Override
083: public boolean isReadOnly() {
084: return false;
085: }
086:
087: @Override
088: public ByteBuffer put(byte value) {
089: if (position == limit) {
090: throw new BufferOverflowException();
091: }
092: getBaseAddress().setByte(offset + position++, value);
093: return this ;
094: }
095:
096: @Override
097: public ByteBuffer put(int index, byte value) {
098: if (index < 0 || index >= limit) {
099: throw new IndexOutOfBoundsException();
100: }
101: getBaseAddress().setByte(offset + index, value);
102: return this ;
103: }
104:
105: /*
106: * Override ByteBuffer.put(byte[], int, int) to improve performance.
107: *
108: * (non-Javadoc)
109: *
110: * @see java.nio.ByteBuffer#put(byte[], int, int)
111: */
112: @Override
113: public ByteBuffer put(byte[] src, int off, int len) {
114: int length = src.length;
115: if (off < 0 || len < 0 || (long) off + (long) len > length) {
116: throw new IndexOutOfBoundsException();
117: }
118: if (len > remaining()) {
119: throw new BufferOverflowException();
120: }
121: if (isReadOnly()) {
122: throw new ReadOnlyBufferException();
123: }
124: getBaseAddress().setByteArray(offset + position, src, off, len);
125: position += len;
126: return this ;
127: }
128:
129: @Override
130: public ByteBuffer putDouble(double value) {
131: int newPosition = position + 8;
132: if (newPosition > limit) {
133: throw new BufferOverflowException();
134: }
135: getBaseAddress().setDouble(offset + position, value, order);
136: position = newPosition;
137: return this ;
138: }
139:
140: @Override
141: public ByteBuffer putDouble(int index, double value) {
142: if (index < 0 || (long) index + 8 > limit) {
143: throw new IndexOutOfBoundsException();
144: }
145: getBaseAddress().setDouble(offset + index, value, order);
146: return this ;
147: }
148:
149: @Override
150: public ByteBuffer putFloat(float value) {
151: int newPosition = position + 4;
152: if (newPosition > limit) {
153: throw new BufferOverflowException();
154: }
155: getBaseAddress().setFloat(offset + position, value, order);
156: position = newPosition;
157: return this ;
158: }
159:
160: @Override
161: public ByteBuffer putFloat(int index, float value) {
162: if (index < 0 || (long) index + 4 > limit) {
163: throw new IndexOutOfBoundsException();
164: }
165: getBaseAddress().setFloat(offset + index, value, order);
166: return this ;
167: }
168:
169: @Override
170: public ByteBuffer putInt(int value) {
171: int newPosition = position + 4;
172: if (newPosition > limit) {
173: throw new BufferOverflowException();
174: }
175: getBaseAddress().setInt(offset + position, value, order);
176: position = newPosition;
177: return this ;
178: }
179:
180: @Override
181: public ByteBuffer putInt(int index, int value) {
182: if (index < 0 || (long) index + 4 > limit) {
183: throw new IndexOutOfBoundsException();
184: }
185: getBaseAddress().setInt(offset + index, value, order);
186: return this ;
187: }
188:
189: @Override
190: public ByteBuffer putLong(long value) {
191: int newPosition = position + 8;
192: if (newPosition > limit) {
193: throw new BufferOverflowException();
194: }
195: getBaseAddress().setLong(offset + position, value, order);
196: position = newPosition;
197: return this ;
198: }
199:
200: @Override
201: public ByteBuffer putLong(int index, long value) {
202: if (index < 0 || (long) index + 8 > limit) {
203: throw new IndexOutOfBoundsException();
204: }
205: getBaseAddress().setLong(offset + index, value, order);
206: return this ;
207: }
208:
209: @Override
210: public ByteBuffer putShort(short value) {
211: int newPosition = position + 2;
212: if (newPosition > limit) {
213: throw new BufferOverflowException();
214: }
215: getBaseAddress().setShort(offset + position, value, order);
216: position = newPosition;
217: return this ;
218: }
219:
220: @Override
221: public ByteBuffer putShort(int index, short value) {
222: if (index < 0 || (long) index + 2 > limit) {
223: throw new IndexOutOfBoundsException();
224: }
225: getBaseAddress().setShort(offset + index, value, order);
226: return this ;
227: }
228:
229: @Override
230: public ByteBuffer slice() {
231: ReadWriteDirectByteBuffer buf = new ReadWriteDirectByteBuffer(
232: safeAddress, remaining(), offset + position);
233: buf.order = order;
234: return buf;
235: }
236:
237: }
|