ByteBufferAsDoubleBuffer.java revision f28b09a791e4e5635e4fcdfd1adde27aec0a1ed6
1/* 2 * Copyright (c) 2000, 2008, Oracle and/or its affiliates. All rights reserved. 3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 4 * 5 * This code is free software; you can redistribute it and/or modify it 6 * under the terms of the GNU General Public License version 2 only, as 7 * published by the Free Software Foundation. Oracle designates this 8 * particular file as subject to the "Classpath" exception as provided 9 * by Oracle in the LICENSE file that accompanied this code. 10 * 11 * This code is distributed in the hope that it will be useful, but WITHOUT 12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 13 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 14 * version 2 for more details (a copy is included in the LICENSE file that 15 * accompanied this code). 16 * 17 * You should have received a copy of the GNU General Public License version 18 * 2 along with this work; if not, write to the Free Software Foundation, 19 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 20 * 21 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 22 * or visit www.oracle.com if you need additional information or have any 23 * questions. 24 */ 25 26package java.nio; 27 28import libcore.io.Memory; 29 30class ByteBufferAsDoubleBuffer 31 extends DoubleBuffer { // package-private 32 33 protected final ByteBuffer bb; 34 protected final int offset; 35 private final ByteOrder order; 36 37 ByteBufferAsDoubleBuffer(ByteBuffer bb, ByteOrder order) { 38 super(-1, 0, 39 bb.remaining() >> 3, 40 bb.remaining() >> 3); 41 this.bb = bb; 42 this.isReadOnly = bb.isReadOnly; 43 this.address = bb.address; 44 this.order = order; 45 int cap = this.capacity(); 46 this.limit(cap); 47 int pos = this.position(); 48 assert (pos <= cap); 49 offset = pos; 50 } 51 52 ByteBufferAsDoubleBuffer(ByteBuffer bb, 53 int mark, int pos, int lim, int cap, 54 int off, ByteOrder order) { 55 super(mark, pos, lim, cap); 56 this.bb = bb; 57 this.isReadOnly = bb.isReadOnly; 58 this.address = bb.address; 59 this.order = order; 60 offset = off; 61 } 62 63 public DoubleBuffer slice() { 64 int pos = this.position(); 65 int lim = this.limit(); 66 assert (pos <= lim); 67 int rem = (pos <= lim ? lim - pos : 0); 68 int off = (pos << 3) + offset; 69 assert (off >= 0); 70 return new ByteBufferAsDoubleBuffer(bb, -1, 0, rem, rem, off, order); 71 } 72 73 public DoubleBuffer duplicate() { 74 return new ByteBufferAsDoubleBuffer(bb, 75 markValue(), 76 position(), 77 limit(), 78 capacity(), 79 offset, 80 order); 81 } 82 83 public DoubleBuffer asReadOnlyBuffer() { 84 return new ByteBufferAsDoubleBuffer(bb.asReadOnlyBuffer(), 85 markValue(), 86 position(), 87 limit(), 88 capacity(), 89 offset, 90 order); 91 } 92 93 protected int ix(int i) { 94 return (i << 3) + offset; 95 } 96 97 public double get() { 98 return get(nextGetIndex()); 99 } 100 101 public double get(int i) { 102 return bb.getDoubleUnchecked(ix(checkIndex(i))); 103 } 104 105 public DoubleBuffer get(double[] dst, int offset, int length) { 106 checkBounds(offset, length, dst.length); 107 if (length > remaining()) 108 throw new BufferUnderflowException(); 109 bb.getUnchecked(ix(position), dst, offset, length); 110 position += length; 111 return this; 112 } 113 114 public DoubleBuffer put(double x) { 115 put(nextPutIndex(), x); 116 return this; 117 } 118 119 public DoubleBuffer put(int i, double x) { 120 if (isReadOnly) { 121 throw new ReadOnlyBufferException(); 122 } 123 bb.putDoubleUnchecked(ix(checkIndex(i)), x); 124 return this; 125 } 126 127 public DoubleBuffer put(double[] src, int offset, int length) { 128 checkBounds(offset, length, src.length); 129 if (length > remaining()) 130 throw new BufferOverflowException(); 131 bb.putUnchecked(ix(position), src, offset, length); 132 position += length; 133 return this; 134 } 135 136 public DoubleBuffer compact() { 137 if (isReadOnly) { 138 throw new ReadOnlyBufferException(); 139 } 140 int pos = position(); 141 int lim = limit(); 142 assert (pos <= lim); 143 int rem = (pos <= lim ? lim - pos : 0); 144 if (!(bb instanceof DirectByteBuffer)) { 145 System.arraycopy(bb.array(), ix(pos), bb.array(), ix(0), rem << 3); 146 } else { 147 Memory.memmove(this, ix(0), this, ix(pos), rem << 3); 148 } 149 position(rem); 150 limit(capacity()); 151 discardMark(); 152 return this; 153 } 154 155 public boolean isDirect() { 156 return bb.isDirect(); 157 } 158 159 public boolean isReadOnly() { 160 return isReadOnly; 161 } 162 163 public ByteOrder order() { 164 return order; 165 } 166} 167