mobile/android/base/sqlite/ByteBufferInputStream.java

changeset 0
6474c204b198
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/mobile/android/base/sqlite/ByteBufferInputStream.java	Wed Dec 31 06:09:35 2014 +0100
     1.3 @@ -0,0 +1,38 @@
     1.4 +/* -*- Mode: Java; c-basic-offset: 4; tab-width: 20; indent-tabs-mode: nil; -*-
     1.5 + * This Source Code Form is subject to the terms of the Mozilla Public
     1.6 + * License, v. 2.0. If a copy of the MPL was not distributed with this
     1.7 + * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
     1.8 +
     1.9 +package org.mozilla.gecko.sqlite;
    1.10 +
    1.11 +import java.io.IOException;
    1.12 +import java.io.InputStream;
    1.13 +import java.nio.ByteBuffer;
    1.14 +
    1.15 +/*
    1.16 + * Helper class to make the ByteBuffers returned by SQLite BLOB
    1.17 + * easier to use.
    1.18 + */
    1.19 +public class ByteBufferInputStream extends InputStream {
    1.20 +    private ByteBuffer mByteBuffer;
    1.21 +
    1.22 +    public ByteBufferInputStream(ByteBuffer aByteBuffer) {
    1.23 +        mByteBuffer = aByteBuffer;
    1.24 +    }
    1.25 +
    1.26 +    @Override
    1.27 +    public synchronized int read() throws IOException {
    1.28 +        if (!mByteBuffer.hasRemaining()) {
    1.29 +            return -1;
    1.30 +        }
    1.31 +        return mByteBuffer.get();
    1.32 +    }
    1.33 +
    1.34 +    @Override
    1.35 +    public synchronized int read(byte[] aBytes, int aOffset, int aLen)
    1.36 +        throws IOException {
    1.37 +        int toRead = Math.min(aLen, mByteBuffer.remaining());
    1.38 +        mByteBuffer.get(aBytes, aOffset, toRead);
    1.39 +        return toRead;
    1.40 +    }
    1.41 +}

mercurial