001: /* Licensed to the Apache Software Foundation (ASF) under one or more
002: * contributor license agreements. See the NOTICE file distributed with
003: * this work for additional information regarding copyright ownership.
004: * The ASF licenses this file to You under the Apache License, Version 2.0
005: * (the "License"); you may not use this file except in compliance with
006: * the License. You may obtain a copy of the License at
007: *
008: * http://www.apache.org/licenses/LICENSE-2.0
009: *
010: * Unless required by applicable law or agreed to in writing, software
011: * distributed under the License is distributed on an "AS IS" BASIS,
012: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013: * See the License for the specific language governing permissions and
014: * limitations under the License.
015: */
016:
017: package java.nio;
018:
019: import org.apache.harmony.nio.internal.DirectBuffer;
020: import org.apache.harmony.luni.platform.PlatformAddress;
021:
022: /**
023: * This class wraps a byte buffer to be a long buffer.
024: * <p>
025: * Implementation notice:
026: * <ul>
027: * <li>After a byte buffer instance is wrapped, it becomes privately owned by
028: * the adapter. It must NOT be accessed outside the adapter any more.</li>
029: * <li>The byte buffer's position and limit are NOT linked with the adapter.
030: * The adapter extends Buffer, thus has its own position and limit.</li>
031: * </ul>
032: * </p>
033: *
034: */
035: final class LongToByteBufferAdapter extends LongBuffer implements
036: DirectBuffer {
037:
038: static LongBuffer wrap(ByteBuffer byteBuffer) {
039: return new LongToByteBufferAdapter(byteBuffer.slice());
040: }
041:
042: private final ByteBuffer byteBuffer;
043:
044: LongToByteBufferAdapter(ByteBuffer byteBuffer) {
045: super ((byteBuffer.capacity() >> 3));
046: this .byteBuffer = byteBuffer;
047: this .byteBuffer.clear();
048: }
049:
050: public int getByteCapacity() {
051: if (byteBuffer instanceof DirectBuffer) {
052: return ((DirectBuffer) byteBuffer).getByteCapacity();
053: }
054: assert false : byteBuffer;
055: return -1;
056: }
057:
058: public PlatformAddress getEffectiveAddress() {
059: if (byteBuffer instanceof DirectBuffer) {
060: return ((DirectBuffer) byteBuffer).getEffectiveAddress();
061: }
062: assert false : byteBuffer;
063: return null;
064: }
065:
066: public PlatformAddress getBaseAddress() {
067: if (byteBuffer instanceof DirectBuffer) {
068: return ((DirectBuffer) byteBuffer).getBaseAddress();
069: }
070: assert false : byteBuffer;
071: return null;
072: }
073:
074: public boolean isAddressValid() {
075: if (byteBuffer instanceof DirectBuffer) {
076: return ((DirectBuffer) byteBuffer).isAddressValid();
077: }
078: assert false : byteBuffer;
079: return false;
080: }
081:
082: public void addressValidityCheck() {
083: if (byteBuffer instanceof DirectBuffer) {
084: ((DirectBuffer) byteBuffer).addressValidityCheck();
085: } else {
086: assert false : byteBuffer;
087: }
088: }
089:
090: public void free() {
091: if (byteBuffer instanceof DirectBuffer) {
092: ((DirectBuffer) byteBuffer).free();
093: } else {
094: assert false : byteBuffer;
095: }
096: }
097:
098: @Override
099: public LongBuffer asReadOnlyBuffer() {
100: LongToByteBufferAdapter buf = new LongToByteBufferAdapter(
101: byteBuffer.asReadOnlyBuffer());
102: buf.limit = limit;
103: buf.position = position;
104: buf.mark = mark;
105: return buf;
106: }
107:
108: @Override
109: public LongBuffer compact() {
110: if (byteBuffer.isReadOnly()) {
111: throw new ReadOnlyBufferException();
112: }
113: byteBuffer.limit(limit << 3);
114: byteBuffer.position(position << 3);
115: byteBuffer.compact();
116: byteBuffer.clear();
117: position = limit - position;
118: limit = capacity;
119: mark = UNSET_MARK;
120: return this ;
121: }
122:
123: @Override
124: public LongBuffer duplicate() {
125: LongToByteBufferAdapter buf = new LongToByteBufferAdapter(
126: byteBuffer.duplicate());
127: buf.limit = limit;
128: buf.position = position;
129: buf.mark = mark;
130: return buf;
131: }
132:
133: @Override
134: public long get() {
135: if (position == limit) {
136: throw new BufferUnderflowException();
137: }
138: return byteBuffer.getLong(position++ << 3);
139: }
140:
141: @Override
142: public long get(int index) {
143: if (index < 0 || index >= limit) {
144: throw new IndexOutOfBoundsException();
145: }
146: return byteBuffer.getLong(index << 3);
147: }
148:
149: @Override
150: public boolean isDirect() {
151: return byteBuffer.isDirect();
152: }
153:
154: @Override
155: public boolean isReadOnly() {
156: return byteBuffer.isReadOnly();
157: }
158:
159: @Override
160: public ByteOrder order() {
161: return byteBuffer.order();
162: }
163:
164: @Override
165: protected long[] protectedArray() {
166: throw new UnsupportedOperationException();
167: }
168:
169: @Override
170: protected int protectedArrayOffset() {
171: throw new UnsupportedOperationException();
172: }
173:
174: @Override
175: protected boolean protectedHasArray() {
176: return false;
177: }
178:
179: @Override
180: public LongBuffer put(long c) {
181: if (position == limit) {
182: throw new BufferOverflowException();
183: }
184: byteBuffer.putLong(position++ << 3, c);
185: return this ;
186: }
187:
188: @Override
189: public LongBuffer put(int index, long c) {
190: if (index < 0 || index >= limit) {
191: throw new IndexOutOfBoundsException();
192: }
193: byteBuffer.putLong(index << 3, c);
194: return this ;
195: }
196:
197: @Override
198: public LongBuffer slice() {
199: byteBuffer.limit(limit << 3);
200: byteBuffer.position(position << 3);
201: LongBuffer result = new LongToByteBufferAdapter(byteBuffer
202: .slice());
203: byteBuffer.clear();
204: return result;
205: }
206:
207: }
|