143a9f774d075e0e441d8b996e3f6c81ea483ec89Elliott Hughes/*
243a9f774d075e0e441d8b996e3f6c81ea483ec89Elliott Hughes * Copyright (C) 2010 The Android Open Source Project
343a9f774d075e0e441d8b996e3f6c81ea483ec89Elliott Hughes *
443a9f774d075e0e441d8b996e3f6c81ea483ec89Elliott Hughes * Licensed under the Apache License, Version 2.0 (the "License");
543a9f774d075e0e441d8b996e3f6c81ea483ec89Elliott Hughes * you may not use this file except in compliance with the License.
643a9f774d075e0e441d8b996e3f6c81ea483ec89Elliott Hughes * You may obtain a copy of the License at
743a9f774d075e0e441d8b996e3f6c81ea483ec89Elliott Hughes *
843a9f774d075e0e441d8b996e3f6c81ea483ec89Elliott Hughes *      http://www.apache.org/licenses/LICENSE-2.0
943a9f774d075e0e441d8b996e3f6c81ea483ec89Elliott Hughes *
1043a9f774d075e0e441d8b996e3f6c81ea483ec89Elliott Hughes * Unless required by applicable law or agreed to in writing, software
1143a9f774d075e0e441d8b996e3f6c81ea483ec89Elliott Hughes * distributed under the License is distributed on an "AS IS" BASIS,
1243a9f774d075e0e441d8b996e3f6c81ea483ec89Elliott Hughes * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
1343a9f774d075e0e441d8b996e3f6c81ea483ec89Elliott Hughes * See the License for the specific language governing permissions and
1443a9f774d075e0e441d8b996e3f6c81ea483ec89Elliott Hughes * limitations under the License.
1543a9f774d075e0e441d8b996e3f6c81ea483ec89Elliott Hughes */
1643a9f774d075e0e441d8b996e3f6c81ea483ec89Elliott Hughes
1743a9f774d075e0e441d8b996e3f6c81ea483ec89Elliott Hughespackage libcore.io;
1843a9f774d075e0e441d8b996e3f6c81ea483ec89Elliott Hughes
19f934c3d2c8dd9e6bc5299cef41adace2a671637dElliott Hughesimport libcore.io.Memory;
2043a9f774d075e0e441d8b996e3f6c81ea483ec89Elliott Hughes
2143a9f774d075e0e441d8b996e3f6c81ea483ec89Elliott Hughes/**
2243a9f774d075e0e441d8b996e3f6c81ea483ec89Elliott Hughes * Iterates over big- or little-endian bytes on the native heap.
2343a9f774d075e0e441d8b996e3f6c81ea483ec89Elliott Hughes * See {@link MemoryMappedFile#bigEndianIterator} and {@link MemoryMappedFile#littleEndianIterator}.
2443a9f774d075e0e441d8b996e3f6c81ea483ec89Elliott Hughes *
2543a9f774d075e0e441d8b996e3f6c81ea483ec89Elliott Hughes * @hide don't make this public without adding bounds checking.
2643a9f774d075e0e441d8b996e3f6c81ea483ec89Elliott Hughes */
2743a9f774d075e0e441d8b996e3f6c81ea483ec89Elliott Hughespublic final class NioBufferIterator extends BufferIterator {
280121106d9dc1ba713b53822886355e4d9339e852Joel Dice    private final long address;
2943a9f774d075e0e441d8b996e3f6c81ea483ec89Elliott Hughes    private final int size;
3043a9f774d075e0e441d8b996e3f6c81ea483ec89Elliott Hughes    private final boolean swap;
3143a9f774d075e0e441d8b996e3f6c81ea483ec89Elliott Hughes
3243a9f774d075e0e441d8b996e3f6c81ea483ec89Elliott Hughes    private int position;
3343a9f774d075e0e441d8b996e3f6c81ea483ec89Elliott Hughes
340121106d9dc1ba713b53822886355e4d9339e852Joel Dice    NioBufferIterator(long address, int size, boolean swap) {
3543a9f774d075e0e441d8b996e3f6c81ea483ec89Elliott Hughes        this.address = address;
3643a9f774d075e0e441d8b996e3f6c81ea483ec89Elliott Hughes        this.size = size;
3743a9f774d075e0e441d8b996e3f6c81ea483ec89Elliott Hughes        this.swap = swap;
3843a9f774d075e0e441d8b996e3f6c81ea483ec89Elliott Hughes    }
3943a9f774d075e0e441d8b996e3f6c81ea483ec89Elliott Hughes
40b5bde2fd72189192b52e726a2d606d70c3c8a34bElliott Hughes    public void seek(int offset) {
41b5bde2fd72189192b52e726a2d606d70c3c8a34bElliott Hughes        position = offset;
42b5bde2fd72189192b52e726a2d606d70c3c8a34bElliott Hughes    }
43b5bde2fd72189192b52e726a2d606d70c3c8a34bElliott Hughes
4443a9f774d075e0e441d8b996e3f6c81ea483ec89Elliott Hughes    public void skip(int byteCount) {
4543a9f774d075e0e441d8b996e3f6c81ea483ec89Elliott Hughes        position += byteCount;
4643a9f774d075e0e441d8b996e3f6c81ea483ec89Elliott Hughes    }
4743a9f774d075e0e441d8b996e3f6c81ea483ec89Elliott Hughes
4843a9f774d075e0e441d8b996e3f6c81ea483ec89Elliott Hughes    public void readByteArray(byte[] dst, int dstOffset, int byteCount) {
49f934c3d2c8dd9e6bc5299cef41adace2a671637dElliott Hughes        Memory.peekByteArray(address + position, dst, dstOffset, byteCount);
5043a9f774d075e0e441d8b996e3f6c81ea483ec89Elliott Hughes        position += byteCount;
5143a9f774d075e0e441d8b996e3f6c81ea483ec89Elliott Hughes    }
5243a9f774d075e0e441d8b996e3f6c81ea483ec89Elliott Hughes
5343a9f774d075e0e441d8b996e3f6c81ea483ec89Elliott Hughes    public byte readByte() {
54f934c3d2c8dd9e6bc5299cef41adace2a671637dElliott Hughes        byte result = Memory.peekByte(address + position);
5543a9f774d075e0e441d8b996e3f6c81ea483ec89Elliott Hughes        ++position;
5643a9f774d075e0e441d8b996e3f6c81ea483ec89Elliott Hughes        return result;
5743a9f774d075e0e441d8b996e3f6c81ea483ec89Elliott Hughes    }
5843a9f774d075e0e441d8b996e3f6c81ea483ec89Elliott Hughes
5943a9f774d075e0e441d8b996e3f6c81ea483ec89Elliott Hughes    public int readInt() {
60f934c3d2c8dd9e6bc5299cef41adace2a671637dElliott Hughes        int result = Memory.peekInt(address + position, swap);
6143a9f774d075e0e441d8b996e3f6c81ea483ec89Elliott Hughes        position += SizeOf.INT;
6243a9f774d075e0e441d8b996e3f6c81ea483ec89Elliott Hughes        return result;
6343a9f774d075e0e441d8b996e3f6c81ea483ec89Elliott Hughes    }
6443a9f774d075e0e441d8b996e3f6c81ea483ec89Elliott Hughes
6543a9f774d075e0e441d8b996e3f6c81ea483ec89Elliott Hughes    public void readIntArray(int[] dst, int dstOffset, int intCount) {
66f934c3d2c8dd9e6bc5299cef41adace2a671637dElliott Hughes        Memory.peekIntArray(address + position, dst, dstOffset, intCount, swap);
6743a9f774d075e0e441d8b996e3f6c81ea483ec89Elliott Hughes        position += SizeOf.INT * intCount;
6843a9f774d075e0e441d8b996e3f6c81ea483ec89Elliott Hughes    }
6943a9f774d075e0e441d8b996e3f6c81ea483ec89Elliott Hughes
7043a9f774d075e0e441d8b996e3f6c81ea483ec89Elliott Hughes    public short readShort() {
71f934c3d2c8dd9e6bc5299cef41adace2a671637dElliott Hughes        short result = Memory.peekShort(address + position, swap);
7243a9f774d075e0e441d8b996e3f6c81ea483ec89Elliott Hughes        position += SizeOf.SHORT;
7343a9f774d075e0e441d8b996e3f6c81ea483ec89Elliott Hughes        return result;
7443a9f774d075e0e441d8b996e3f6c81ea483ec89Elliott Hughes    }
7543a9f774d075e0e441d8b996e3f6c81ea483ec89Elliott Hughes}
76