1/*
2 *  Licensed to the Apache Software Foundation (ASF) under one or more
3 *  contributor license agreements.  See the NOTICE file distributed with
4 *  this work for additional information regarding copyright ownership.
5 *  The ASF licenses this file to You under the Apache License, Version 2.0
6 *  (the "License"); you may not use this file except in compliance with
7 *  the License.  You may obtain a copy of the License at
8 *
9 *     http://www.apache.org/licenses/LICENSE-2.0
10 *
11 *  Unless required by applicable law or agreed to in writing, software
12 *  distributed under the License is distributed on an "AS IS" BASIS,
13 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 *  See the License for the specific language governing permissions and
15 *  limitations under the License.
16 */
17
18package java.nio;
19
20/**
21 * LongArrayBuffer, ReadWriteLongArrayBuffer and ReadOnlyLongArrayBuffer compose
22 * the implementation of array based long buffers.
23 * <p>
24 * ReadWriteLongArrayBuffer extends LongArrayBuffer with all the write methods.
25 * </p>
26 * <p>
27 * This class is marked final for runtime performance.
28 * </p>
29 *
30 */
31final class ReadWriteLongArrayBuffer extends LongArrayBuffer {
32
33    static ReadWriteLongArrayBuffer copy(LongArrayBuffer other, int markOfOther) {
34        ReadWriteLongArrayBuffer buf =
35                new ReadWriteLongArrayBuffer(other.capacity(), other.backingArray, other.offset);
36        buf.limit = other.limit;
37        buf.position = other.position();
38        buf.mark = markOfOther;
39        return buf;
40    }
41
42    ReadWriteLongArrayBuffer(long[] array) {
43        super(array);
44    }
45
46    ReadWriteLongArrayBuffer(int capacity) {
47        super(capacity);
48    }
49
50    ReadWriteLongArrayBuffer(int capacity, long[] backingArray, int arrayOffset) {
51        super(capacity, backingArray, arrayOffset);
52    }
53
54    @Override
55    public LongBuffer asReadOnlyBuffer() {
56        return ReadOnlyLongArrayBuffer.copy(this, mark);
57    }
58
59    @Override
60    public LongBuffer compact() {
61        System.arraycopy(backingArray, position + offset, backingArray, offset, remaining());
62        position = limit - position;
63        limit = capacity;
64        mark = UNSET_MARK;
65        return this;
66    }
67
68    @Override
69    public LongBuffer duplicate() {
70        return copy(this, mark);
71    }
72
73    @Override
74    public boolean isReadOnly() {
75        return false;
76    }
77
78    @Override long[] protectedArray() {
79        return backingArray;
80    }
81
82    @Override int protectedArrayOffset() {
83        return offset;
84    }
85
86    @Override boolean protectedHasArray() {
87        return true;
88    }
89
90    @Override
91    public LongBuffer put(long c) {
92        if (position == limit) {
93            throw new BufferOverflowException();
94        }
95        backingArray[offset + position++] = c;
96        return this;
97    }
98
99    @Override
100    public LongBuffer put(int index, long c) {
101        checkIndex(index);
102        backingArray[offset + index] = c;
103        return this;
104    }
105
106    @Override
107    public LongBuffer put(long[] src, int srcOffset, int longCount) {
108        if (longCount > remaining()) {
109            throw new BufferOverflowException();
110        }
111        System.arraycopy(src, srcOffset, backingArray, offset + position, longCount);
112        position += longCount;
113        return this;
114    }
115
116    @Override
117    public LongBuffer slice() {
118        return new ReadWriteLongArrayBuffer(remaining(), backingArray, offset + position);
119    }
120
121}
122