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 implements short[]-based ShortBuffers.
22 */
23final class ShortArrayBuffer extends ShortBuffer {
24
25  private final short[] backingArray;
26
27  private final int arrayOffset;
28
29  private final boolean isReadOnly;
30
31  ShortArrayBuffer(short[] array) {
32    this(array.length, array, 0, false);
33  }
34
35  private ShortArrayBuffer(int capacity, short[] backingArray, int arrayOffset, boolean isReadOnly) {
36    super(capacity, 0);
37    this.backingArray = backingArray;
38    this.arrayOffset = arrayOffset;
39    this.isReadOnly = isReadOnly;
40  }
41
42  private static ShortArrayBuffer copy(ShortArrayBuffer other, int markOfOther, boolean isReadOnly) {
43    ShortArrayBuffer buf = new ShortArrayBuffer(other.capacity(), other.backingArray, other.arrayOffset, isReadOnly);
44    buf.limit = other.limit;
45    buf.position = other.position();
46    buf.mark = markOfOther;
47    return buf;
48  }
49
50  @Override public ShortBuffer asReadOnlyBuffer() {
51    return copy(this, mark, true);
52  }
53
54  @Override public ShortBuffer compact() {
55    if (isReadOnly) {
56      throw new ReadOnlyBufferException();
57    }
58    System.arraycopy(backingArray, position + arrayOffset, backingArray, arrayOffset, remaining());
59    position = limit - position;
60    limit = capacity;
61    mark = UNSET_MARK;
62    return this;
63  }
64
65  @Override public ShortBuffer duplicate() {
66    return copy(this, mark, isReadOnly);
67  }
68
69  @Override public ShortBuffer slice() {
70    return new ShortArrayBuffer(remaining(), backingArray, arrayOffset + position, isReadOnly);
71  }
72
73  @Override public boolean isReadOnly() {
74    return isReadOnly;
75  }
76
77  @Override short[] protectedArray() {
78    if (isReadOnly) {
79      throw new ReadOnlyBufferException();
80    }
81    return backingArray;
82  }
83
84  @Override int protectedArrayOffset() {
85    if (isReadOnly) {
86      throw new ReadOnlyBufferException();
87    }
88    return arrayOffset;
89  }
90
91  @Override boolean protectedHasArray() {
92    if (isReadOnly) {
93      return false;
94    }
95    return true;
96  }
97
98  @Override public final short get() {
99    if (position == limit) {
100      throw new BufferUnderflowException();
101    }
102    return backingArray[arrayOffset + position++];
103  }
104
105  @Override public final short get(int index) {
106    checkIndex(index);
107    return backingArray[arrayOffset + index];
108  }
109
110  @Override public final ShortBuffer get(short[] dst, int dstOffset, int shortCount) {
111    if (shortCount > remaining()) {
112      throw new BufferUnderflowException();
113    }
114    System.arraycopy(backingArray, arrayOffset + position, dst, dstOffset, shortCount);
115    position += shortCount;
116    return this;
117  }
118
119  @Override public final boolean isDirect() {
120    return false;
121  }
122
123  @Override public final ByteOrder order() {
124    return ByteOrder.nativeOrder();
125  }
126
127  @Override public ShortBuffer put(short c) {
128    if (isReadOnly) {
129      throw new ReadOnlyBufferException();
130    }
131    if (position == limit) {
132      throw new BufferOverflowException();
133    }
134    backingArray[arrayOffset + position++] = c;
135    return this;
136  }
137
138  @Override public ShortBuffer put(int index, short c) {
139    if (isReadOnly) {
140      throw new ReadOnlyBufferException();
141    }
142    checkIndex(index);
143    backingArray[arrayOffset + index] = c;
144    return this;
145  }
146
147  @Override public ShortBuffer put(short[] src, int srcOffset, int shortCount) {
148    if (isReadOnly) {
149      throw new ReadOnlyBufferException();
150    }
151    if (shortCount > remaining()) {
152      throw new BufferOverflowException();
153    }
154    System.arraycopy(src, srcOffset, backingArray, arrayOffset + position, shortCount);
155    position += shortCount;
156    return this;
157  }
158}
159