1/* 2 * Copyright (C) 2014 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 com.android.layoutlib.bridge.libcore.io; 18 19import java.nio.ByteBuffer; 20 21import libcore.io.BufferIterator; 22 23/** 24 * Provides an implementation of {@link BufferIterator} over a {@link ByteBuffer}. 25 */ 26public class BridgeBufferIterator extends BufferIterator { 27 28 private final long mSize; 29 private final ByteBuffer mByteBuffer; 30 31 public BridgeBufferIterator(long size, ByteBuffer buffer) { 32 mSize = size; 33 mByteBuffer = buffer; 34 } 35 36 @Override 37 public void seek(int offset) { 38 assert offset <= mSize; 39 mByteBuffer.position(offset); 40 } 41 42 @Override 43 public void skip(int byteCount) { 44 int newPosition = mByteBuffer.position() + byteCount; 45 assert newPosition <= mSize; 46 mByteBuffer.position(newPosition); 47 } 48 49 @Override 50 public void readByteArray(byte[] dst, int dstOffset, int byteCount) { 51 assert dst.length >= dstOffset + byteCount; 52 mByteBuffer.get(dst, dstOffset, byteCount); 53 } 54 55 @Override 56 public byte readByte() { 57 return mByteBuffer.get(); 58 } 59 60 @Override 61 public int readInt() { 62 return mByteBuffer.getInt(); 63 } 64 65 @Override 66 public void readIntArray(int[] dst, int dstOffset, int intCount) { 67 while (--intCount >= 0) { 68 dst[dstOffset++] = mByteBuffer.getInt(); 69 } 70 } 71 72 @Override 73 public short readShort() { 74 return mByteBuffer.getShort(); 75 } 76} 77