1 /* Licensed to the Apache Software Foundation (ASF) under one or more
2 * contributor license agreements. See the NOTICE file distributed with
3 * this work for additional information regarding copyright ownership.
4 * The ASF licenses this file to You under the Apache License, Version 2.0
5 * (the "License"); you may not use this file except in compliance with
6 * the License. You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
19 // BEGIN android-added
20 // copied from newer version of harmony
21 import org.apache.harmony.nio.internal.DirectBuffer;
22 import org.apache.harmony.luni.platform.PlatformAddress;
26 * This class wraps a byte buffer to be a long buffer.
28 * Implementation notice:
30 * <li>After a byte buffer instance is wrapped, it becomes privately owned by
31 * the adapter. It must NOT be accessed outside the adapter any more.</li>
32 * <li>The byte buffer's position and limit are NOT linked with the adapter.
33 * The adapter extends Buffer, thus has its own position and limit.</li>
38 // BEGIN android-changed
39 // copied from newer version of harmony
40 final class LongToByteBufferAdapter extends LongBuffer implements DirectBuffer {
41 // END android-changed
43 static LongBuffer wrap(ByteBuffer byteBuffer) {
44 return new LongToByteBufferAdapter(byteBuffer.slice());
47 private final ByteBuffer byteBuffer;
49 LongToByteBufferAdapter(ByteBuffer byteBuffer) {
50 super((byteBuffer.capacity() >> 3));
51 this.byteBuffer = byteBuffer;
52 this.byteBuffer.clear();
55 // BEGIN android-added
56 // copied from newer version of harmony
57 public int getByteCapacity() {
58 if (byteBuffer instanceof DirectBuffer) {
59 return ((DirectBuffer)byteBuffer).getByteCapacity();
61 assert false : byteBuffer;
66 public PlatformAddress getEffectiveAddress() {
67 if (byteBuffer instanceof DirectBuffer) {
68 return ((DirectBuffer)byteBuffer).getEffectiveAddress();
70 assert false : byteBuffer;
75 public PlatformAddress getBaseAddress() {
76 if (byteBuffer instanceof DirectBuffer) {
77 return ((DirectBuffer)byteBuffer).getBaseAddress();
79 assert false : byteBuffer;
84 public boolean isAddressValid() {
85 if (byteBuffer instanceof DirectBuffer) {
86 return ((DirectBuffer)byteBuffer).isAddressValid();
88 assert false : byteBuffer;
93 public void addressValidityCheck() {
94 if (byteBuffer instanceof DirectBuffer) {
95 ((DirectBuffer)byteBuffer).addressValidityCheck();
97 assert false : byteBuffer;
102 if (byteBuffer instanceof DirectBuffer) {
103 ((DirectBuffer)byteBuffer).free();
105 assert false : byteBuffer;
110 public LongBuffer asReadOnlyBuffer() {
111 LongToByteBufferAdapter buf = new LongToByteBufferAdapter(byteBuffer
112 .asReadOnlyBuffer());
114 buf.position = position;
119 public LongBuffer compact() {
120 if (byteBuffer.isReadOnly()) {
121 throw new ReadOnlyBufferException();
123 byteBuffer.limit(limit << 3);
124 byteBuffer.position(position << 3);
125 byteBuffer.compact();
127 position = limit - position;
133 public LongBuffer duplicate() {
134 LongToByteBufferAdapter buf = new LongToByteBufferAdapter(byteBuffer
137 buf.position = position;
143 if (position == limit) {
144 throw new BufferUnderflowException();
146 return byteBuffer.getLong(position++ << 3);
149 public long get(int index) {
150 if (index < 0 || index >= limit) {
151 throw new IndexOutOfBoundsException();
153 return byteBuffer.getLong(index << 3);
156 public boolean isDirect() {
157 return byteBuffer.isDirect();
160 public boolean isReadOnly() {
161 return byteBuffer.isReadOnly();
164 public ByteOrder order() {
165 return byteBuffer.order();
168 protected long[] protectedArray() {
169 throw new UnsupportedOperationException();
172 protected int protectedArrayOffset() {
173 throw new UnsupportedOperationException();
176 protected boolean protectedHasArray() {
180 public LongBuffer put(long c) {
181 if (position == limit) {
182 throw new BufferOverflowException();
184 byteBuffer.putLong(position++ << 3, c);
188 public LongBuffer put(int index, long c) {
189 if (index < 0 || index >= limit) {
190 throw new IndexOutOfBoundsException();
192 byteBuffer.putLong(index << 3, c);
196 public LongBuffer slice() {
197 byteBuffer.limit(limit << 3);
198 byteBuffer.position(position << 3);
199 LongBuffer result = new LongToByteBufferAdapter(byteBuffer.slice());