• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 
HeapIntBuffer(int cap, int lim)44     HeapIntBuffer(int cap, int lim) {            // package-private
45         this(cap, lim, false);
46     }
47 
HeapIntBuffer(int cap, int lim, boolean isReadOnly)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 
HeapIntBuffer(int[] buf, int off, int len)53     HeapIntBuffer(int[] buf, int off, int len) { // package-private
54         this(buf, off, len, false);
55     }
56 
HeapIntBuffer(int[] buf, int off, int len, boolean isReadOnly)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 
HeapIntBuffer(int[] buf, int mark, int pos, int lim, int cap, int off)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 
HeapIntBuffer(int[] buf, int mark, int pos, int lim, int cap, int off, boolean isReadOnly)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 
slice()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 
duplicate()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 
asReadOnlyBuffer()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 
ix(int i)105     protected int ix(int i) {
106         return i + offset;
107     }
108 
get()109     public int get() {
110         return hb[ix(nextGetIndex())];
111     }
112 
get(int i)113     public int get(int i) {
114         return hb[ix(checkIndex(i))];
115     }
116 
get(int[] dst, int offset, int length)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 
isDirect()126     public boolean isDirect() {
127         return false;
128     }
129 
isReadOnly()130     public boolean isReadOnly() {
131         return isReadOnly;
132     }
133 
put(int x)134     public IntBuffer put(int x) {
135         if (isReadOnly) {
136             throw new ReadOnlyBufferException();
137         }
138         hb[ix(nextPutIndex())] = x;
139         return this;
140     }
141 
put(int i, int x)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 
put(int[] src, int offset, int length)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 
put(IntBuffer src)162     public IntBuffer put(IntBuffer src) {
163         if (isReadOnly) {
164             throw new ReadOnlyBufferException();
165         }
166         if (src instanceof HeapIntBuffer) {
167             if (src == this)
168                 throw new IllegalArgumentException();
169             HeapIntBuffer sb = (HeapIntBuffer) src;
170             int n = sb.remaining();
171             if (n > remaining())
172                 throw new BufferOverflowException();
173             System.arraycopy(sb.hb, sb.ix(sb.position()),
174                     hb, ix(position()), n);
175             sb.position(sb.position() + n);
176             position(position() + n);
177         } else if (src.isDirect()) {
178             int n = src.remaining();
179             if (n > remaining())
180                 throw new BufferOverflowException();
181             src.get(hb, ix(position()), n);
182             position(position() + n);
183         } else {
184             super.put(src);
185         }
186         return this;
187     }
188 
compact()189     public IntBuffer compact() {
190         if (isReadOnly) {
191             throw new ReadOnlyBufferException();
192         }
193         System.arraycopy(hb, ix(position()), hb, ix(0), remaining());
194         position(remaining());
195         limit(capacity());
196         discardMark();
197         return this;
198     }
199 
order()200     public ByteOrder order() {
201         return ByteOrder.nativeOrder();
202     }
203 }
204