Thu, 22 Jan 2015 13:21:57 +0100
Incorporate requested changes from Mozilla in review:
https://bugzilla.mozilla.org/show_bug.cgi?id=1123480#c6
michael@0 | 1 | /* -*- Mode: Java; c-basic-offset: 4; tab-width: 20; indent-tabs-mode: nil; -*- |
michael@0 | 2 | * This Source Code Form is subject to the terms of the Mozilla Public |
michael@0 | 3 | * License, v. 2.0. If a copy of the MPL was not distributed with this |
michael@0 | 4 | * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ |
michael@0 | 5 | |
michael@0 | 6 | package org.mozilla.gecko.sqlite; |
michael@0 | 7 | |
michael@0 | 8 | import java.io.IOException; |
michael@0 | 9 | import java.io.InputStream; |
michael@0 | 10 | import java.nio.ByteBuffer; |
michael@0 | 11 | |
michael@0 | 12 | /* |
michael@0 | 13 | * Helper class to make the ByteBuffers returned by SQLite BLOB |
michael@0 | 14 | * easier to use. |
michael@0 | 15 | */ |
michael@0 | 16 | public class ByteBufferInputStream extends InputStream { |
michael@0 | 17 | private ByteBuffer mByteBuffer; |
michael@0 | 18 | |
michael@0 | 19 | public ByteBufferInputStream(ByteBuffer aByteBuffer) { |
michael@0 | 20 | mByteBuffer = aByteBuffer; |
michael@0 | 21 | } |
michael@0 | 22 | |
michael@0 | 23 | @Override |
michael@0 | 24 | public synchronized int read() throws IOException { |
michael@0 | 25 | if (!mByteBuffer.hasRemaining()) { |
michael@0 | 26 | return -1; |
michael@0 | 27 | } |
michael@0 | 28 | return mByteBuffer.get(); |
michael@0 | 29 | } |
michael@0 | 30 | |
michael@0 | 31 | @Override |
michael@0 | 32 | public synchronized int read(byte[] aBytes, int aOffset, int aLen) |
michael@0 | 33 | throws IOException { |
michael@0 | 34 | int toRead = Math.min(aLen, mByteBuffer.remaining()); |
michael@0 | 35 | mByteBuffer.get(aBytes, aOffset, toRead); |
michael@0 | 36 | return toRead; |
michael@0 | 37 | } |
michael@0 | 38 | } |