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
18 package 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 */
32 final class ReadWriteShortArrayBuffer extends ShortArrayBuffer {
33
34 static ReadWriteShortArrayBuffer copy(ShortArrayBuffer other,
35 int markOfOther) {
36 ReadWriteShortArrayBuffer buf = new ReadWriteShortArrayBuffer(other
37 .capacity(), other.backingArray, other.offset);
38 buf.limit = other.limit();
39 buf.position = other.position();
40 buf.mark = markOfOther;
41 return buf;
42 }
43
44 ReadWriteShortArrayBuffer(short[] array) {
45 super(array);
46 }
47
48 ReadWriteShortArrayBuffer(int capacity) {
49 super(capacity);
50 }
51
52 ReadWriteShortArrayBuffer(int capacity, short[] backingArray,
53 int arrayOffset) {
54 super(capacity, backingArray, arrayOffset);
55 }
56
57 @Override
58 public ShortBuffer asReadOnlyBuffer() {
59 return ReadOnlyShortArrayBuffer.copy(this, mark);
60 }
61
62 @Override
63 public ShortBuffer compact() {
64 System.arraycopy(backingArray, position + offset, backingArray, offset,
65 remaining());
66 position = limit - position;
67 limit = capacity;
68 mark = UNSET_MARK;
69 return this;
70 }
71
72 @Override
73 public ShortBuffer duplicate() {
74 return copy(this, mark);
75 }
76
77 @Override
78 public boolean isReadOnly() {
79 return false;
80 }
81
82 @Override
83 protected short[] protectedArray() {
84 return backingArray;
85 }
86
87 @Override
88 protected int protectedArrayOffset() {
89 return offset;
90 }
91
92 @Override
93 protected boolean protectedHasArray() {
94 return true;
95 }
96
97 @Override
98 public ShortBuffer put(short c) {
99 if (position == limit) {
100 throw new BufferOverflowException();
101 }
102 backingArray[offset + position++] = c;
103 return this;
104 }
105
106 @Override
107 public ShortBuffer put(int index, short 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 ShortBuffer put(short[] 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.arraycopy(src, off, backingArray, offset + position, len);
125 position += len;
126 return this;
127 }
128
129 @Override
130 public ShortBuffer slice() {
131 return new ReadWriteShortArrayBuffer(remaining(), backingArray, offset
132 + position);
133 }
134
135 }