Wed, 31 Dec 2014 07:22:50 +0100
Correct previous dual key logic pending first delivery installment.
michael@0 | 1 | /* -*- Mode: Java; c-basic-offset: 4; tab-width: 4; 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.mozglue; |
michael@0 | 7 | |
michael@0 | 8 | import java.io.InputStream; |
michael@0 | 9 | import java.nio.ByteBuffer; |
michael@0 | 10 | |
michael@0 | 11 | class ByteBufferInputStream extends InputStream { |
michael@0 | 12 | |
michael@0 | 13 | protected ByteBuffer mBuf; |
michael@0 | 14 | // Reference to a native object holding the data backing the ByteBuffer. |
michael@0 | 15 | private NativeReference mNativeRef; |
michael@0 | 16 | |
michael@0 | 17 | protected ByteBufferInputStream(ByteBuffer buffer, NativeReference ref) { |
michael@0 | 18 | mBuf = buffer; |
michael@0 | 19 | mNativeRef = ref; |
michael@0 | 20 | } |
michael@0 | 21 | |
michael@0 | 22 | @Override |
michael@0 | 23 | public int available() { |
michael@0 | 24 | return mBuf.remaining(); |
michael@0 | 25 | } |
michael@0 | 26 | |
michael@0 | 27 | @Override |
michael@0 | 28 | public void close() { |
michael@0 | 29 | mBuf = null; |
michael@0 | 30 | mNativeRef.release(); |
michael@0 | 31 | } |
michael@0 | 32 | |
michael@0 | 33 | @Override |
michael@0 | 34 | public int read() { |
michael@0 | 35 | if (!mBuf.hasRemaining() || mNativeRef.isReleased()) { |
michael@0 | 36 | return -1; |
michael@0 | 37 | } |
michael@0 | 38 | |
michael@0 | 39 | return mBuf.get() & 0xff; // Avoid sign extension |
michael@0 | 40 | } |
michael@0 | 41 | |
michael@0 | 42 | @Override |
michael@0 | 43 | public int read(byte[] buffer, int offset, int length) { |
michael@0 | 44 | if (!mBuf.hasRemaining() || mNativeRef.isReleased()) { |
michael@0 | 45 | return -1; |
michael@0 | 46 | } |
michael@0 | 47 | |
michael@0 | 48 | length = Math.min(length, mBuf.remaining()); |
michael@0 | 49 | mBuf.get(buffer, offset, length); |
michael@0 | 50 | return length; |
michael@0 | 51 | } |
michael@0 | 52 | |
michael@0 | 53 | @Override |
michael@0 | 54 | public long skip(long byteCount) { |
michael@0 | 55 | if (byteCount < 0 || mNativeRef.isReleased()) { |
michael@0 | 56 | return 0; |
michael@0 | 57 | } |
michael@0 | 58 | |
michael@0 | 59 | byteCount = Math.min(byteCount, mBuf.remaining()); |
michael@0 | 60 | mBuf.position(mBuf.position() + (int)byteCount); |
michael@0 | 61 | return byteCount; |
michael@0 | 62 | } |
michael@0 | 63 | |
michael@0 | 64 | } |