OSDN Git Service

original
[gb-231r1-is01/Gingerbread_2.3.3_r1_IS01.git] / libcore / luni / src / main / java / java / nio / ReadWriteFloatArrayBuffer.java
1 /*
2  *  Licensed to the Apache Software Foundation (ASF) under one or more
3  *  contributor license agreements.  See the NOTICE file distributed with
4  *  this work for additional information regarding copyright ownership.
5  *  The ASF licenses this file to You under the Apache License, Version 2.0
6  *  (the "License"); you may not use this file except in compliance with
7  *  the License.  You may obtain a copy of the License at
8  *
9  *     http://www.apache.org/licenses/LICENSE-2.0
10  *
11  *  Unless required by applicable law or agreed to in writing, software
12  *  distributed under the License is distributed on an "AS IS" BASIS,
13  *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  *  See the License for the specific language governing permissions and
15  *  limitations under the License.
16  */
17
18 package java.nio;
19
20 /**
21  * FloatArrayBuffer, ReadWriteFloatArrayBuffer and ReadOnlyFloatArrayBuffer
22  * compose the implementation of array based float buffers.
23  * <p>
24  * ReadWriteFloatArrayBuffer extends FloatArrayBuffer with all the write
25  * methods.
26  * </p>
27  * <p>
28  * This class is marked final for runtime performance.
29  * </p>
30  *
31  */
32 final class ReadWriteFloatArrayBuffer extends FloatArrayBuffer {
33
34     static ReadWriteFloatArrayBuffer copy(FloatArrayBuffer other,
35             int markOfOther) {
36         ReadWriteFloatArrayBuffer buf = new ReadWriteFloatArrayBuffer(other
37                 .capacity(), other.backingArray, other.offset);
38         buf.limit = other.limit();
39         buf.position = other.position();
40         buf.mark = markOfOther;
41         return buf;
42     }
43
44     ReadWriteFloatArrayBuffer(float[] array) {
45         super(array);
46     }
47
48     ReadWriteFloatArrayBuffer(int capacity) {
49         super(capacity);
50     }
51
52     ReadWriteFloatArrayBuffer(int capacity, float[] backingArray,
53             int arrayOffset) {
54         super(capacity, backingArray, arrayOffset);
55     }
56
57     @Override
58     public FloatBuffer asReadOnlyBuffer() {
59         return ReadOnlyFloatArrayBuffer.copy(this, mark);
60     }
61
62     @Override
63     public FloatBuffer compact() {
64         System.arraycopy(backingArray, position + offset, backingArray, offset,
65                 remaining());
66         position = limit - position;
67         limit = capacity;
68         mark = UNSET_MARK;
69         return this;
70     }
71
72     @Override
73     public FloatBuffer duplicate() {
74         return copy(this, mark);
75     }
76
77     @Override
78     public boolean isReadOnly() {
79         return false;
80     }
81
82     @Override
83     protected float[] protectedArray() {
84         return backingArray;
85     }
86
87     @Override
88     protected int protectedArrayOffset() {
89         return offset;
90     }
91
92     @Override
93     protected boolean protectedHasArray() {
94         return true;
95     }
96
97     @Override
98     public FloatBuffer put(float c) {
99         if (position == limit) {
100             throw new BufferOverflowException();
101         }
102         backingArray[offset + position++] = c;
103         return this;
104     }
105
106     @Override
107     public FloatBuffer put(int index, float c) {
108         if (index < 0 || index >= limit) {
109             throw new IndexOutOfBoundsException();
110         }
111         backingArray[offset + index] = c;
112         return this;
113     }
114
115     @Override
116     public FloatBuffer put(float[] src, int off, int len) {
117         int length = src.length;
118         if (off < 0 || len < 0 || (long) off + (long) len > length) {
119             throw new IndexOutOfBoundsException();
120         }
121         if (len > remaining()) {
122             throw new BufferOverflowException();
123         }
124         System.arraycopy(src, off, backingArray, offset + position, len);
125         position += len;
126         return this;
127     }
128
129     @Override
130     public FloatBuffer slice() {
131         return new ReadWriteFloatArrayBuffer(remaining(), backingArray, offset
132                 + position);
133     }
134
135 }