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 * ShortArrayBuffer, ReadWriteShortArrayBuffer and ReadOnlyShortArrayBuffer
22 * compose the implementation of array based short buffers.
23 * <p>
24 * ReadWriteShortArrayBuffer extends ShortArrayBuffer with all the write
25 * methods.
26 * </p>
27 * <p>
28 * This class is marked final for runtime performance.
29 * </p>
30 *
31 */
32final class ReadWriteShortArrayBuffer extends ShortArrayBuffer {
33
34    static ReadWriteShortArrayBuffer copy(ShortArrayBuffer other, int markOfOther) {
35        ReadWriteShortArrayBuffer buf =
36                new ReadWriteShortArrayBuffer(other.capacity(), other.backingArray, other.offset);
37        buf.limit = other.limit;
38        buf.position = other.position();
39        buf.mark = markOfOther;
40        return buf;
41    }
42
43    ReadWriteShortArrayBuffer(short[] array) {
44        super(array);
45    }
46
47    ReadWriteShortArrayBuffer(int capacity) {
48        super(capacity);
49    }
50
51    ReadWriteShortArrayBuffer(int capacity, short[] backingArray,
52            int arrayOffset) {
53        super(capacity, backingArray, arrayOffset);
54    }
55
56    @Override
57    public ShortBuffer asReadOnlyBuffer() {
58        return ReadOnlyShortArrayBuffer.copy(this, mark);
59    }
60
61    @Override
62    public ShortBuffer compact() {
63        System.arraycopy(backingArray, position + offset, backingArray, offset, remaining());
64        position = limit - position;
65        limit = capacity;
66        mark = UNSET_MARK;
67        return this;
68    }
69
70    @Override
71    public ShortBuffer duplicate() {
72        return copy(this, mark);
73    }
74
75    @Override
76    public boolean isReadOnly() {
77        return false;
78    }
79
80    @Override short[] protectedArray() {
81        return backingArray;
82    }
83
84    @Override int protectedArrayOffset() {
85        return offset;
86    }
87
88    @Override boolean protectedHasArray() {
89        return true;
90    }
91
92    @Override
93    public ShortBuffer put(short c) {
94        if (position == limit) {
95            throw new BufferOverflowException();
96        }
97        backingArray[offset + position++] = c;
98        return this;
99    }
100
101    @Override
102    public ShortBuffer put(int index, short c) {
103        checkIndex(index);
104        backingArray[offset + index] = c;
105        return this;
106    }
107
108    @Override
109    public ShortBuffer put(short[] src, int srcOffset, int shortCount) {
110        if (shortCount > remaining()) {
111            throw new BufferOverflowException();
112        }
113        System.arraycopy(src, srcOffset, backingArray, offset + position, shortCount);
114        position += shortCount;
115        return this;
116    }
117
118    @Override
119    public ShortBuffer slice() {
120        return new ReadWriteShortArrayBuffer(remaining(), backingArray, offset + position);
121    }
122
123}
124