• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 
26 package java.nio;
27 
28 import libcore.io.Memory;
29 
30 class ByteBufferAsDoubleBuffer
31         extends DoubleBuffer {            // package-private
32 
33     protected final ByteBuffer bb;
34     protected final int offset;
35     private final ByteOrder order;
36 
ByteBufferAsDoubleBuffer(ByteBuffer bb, int mark, int pos, int lim, int cap, int off, ByteOrder order)37     ByteBufferAsDoubleBuffer(ByteBuffer bb,
38                              int mark, int pos, int lim, int cap,
39                              int off, ByteOrder order) {
40         super(mark, pos, lim, cap);
41         this.bb = bb;
42         this.isReadOnly = bb.isReadOnly;
43         // There are only two possibilities for the type of ByteBuffer "bb", viz, DirectByteBuffer and
44         // HeapByteBuffer. We only have to initialize the field when bb is an instance of
45         // DirectByteBuffer.
46         // The address field is used by NIOAccess#getBasePointer and GetDirectBufferAddress method
47         // in art which return the address of the first usable byte of the underlying memory, i.e,
48         // the position of parent buffer. Therefore, value of "off" will be equal to parent buffer's
49         // position when the method is called from either HeapByteBuffer or DirectByteBuffer.
50         if (bb instanceof DirectByteBuffer) {
51             this.address = bb.address + off;
52         }
53         this.order = order;
54         offset = off;
55     }
56 
slice()57     public DoubleBuffer slice() {
58         int pos = this.position();
59         int lim = this.limit();
60         assert (pos <= lim);
61         int rem = (pos <= lim ? lim - pos : 0);
62         int off = (pos << 3) + offset;
63         assert (off >= 0);
64         return new ByteBufferAsDoubleBuffer(bb, -1, 0, rem, rem, off, order);
65     }
66 
duplicate()67     public DoubleBuffer duplicate() {
68         return new ByteBufferAsDoubleBuffer(bb,
69                 markValue(),
70                 position(),
71                 limit(),
72                 capacity(),
73                 offset,
74                 order);
75     }
76 
asReadOnlyBuffer()77     public DoubleBuffer asReadOnlyBuffer() {
78         return new ByteBufferAsDoubleBuffer(bb.asReadOnlyBuffer(),
79                 markValue(),
80                 position(),
81                 limit(),
82                 capacity(),
83                 offset,
84                 order);
85     }
86 
ix(int i)87     protected int ix(int i) {
88         return (i << 3) + offset;
89     }
90 
get()91     public double get() {
92         return get(nextGetIndex());
93     }
94 
get(int i)95     public double get(int i) {
96         return bb.getDoubleUnchecked(ix(checkIndex(i)));
97     }
98 
get(double[] dst, int offset, int length)99     public DoubleBuffer get(double[] dst, int offset, int length) {
100         checkBounds(offset, length, dst.length);
101         if (length > remaining())
102             throw new BufferUnderflowException();
103         bb.getUnchecked(ix(position), dst, offset, length);
104         position += length;
105         return this;
106     }
107 
put(double x)108     public DoubleBuffer put(double x) {
109         put(nextPutIndex(), x);
110         return this;
111     }
112 
put(int i, double x)113     public DoubleBuffer put(int i, double x) {
114         if (isReadOnly) {
115             throw new ReadOnlyBufferException();
116         }
117         bb.putDoubleUnchecked(ix(checkIndex(i)), x);
118         return this;
119     }
120 
put(double[] src, int offset, int length)121     public DoubleBuffer put(double[] src, int offset, int length) {
122         checkBounds(offset, length, src.length);
123         if (length > remaining())
124             throw new BufferOverflowException();
125         bb.putUnchecked(ix(position), src, offset, length);
126         position += length;
127         return this;
128     }
129 
compact()130     public DoubleBuffer compact() {
131         if (isReadOnly) {
132             throw new ReadOnlyBufferException();
133         }
134         int pos = position();
135         int lim = limit();
136         assert (pos <= lim);
137         int rem = (pos <= lim ? lim - pos : 0);
138         if (!(bb instanceof DirectByteBuffer)) {
139             System.arraycopy(bb.array(), ix(pos), bb.array(), ix(0), rem << 3);
140         } else {
141             Memory.memmove(this, ix(0), this, ix(pos), rem << 3);
142         }
143         position(rem);
144         limit(capacity());
145         discardMark();
146         return this;
147     }
148 
isDirect()149     public boolean isDirect() {
150         return bb.isDirect();
151     }
152 
isReadOnly()153     public boolean isReadOnly() {
154         return isReadOnly;
155     }
156 
order()157     public ByteOrder order() {
158         return order;
159     }
160 }
161