michael@0: /* -*- Mode: Java; c-basic-offset: 4; tab-width: 20; indent-tabs-mode: nil; -*- michael@0: * This Source Code Form is subject to the terms of the Mozilla Public michael@0: * License, v. 2.0. If a copy of the MPL was not distributed with this michael@0: * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ michael@0: michael@0: package org.mozilla.gecko.sqlite; michael@0: michael@0: import java.io.IOException; michael@0: import java.io.InputStream; michael@0: import java.nio.ByteBuffer; michael@0: michael@0: /* michael@0: * Helper class to make the ByteBuffers returned by SQLite BLOB michael@0: * easier to use. michael@0: */ michael@0: public class ByteBufferInputStream extends InputStream { michael@0: private ByteBuffer mByteBuffer; michael@0: michael@0: public ByteBufferInputStream(ByteBuffer aByteBuffer) { michael@0: mByteBuffer = aByteBuffer; michael@0: } michael@0: michael@0: @Override michael@0: public synchronized int read() throws IOException { michael@0: if (!mByteBuffer.hasRemaining()) { michael@0: return -1; michael@0: } michael@0: return mByteBuffer.get(); michael@0: } michael@0: michael@0: @Override michael@0: public synchronized int read(byte[] aBytes, int aOffset, int aLen) michael@0: throws IOException { michael@0: int toRead = Math.min(aLen, mByteBuffer.remaining()); michael@0: mByteBuffer.get(aBytes, aOffset, toRead); michael@0: return toRead; michael@0: } michael@0: }