Rahul Ravikumar | 0533600 | 2019-10-14 15:04:32 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2014 The Android Open Source Project |
| 3 | * Copyright (c) 2000, 2008, Oracle and/or its affiliates. All rights reserved. |
| 4 | * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. |
| 5 | * |
| 6 | * This code is free software; you can redistribute it and/or modify it |
| 7 | * under the terms of the GNU General Public License version 2 only, as |
| 8 | * published by the Free Software Foundation. Oracle designates this |
| 9 | * particular file as subject to the "Classpath" exception as provided |
| 10 | * by Oracle in the LICENSE file that accompanied this code. |
| 11 | * |
| 12 | * This code is distributed in the hope that it will be useful, but WITHOUT |
| 13 | * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
| 14 | * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
| 15 | * version 2 for more details (a copy is included in the LICENSE file that |
| 16 | * accompanied this code). |
| 17 | * |
| 18 | * You should have received a copy of the GNU General Public License version |
| 19 | * 2 along with this work; if not, write to the Free Software Foundation, |
| 20 | * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. |
| 21 | * |
| 22 | * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA |
| 23 | * or visit www.oracle.com if you need additional information or have any |
| 24 | * questions. |
| 25 | */ |
| 26 | |
| 27 | package java.nio; |
| 28 | |
| 29 | /** |
| 30 | * A read/write HeapIntBuffer. |
| 31 | */ |
| 32 | |
| 33 | class HeapIntBuffer extends IntBuffer { |
| 34 | |
| 35 | // For speed these fields are actually declared in X-Buffer; |
| 36 | // these declarations are here as documentation |
| 37 | /* |
| 38 | |
| 39 | protected final int[] hb; |
| 40 | protected final int offset; |
| 41 | |
| 42 | */ |
| 43 | |
| 44 | HeapIntBuffer(int cap, int lim) { // package-private |
| 45 | this(cap, lim, false); |
| 46 | } |
| 47 | |
| 48 | HeapIntBuffer(int cap, int lim, boolean isReadOnly) { // package-private |
| 49 | super(-1, 0, lim, cap, new int[cap], 0); |
| 50 | this.isReadOnly = isReadOnly; |
| 51 | } |
| 52 | |
| 53 | HeapIntBuffer(int[] buf, int off, int len) { // package-private |
| 54 | this(buf, off, len, false); |
| 55 | } |
| 56 | |
| 57 | HeapIntBuffer(int[] buf, int off, int len, boolean isReadOnly) { // package-private |
| 58 | super(-1, off, off + len, buf.length, buf, 0); |
| 59 | this.isReadOnly = isReadOnly; |
| 60 | } |
| 61 | |
| 62 | protected HeapIntBuffer(int[] buf, |
| 63 | int mark, int pos, int lim, int cap, |
| 64 | int off) { |
| 65 | this(buf, mark, pos, lim, cap, off, false); |
| 66 | } |
| 67 | |
| 68 | protected HeapIntBuffer(int[] buf, |
| 69 | int mark, int pos, int lim, int cap, |
| 70 | int off, boolean isReadOnly) { |
| 71 | super(mark, pos, lim, cap, buf, off); |
| 72 | this.isReadOnly = isReadOnly; |
| 73 | } |
| 74 | |
| 75 | public IntBuffer slice() { |
| 76 | return new HeapIntBuffer(hb, |
| 77 | -1, |
| 78 | 0, |
| 79 | this.remaining(), |
| 80 | this.remaining(), |
| 81 | this.position() + offset, |
| 82 | isReadOnly); |
| 83 | } |
| 84 | |
| 85 | public IntBuffer duplicate() { |
| 86 | return new HeapIntBuffer(hb, |
| 87 | this.markValue(), |
| 88 | this.position(), |
| 89 | this.limit(), |
| 90 | this.capacity(), |
| 91 | offset, |
| 92 | isReadOnly); |
| 93 | } |
| 94 | |
| 95 | public IntBuffer asReadOnlyBuffer() { |
| 96 | |
| 97 | return new HeapIntBuffer(hb, |
| 98 | this.markValue(), |
| 99 | this.position(), |
| 100 | this.limit(), |
| 101 | this.capacity(), |
| 102 | offset, true); |
| 103 | } |
| 104 | |
| 105 | protected int ix(int i) { |
| 106 | return i + offset; |
| 107 | } |
| 108 | |
| 109 | public int get() { |
| 110 | return hb[ix(nextGetIndex())]; |
| 111 | } |
| 112 | |
| 113 | public int get(int i) { |
| 114 | return hb[ix(checkIndex(i))]; |
| 115 | } |
| 116 | |
| 117 | public IntBuffer get(int[] dst, int offset, int length) { |
| 118 | checkBounds(offset, length, dst.length); |
| 119 | if (length > remaining()) |
| 120 | throw new BufferUnderflowException(); |
| 121 | System.arraycopy(hb, ix(position()), dst, offset, length); |
| 122 | position(position() + length); |
| 123 | return this; |
| 124 | } |
| 125 | |
| 126 | public boolean isDirect() { |
| 127 | return false; |
| 128 | } |
| 129 | |
| 130 | public boolean isReadOnly() { |
| 131 | return isReadOnly; |
| 132 | } |
| 133 | |
| 134 | public IntBuffer put(int x) { |
| 135 | if (isReadOnly) { |
| 136 | throw new ReadOnlyBufferException(); |
| 137 | } |
| 138 | hb[ix(nextPutIndex())] = x; |
| 139 | return this; |
| 140 | } |
| 141 | |
| 142 | public IntBuffer put(int i, int x) { |
| 143 | if (isReadOnly) { |
| 144 | throw new ReadOnlyBufferException(); |
| 145 | } |
| 146 | hb[ix(checkIndex(i))] = x; |
| 147 | return this; |
| 148 | } |
| 149 | |
| 150 | public IntBuffer put(int[] src, int offset, int length) { |
| 151 | if (isReadOnly) { |
| 152 | throw new ReadOnlyBufferException(); |
| 153 | } |
| 154 | checkBounds(offset, length, src.length); |
| 155 | if (length > remaining()) |
| 156 | throw new BufferOverflowException(); |
| 157 | System.arraycopy(src, offset, hb, ix(position()), length); |
| 158 | position(position() + length); |
| 159 | return this; |
| 160 | } |
| 161 | |
| 162 | public IntBuffer put(IntBuffer src) { |
| 163 | if (src == this) { |
| 164 | throw new IllegalArgumentException(); |
| 165 | } |
| 166 | if (isReadOnly) { |
| 167 | throw new ReadOnlyBufferException(); |
| 168 | } |
| 169 | if (src instanceof HeapIntBuffer) { |
| 170 | HeapIntBuffer sb = (HeapIntBuffer) src; |
| 171 | int n = sb.remaining(); |
| 172 | if (n > remaining()) |
| 173 | throw new BufferOverflowException(); |
| 174 | System.arraycopy(sb.hb, sb.ix(sb.position()), |
| 175 | hb, ix(position()), n); |
| 176 | sb.position(sb.position() + n); |
| 177 | position(position() + n); |
| 178 | } else if (src.isDirect()) { |
| 179 | int n = src.remaining(); |
| 180 | if (n > remaining()) |
| 181 | throw new BufferOverflowException(); |
| 182 | src.get(hb, ix(position()), n); |
| 183 | position(position() + n); |
| 184 | } else { |
| 185 | super.put(src); |
| 186 | } |
| 187 | return this; |
| 188 | } |
| 189 | |
| 190 | public IntBuffer compact() { |
| 191 | if (isReadOnly) { |
| 192 | throw new ReadOnlyBufferException(); |
| 193 | } |
| 194 | System.arraycopy(hb, ix(position()), hb, ix(0), remaining()); |
| 195 | position(remaining()); |
| 196 | limit(capacity()); |
| 197 | discardMark(); |
| 198 | return this; |
| 199 | } |
| 200 | |
| 201 | public ByteOrder order() { |
| 202 | return ByteOrder.nativeOrder(); |
| 203 | } |
| 204 | } |