NioBufferIterator.java revision 43a9f774d075e0e441d8b996e3f6c81ea483ec89
1/*
2 * Copyright (C) 2010 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * 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
17package libcore.io;
18
19import org.apache.harmony.luni.platform.OSMemory;
20
21/**
22 * Iterates over big- or little-endian bytes on the native heap.
23 * See {@link MemoryMappedFile#bigEndianIterator} and {@link MemoryMappedFile#littleEndianIterator}.
24 *
25 * @hide don't make this public without adding bounds checking.
26 */
27public final class NioBufferIterator extends BufferIterator {
28    private final int address;
29    private final int size;
30    private final boolean swap;
31
32    private int position;
33
34    NioBufferIterator(int address, int size, boolean swap) {
35        this.address = address;
36        this.size = size;
37        this.swap = swap;
38    }
39
40    public void skip(int byteCount) {
41        position += byteCount;
42    }
43
44    public void readByteArray(byte[] dst, int dstOffset, int byteCount) {
45        OSMemory.peekByteArray(address + position, dst, dstOffset, byteCount);
46        position += byteCount;
47    }
48
49    public byte readByte() {
50        byte result = OSMemory.peekByte(address + position);
51        ++position;
52        return result;
53    }
54
55    public int readInt() {
56        int result = OSMemory.peekInt(address + position, swap);
57        position += SizeOf.INT;
58        return result;
59    }
60
61    public void readIntArray(int[] dst, int dstOffset, int intCount) {
62        OSMemory.peekIntArray(address + position, dst, dstOffset, intCount, swap);
63        position += SizeOf.INT * intCount;
64    }
65
66    public short readShort() {
67        short result = OSMemory.peekShort(address + position, swap);
68        position += SizeOf.SHORT;
69        return result;
70    }
71}
72