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 HeapLongBuffer. 31 */ 32 33 class HeapLongBuffer 34 extends LongBuffer { 35 36 // For speed these fields are actually declared in X-Buffer; 37 // these declarations are here as documentation 38 /* 39 40 protected final long[] hb; 41 protected final int offset; 42 43 */ 44 HeapLongBuffer(int cap, int lim)45 HeapLongBuffer(int cap, int lim) { // package-private 46 this(cap, lim, false); 47 } 48 HeapLongBuffer(int cap, int lim, boolean isReadOnly)49 HeapLongBuffer(int cap, int lim, boolean isReadOnly) { // package-private 50 super(-1, 0, lim, cap, new long[cap], 0); 51 this.isReadOnly = isReadOnly; 52 } 53 HeapLongBuffer(long[] buf, int off, int len)54 HeapLongBuffer(long[] buf, int off, int len) { // package-private 55 this(buf, off, len, false); 56 } 57 HeapLongBuffer(long[] buf, int off, int len, boolean isReadOnly)58 HeapLongBuffer(long[] buf, int off, int len, boolean isReadOnly) { // package-private 59 super(-1, off, off + len, buf.length, buf, 0); 60 this.isReadOnly = isReadOnly; 61 } 62 HeapLongBuffer(long[] buf, int mark, int pos, int lim, int cap, int off)63 protected HeapLongBuffer(long[] buf, 64 int mark, int pos, int lim, int cap, 65 int off) { 66 this(buf, mark, pos, lim, cap, off, false); 67 } 68 HeapLongBuffer(long[] buf, int mark, int pos, int lim, int cap, int off, boolean isReadOnly)69 protected HeapLongBuffer(long[] buf, 70 int mark, int pos, int lim, int cap, 71 int off, boolean isReadOnly) { 72 super(mark, pos, lim, cap, buf, off); 73 this.isReadOnly = isReadOnly; 74 } 75 slice()76 public LongBuffer slice() { 77 return new HeapLongBuffer(hb, 78 -1, 79 0, 80 this.remaining(), 81 this.remaining(), 82 this.position() + offset, 83 isReadOnly); 84 } 85 duplicate()86 public LongBuffer duplicate() { 87 return new HeapLongBuffer(hb, 88 this.markValue(), 89 this.position(), 90 this.limit(), 91 this.capacity(), 92 offset, 93 isReadOnly); 94 } 95 asReadOnlyBuffer()96 public LongBuffer asReadOnlyBuffer() { 97 return new HeapLongBuffer(hb, 98 this.markValue(), 99 this.position(), 100 this.limit(), 101 this.capacity(), 102 offset, true); 103 } 104 105 ix(int i)106 protected int ix(int i) { 107 return i + offset; 108 } 109 get()110 public long get() { 111 return hb[ix(nextGetIndex())]; 112 } 113 get(int i)114 public long get(int i) { 115 return hb[ix(checkIndex(i))]; 116 } 117 get(long[] dst, int offset, int length)118 public LongBuffer get(long[] dst, int offset, int length) { 119 checkBounds(offset, length, dst.length); 120 if (length > remaining()) 121 throw new BufferUnderflowException(); 122 System.arraycopy(hb, ix(position()), dst, offset, length); 123 position(position() + length); 124 return this; 125 } 126 isDirect()127 public boolean isDirect() { 128 return false; 129 } 130 isReadOnly()131 public boolean isReadOnly() { 132 return isReadOnly; 133 } 134 put(long x)135 public LongBuffer put(long x) { 136 if (isReadOnly) { 137 throw new ReadOnlyBufferException(); 138 } 139 hb[ix(nextPutIndex())] = x; 140 return this; 141 } 142 put(int i, long x)143 public LongBuffer put(int i, long x) { 144 if (isReadOnly) { 145 throw new ReadOnlyBufferException(); 146 } 147 hb[ix(checkIndex(i))] = x; 148 return this; 149 } 150 put(long[] src, int offset, int length)151 public LongBuffer put(long[] src, int offset, int length) { 152 if (isReadOnly) { 153 throw new ReadOnlyBufferException(); 154 } 155 checkBounds(offset, length, src.length); 156 if (length > remaining()) 157 throw new BufferOverflowException(); 158 System.arraycopy(src, offset, hb, ix(position()), length); 159 position(position() + length); 160 return this; 161 } 162 put(LongBuffer src)163 public LongBuffer put(LongBuffer src) { 164 if (src == this) { 165 throw new IllegalArgumentException(); 166 } 167 if (isReadOnly) { 168 throw new ReadOnlyBufferException(); 169 } 170 if (src instanceof HeapLongBuffer) { 171 HeapLongBuffer sb = (HeapLongBuffer) src; 172 int n = sb.remaining(); 173 if (n > remaining()) 174 throw new BufferOverflowException(); 175 System.arraycopy(sb.hb, sb.ix(sb.position()), 176 hb, ix(position()), n); 177 sb.position(sb.position() + n); 178 position(position() + n); 179 } else if (src.isDirect()) { 180 int n = src.remaining(); 181 if (n > remaining()) 182 throw new BufferOverflowException(); 183 src.get(hb, ix(position()), n); 184 position(position() + n); 185 } else { 186 super.put(src); 187 } 188 return this; 189 } 190 compact()191 public LongBuffer compact() { 192 if (isReadOnly) { 193 throw new ReadOnlyBufferException(); 194 } 195 System.arraycopy(hb, ix(position()), hb, ix(0), remaining()); 196 position(remaining()); 197 limit(capacity()); 198 discardMark(); 199 return this; 200 } 201 order()202 public ByteOrder order() { 203 return ByteOrder.nativeOrder(); 204 } 205 } 206