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 short 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: final class ShortToByteBufferAdapter extends ShortBuffer implements
035: DirectBuffer {
036:
037: static ShortBuffer wrap(ByteBuffer byteBuffer) {
038: return new ShortToByteBufferAdapter(byteBuffer.slice());
039: }
040:
041: private final ByteBuffer byteBuffer;
042:
043: ShortToByteBufferAdapter(ByteBuffer byteBuffer) {
044: super ((byteBuffer.capacity() >> 1));
045: this .byteBuffer = byteBuffer;
046: this .byteBuffer.clear();
047: }
048:
049: public int getByteCapacity() {
050: if (byteBuffer instanceof DirectBuffer) {
051: return ((DirectBuffer) byteBuffer).getByteCapacity();
052: }
053: assert false : byteBuffer;
054: return -1;
055: }
056:
057: public PlatformAddress getEffectiveAddress() {
058: if (byteBuffer instanceof DirectBuffer) {
059: return ((DirectBuffer) byteBuffer).getEffectiveAddress();
060: }
061: assert false : byteBuffer;
062: return null;
063: }
064:
065: public PlatformAddress getBaseAddress() {
066: if (byteBuffer instanceof DirectBuffer) {
067: return ((DirectBuffer) byteBuffer).getBaseAddress();
068: }
069: assert false : byteBuffer;
070: return null;
071: }
072:
073: public boolean isAddressValid() {
074: if (byteBuffer instanceof DirectBuffer) {
075: return ((DirectBuffer) byteBuffer).isAddressValid();
076: }
077: assert false : byteBuffer;
078: return false;
079: }
080:
081: public void addressValidityCheck() {
082: if (byteBuffer instanceof DirectBuffer) {
083: ((DirectBuffer) byteBuffer).addressValidityCheck();
084: } else {
085: assert false : byteBuffer;
086: }
087: }
088:
089: public void free() {
090: if (byteBuffer instanceof DirectBuffer) {
091: ((DirectBuffer) byteBuffer).free();
092: } else {
093: assert false : byteBuffer;
094: }
095: }
096:
097: @Override
098: public ShortBuffer asReadOnlyBuffer() {
099: ShortToByteBufferAdapter buf = new ShortToByteBufferAdapter(
100: byteBuffer.asReadOnlyBuffer());
101: buf.limit = limit;
102: buf.position = position;
103: buf.mark = mark;
104: return buf;
105: }
106:
107: @Override
108: public ShortBuffer compact() {
109: if (byteBuffer.isReadOnly()) {
110: throw new ReadOnlyBufferException();
111: }
112: byteBuffer.limit(limit << 1);
113: byteBuffer.position(position << 1);
114: byteBuffer.compact();
115: byteBuffer.clear();
116: position = limit - position;
117: limit = capacity;
118: mark = UNSET_MARK;
119: return this ;
120: }
121:
122: @Override
123: public ShortBuffer duplicate() {
124: ShortToByteBufferAdapter buf = new ShortToByteBufferAdapter(
125: byteBuffer.duplicate());
126: buf.limit = limit;
127: buf.position = position;
128: buf.mark = mark;
129: return buf;
130: }
131:
132: @Override
133: public short get() {
134: if (position == limit) {
135: throw new BufferUnderflowException();
136: }
137: return byteBuffer.getShort(position++ << 1);
138: }
139:
140: @Override
141: public short get(int index) {
142: if (index < 0 || index >= limit) {
143: throw new IndexOutOfBoundsException();
144: }
145: return byteBuffer.getShort(index << 1);
146: }
147:
148: @Override
149: public boolean isDirect() {
150: return byteBuffer.isDirect();
151: }
152:
153: @Override
154: public boolean isReadOnly() {
155: return byteBuffer.isReadOnly();
156: }
157:
158: @Override
159: public ByteOrder order() {
160: return byteBuffer.order();
161: }
162:
163: @Override
164: protected short[] protectedArray() {
165: throw new UnsupportedOperationException();
166: }
167:
168: @Override
169: protected int protectedArrayOffset() {
170: throw new UnsupportedOperationException();
171: }
172:
173: @Override
174: protected boolean protectedHasArray() {
175: return false;
176: }
177:
178: @Override
179: public ShortBuffer put(short c) {
180: if (position == limit) {
181: throw new BufferOverflowException();
182: }
183: byteBuffer.putShort(position++ << 1, c);
184: return this ;
185: }
186:
187: @Override
188: public ShortBuffer put(int index, short c) {
189: if (index < 0 || index >= limit) {
190: throw new IndexOutOfBoundsException();
191: }
192: byteBuffer.putShort(index << 1, c);
193: return this ;
194: }
195:
196: @Override
197: public ShortBuffer slice() {
198: byteBuffer.limit(limit << 1);
199: byteBuffer.position(position << 1);
200: ShortBuffer result = new ShortToByteBufferAdapter(byteBuffer
201: .slice());
202: byteBuffer.clear();
203: return result;
204: }
205:
206: }
|