OSDN Git Service

0bd3ce4a2befeef7de7001d32e8c1de08a5537e8
[android-x86/dalvik.git] / libcore / nio / src / main / java / java / nio / LongToByteBufferAdapter.java
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
7  * 
8  *     http://www.apache.org/licenses/LICENSE-2.0
9  * 
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.
15  */
16
17 package java.nio;
18
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;
23 // END android-added
24
25 /**
26  * This class wraps a byte buffer to be a long buffer.
27  * <p>
28  * Implementation notice:
29  * <ul>
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>
34  * </ul>
35  * </p>
36  * 
37  */
38 // BEGIN android-changed
39 // copied from newer version of harmony
40 final class LongToByteBufferAdapter extends LongBuffer implements DirectBuffer {
41 // END android-changed
42
43     static LongBuffer wrap(ByteBuffer byteBuffer) {
44         return new LongToByteBufferAdapter(byteBuffer.slice());
45     }
46
47     private final ByteBuffer byteBuffer;
48
49     LongToByteBufferAdapter(ByteBuffer byteBuffer) {
50         super((byteBuffer.capacity() >> 3));
51         this.byteBuffer = byteBuffer;
52         this.byteBuffer.clear();
53     }
54
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();
60         } else {
61             assert false : byteBuffer;
62             return -1;
63         }            
64     }
65
66     public PlatformAddress getEffectiveAddress() {
67         if (byteBuffer instanceof DirectBuffer) {
68             return ((DirectBuffer)byteBuffer).getEffectiveAddress();
69         } else {
70             assert false : byteBuffer;
71             return null;
72         }
73     }
74
75     public PlatformAddress getBaseAddress() {
76         if (byteBuffer instanceof DirectBuffer) {
77             return ((DirectBuffer)byteBuffer).getBaseAddress();
78         } else {
79             assert false : byteBuffer;
80             return null;
81         }
82     }
83             
84     public boolean isAddressValid() {
85         if (byteBuffer instanceof DirectBuffer) {
86             return ((DirectBuffer)byteBuffer).isAddressValid();
87         } else {
88             assert false : byteBuffer;
89             return false;
90         }
91     }
92
93   public void addressValidityCheck() {
94         if (byteBuffer instanceof DirectBuffer) {
95             ((DirectBuffer)byteBuffer).addressValidityCheck();
96         } else {
97             assert false : byteBuffer;
98         }
99     }
100             
101     public void free() {
102         if (byteBuffer instanceof DirectBuffer) {
103             ((DirectBuffer)byteBuffer).free();
104         } else {
105             assert false : byteBuffer;
106         }   
107     }
108     // END android-added
109
110     public LongBuffer asReadOnlyBuffer() {
111         LongToByteBufferAdapter buf = new LongToByteBufferAdapter(byteBuffer
112                 .asReadOnlyBuffer());
113         buf.limit = limit;
114         buf.position = position;
115         buf.mark = mark;
116         return buf;
117     }
118
119     public LongBuffer compact() {
120         if (byteBuffer.isReadOnly()) {
121             throw new ReadOnlyBufferException();
122         }
123         byteBuffer.limit(limit << 3);
124         byteBuffer.position(position << 3);
125         byteBuffer.compact();
126         byteBuffer.clear();
127         position = limit - position;
128         limit = capacity;
129         mark = UNSET_MARK;
130         return this;
131     }
132
133     public LongBuffer duplicate() {
134         LongToByteBufferAdapter buf = new LongToByteBufferAdapter(byteBuffer
135                 .duplicate());
136         buf.limit = limit;
137         buf.position = position;
138         buf.mark = mark;
139         return buf;
140     }
141
142     public long get() {
143         if (position == limit) {
144             throw new BufferUnderflowException();
145         }
146         return byteBuffer.getLong(position++ << 3);
147     }
148
149     public long get(int index) {
150         if (index < 0 || index >= limit) {
151             throw new IndexOutOfBoundsException();
152         }
153         return byteBuffer.getLong(index << 3);
154     }
155
156     public boolean isDirect() {
157         return byteBuffer.isDirect();
158     }
159
160     public boolean isReadOnly() {
161         return byteBuffer.isReadOnly();
162     }
163
164     public ByteOrder order() {
165         return byteBuffer.order();
166     }
167
168     protected long[] protectedArray() {
169         throw new UnsupportedOperationException();
170     }
171
172     protected int protectedArrayOffset() {
173         throw new UnsupportedOperationException();
174     }
175
176     protected boolean protectedHasArray() {
177         return false;
178     }
179
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     public LongBuffer put(int index, long c) {
189         if (index < 0 || index >= limit) {
190             throw new IndexOutOfBoundsException();
191         }
192         byteBuffer.putLong(index << 3, c);
193         return this;
194     }
195
196     public LongBuffer slice() {
197         byteBuffer.limit(limit << 3);
198         byteBuffer.position(position << 3);
199         LongBuffer result = new LongToByteBufferAdapter(byteBuffer.slice());
200         byteBuffer.clear();
201         return result;
202     }
203
204 }