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: /**
021: * DoubleArrayBuffer, ReadWriteDoubleArrayBuffer and ReadOnlyDoubleArrayBuffer
022: * compose the implementation of array based double buffers.
023: * <p>
024: * ReadWriteDoubleArrayBuffer extends DoubleArrayBuffer with all the write
025: * methods.
026: * </p>
027: * <p>
028: * This class is marked final for runtime performance.
029: * </p>
030: *
031: */
032: final class ReadWriteDoubleArrayBuffer extends DoubleArrayBuffer {
033:
034: static ReadWriteDoubleArrayBuffer copy(DoubleArrayBuffer other,
035: int markOfOther) {
036: ReadWriteDoubleArrayBuffer buf = new ReadWriteDoubleArrayBuffer(
037: other.capacity(), other.backingArray, other.offset);
038: buf.limit = other.limit();
039: buf.position = other.position();
040: buf.mark = markOfOther;
041: return buf;
042: }
043:
044: ReadWriteDoubleArrayBuffer(double[] array) {
045: super (array);
046: }
047:
048: ReadWriteDoubleArrayBuffer(int capacity) {
049: super (capacity);
050: }
051:
052: ReadWriteDoubleArrayBuffer(int capacity, double[] backingArray,
053: int arrayOffset) {
054: super (capacity, backingArray, arrayOffset);
055: }
056:
057: @Override
058: public DoubleBuffer asReadOnlyBuffer() {
059: return ReadOnlyDoubleArrayBuffer.copy(this , mark);
060: }
061:
062: @Override
063: public DoubleBuffer compact() {
064: System.arraycopy(backingArray, position + offset, backingArray,
065: offset, remaining());
066: position = limit - position;
067: limit = capacity;
068: mark = UNSET_MARK;
069: return this ;
070: }
071:
072: @Override
073: public DoubleBuffer duplicate() {
074: return copy(this , mark);
075: }
076:
077: @Override
078: public boolean isReadOnly() {
079: return false;
080: }
081:
082: @Override
083: protected double[] protectedArray() {
084: return backingArray;
085: }
086:
087: @Override
088: protected int protectedArrayOffset() {
089: return offset;
090: }
091:
092: @Override
093: protected boolean protectedHasArray() {
094: return true;
095: }
096:
097: @Override
098: public DoubleBuffer put(double c) {
099: if (position == limit) {
100: throw new BufferOverflowException();
101: }
102: backingArray[offset + position++] = c;
103: return this ;
104: }
105:
106: @Override
107: public DoubleBuffer put(int index, double c) {
108: if (index < 0 || index >= limit) {
109: throw new IndexOutOfBoundsException();
110: }
111: backingArray[offset + index] = c;
112: return this ;
113: }
114:
115: @Override
116: public DoubleBuffer put(double[] src, int off, int len) {
117: int length = src.length;
118: if (off < 0 || len < 0 || (long) off + (long) len > length) {
119: throw new IndexOutOfBoundsException();
120: }
121: if (len > remaining()) {
122: throw new BufferOverflowException();
123: }
124: System
125: .arraycopy(src, off, backingArray, offset + position,
126: len);
127: position += len;
128: return this ;
129: }
130:
131: @Override
132: public DoubleBuffer slice() {
133: return new ReadWriteDoubleArrayBuffer(remaining(),
134: backingArray, offset + position);
135: }
136:
137: }
|